11 const char *__nl_langinfo_l(nl_item, locale_t);
13 static int is_leap(int y)
16 if (y>INT_MAX-1900) y -= 2000;
18 return !(y%4) && ((y%100) || !(y%400));
21 static int week_num(const struct tm *tm)
23 int val = (tm->tm_yday + 7 - (tm->tm_wday+6)%7) / 7;
24 /* If 1 Jan is just 1-3 days past Monday,
25 * the previous week is also in this year. */
26 if ((tm->tm_wday - tm->tm_yday - 2 + 371) % 7 <= 2)
30 /* If 31 December of prev year a Thursday,
31 * or Friday of a leap year, then the
32 * prev year has 53 weeks. */
33 int dec31 = (tm->tm_wday - tm->tm_yday - 1 + 7) % 7;
34 if (dec31 == 4 || (dec31 == 5 && is_leap(tm->tm_year%400-1)))
36 } else if (val == 53) {
37 /* If 1 January is not a Thursday, and not
38 * a Wednesday of a leap year, then this
39 * year has only 52 weeks. */
40 int jan1 = (tm->tm_wday - tm->tm_yday + 371) % 7;
41 if (jan1 != 4 && (jan1 != 3 || !is_leap(tm->tm_year)))
47 const char *__tm_to_tzname(const struct tm *);
48 size_t __strftime_l(char *restrict, size_t, const char *restrict, const struct tm *restrict, locale_t);
50 const char *__strftime_fmt_1(char (*s)[100], size_t *l, int f, const struct tm *tm, locale_t loc)
59 item = ABDAY_1 + tm->tm_wday;
62 item = DAY_1 + tm->tm_wday;
66 item = ABMON_1 + tm->tm_mon;
69 item = MON_1 + tm->tm_mon;
75 val = (1900LL+tm->tm_year) / 100;
84 *l = snprintf(*s, sizeof *s, "%2d", tm->tm_mday);
91 val = tm->tm_year + 1900LL;
92 if (tm->tm_yday < 3 && week_num(tm) != 1) val--;
93 else if (tm->tm_yday > 360 && week_num(tm) == 1) val++;
94 if (f=='g') val %= 100;
103 else if (val > 12) val -= 12;
119 item = tm->tm_hour >= 12 ? PM_STR : AM_STR;
128 val = __tm_to_secs(tm) + tm->__tm_gmtoff;
140 val = tm->tm_wday ? tm->tm_wday : 7;
144 val = (tm->tm_yday + 7 - tm->tm_wday) / 7;
147 val = (tm->tm_yday + 7 - (tm->tm_wday+6)%7) / 7;
163 val = tm->tm_year % 100;
166 val = tm->tm_year + 1900;
168 *l = snprintf(*s, sizeof *s, "+%lld", val);
174 if (tm->tm_isdst < 0) {
178 *l = snprintf(*s, sizeof *s, "%+.2d%.2d",
179 (-tm->__tm_gmtoff)/3600,
180 abs(tm->__tm_gmtoff%3600)/60);
183 if (tm->tm_isdst < 0) {
187 fmt = __tm_to_tzname(tm);
196 *l = snprintf(*s, sizeof *s, "%0*lld", width, val);
199 fmt = __nl_langinfo_l(item, loc);
204 fmt = __nl_langinfo_l(item, loc);
206 *l = __strftime_l(*s, sizeof *s, fmt, tm, loc);
211 size_t __strftime_l(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm, locale_t loc)
219 for (l=0; l+1<n; f++) {
229 if ((plus = (*f == '+'))) f++;
230 width = strtoul(f, &p, 10);
231 if (*p == 'C' || *p == 'F' || *p == 'G' || *p == 'Y') {
232 if (!width && p!=f) width = 1;
233 if (width >= n-l) return 0;
238 if (*f == 'E' || *f == 'O') f++;
239 t = __strftime_fmt_1(&buf, &k, *f, tm, loc);
242 for (; *t=='+' || *t=='-' || (*t=='0'&&t[1]); t++, k--);
244 if (plus && tm->tm_year >= 10000-1900)
246 else if (tm->tm_year < -1900)
250 if (width >= n-l) return 0;
251 for (; width > k; width--)
254 if (k >= n-l) return 0;
261 size_t strftime(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm)
263 return __strftime_l(s, n, f, tm, 0);
266 weak_alias(__strftime_l, strftime_l);