X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Ftime%2Fstrftime.c;h=708875eeae52fc69be10418cd9fbdaa0e3e33a00;hb=ebd8ef50d5012733d86ed4a6834ca6d776b069ae;hp=d1ca7cae99429536a53901aab554cbda72776210;hpb=8a6bd7307da3fc4d08dd6a9277b611ccb4971354;p=musl diff --git a/src/time/strftime.c b/src/time/strftime.c index d1ca7cae..708875ee 100644 --- a/src/time/strftime.c +++ b/src/time/strftime.c @@ -251,14 +251,21 @@ size_t __strftime_l(char *restrict s, size_t n, const char *restrict f, const st t = __strftime_fmt_1(&buf, &k, *f, tm, loc, pad); if (!t) break; if (width) { - for (; *t=='+' || *t=='-' || (*t=='0'&&t[1]); t++, k--); - width--; - if (plus && tm->tm_year >= 10000-1900) - s[l++] = '+'; - else if (tm->tm_year < -1900) + /* Trim off any sign and leading zeros, then + * count remaining digits to determine behavior + * for the + flag. */ + if (*t=='+' || *t=='-') t++, k--; + for (; *t=='0' && t[1]-'0'<10U; t++, k--); + if (width < k) width = k; + size_t d; + for (d=0; t[d]-'0'<10U; d++); + if (tm->tm_year < -1900) { s[l++] = '-'; - else - width++; + width--; + } else if (plus && d+(width-k) >= (*p=='C'?3:5)) { + s[l++] = '+'; + width--; + } for (; width > k && l < n; width--) s[l++] = '0'; }