- t = __strftime_fmt_1(&buf, *f, tm, loc);
- if (!t || (k = strlen(t)) >= n-l)
- return 0;
+ t = __strftime_fmt_1(&buf, &k, *f, tm, loc, pad);
+ if (!t) break;
+ if (width) {
+ /* 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++] = '-';
+ width--;
+ } else if (plus && d+(width-k) >= (*p=='C'?3:5)) {
+ s[l++] = '+';
+ width--;
+ }
+ for (; width > k && l < n; width--)
+ s[l++] = '0';
+ }
+ if (k > n-l) k = n-l;