148d96b17SGarrett Wollman /*
2*7098712bSAlexander Ziaee * SPDX-License-Identifier: BSD-4.3TAHOE
3*7098712bSAlexander Ziaee *
448d96b17SGarrett Wollman * Copyright (c) 1989 The Regents of the University of California.
548d96b17SGarrett Wollman * All rights reserved.
648d96b17SGarrett Wollman *
73c87aa1dSDavid Chisnall * Copyright (c) 2011 The FreeBSD Foundation
85b5fa75aSEd Maste *
93c87aa1dSDavid Chisnall * Portions of this software were developed by David Chisnall
103c87aa1dSDavid Chisnall * under sponsorship from the FreeBSD Foundation.
113c87aa1dSDavid Chisnall *
1248d96b17SGarrett Wollman * Redistribution and use in source and binary forms are permitted
1348d96b17SGarrett Wollman * provided that the above copyright notice and this paragraph are
1448d96b17SGarrett Wollman * duplicated in all such forms and that any documentation,
1548d96b17SGarrett Wollman * advertising materials, and other materials related to such
1648d96b17SGarrett Wollman * distribution and use acknowledge that the software was developed
1748d96b17SGarrett Wollman * by the University of California, Berkeley. The name of the
1848d96b17SGarrett Wollman * University may not be used to endorse or promote products derived
1948d96b17SGarrett Wollman * from this software without specific prior written permission.
2048d96b17SGarrett Wollman * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
2148d96b17SGarrett Wollman * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
2248d96b17SGarrett Wollman * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2348d96b17SGarrett Wollman */
2448d96b17SGarrett Wollman
25d201fe46SDaniel Eischen #include "namespace.h"
26c28fbb7bSGarrett Wollman #include "private.h"
27c28fbb7bSGarrett Wollman
2848d96b17SGarrett Wollman #include "tzfile.h"
29c28fbb7bSGarrett Wollman #include <fcntl.h>
30c28fbb7bSGarrett Wollman #include <sys/stat.h>
31bc421551SDag-Erling Smørgrav #include <stdio.h>
32d201fe46SDaniel Eischen #include "un-namespace.h"
3337486f03SJoerg Wunsch #include "timelocal.h"
3448d96b17SGarrett Wollman
351d145de8SAlfred Perlstein static char * _add(const char *, char *, const char *);
36e66c50c7SPedro F. Giffuni static char * _conv(int, const char *, char *, const char *, locale_t);
37dfc79e89SEdwin Groothuis static char * _fmt(const char *, const struct tm *, char *, const char *,
383c87aa1dSDavid Chisnall int *, locale_t);
39e66c50c7SPedro F. Giffuni static char * _yconv(int, int, int, int, char *, const char *, locale_t);
4048d96b17SGarrett Wollman
4148d96b17SGarrett Wollman extern char * tzname[];
4248d96b17SGarrett Wollman
43cdff05faSStefan Farfeleder #ifndef YEAR_2000_NAME
44cdff05faSStefan Farfeleder #define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
45cdff05faSStefan Farfeleder #endif /* !defined YEAR_2000_NAME */
46cdff05faSStefan Farfeleder
47cdff05faSStefan Farfeleder #define IN_NONE 0
48cdff05faSStefan Farfeleder #define IN_SOME 1
49cdff05faSStefan Farfeleder #define IN_THIS 2
50cdff05faSStefan Farfeleder #define IN_ALL 3
51cdff05faSStefan Farfeleder
52d9506686SXin LI #define PAD_DEFAULT 0
53d9506686SXin LI #define PAD_LESS 1
54d9506686SXin LI #define PAD_SPACE 2
55d9506686SXin LI #define PAD_ZERO 3
56d9506686SXin LI
57770284d8SJilles Tjoelker static const char fmt_padding[][4][5] = {
58d9506686SXin LI /* DEFAULT, LESS, SPACE, ZERO */
59d9506686SXin LI #define PAD_FMT_MONTHDAY 0
60d9506686SXin LI #define PAD_FMT_HMS 0
61d9506686SXin LI #define PAD_FMT_CENTURY 0
62d9506686SXin LI #define PAD_FMT_SHORTYEAR 0
63d9506686SXin LI #define PAD_FMT_MONTH 0
64d9506686SXin LI #define PAD_FMT_WEEKOFYEAR 0
65d9506686SXin LI #define PAD_FMT_DAYOFMONTH 0
66d9506686SXin LI { "%02d", "%d", "%2d", "%02d" },
67d9506686SXin LI #define PAD_FMT_SDAYOFMONTH 1
68d9506686SXin LI #define PAD_FMT_SHMS 1
69d9506686SXin LI { "%2d", "%d", "%2d", "%02d" },
70d9506686SXin LI #define PAD_FMT_DAYOFYEAR 2
71d9506686SXin LI { "%03d", "%d", "%3d", "%03d" },
72d9506686SXin LI #define PAD_FMT_YEAR 3
73d9506686SXin LI { "%04d", "%d", "%4d", "%04d" }
74d9506686SXin LI };
75d9506686SXin LI
7648d96b17SGarrett Wollman size_t
strftime_l(char * __restrict s,size_t maxsize,const char * __restrict format,const struct tm * __restrict t,locale_t loc)773c87aa1dSDavid Chisnall strftime_l(char * __restrict s, size_t maxsize, const char * __restrict format,
783c87aa1dSDavid Chisnall const struct tm * __restrict t, locale_t loc)
7948d96b17SGarrett Wollman {
8048d96b17SGarrett Wollman char * p;
81cdff05faSStefan Farfeleder int warn;
823c87aa1dSDavid Chisnall FIX_LOCALE(loc);
8348d96b17SGarrett Wollman
84c28fbb7bSGarrett Wollman tzset();
85cdff05faSStefan Farfeleder warn = IN_NONE;
863c87aa1dSDavid Chisnall p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn, loc);
87cdff05faSStefan Farfeleder #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
88cdff05faSStefan Farfeleder if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {
893c87aa1dSDavid Chisnall (void) fprintf_l(stderr, loc, "\n");
90cdff05faSStefan Farfeleder if (format == NULL)
91e66c50c7SPedro F. Giffuni (void) fputs("NULL strftime format ", stderr);
923c87aa1dSDavid Chisnall else (void) fprintf_l(stderr, loc, "strftime format \"%s\" ",
93cdff05faSStefan Farfeleder format);
94e66c50c7SPedro F. Giffuni (void) fputs("yields only two digits of years in ", stderr);
95cdff05faSStefan Farfeleder if (warn == IN_SOME)
96e66c50c7SPedro F. Giffuni (void) fputs("some locales", stderr);
97cdff05faSStefan Farfeleder else if (warn == IN_THIS)
98e66c50c7SPedro F. Giffuni (void) fputs("the current locale", stderr);
99e66c50c7SPedro F. Giffuni else (void) fputs("all locales", stderr);
100e66c50c7SPedro F. Giffuni (void) fputs("\n", stderr);
101cdff05faSStefan Farfeleder }
102cdff05faSStefan Farfeleder #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
10348d96b17SGarrett Wollman if (p == s + maxsize)
1043624c752SPedro F. Giffuni return (0);
10548d96b17SGarrett Wollman *p = '\0';
10648d96b17SGarrett Wollman return p - s;
10748d96b17SGarrett Wollman }
10848d96b17SGarrett Wollman
1093c87aa1dSDavid Chisnall size_t
strftime(char * __restrict s,size_t maxsize,const char * __restrict format,const struct tm * __restrict t)1103c87aa1dSDavid Chisnall strftime(char * __restrict s, size_t maxsize, const char * __restrict format,
1113c87aa1dSDavid Chisnall const struct tm * __restrict t)
1123c87aa1dSDavid Chisnall {
1133c87aa1dSDavid Chisnall return strftime_l(s, maxsize, format, t, __get_locale());
1143c87aa1dSDavid Chisnall }
1153c87aa1dSDavid Chisnall
11648d96b17SGarrett Wollman static char *
_fmt(const char * format,const struct tm * const t,char * pt,const char * const ptlim,int * warnp,locale_t loc)11785cef632SCraig Rodrigues _fmt(const char *format, const struct tm * const t, char *pt,
11885cef632SCraig Rodrigues const char * const ptlim, int *warnp, locale_t loc)
11948d96b17SGarrett Wollman {
120d9506686SXin LI int Ealternative, Oalternative, PadIndex;
1213c87aa1dSDavid Chisnall struct lc_time_T *tptr = __get_current_time_locale(loc);
12211cd0d32SAndrey A. Chernov
12348d96b17SGarrett Wollman for ( ; *format; ++format) {
12448d96b17SGarrett Wollman if (*format == '%') {
12511cd0d32SAndrey A. Chernov Ealternative = 0;
12611cd0d32SAndrey A. Chernov Oalternative = 0;
127d9506686SXin LI PadIndex = PAD_DEFAULT;
12848d96b17SGarrett Wollman label:
12948d96b17SGarrett Wollman switch (*++format) {
13048d96b17SGarrett Wollman case '\0':
13148d96b17SGarrett Wollman --format;
13248d96b17SGarrett Wollman break;
13348d96b17SGarrett Wollman case 'A':
134cdff05faSStefan Farfeleder pt = _add((t->tm_wday < 0 ||
135cdff05faSStefan Farfeleder t->tm_wday >= DAYSPERWEEK) ?
136930cd711SAlexey Zelkin "?" : tptr->weekday[t->tm_wday],
137c28fbb7bSGarrett Wollman pt, ptlim);
13848d96b17SGarrett Wollman continue;
13948d96b17SGarrett Wollman case 'a':
140cdff05faSStefan Farfeleder pt = _add((t->tm_wday < 0 ||
141cdff05faSStefan Farfeleder t->tm_wday >= DAYSPERWEEK) ?
142930cd711SAlexey Zelkin "?" : tptr->wday[t->tm_wday],
143c28fbb7bSGarrett Wollman pt, ptlim);
14448d96b17SGarrett Wollman continue;
14548d96b17SGarrett Wollman case 'B':
146cdff05faSStefan Farfeleder pt = _add((t->tm_mon < 0 ||
147cdff05faSStefan Farfeleder t->tm_mon >= MONSPERYEAR) ?
148930cd711SAlexey Zelkin "?" : (Oalternative ? tptr->alt_month :
149930cd711SAlexey Zelkin tptr->month)[t->tm_mon],
150c28fbb7bSGarrett Wollman pt, ptlim);
15148d96b17SGarrett Wollman continue;
15248d96b17SGarrett Wollman case 'b':
15348d96b17SGarrett Wollman case 'h':
154cdff05faSStefan Farfeleder pt = _add((t->tm_mon < 0 ||
155cdff05faSStefan Farfeleder t->tm_mon >= MONSPERYEAR) ?
156930cd711SAlexey Zelkin "?" : tptr->mon[t->tm_mon],
157c28fbb7bSGarrett Wollman pt, ptlim);
15848d96b17SGarrett Wollman continue;
15948d96b17SGarrett Wollman case 'C':
16048d96b17SGarrett Wollman /*
1613624c752SPedro F. Giffuni * %C used to do a...
1623624c752SPedro F. Giffuni * _fmt("%a %b %e %X %Y", t);
1633624c752SPedro F. Giffuni * ...whereas now POSIX 1003.2 calls for
1643624c752SPedro F. Giffuni * something completely different.
1653624c752SPedro F. Giffuni * (ado, 1993-05-24)
16648d96b17SGarrett Wollman */
167dfc79e89SEdwin Groothuis pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0,
168e66c50c7SPedro F. Giffuni pt, ptlim, loc);
16948d96b17SGarrett Wollman continue;
170c28fbb7bSGarrett Wollman case 'c':
171cdff05faSStefan Farfeleder {
172cdff05faSStefan Farfeleder int warn2 = IN_SOME;
173cdff05faSStefan Farfeleder
1743c87aa1dSDavid Chisnall pt = _fmt(tptr->c_fmt, t, pt, ptlim, &warn2, loc);
175cdff05faSStefan Farfeleder if (warn2 == IN_ALL)
176cdff05faSStefan Farfeleder warn2 = IN_THIS;
177cdff05faSStefan Farfeleder if (warn2 > *warnp)
178cdff05faSStefan Farfeleder *warnp = warn2;
179cdff05faSStefan Farfeleder }
18048d96b17SGarrett Wollman continue;
181c28fbb7bSGarrett Wollman case 'D':
1823c87aa1dSDavid Chisnall pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp, loc);
18348d96b17SGarrett Wollman continue;
18448d96b17SGarrett Wollman case 'd':
185e66c50c7SPedro F. Giffuni pt = _conv(t->tm_mday,
186e66c50c7SPedro F. Giffuni fmt_padding[PAD_FMT_DAYOFMONTH][PadIndex],
187e66c50c7SPedro F. Giffuni pt, ptlim, loc);
18848d96b17SGarrett Wollman continue;
18948d96b17SGarrett Wollman case 'E':
190c63a4303SAndrey A. Chernov if (Ealternative || Oalternative)
191c63a4303SAndrey A. Chernov break;
19211cd0d32SAndrey A. Chernov Ealternative++;
19311cd0d32SAndrey A. Chernov goto label;
19448d96b17SGarrett Wollman case 'O':
19548d96b17SGarrett Wollman /*
1963624c752SPedro F. Giffuni * C99 locale modifiers.
1973624c752SPedro F. Giffuni * The sequences
1983624c752SPedro F. Giffuni * %Ec %EC %Ex %EX %Ey %EY
1993624c752SPedro F. Giffuni * %Od %oe %OH %OI %Om %OM
2003624c752SPedro F. Giffuni * %OS %Ou %OU %OV %Ow %OW %Oy
2013624c752SPedro F. Giffuni * are supposed to provide alternate
2023624c752SPedro F. Giffuni * representations.
2033624c752SPedro F. Giffuni *
2043624c752SPedro F. Giffuni * FreeBSD extension
2053624c752SPedro F. Giffuni * %OB
20648d96b17SGarrett Wollman */
207c63a4303SAndrey A. Chernov if (Ealternative || Oalternative)
208c63a4303SAndrey A. Chernov break;
20911cd0d32SAndrey A. Chernov Oalternative++;
21048d96b17SGarrett Wollman goto label;
21148d96b17SGarrett Wollman case 'e':
212d9506686SXin LI pt = _conv(t->tm_mday,
213e66c50c7SPedro F. Giffuni fmt_padding[PAD_FMT_SDAYOFMONTH][PadIndex],
214e66c50c7SPedro F. Giffuni pt, ptlim, loc);
21548d96b17SGarrett Wollman continue;
216c63a4303SAndrey A. Chernov case 'F':
2173c87aa1dSDavid Chisnall pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp, loc);
218c63a4303SAndrey A. Chernov continue;
21948d96b17SGarrett Wollman case 'H':
220d9506686SXin LI pt = _conv(t->tm_hour, fmt_padding[PAD_FMT_HMS][PadIndex],
221e66c50c7SPedro F. Giffuni pt, ptlim, loc);
22248d96b17SGarrett Wollman continue;
22348d96b17SGarrett Wollman case 'I':
22448d96b17SGarrett Wollman pt = _conv((t->tm_hour % 12) ?
22548d96b17SGarrett Wollman (t->tm_hour % 12) : 12,
2263624c752SPedro F. Giffuni fmt_padding[PAD_FMT_HMS][PadIndex],
227e66c50c7SPedro F. Giffuni pt, ptlim, loc);
22848d96b17SGarrett Wollman continue;
22948d96b17SGarrett Wollman case 'j':
230d9506686SXin LI pt = _conv(t->tm_yday + 1,
231e66c50c7SPedro F. Giffuni fmt_padding[PAD_FMT_DAYOFYEAR][PadIndex],
232e66c50c7SPedro F. Giffuni pt, ptlim, loc);
23348d96b17SGarrett Wollman continue;
23448d96b17SGarrett Wollman case 'k':
23548d96b17SGarrett Wollman /*
2363624c752SPedro F. Giffuni * This used to be...
2373624c752SPedro F. Giffuni * _conv(t->tm_hour % 12 ?
2383624c752SPedro F. Giffuni * t->tm_hour % 12 : 12, 2, ' ');
2393624c752SPedro F. Giffuni * ...and has been changed to the below to
2403624c752SPedro F. Giffuni * match SunOS 4.1.1 and Arnold Robbins'
2413624c752SPedro F. Giffuni * strftime version 3.0. That is, "%k" and
2423624c752SPedro F. Giffuni * "%l" have been swapped.
2433624c752SPedro F. Giffuni * (ado, 1993-05-24)
24448d96b17SGarrett Wollman */
245d9506686SXin LI pt = _conv(t->tm_hour, fmt_padding[PAD_FMT_SHMS][PadIndex],
246e66c50c7SPedro F. Giffuni pt, ptlim, loc);
24748d96b17SGarrett Wollman continue;
24848d96b17SGarrett Wollman #ifdef KITCHEN_SINK
24948d96b17SGarrett Wollman case 'K':
25048d96b17SGarrett Wollman /*
25148d96b17SGarrett Wollman ** After all this time, still unclaimed!
25248d96b17SGarrett Wollman */
25348d96b17SGarrett Wollman pt = _add("kitchen sink", pt, ptlim);
25448d96b17SGarrett Wollman continue;
25548d96b17SGarrett Wollman #endif /* defined KITCHEN_SINK */
25648d96b17SGarrett Wollman case 'l':
25748d96b17SGarrett Wollman /*
2583624c752SPedro F. Giffuni * This used to be...
2593624c752SPedro F. Giffuni * _conv(t->tm_hour, 2, ' ');
2603624c752SPedro F. Giffuni * ...and has been changed to the below to
2613624c752SPedro F. Giffuni * match SunOS 4.1.1 and Arnold Robbin's
2623624c752SPedro F. Giffuni * strftime version 3.0. That is, "%k" and
2633624c752SPedro F. Giffuni * "%l" have been swapped.
2643624c752SPedro F. Giffuni * (ado, 1993-05-24)
26548d96b17SGarrett Wollman */
26648d96b17SGarrett Wollman pt = _conv((t->tm_hour % 12) ?
26748d96b17SGarrett Wollman (t->tm_hour % 12) : 12,
2683624c752SPedro F. Giffuni fmt_padding[PAD_FMT_SHMS][PadIndex],
269e66c50c7SPedro F. Giffuni pt, ptlim, loc);
27048d96b17SGarrett Wollman continue;
27148d96b17SGarrett Wollman case 'M':
272d9506686SXin LI pt = _conv(t->tm_min, fmt_padding[PAD_FMT_HMS][PadIndex],
273e66c50c7SPedro F. Giffuni pt, ptlim, loc);
27448d96b17SGarrett Wollman continue;
27548d96b17SGarrett Wollman case 'm':
276d9506686SXin LI pt = _conv(t->tm_mon + 1,
2773624c752SPedro F. Giffuni fmt_padding[PAD_FMT_MONTH][PadIndex],
278e66c50c7SPedro F. Giffuni pt, ptlim, loc);
27948d96b17SGarrett Wollman continue;
28048d96b17SGarrett Wollman case 'n':
28148d96b17SGarrett Wollman pt = _add("\n", pt, ptlim);
28248d96b17SGarrett Wollman continue;
28348d96b17SGarrett Wollman case 'p':
284cdff05faSStefan Farfeleder pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
2853624c752SPedro F. Giffuni tptr->pm : tptr->am,
28648d96b17SGarrett Wollman pt, ptlim);
28748d96b17SGarrett Wollman continue;
28848d96b17SGarrett Wollman case 'R':
2893c87aa1dSDavid Chisnall pt = _fmt("%H:%M", t, pt, ptlim, warnp, loc);
29048d96b17SGarrett Wollman continue;
29148d96b17SGarrett Wollman case 'r':
292cdff05faSStefan Farfeleder pt = _fmt(tptr->ampm_fmt, t, pt, ptlim,
2933c87aa1dSDavid Chisnall warnp, loc);
29448d96b17SGarrett Wollman continue;
29548d96b17SGarrett Wollman case 'S':
296d9506686SXin LI pt = _conv(t->tm_sec, fmt_padding[PAD_FMT_HMS][PadIndex],
297e66c50c7SPedro F. Giffuni pt, ptlim, loc);
29848d96b17SGarrett Wollman continue;
2995ad178d8SAndrey A. Chernov case 's':
300c6966b0cSBruce Evans {
301c6966b0cSBruce Evans struct tm tm;
302c6966b0cSBruce Evans char buf[INT_STRLEN_MAXIMUM(
303c6966b0cSBruce Evans time_t) + 1];
304c6966b0cSBruce Evans time_t mkt;
305c6966b0cSBruce Evans
306c6966b0cSBruce Evans tm = *t;
30746c59934SDag-Erling Smørgrav mkt = timeoff(&tm, t->tm_gmtoff);
308c6966b0cSBruce Evans if (TYPE_SIGNED(time_t))
309e66c50c7SPedro F. Giffuni (void) sprintf_l(buf, loc, "%ld",
310c6966b0cSBruce Evans (long) mkt);
311e66c50c7SPedro F. Giffuni else (void) sprintf_l(buf, loc, "%lu",
312c6966b0cSBruce Evans (unsigned long) mkt);
313c6966b0cSBruce Evans pt = _add(buf, pt, ptlim);
314c6966b0cSBruce Evans }
3155ad178d8SAndrey A. Chernov continue;
31648d96b17SGarrett Wollman case 'T':
3173c87aa1dSDavid Chisnall pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp, loc);
31848d96b17SGarrett Wollman continue;
31948d96b17SGarrett Wollman case 't':
32048d96b17SGarrett Wollman pt = _add("\t", pt, ptlim);
32148d96b17SGarrett Wollman continue;
32248d96b17SGarrett Wollman case 'U':
323cdff05faSStefan Farfeleder pt = _conv((t->tm_yday + DAYSPERWEEK -
324cdff05faSStefan Farfeleder t->tm_wday) / DAYSPERWEEK,
325e66c50c7SPedro F. Giffuni fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex],
326e66c50c7SPedro F. Giffuni pt, ptlim, loc);
32748d96b17SGarrett Wollman continue;
32848d96b17SGarrett Wollman case 'u':
32948d96b17SGarrett Wollman /*
3303624c752SPedro F. Giffuni * From Arnold Robbins' strftime version 3.0:
3313624c752SPedro F. Giffuni * "ISO 8601: Weekday as a decimal number
3323624c752SPedro F. Giffuni * [1 (Monday) - 7]"
3333624c752SPedro F. Giffuni * (ado, 1993-05-24)
33448d96b17SGarrett Wollman */
335cdff05faSStefan Farfeleder pt = _conv((t->tm_wday == 0) ?
336cdff05faSStefan Farfeleder DAYSPERWEEK : t->tm_wday,
337e66c50c7SPedro F. Giffuni "%d", pt, ptlim, loc);
33848d96b17SGarrett Wollman continue;
339b9b51fb7SWolfgang Helbig case 'V': /* ISO 8601 week number */
340b9b51fb7SWolfgang Helbig case 'G': /* ISO 8601 year (four digits) */
341b9b51fb7SWolfgang Helbig case 'g': /* ISO 8601 year (two digits) */
34248d96b17SGarrett Wollman /*
3433624c752SPedro F. Giffuni * From Arnold Robbins' strftime version 3.0: "the week number of the
3443624c752SPedro F. Giffuni * year (the first Monday as the first day of week 1) as a decimal number
3453624c752SPedro F. Giffuni * (01-53)."
3463624c752SPedro F. Giffuni * (ado, 1993-05-24)
3473624c752SPedro F. Giffuni *
3483624c752SPedro F. Giffuni * From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
3493624c752SPedro F. Giffuni * "Week 01 of a year is per definition the first week which has the
3503624c752SPedro F. Giffuni * Thursday in this year, which is equivalent to the week which contains
3513624c752SPedro F. Giffuni * the fourth day of January. In other words, the first week of a new year
3523624c752SPedro F. Giffuni * is the week which has the majority of its days in the new year. Week 01
3533624c752SPedro F. Giffuni * might also contain days from the previous year and the week before week
3543624c752SPedro F. Giffuni * 01 of a year is the last week (52 or 53) of the previous year even if
3553624c752SPedro F. Giffuni * it contains days from the new year. A week starts with Monday (day 1)
3563624c752SPedro F. Giffuni * and ends with Sunday (day 7). For example, the first week of the year
3573624c752SPedro F. Giffuni * 1997 lasts from 1996-12-30 to 1997-01-05..."
3583624c752SPedro F. Giffuni * (ado, 1996-01-02)
35948d96b17SGarrett Wollman */
36048d96b17SGarrett Wollman {
361b9b51fb7SWolfgang Helbig int year;
362dfc79e89SEdwin Groothuis int base;
363b9b51fb7SWolfgang Helbig int yday;
364b9b51fb7SWolfgang Helbig int wday;
365b9b51fb7SWolfgang Helbig int w;
36648d96b17SGarrett Wollman
367dfc79e89SEdwin Groothuis year = t->tm_year;
368dfc79e89SEdwin Groothuis base = TM_YEAR_BASE;
369b9b51fb7SWolfgang Helbig yday = t->tm_yday;
370b9b51fb7SWolfgang Helbig wday = t->tm_wday;
371b9b51fb7SWolfgang Helbig for ( ; ; ) {
372b9b51fb7SWolfgang Helbig int len;
373b9b51fb7SWolfgang Helbig int bot;
374b9b51fb7SWolfgang Helbig int top;
375b9b51fb7SWolfgang Helbig
376dfc79e89SEdwin Groothuis len = isleap_sum(year, base) ?
377b9b51fb7SWolfgang Helbig DAYSPERLYEAR :
378b9b51fb7SWolfgang Helbig DAYSPERNYEAR;
37948d96b17SGarrett Wollman /*
3803624c752SPedro F. Giffuni * What yday (-3 ... 3) does
3813624c752SPedro F. Giffuni * the ISO year begin on?
38248d96b17SGarrett Wollman */
383b9b51fb7SWolfgang Helbig bot = ((yday + 11 - wday) %
384b9b51fb7SWolfgang Helbig DAYSPERWEEK) - 3;
38548d96b17SGarrett Wollman /*
3863624c752SPedro F. Giffuni * What yday does the NEXT
3873624c752SPedro F. Giffuni * ISO year begin on?
38848d96b17SGarrett Wollman */
389b9b51fb7SWolfgang Helbig top = bot -
390b9b51fb7SWolfgang Helbig (len % DAYSPERWEEK);
391b9b51fb7SWolfgang Helbig if (top < -3)
392b9b51fb7SWolfgang Helbig top += DAYSPERWEEK;
393b9b51fb7SWolfgang Helbig top += len;
394b9b51fb7SWolfgang Helbig if (yday >= top) {
395dfc79e89SEdwin Groothuis ++base;
396b9b51fb7SWolfgang Helbig w = 1;
397b9b51fb7SWolfgang Helbig break;
398fe463a8fSGarrett Wollman }
399b9b51fb7SWolfgang Helbig if (yday >= bot) {
400b9b51fb7SWolfgang Helbig w = 1 + ((yday - bot) /
401b9b51fb7SWolfgang Helbig DAYSPERWEEK);
402b9b51fb7SWolfgang Helbig break;
403b9b51fb7SWolfgang Helbig }
404dfc79e89SEdwin Groothuis --base;
405dfc79e89SEdwin Groothuis yday += isleap_sum(year, base) ?
406b9b51fb7SWolfgang Helbig DAYSPERLYEAR :
407b9b51fb7SWolfgang Helbig DAYSPERNYEAR;
408b9b51fb7SWolfgang Helbig }
409b9b51fb7SWolfgang Helbig #ifdef XPG4_1994_04_09
410dfc79e89SEdwin Groothuis if ((w == 52 &&
411dfc79e89SEdwin Groothuis t->tm_mon == TM_JANUARY) ||
412dfc79e89SEdwin Groothuis (w == 1 &&
413dfc79e89SEdwin Groothuis t->tm_mon == TM_DECEMBER))
414b9b51fb7SWolfgang Helbig w = 53;
415b9b51fb7SWolfgang Helbig #endif /* defined XPG4_1994_04_09 */
416b9b51fb7SWolfgang Helbig if (*format == 'V')
417d9506686SXin LI pt = _conv(w, fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex],
418e66c50c7SPedro F. Giffuni pt, ptlim, loc);
419b9b51fb7SWolfgang Helbig else if (*format == 'g') {
420cdff05faSStefan Farfeleder *warnp = IN_ALL;
421dfc79e89SEdwin Groothuis pt = _yconv(year, base, 0, 1,
422e66c50c7SPedro F. Giffuni pt, ptlim, loc);
423dfc79e89SEdwin Groothuis } else pt = _yconv(year, base, 1, 1,
424e66c50c7SPedro F. Giffuni pt, ptlim, loc);
42548d96b17SGarrett Wollman }
42648d96b17SGarrett Wollman continue;
42748d96b17SGarrett Wollman case 'v':
42848d96b17SGarrett Wollman /*
4293624c752SPedro F. Giffuni * From Arnold Robbins' strftime version 3.0:
4303624c752SPedro F. Giffuni * "date as dd-bbb-YYYY"
4313624c752SPedro F. Giffuni * (ado, 1993-05-24)
43248d96b17SGarrett Wollman */
4333c87aa1dSDavid Chisnall pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp, loc);
43448d96b17SGarrett Wollman continue;
43548d96b17SGarrett Wollman case 'W':
436cdff05faSStefan Farfeleder pt = _conv((t->tm_yday + DAYSPERWEEK -
43748d96b17SGarrett Wollman (t->tm_wday ?
438cdff05faSStefan Farfeleder (t->tm_wday - 1) :
439cdff05faSStefan Farfeleder (DAYSPERWEEK - 1))) / DAYSPERWEEK,
440e66c50c7SPedro F. Giffuni fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex],
441e66c50c7SPedro F. Giffuni pt, ptlim, loc);
44248d96b17SGarrett Wollman continue;
44348d96b17SGarrett Wollman case 'w':
444e66c50c7SPedro F. Giffuni pt = _conv(t->tm_wday, "%d", pt, ptlim, loc);
44548d96b17SGarrett Wollman continue;
446c28fbb7bSGarrett Wollman case 'X':
4473c87aa1dSDavid Chisnall pt = _fmt(tptr->X_fmt, t, pt, ptlim, warnp, loc);
448c28fbb7bSGarrett Wollman continue;
449c28fbb7bSGarrett Wollman case 'x':
450cdff05faSStefan Farfeleder {
451cdff05faSStefan Farfeleder int warn2 = IN_SOME;
452cdff05faSStefan Farfeleder
4533c87aa1dSDavid Chisnall pt = _fmt(tptr->x_fmt, t, pt, ptlim, &warn2, loc);
454cdff05faSStefan Farfeleder if (warn2 == IN_ALL)
455cdff05faSStefan Farfeleder warn2 = IN_THIS;
456cdff05faSStefan Farfeleder if (warn2 > *warnp)
457cdff05faSStefan Farfeleder *warnp = warn2;
458cdff05faSStefan Farfeleder }
459c28fbb7bSGarrett Wollman continue;
46048d96b17SGarrett Wollman case 'y':
461cdff05faSStefan Farfeleder *warnp = IN_ALL;
462dfc79e89SEdwin Groothuis pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1,
463e66c50c7SPedro F. Giffuni pt, ptlim, loc);
46448d96b17SGarrett Wollman continue;
46548d96b17SGarrett Wollman case 'Y':
466dfc79e89SEdwin Groothuis pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1,
467e66c50c7SPedro F. Giffuni pt, ptlim, loc);
46848d96b17SGarrett Wollman continue;
46948d96b17SGarrett Wollman case 'Z':
470cdff05faSStefan Farfeleder #ifdef TM_ZONE
471cdff05faSStefan Farfeleder if (t->TM_ZONE != NULL)
472cdff05faSStefan Farfeleder pt = _add(t->TM_ZONE, pt, ptlim);
47348d96b17SGarrett Wollman else
474cdff05faSStefan Farfeleder #endif /* defined TM_ZONE */
475cdff05faSStefan Farfeleder if (t->tm_isdst >= 0)
476cdff05faSStefan Farfeleder pt = _add(tzname[t->tm_isdst != 0],
47748d96b17SGarrett Wollman pt, ptlim);
478cdff05faSStefan Farfeleder /*
4793624c752SPedro F. Giffuni * C99 says that %Z must be replaced by the
4803624c752SPedro F. Giffuni * empty string if the time zone is not
4813624c752SPedro F. Giffuni * determinable.
482cdff05faSStefan Farfeleder */
48348d96b17SGarrett Wollman continue;
484208b5822SJoerg Wunsch case 'z':
485208b5822SJoerg Wunsch {
486cdff05faSStefan Farfeleder int diff;
487cdff05faSStefan Farfeleder char const * sign;
488cdff05faSStefan Farfeleder
489cdff05faSStefan Farfeleder if (t->tm_isdst < 0)
490cdff05faSStefan Farfeleder continue;
491cdff05faSStefan Farfeleder #ifdef TM_GMTOFF
492cdff05faSStefan Farfeleder diff = t->TM_GMTOFF;
493cdff05faSStefan Farfeleder #else /* !defined TM_GMTOFF */
494cdff05faSStefan Farfeleder /*
4953624c752SPedro F. Giffuni * C99 says that the UTC offset must
4963624c752SPedro F. Giffuni * be computed by looking only at
4973624c752SPedro F. Giffuni * tm_isdst. This requirement is
4983624c752SPedro F. Giffuni * incorrect, since it means the code
4993624c752SPedro F. Giffuni * must rely on magic (in this case
5003624c752SPedro F. Giffuni * altzone and timezone), and the
5013624c752SPedro F. Giffuni * magic might not have the correct
5023624c752SPedro F. Giffuni * offset. Doing things correctly is
5033624c752SPedro F. Giffuni * tricky and requires disobeying C99;
5043624c752SPedro F. Giffuni * see GNU C strftime for details.
5053624c752SPedro F. Giffuni * For now, punt and conform to the
5063624c752SPedro F. Giffuni * standard, even though it's incorrect.
5073624c752SPedro F. Giffuni *
5083624c752SPedro F. Giffuni * C99 says that %z must be replaced by the
5093624c752SPedro F. Giffuni * empty string if the time zone is not
5103624c752SPedro F. Giffuni * determinable, so output nothing if the
5113624c752SPedro F. Giffuni * appropriate variables are not available.
512cdff05faSStefan Farfeleder */
513cdff05faSStefan Farfeleder if (t->tm_isdst == 0)
514cdff05faSStefan Farfeleder #ifdef USG_COMPAT
515cdff05faSStefan Farfeleder diff = -timezone;
516cdff05faSStefan Farfeleder #else /* !defined USG_COMPAT */
517cdff05faSStefan Farfeleder continue;
518cdff05faSStefan Farfeleder #endif /* !defined USG_COMPAT */
519cdff05faSStefan Farfeleder else
520cdff05faSStefan Farfeleder #ifdef ALTZONE
521cdff05faSStefan Farfeleder diff = -altzone;
522cdff05faSStefan Farfeleder #else /* !defined ALTZONE */
523cdff05faSStefan Farfeleder continue;
524cdff05faSStefan Farfeleder #endif /* !defined ALTZONE */
525cdff05faSStefan Farfeleder #endif /* !defined TM_GMTOFF */
526cdff05faSStefan Farfeleder if (diff < 0) {
527cdff05faSStefan Farfeleder sign = "-";
528cdff05faSStefan Farfeleder diff = -diff;
5293624c752SPedro F. Giffuni } else
5303624c752SPedro F. Giffuni sign = "+";
531cdff05faSStefan Farfeleder pt = _add(sign, pt, ptlim);
532dfc79e89SEdwin Groothuis diff /= SECSPERMIN;
533dfc79e89SEdwin Groothuis diff = (diff / MINSPERHOUR) * 100 +
534dfc79e89SEdwin Groothuis (diff % MINSPERHOUR);
535dfc79e89SEdwin Groothuis pt = _conv(diff,
536e66c50c7SPedro F. Giffuni fmt_padding[PAD_FMT_YEAR][PadIndex],
537e66c50c7SPedro F. Giffuni pt, ptlim, loc);
538208b5822SJoerg Wunsch }
539208b5822SJoerg Wunsch continue;
540c28fbb7bSGarrett Wollman case '+':
541cdff05faSStefan Farfeleder pt = _fmt(tptr->date_fmt, t, pt, ptlim,
5423c87aa1dSDavid Chisnall warnp, loc);
543c28fbb7bSGarrett Wollman continue;
544d9506686SXin LI case '-':
545d9506686SXin LI if (PadIndex != PAD_DEFAULT)
546d9506686SXin LI break;
547d9506686SXin LI PadIndex = PAD_LESS;
548d9506686SXin LI goto label;
549d9506686SXin LI case '_':
550d9506686SXin LI if (PadIndex != PAD_DEFAULT)
551d9506686SXin LI break;
552d9506686SXin LI PadIndex = PAD_SPACE;
553d9506686SXin LI goto label;
554d9506686SXin LI case '0':
555d9506686SXin LI if (PadIndex != PAD_DEFAULT)
556d9506686SXin LI break;
557d9506686SXin LI PadIndex = PAD_ZERO;
558d9506686SXin LI goto label;
55948d96b17SGarrett Wollman case '%':
56048d96b17SGarrett Wollman /*
5613624c752SPedro F. Giffuni * X311J/88-090 (4.12.3.5): if conversion char is
5623624c752SPedro F. Giffuni * undefined, behavior is undefined. Print out the
5633624c752SPedro F. Giffuni * character itself as printf(3) also does.
56448d96b17SGarrett Wollman */
56548d96b17SGarrett Wollman default:
56648d96b17SGarrett Wollman break;
56748d96b17SGarrett Wollman }
56848d96b17SGarrett Wollman }
56948d96b17SGarrett Wollman if (pt == ptlim)
57048d96b17SGarrett Wollman break;
57148d96b17SGarrett Wollman *pt++ = *format;
57248d96b17SGarrett Wollman }
5733624c752SPedro F. Giffuni return (pt);
57448d96b17SGarrett Wollman }
57548d96b17SGarrett Wollman
57648d96b17SGarrett Wollman static char *
_conv(const int n,const char * const format,char * const pt,const char * const ptlim,locale_t loc)57785cef632SCraig Rodrigues _conv(const int n, const char * const format, char * const pt,
57885cef632SCraig Rodrigues const char * const ptlim, locale_t loc)
57948d96b17SGarrett Wollman {
58048d96b17SGarrett Wollman char buf[INT_STRLEN_MAXIMUM(int) + 1];
58148d96b17SGarrett Wollman
582e66c50c7SPedro F. Giffuni (void) sprintf_l(buf, loc, format, n);
58348d96b17SGarrett Wollman return _add(buf, pt, ptlim);
58448d96b17SGarrett Wollman }
58548d96b17SGarrett Wollman
58648d96b17SGarrett Wollman static char *
_add(const char * str,char * pt,const char * const ptlim)58785cef632SCraig Rodrigues _add(const char *str, char *pt, const char * const ptlim)
58848d96b17SGarrett Wollman {
58948d96b17SGarrett Wollman while (pt < ptlim && (*pt = *str++) != '\0')
59048d96b17SGarrett Wollman ++pt;
5913624c752SPedro F. Giffuni return (pt);
59248d96b17SGarrett Wollman }
593dfc79e89SEdwin Groothuis
594dfc79e89SEdwin Groothuis /*
5953624c752SPedro F. Giffuni * POSIX and the C Standard are unclear or inconsistent about
5963624c752SPedro F. Giffuni * what %C and %y do if the year is negative or exceeds 9999.
5973624c752SPedro F. Giffuni * Use the convention that %C concatenated with %y yields the
5983624c752SPedro F. Giffuni * same output as %Y, and that %Y contains at least 4 bytes,
5993624c752SPedro F. Giffuni * with more only if necessary.
600dfc79e89SEdwin Groothuis */
601dfc79e89SEdwin Groothuis
602dfc79e89SEdwin Groothuis static char *
_yconv(const int a,const int b,const int convert_top,const int convert_yy,char * pt,const char * const ptlim,locale_t loc)60385cef632SCraig Rodrigues _yconv(const int a, const int b, const int convert_top, const int convert_yy,
60485cef632SCraig Rodrigues char *pt, const char * const ptlim, locale_t loc)
605dfc79e89SEdwin Groothuis {
606dfc79e89SEdwin Groothuis register int lead;
607dfc79e89SEdwin Groothuis register int trail;
608dfc79e89SEdwin Groothuis
609dfc79e89SEdwin Groothuis #define DIVISOR 100
610dfc79e89SEdwin Groothuis trail = a % DIVISOR + b % DIVISOR;
611dfc79e89SEdwin Groothuis lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
612dfc79e89SEdwin Groothuis trail %= DIVISOR;
613dfc79e89SEdwin Groothuis if (trail < 0 && lead > 0) {
614dfc79e89SEdwin Groothuis trail += DIVISOR;
615dfc79e89SEdwin Groothuis --lead;
616dfc79e89SEdwin Groothuis } else if (lead < 0 && trail > 0) {
617dfc79e89SEdwin Groothuis trail -= DIVISOR;
618dfc79e89SEdwin Groothuis ++lead;
619dfc79e89SEdwin Groothuis }
620dfc79e89SEdwin Groothuis if (convert_top) {
621dfc79e89SEdwin Groothuis if (lead == 0 && trail < 0)
622dfc79e89SEdwin Groothuis pt = _add("-0", pt, ptlim);
623e66c50c7SPedro F. Giffuni else pt = _conv(lead, "%02d", pt, ptlim, loc);
624dfc79e89SEdwin Groothuis }
625dfc79e89SEdwin Groothuis if (convert_yy)
626e66c50c7SPedro F. Giffuni pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt,
627e66c50c7SPedro F. Giffuni ptlim, loc);
6283624c752SPedro F. Giffuni return (pt);
629dfc79e89SEdwin Groothuis }
630