strftime.c revision 1.51 1 1.51 christos /* $NetBSD: strftime.c,v 1.51 2022/12/11 17:57:23 christos Exp $ */
2 1.36 christos
3 1.39 christos /* Convert a broken-down timestamp to a string. */
4 1.36 christos
5 1.36 christos /* Copyright 1989 The Regents of the University of California.
6 1.36 christos All rights reserved.
7 1.36 christos
8 1.36 christos Redistribution and use in source and binary forms, with or without
9 1.36 christos modification, are permitted provided that the following conditions
10 1.36 christos are met:
11 1.36 christos 1. Redistributions of source code must retain the above copyright
12 1.36 christos notice, this list of conditions and the following disclaimer.
13 1.36 christos 2. Redistributions in binary form must reproduce the above copyright
14 1.36 christos notice, this list of conditions and the following disclaimer in the
15 1.36 christos documentation and/or other materials provided with the distribution.
16 1.36 christos 3. Neither the name of the University nor the names of its contributors
17 1.36 christos may be used to endorse or promote products derived from this software
18 1.36 christos without specific prior written permission.
19 1.36 christos
20 1.36 christos THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
21 1.36 christos ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.36 christos IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.36 christos ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 1.36 christos FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.36 christos DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.36 christos OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.36 christos HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.36 christos LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.36 christos OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.36 christos SUCH DAMAGE. */
31 1.1 mrg
32 1.3 christos #include <sys/cdefs.h>
33 1.1 mrg #if defined(LIBC_SCCS) && !defined(lint)
34 1.3 christos #if 0
35 1.13 kleink static char elsieid[] = "@(#)strftime.c 7.64";
36 1.20 mlelstv static char elsieid[] = "@(#)strftime.c 8.3";
37 1.3 christos #else
38 1.51 christos __RCSID("$NetBSD: strftime.c,v 1.51 2022/12/11 17:57:23 christos Exp $");
39 1.3 christos #endif
40 1.1 mrg #endif /* LIBC_SCCS and not lint */
41 1.1 mrg
42 1.4 jtc #include "namespace.h"
43 1.12 kleink
44 1.25 joerg #include <stddef.h>
45 1.30 christos #include <assert.h>
46 1.25 joerg #include <locale.h>
47 1.25 joerg #include "setlocale_local.h"
48 1.25 joerg
49 1.12 kleink /*
50 1.36 christos ** Based on the UCB version with the copyright notice appearing above.
51 1.24 christos **
52 1.12 kleink ** This is ANSIish only when "multibyte character == plain character".
53 1.12 kleink */
54 1.12 kleink
55 1.11 taca #include "private.h"
56 1.12 kleink
57 1.16 kleink /*
58 1.16 kleink ** We don't use these extensions in strftime operation even when
59 1.16 kleink ** supported by the local tzcode configuration. A strictly
60 1.16 kleink ** conforming C application may leave them in undefined state.
61 1.16 kleink */
62 1.16 kleink
63 1.15 kleink #ifdef _LIBC
64 1.15 kleink #undef TM_ZONE
65 1.16 kleink #undef TM_GMTOFF
66 1.15 kleink #endif
67 1.15 kleink
68 1.39 christos #include <fcntl.h>
69 1.39 christos #include <locale.h>
70 1.40 christos #include <stdio.h>
71 1.40 christos
72 1.40 christos #ifndef DEPRECATE_TWO_DIGIT_YEARS
73 1.40 christos # define DEPRECATE_TWO_DIGIT_YEARS false
74 1.40 christos #endif
75 1.12 kleink
76 1.21 christos #ifdef __weak_alias
77 1.25 joerg __weak_alias(strftime_l, _strftime_l)
78 1.25 joerg __weak_alias(strftime_lz, _strftime_lz)
79 1.21 christos __weak_alias(strftime_z, _strftime_z)
80 1.21 christos #endif
81 1.21 christos
82 1.12 kleink #include "sys/localedef.h"
83 1.25 joerg #define _TIME_LOCALE(loc) \
84 1.25 joerg ((_TimeLocale *)((loc)->part_impl[(size_t)LC_TIME]))
85 1.20 mlelstv #define c_fmt d_t_fmt
86 1.12 kleink
87 1.40 christos enum warn { IN_NONE, IN_SOME, IN_THIS, IN_ALL };
88 1.40 christos
89 1.20 mlelstv static char * _add(const char *, char *, const char *);
90 1.44 christos static char * _conv(int, const char *, char *, const char *, locale_t);
91 1.21 christos static char * _fmt(const timezone_t, const char *, const struct tm *, char *,
92 1.40 christos const char *, enum warn *, locale_t);
93 1.44 christos static char * _yconv(int, int, bool, bool, char *, const char *, locale_t);
94 1.12 kleink
95 1.12 kleink #ifndef YEAR_2000_NAME
96 1.50 christos # define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
97 1.12 kleink #endif /* !defined YEAR_2000_NAME */
98 1.12 kleink
99 1.44 christos #define IN_NONE 0
100 1.44 christos #define IN_SOME 1
101 1.44 christos #define IN_THIS 2
102 1.44 christos #define IN_ALL 3
103 1.44 christos
104 1.44 christos #define PAD_DEFAULT 0
105 1.44 christos #define PAD_LESS 1
106 1.44 christos #define PAD_SPACE 2
107 1.44 christos #define PAD_ZERO 3
108 1.44 christos
109 1.44 christos static const char fmt_padding[][4][5] = {
110 1.44 christos /* DEFAULT, LESS, SPACE, ZERO */
111 1.44 christos #define PAD_FMT_MONTHDAY 0
112 1.44 christos #define PAD_FMT_HMS 0
113 1.44 christos #define PAD_FMT_CENTURY 0
114 1.44 christos #define PAD_FMT_SHORTYEAR 0
115 1.44 christos #define PAD_FMT_MONTH 0
116 1.44 christos #define PAD_FMT_WEEKOFYEAR 0
117 1.44 christos #define PAD_FMT_DAYOFMONTH 0
118 1.44 christos { "%02d", "%d", "%2d", "%02d" },
119 1.44 christos #define PAD_FMT_SDAYOFMONTH 1
120 1.44 christos #define PAD_FMT_SHMS 1
121 1.44 christos { "%2d", "%d", "%2d", "%02d" },
122 1.44 christos #define PAD_FMT_DAYOFYEAR 2
123 1.44 christos { "%03d", "%d", "%3d", "%03d" },
124 1.44 christos #define PAD_FMT_YEAR 3
125 1.44 christos { "%04d", "%d", "%4d", "%04d" }
126 1.44 christos };
127 1.44 christos
128 1.1 mrg size_t
129 1.25 joerg strftime_z(const timezone_t sp, char * __restrict s, size_t maxsize,
130 1.25 joerg const char * __restrict format, const struct tm * __restrict t)
131 1.25 joerg {
132 1.26 joerg return strftime_lz(sp, s, maxsize, format, t, _current_locale());
133 1.25 joerg }
134 1.25 joerg
135 1.33 christos #if HAVE_STRFTIME_L
136 1.33 christos size_t
137 1.33 christos strftime_l(char *s, size_t maxsize, char const *format, struct tm const *t,
138 1.51 christos ATTRIBUTE_MAYBE_UNUSED locale_t locale)
139 1.33 christos {
140 1.33 christos /* Just call strftime, as only the C locale is supported. */
141 1.33 christos return strftime(s, maxsize, format, t);
142 1.33 christos }
143 1.33 christos #endif
144 1.33 christos
145 1.25 joerg size_t
146 1.25 joerg strftime_lz(const timezone_t sp, char *const s, const size_t maxsize,
147 1.25 joerg const char *const format, const struct tm *const t, locale_t loc)
148 1.1 mrg {
149 1.12 kleink char * p;
150 1.48 christos int saved_errno = errno;
151 1.40 christos enum warn warn = IN_NONE;
152 1.5 kleink
153 1.37 christos p = _fmt(sp, format, t, s, s + maxsize, &warn, loc);
154 1.48 christos if (!p) {
155 1.48 christos errno = EOVERFLOW;
156 1.48 christos return 0;
157 1.48 christos }
158 1.40 christos if (/*CONSTCOND*/DEPRECATE_TWO_DIGIT_YEARS
159 1.40 christos && warn != IN_NONE && getenv(YEAR_2000_NAME)) {
160 1.12 kleink (void) fprintf(stderr, "\n");
161 1.37 christos (void) fprintf(stderr, "strftime format \"%s\" ", format);
162 1.12 kleink (void) fprintf(stderr, "yields only two digits of years in ");
163 1.12 kleink if (warn == IN_SOME)
164 1.12 kleink (void) fprintf(stderr, "some locales");
165 1.12 kleink else if (warn == IN_THIS)
166 1.12 kleink (void) fprintf(stderr, "the current locale");
167 1.12 kleink else (void) fprintf(stderr, "all locales");
168 1.12 kleink (void) fprintf(stderr, "\n");
169 1.12 kleink }
170 1.48 christos if (p == s + maxsize) {
171 1.48 christos errno = ERANGE;
172 1.12 kleink return 0;
173 1.48 christos }
174 1.12 kleink *p = '\0';
175 1.48 christos errno = saved_errno;
176 1.12 kleink return p - s;
177 1.1 mrg }
178 1.1 mrg
179 1.12 kleink static char *
180 1.34 christos _fmt(const timezone_t sp, const char *format, const struct tm *t, char *pt,
181 1.40 christos const char *ptlim, enum warn *warnp, locale_t loc)
182 1.1 mrg {
183 1.44 christos int Ealternative, Oalternative, PadIndex;
184 1.44 christos _TimeLocale *tptr = _TIME_LOCALE(loc);
185 1.44 christos
186 1.12 kleink for ( ; *format; ++format) {
187 1.1 mrg if (*format == '%') {
188 1.44 christos Ealternative = 0;
189 1.44 christos Oalternative = 0;
190 1.44 christos PadIndex = PAD_DEFAULT;
191 1.12 kleink label:
192 1.12 kleink switch (*++format) {
193 1.1 mrg case '\0':
194 1.1 mrg --format;
195 1.1 mrg break;
196 1.1 mrg case 'A':
197 1.12 kleink pt = _add((t->tm_wday < 0 ||
198 1.12 kleink t->tm_wday >= DAYSPERWEEK) ?
199 1.44 christos "?" : tptr->day[t->tm_wday],
200 1.12 kleink pt, ptlim);
201 1.1 mrg continue;
202 1.1 mrg case 'a':
203 1.12 kleink pt = _add((t->tm_wday < 0 ||
204 1.12 kleink t->tm_wday >= DAYSPERWEEK) ?
205 1.44 christos "?" : tptr->abday[t->tm_wday],
206 1.12 kleink pt, ptlim);
207 1.1 mrg continue;
208 1.1 mrg case 'B':
209 1.12 kleink pt = _add((t->tm_mon < 0 ||
210 1.12 kleink t->tm_mon >= MONSPERYEAR) ?
211 1.44 christos "?" :
212 1.44 christos /* no alt_month in _TimeLocale */
213 1.44 christos (Oalternative ? tptr->mon/*alt_month*/:
214 1.44 christos tptr->mon)[t->tm_mon],
215 1.12 kleink pt, ptlim);
216 1.1 mrg continue;
217 1.1 mrg case 'b':
218 1.1 mrg case 'h':
219 1.12 kleink pt = _add((t->tm_mon < 0 ||
220 1.12 kleink t->tm_mon >= MONSPERYEAR) ?
221 1.44 christos "?" : tptr->abmon[t->tm_mon],
222 1.12 kleink pt, ptlim);
223 1.1 mrg continue;
224 1.1 mrg case 'C':
225 1.12 kleink /*
226 1.12 kleink ** %C used to do a...
227 1.12 kleink ** _fmt("%a %b %e %X %Y", t);
228 1.12 kleink ** ...whereas now POSIX 1003.2 calls for
229 1.12 kleink ** something completely different.
230 1.12 kleink ** (ado, 1993-05-24)
231 1.12 kleink */
232 1.33 christos pt = _yconv(t->tm_year, TM_YEAR_BASE,
233 1.44 christos true, false, pt, ptlim, loc);
234 1.1 mrg continue;
235 1.1 mrg case 'c':
236 1.12 kleink {
237 1.40 christos enum warn warn2 = IN_SOME;
238 1.12 kleink
239 1.44 christos pt = _fmt(sp, tptr->c_fmt, t, pt,
240 1.25 joerg ptlim, &warn2, loc);
241 1.12 kleink if (warn2 == IN_ALL)
242 1.12 kleink warn2 = IN_THIS;
243 1.12 kleink if (warn2 > *warnp)
244 1.12 kleink *warnp = warn2;
245 1.12 kleink }
246 1.1 mrg continue;
247 1.1 mrg case 'D':
248 1.25 joerg pt = _fmt(sp, "%m/%d/%y", t, pt, ptlim, warnp,
249 1.25 joerg loc);
250 1.1 mrg continue;
251 1.1 mrg case 'd':
252 1.44 christos pt = _conv(t->tm_mday,
253 1.44 christos fmt_padding[PAD_FMT_DAYOFMONTH][PadIndex],
254 1.44 christos pt, ptlim, loc);
255 1.1 mrg continue;
256 1.12 kleink case 'E':
257 1.44 christos if (Ealternative || Oalternative)
258 1.44 christos break;
259 1.44 christos Ealternative++;
260 1.44 christos goto label;
261 1.12 kleink case 'O':
262 1.12 kleink /*
263 1.40 christos ** Locale modifiers of C99 and later.
264 1.12 kleink ** The sequences
265 1.12 kleink ** %Ec %EC %Ex %EX %Ey %EY
266 1.12 kleink ** %Od %oe %OH %OI %Om %OM
267 1.12 kleink ** %OS %Ou %OU %OV %Ow %OW %Oy
268 1.41 christos ** are supposed to provide alternative
269 1.12 kleink ** representations.
270 1.12 kleink */
271 1.44 christos if (Ealternative || Oalternative)
272 1.44 christos break;
273 1.44 christos Oalternative++;
274 1.12 kleink goto label;
275 1.1 mrg case 'e':
276 1.44 christos pt = _conv(t->tm_mday,
277 1.44 christos fmt_padding[PAD_FMT_SDAYOFMONTH][PadIndex],
278 1.44 christos pt, ptlim, loc);
279 1.10 kleink continue;
280 1.10 kleink case 'F':
281 1.25 joerg pt = _fmt(sp, "%Y-%m-%d", t, pt, ptlim, warnp,
282 1.25 joerg loc);
283 1.1 mrg continue;
284 1.1 mrg case 'H':
285 1.44 christos pt = _conv(t->tm_hour,
286 1.44 christos fmt_padding[PAD_FMT_HMS][PadIndex],
287 1.44 christos pt, ptlim, loc);
288 1.1 mrg continue;
289 1.1 mrg case 'I':
290 1.12 kleink pt = _conv((t->tm_hour % 12) ?
291 1.44 christos (t->tm_hour % 12) : 12,
292 1.44 christos fmt_padding[PAD_FMT_HMS][PadIndex],
293 1.44 christos pt, ptlim, loc);
294 1.1 mrg continue;
295 1.1 mrg case 'j':
296 1.44 christos pt = _conv(t->tm_yday + 1,
297 1.44 christos fmt_padding[PAD_FMT_DAYOFYEAR][PadIndex],
298 1.44 christos pt, ptlim, loc);
299 1.1 mrg continue;
300 1.1 mrg case 'k':
301 1.12 kleink /*
302 1.12 kleink ** This used to be...
303 1.12 kleink ** _conv(t->tm_hour % 12 ?
304 1.12 kleink ** t->tm_hour % 12 : 12, 2, ' ');
305 1.12 kleink ** ...and has been changed to the below to
306 1.12 kleink ** match SunOS 4.1.1 and Arnold Robbins'
307 1.20 mlelstv ** strftime version 3.0. That is, "%k" and
308 1.12 kleink ** "%l" have been swapped.
309 1.12 kleink ** (ado, 1993-05-24)
310 1.12 kleink */
311 1.44 christos pt = _conv(t->tm_hour,
312 1.44 christos fmt_padding[PAD_FMT_SHMS][PadIndex],
313 1.44 christos pt, ptlim, loc);
314 1.12 kleink continue;
315 1.12 kleink #ifdef KITCHEN_SINK
316 1.12 kleink case 'K':
317 1.12 kleink /*
318 1.12 kleink ** After all this time, still unclaimed!
319 1.12 kleink */
320 1.12 kleink pt = _add("kitchen sink", pt, ptlim);
321 1.1 mrg continue;
322 1.12 kleink #endif /* defined KITCHEN_SINK */
323 1.1 mrg case 'l':
324 1.12 kleink /*
325 1.12 kleink ** This used to be...
326 1.12 kleink ** _conv(t->tm_hour, 2, ' ');
327 1.12 kleink ** ...and has been changed to the below to
328 1.12 kleink ** match SunOS 4.1.1 and Arnold Robbin's
329 1.20 mlelstv ** strftime version 3.0. That is, "%k" and
330 1.12 kleink ** "%l" have been swapped.
331 1.12 kleink ** (ado, 1993-05-24)
332 1.12 kleink */
333 1.12 kleink pt = _conv((t->tm_hour % 12) ?
334 1.12 kleink (t->tm_hour % 12) : 12,
335 1.44 christos fmt_padding[PAD_FMT_SHMS][PadIndex],
336 1.44 christos pt, ptlim, loc);
337 1.1 mrg continue;
338 1.1 mrg case 'M':
339 1.44 christos pt = _conv(t->tm_min,
340 1.44 christos fmt_padding[PAD_FMT_HMS][PadIndex],
341 1.44 christos pt, ptlim, loc);
342 1.1 mrg continue;
343 1.1 mrg case 'm':
344 1.44 christos pt = _conv(t->tm_mon + 1,
345 1.44 christos fmt_padding[PAD_FMT_MONTH][PadIndex],
346 1.44 christos pt, ptlim, loc);
347 1.1 mrg continue;
348 1.1 mrg case 'n':
349 1.12 kleink pt = _add("\n", pt, ptlim);
350 1.1 mrg continue;
351 1.1 mrg case 'p':
352 1.12 kleink pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
353 1.44 christos tptr->am_pm[1] :
354 1.44 christos tptr->am_pm[0],
355 1.12 kleink pt, ptlim);
356 1.1 mrg continue;
357 1.1 mrg case 'R':
358 1.25 joerg pt = _fmt(sp, "%H:%M", t, pt, ptlim, warnp,
359 1.25 joerg loc);
360 1.1 mrg continue;
361 1.1 mrg case 'r':
362 1.44 christos pt = _fmt(sp, tptr->t_fmt_ampm, t,
363 1.25 joerg pt, ptlim, warnp, loc);
364 1.1 mrg continue;
365 1.1 mrg case 'S':
366 1.44 christos pt = _conv(t->tm_sec,
367 1.44 christos fmt_padding[PAD_FMT_HMS][PadIndex],
368 1.44 christos pt, ptlim, loc);
369 1.1 mrg continue;
370 1.1 mrg case 's':
371 1.12 kleink {
372 1.12 kleink struct tm tm;
373 1.12 kleink char buf[INT_STRLEN_MAXIMUM(
374 1.12 kleink time_t) + 1];
375 1.12 kleink time_t mkt;
376 1.12 kleink
377 1.51 christos tm.tm_sec = t->tm_sec;
378 1.51 christos tm.tm_min = t->tm_min;
379 1.51 christos tm.tm_hour = t->tm_hour;
380 1.51 christos tm.tm_mday = t->tm_mday;
381 1.51 christos tm.tm_mon = t->tm_mon;
382 1.51 christos tm.tm_year = t->tm_year;
383 1.51 christos tm.tm_isdst = t->tm_isdst;
384 1.51 christos #if defined TM_GMTOFF && ! UNINIT_TRAP
385 1.51 christos tm.TM_GMTOFF = t->TM_GMTOFF;
386 1.51 christos #endif
387 1.43 christos mkt = mktime_z(sp, &tm);
388 1.51 christos /* If mktime fails, %s expands to the
389 1.51 christos value of (time_t) -1 as a failure
390 1.51 christos marker; this is better in practice
391 1.51 christos than strftime failing. */
392 1.12 kleink /* CONSTCOND */
393 1.49 christos if (TYPE_SIGNED(time_t)) {
394 1.49 christos intmax_t n = mkt;
395 1.27 christos (void)snprintf(buf, sizeof(buf),
396 1.49 christos "%"PRIdMAX, n);
397 1.49 christos } else {
398 1.49 christos uintmax_t n = mkt;
399 1.49 christos (void)snprintf(buf, sizeof(buf),
400 1.49 christos "%"PRIuMAX, n);
401 1.49 christos }
402 1.12 kleink pt = _add(buf, pt, ptlim);
403 1.12 kleink }
404 1.1 mrg continue;
405 1.1 mrg case 'T':
406 1.25 joerg pt = _fmt(sp, "%H:%M:%S", t, pt, ptlim, warnp,
407 1.25 joerg loc);
408 1.1 mrg continue;
409 1.1 mrg case 't':
410 1.12 kleink pt = _add("\t", pt, ptlim);
411 1.1 mrg continue;
412 1.1 mrg case 'U':
413 1.12 kleink pt = _conv((t->tm_yday + DAYSPERWEEK -
414 1.44 christos t->tm_wday) / DAYSPERWEEK,
415 1.44 christos fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex],
416 1.44 christos pt, ptlim, loc);
417 1.1 mrg continue;
418 1.1 mrg case 'u':
419 1.12 kleink /*
420 1.12 kleink ** From Arnold Robbins' strftime version 3.0:
421 1.12 kleink ** "ISO 8601: Weekday as a decimal number
422 1.12 kleink ** [1 (Monday) - 7]"
423 1.12 kleink ** (ado, 1993-05-24)
424 1.12 kleink */
425 1.12 kleink pt = _conv((t->tm_wday == 0) ?
426 1.12 kleink DAYSPERWEEK : t->tm_wday,
427 1.44 christos "%d", pt, ptlim, loc);
428 1.1 mrg continue;
429 1.8 augustss case 'V': /* ISO 8601 week number */
430 1.8 augustss case 'G': /* ISO 8601 year (four digits) */
431 1.8 augustss case 'g': /* ISO 8601 year (two digits) */
432 1.8 augustss /*
433 1.20 mlelstv ** From Arnold Robbins' strftime version 3.0: "the week number of the
434 1.8 augustss ** year (the first Monday as the first day of week 1) as a decimal number
435 1.8 augustss ** (01-53)."
436 1.8 augustss ** (ado, 1993-05-24)
437 1.8 augustss **
438 1.40 christos ** From <https://www.cl.cam.ac.uk/~mgk25/iso-time.html> by Markus Kuhn:
439 1.8 augustss ** "Week 01 of a year is per definition the first week which has the
440 1.8 augustss ** Thursday in this year, which is equivalent to the week which contains
441 1.8 augustss ** the fourth day of January. In other words, the first week of a new year
442 1.8 augustss ** is the week which has the majority of its days in the new year. Week 01
443 1.8 augustss ** might also contain days from the previous year and the week before week
444 1.8 augustss ** 01 of a year is the last week (52 or 53) of the previous year even if
445 1.8 augustss ** it contains days from the new year. A week starts with Monday (day 1)
446 1.20 mlelstv ** and ends with Sunday (day 7). For example, the first week of the year
447 1.8 augustss ** 1997 lasts from 1996-12-30 to 1997-01-05..."
448 1.8 augustss ** (ado, 1996-01-02)
449 1.8 augustss */
450 1.1 mrg {
451 1.8 augustss int year;
452 1.20 mlelstv int base;
453 1.8 augustss int yday;
454 1.8 augustss int wday;
455 1.8 augustss int w;
456 1.8 augustss
457 1.20 mlelstv year = t->tm_year;
458 1.20 mlelstv base = TM_YEAR_BASE;
459 1.8 augustss yday = t->tm_yday;
460 1.8 augustss wday = t->tm_wday;
461 1.8 augustss for ( ; ; ) {
462 1.8 augustss int len;
463 1.8 augustss int bot;
464 1.8 augustss int top;
465 1.8 augustss
466 1.20 mlelstv len = isleap_sum(year, base) ?
467 1.8 augustss DAYSPERLYEAR :
468 1.8 augustss DAYSPERNYEAR;
469 1.8 augustss /*
470 1.8 augustss ** What yday (-3 ... 3) does
471 1.8 augustss ** the ISO year begin on?
472 1.8 augustss */
473 1.8 augustss bot = ((yday + 11 - wday) %
474 1.8 augustss DAYSPERWEEK) - 3;
475 1.8 augustss /*
476 1.8 augustss ** What yday does the NEXT
477 1.8 augustss ** ISO year begin on?
478 1.8 augustss */
479 1.8 augustss top = bot -
480 1.8 augustss (len % DAYSPERWEEK);
481 1.8 augustss if (top < -3)
482 1.8 augustss top += DAYSPERWEEK;
483 1.8 augustss top += len;
484 1.8 augustss if (yday >= top) {
485 1.20 mlelstv ++base;
486 1.8 augustss w = 1;
487 1.8 augustss break;
488 1.8 augustss }
489 1.8 augustss if (yday >= bot) {
490 1.8 augustss w = 1 + ((yday - bot) /
491 1.8 augustss DAYSPERWEEK);
492 1.8 augustss break;
493 1.8 augustss }
494 1.20 mlelstv --base;
495 1.20 mlelstv yday += isleap_sum(year, base) ?
496 1.8 augustss DAYSPERLYEAR :
497 1.8 augustss DAYSPERNYEAR;
498 1.8 augustss }
499 1.8 augustss #ifdef XPG4_1994_04_09
500 1.20 mlelstv if ((w == 52 &&
501 1.20 mlelstv t->tm_mon == TM_JANUARY) ||
502 1.20 mlelstv (w == 1 &&
503 1.20 mlelstv t->tm_mon == TM_DECEMBER))
504 1.20 mlelstv w = 53;
505 1.8 augustss #endif /* defined XPG4_1994_04_09 */
506 1.12 kleink if (*format == 'V')
507 1.44 christos pt = _conv(w,
508 1.44 christos fmt_padding[
509 1.44 christos PAD_FMT_WEEKOFYEAR][
510 1.44 christos PadIndex], pt, ptlim, loc);
511 1.12 kleink else if (*format == 'g') {
512 1.12 kleink *warnp = IN_ALL;
513 1.33 christos pt = _yconv(year, base,
514 1.33 christos false, true,
515 1.44 christos pt, ptlim, loc);
516 1.33 christos } else pt = _yconv(year, base,
517 1.33 christos true, true,
518 1.44 christos pt, ptlim, loc);
519 1.1 mrg }
520 1.1 mrg continue;
521 1.12 kleink case 'v':
522 1.12 kleink /*
523 1.12 kleink ** From Arnold Robbins' strftime version 3.0:
524 1.12 kleink ** "date as dd-bbb-YYYY"
525 1.12 kleink ** (ado, 1993-05-24)
526 1.12 kleink */
527 1.25 joerg pt = _fmt(sp, "%e-%b-%Y", t, pt, ptlim, warnp,
528 1.25 joerg loc);
529 1.12 kleink continue;
530 1.1 mrg case 'W':
531 1.12 kleink pt = _conv((t->tm_yday + DAYSPERWEEK -
532 1.44 christos (t->tm_wday ?
533 1.44 christos (t->tm_wday - 1) :
534 1.44 christos (DAYSPERWEEK - 1))) / DAYSPERWEEK,
535 1.44 christos fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex],
536 1.44 christos pt, ptlim, loc);
537 1.1 mrg continue;
538 1.1 mrg case 'w':
539 1.44 christos pt = _conv(t->tm_wday, "%d", pt, ptlim, loc);
540 1.12 kleink continue;
541 1.12 kleink case 'X':
542 1.44 christos pt = _fmt(sp, tptr->t_fmt, t, pt,
543 1.25 joerg ptlim, warnp, loc);
544 1.1 mrg continue;
545 1.1 mrg case 'x':
546 1.12 kleink {
547 1.40 christos enum warn warn2 = IN_SOME;
548 1.12 kleink
549 1.44 christos pt = _fmt(sp, tptr->d_fmt, t, pt,
550 1.25 joerg ptlim, &warn2, loc);
551 1.12 kleink if (warn2 == IN_ALL)
552 1.12 kleink warn2 = IN_THIS;
553 1.12 kleink if (warn2 > *warnp)
554 1.12 kleink *warnp = warn2;
555 1.12 kleink }
556 1.1 mrg continue;
557 1.1 mrg case 'y':
558 1.12 kleink *warnp = IN_ALL;
559 1.33 christos pt = _yconv(t->tm_year, TM_YEAR_BASE,
560 1.33 christos false, true,
561 1.44 christos pt, ptlim, loc);
562 1.1 mrg continue;
563 1.1 mrg case 'Y':
564 1.33 christos pt = _yconv(t->tm_year, TM_YEAR_BASE,
565 1.33 christos true, true,
566 1.44 christos pt, ptlim, loc);
567 1.1 mrg continue;
568 1.1 mrg case 'Z':
569 1.11 taca #ifdef TM_ZONE
570 1.35 christos pt = _add(t->TM_ZONE, pt, ptlim);
571 1.40 christos #elif HAVE_TZNAME
572 1.45 christos if (t->tm_isdst >= 0) {
573 1.45 christos int oerrno = errno, dst = t->tm_isdst;
574 1.45 christos const char *z =
575 1.45 christos tzgetname(sp, dst);
576 1.45 christos if (z == NULL)
577 1.45 christos z = tzgetname(sp, !dst);
578 1.46 christos if (z != NULL)
579 1.46 christos pt = _add(z, pt, ptlim);
580 1.45 christos errno = oerrno;
581 1.45 christos }
582 1.40 christos #endif
583 1.12 kleink /*
584 1.40 christos ** C99 and later say that %Z must be
585 1.40 christos ** replaced by the empty string if the
586 1.42 christos ** time zone abbreviation is not
587 1.42 christos ** determinable.
588 1.12 kleink */
589 1.12 kleink continue;
590 1.12 kleink case 'z':
591 1.47 christos #if defined TM_GMTOFF || USG_COMPAT || ALTZONE
592 1.12 kleink {
593 1.30 christos long diff;
594 1.12 kleink char const * sign;
595 1.39 christos bool negative;
596 1.12 kleink
597 1.12 kleink if (t->tm_isdst < 0)
598 1.12 kleink continue;
599 1.38 christos # ifdef TM_GMTOFF
600 1.12 kleink diff = (int)t->TM_GMTOFF;
601 1.38 christos # else
602 1.12 kleink /*
603 1.40 christos ** C99 and later say that the UT offset must
604 1.12 kleink ** be computed by looking only at
605 1.20 mlelstv ** tm_isdst. This requirement is
606 1.12 kleink ** incorrect, since it means the code
607 1.12 kleink ** must rely on magic (in this case
608 1.12 kleink ** altzone and timezone), and the
609 1.12 kleink ** magic might not have the correct
610 1.20 mlelstv ** offset. Doing things correctly is
611 1.40 christos ** tricky and requires disobeying the standard;
612 1.12 kleink ** see GNU C strftime for details.
613 1.12 kleink ** For now, punt and conform to the
614 1.12 kleink ** standard, even though it's incorrect.
615 1.12 kleink **
616 1.40 christos ** C99 and later say that %z must be replaced by
617 1.40 christos ** the empty string if the time zone is not
618 1.12 kleink ** determinable, so output nothing if the
619 1.12 kleink ** appropriate variables are not available.
620 1.12 kleink */
621 1.38 christos # ifndef STD_INSPIRED
622 1.12 kleink if (t->tm_isdst == 0)
623 1.40 christos # if USG_COMPAT
624 1.12 kleink diff = -timezone;
625 1.38 christos # else
626 1.12 kleink continue;
627 1.38 christos # endif
628 1.12 kleink else
629 1.47 christos # if ALTZONE
630 1.12 kleink diff = -altzone;
631 1.38 christos # else
632 1.12 kleink continue;
633 1.38 christos # endif
634 1.38 christos # else
635 1.16 kleink {
636 1.16 kleink struct tm tmp;
637 1.16 kleink time_t lct, gct;
638 1.16 kleink
639 1.16 kleink /*
640 1.16 kleink ** Get calendar time from t
641 1.16 kleink ** being treated as local.
642 1.16 kleink */
643 1.16 kleink tmp = *t; /* mktime discards const */
644 1.43 christos lct = mktime_z(sp, &tmp);
645 1.16 kleink
646 1.16 kleink if (lct == (time_t)-1)
647 1.16 kleink continue;
648 1.16 kleink
649 1.16 kleink /*
650 1.16 kleink ** Get calendar time from t
651 1.16 kleink ** being treated as GMT.
652 1.16 kleink **/
653 1.16 kleink tmp = *t; /* mktime discards const */
654 1.16 kleink gct = timegm(&tmp);
655 1.16 kleink
656 1.16 kleink if (gct == (time_t)-1)
657 1.16 kleink continue;
658 1.16 kleink
659 1.16 kleink /* LINTED difference will fit int */
660 1.16 kleink diff = (intmax_t)gct - (intmax_t)lct;
661 1.16 kleink }
662 1.38 christos # endif
663 1.38 christos # endif
664 1.39 christos negative = diff < 0;
665 1.39 christos if (diff == 0) {
666 1.50 christos # ifdef TM_ZONE
667 1.39 christos negative = t->TM_ZONE[0] == '-';
668 1.50 christos # else
669 1.40 christos negative = t->tm_isdst < 0;
670 1.50 christos # if HAVE_TZNAME
671 1.40 christos if (tzname[t->tm_isdst != 0][0] == '-')
672 1.40 christos negative = true;
673 1.50 christos # endif
674 1.40 christos # endif
675 1.39 christos }
676 1.39 christos if (negative) {
677 1.12 kleink sign = "-";
678 1.12 kleink diff = -diff;
679 1.12 kleink } else sign = "+";
680 1.12 kleink pt = _add(sign, pt, ptlim);
681 1.20 mlelstv diff /= SECSPERMIN;
682 1.20 mlelstv diff = (diff / MINSPERHOUR) * 100 +
683 1.20 mlelstv (diff % MINSPERHOUR);
684 1.30 christos _DIAGASSERT(__type_fit(int, diff));
685 1.44 christos pt = _conv((int)diff,
686 1.44 christos fmt_padding[PAD_FMT_YEAR][PadIndex],
687 1.44 christos pt, ptlim, loc);
688 1.12 kleink }
689 1.38 christos #endif
690 1.1 mrg continue;
691 1.12 kleink case '+':
692 1.44 christos #ifdef notyet
693 1.44 christos /* XXX: no date_fmt in _TimeLocale */
694 1.44 christos pt = _fmt(sp, tptr->date_fmt, t,
695 1.25 joerg pt, ptlim, warnp, loc);
696 1.44 christos #else
697 1.44 christos pt = _fmt(sp, "%a %b %e %H:%M:%S %Z %Y", t,
698 1.44 christos pt, ptlim, warnp, loc);
699 1.44 christos #endif
700 1.12 kleink continue;
701 1.44 christos case '-':
702 1.44 christos if (PadIndex != PAD_DEFAULT)
703 1.44 christos break;
704 1.44 christos PadIndex = PAD_LESS;
705 1.44 christos goto label;
706 1.44 christos case '_':
707 1.44 christos if (PadIndex != PAD_DEFAULT)
708 1.44 christos break;
709 1.44 christos PadIndex = PAD_SPACE;
710 1.44 christos goto label;
711 1.44 christos case '0':
712 1.44 christos if (PadIndex != PAD_DEFAULT)
713 1.44 christos break;
714 1.44 christos PadIndex = PAD_ZERO;
715 1.44 christos goto label;
716 1.1 mrg case '%':
717 1.1 mrg /*
718 1.12 kleink ** X311J/88-090 (4.12.3.5): if conversion char is
719 1.20 mlelstv ** undefined, behavior is undefined. Print out the
720 1.12 kleink ** character itself as printf(3) also does.
721 1.12 kleink */
722 1.1 mrg default:
723 1.1 mrg break;
724 1.1 mrg }
725 1.1 mrg }
726 1.12 kleink if (pt == ptlim)
727 1.12 kleink break;
728 1.12 kleink *pt++ = *format;
729 1.1 mrg }
730 1.12 kleink return pt;
731 1.1 mrg }
732 1.1 mrg
733 1.21 christos size_t
734 1.34 christos strftime(char *s, size_t maxsize, const char *format, const struct tm *t)
735 1.21 christos {
736 1.43 christos size_t r;
737 1.43 christos
738 1.43 christos rwlock_wrlock(&__lcl_lock);
739 1.43 christos tzset_unlocked();
740 1.43 christos r = strftime_z(__lclptr, s, maxsize, format, t);
741 1.43 christos rwlock_unlock(&__lcl_lock);
742 1.43 christos
743 1.43 christos return r;
744 1.21 christos }
745 1.21 christos
746 1.25 joerg size_t
747 1.25 joerg strftime_l(char * __restrict s, size_t maxsize, const char * __restrict format,
748 1.25 joerg const struct tm * __restrict t, locale_t loc)
749 1.25 joerg {
750 1.43 christos size_t r;
751 1.43 christos
752 1.43 christos rwlock_wrlock(&__lcl_lock);
753 1.43 christos tzset_unlocked();
754 1.43 christos r = strftime_lz(__lclptr, s, maxsize, format, t, loc);
755 1.43 christos rwlock_unlock(&__lcl_lock);
756 1.43 christos
757 1.43 christos return r;
758 1.25 joerg }
759 1.25 joerg
760 1.12 kleink static char *
761 1.44 christos _conv(int n, const char *format, char *pt, const char *ptlim, locale_t loc)
762 1.1 mrg {
763 1.12 kleink char buf[INT_STRLEN_MAXIMUM(int) + 1];
764 1.1 mrg
765 1.44 christos (void) snprintf_l(buf, sizeof(buf), loc, format, n);
766 1.12 kleink return _add(buf, pt, ptlim);
767 1.1 mrg }
768 1.1 mrg
769 1.12 kleink static char *
770 1.34 christos _add(const char *str, char *pt, const char *ptlim)
771 1.1 mrg {
772 1.12 kleink while (pt < ptlim && (*pt = *str++) != '\0')
773 1.12 kleink ++pt;
774 1.12 kleink return pt;
775 1.1 mrg }
776 1.20 mlelstv
777 1.20 mlelstv /*
778 1.20 mlelstv ** POSIX and the C Standard are unclear or inconsistent about
779 1.20 mlelstv ** what %C and %y do if the year is negative or exceeds 9999.
780 1.20 mlelstv ** Use the convention that %C concatenated with %y yields the
781 1.20 mlelstv ** same output as %Y, and that %Y contains at least 4 bytes,
782 1.20 mlelstv ** with more only if necessary.
783 1.20 mlelstv */
784 1.20 mlelstv
785 1.20 mlelstv static char *
786 1.33 christos _yconv(int a, int b, bool convert_top, bool convert_yy,
787 1.44 christos char *pt, const char * ptlim, locale_t loc)
788 1.20 mlelstv {
789 1.28 christos int lead;
790 1.28 christos int trail;
791 1.20 mlelstv
792 1.50 christos int DIVISOR = 100;
793 1.20 mlelstv trail = a % DIVISOR + b % DIVISOR;
794 1.20 mlelstv lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
795 1.20 mlelstv trail %= DIVISOR;
796 1.20 mlelstv if (trail < 0 && lead > 0) {
797 1.20 mlelstv trail += DIVISOR;
798 1.20 mlelstv --lead;
799 1.20 mlelstv } else if (lead < 0 && trail > 0) {
800 1.20 mlelstv trail -= DIVISOR;
801 1.20 mlelstv ++lead;
802 1.20 mlelstv }
803 1.20 mlelstv if (convert_top) {
804 1.20 mlelstv if (lead == 0 && trail < 0)
805 1.20 mlelstv pt = _add("-0", pt, ptlim);
806 1.44 christos else pt = _conv(lead, "%02d", pt, ptlim, loc);
807 1.20 mlelstv }
808 1.20 mlelstv if (convert_yy)
809 1.44 christos pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim,
810 1.44 christos loc);
811 1.20 mlelstv return pt;
812 1.20 mlelstv }
813