strftime.c revision 1.50 1 1.50 christos /* $NetBSD: strftime.c,v 1.50 2022/08/16 10:56:21 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.50 christos __RCSID("$NetBSD: strftime.c,v 1.50 2022/08/16 10:56:21 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.33 christos 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.12 kleink tm = *t;
378 1.43 christos mkt = mktime_z(sp, &tm);
379 1.50 christos /* There is no portable, definitive
380 1.50 christos test for whether whether mktime
381 1.50 christos succeeded, so treat (time_t) -1 as
382 1.50 christos the success that it might be. */
383 1.12 kleink /* CONSTCOND */
384 1.49 christos if (TYPE_SIGNED(time_t)) {
385 1.49 christos intmax_t n = mkt;
386 1.27 christos (void)snprintf(buf, sizeof(buf),
387 1.49 christos "%"PRIdMAX, n);
388 1.49 christos } else {
389 1.49 christos uintmax_t n = mkt;
390 1.49 christos (void)snprintf(buf, sizeof(buf),
391 1.49 christos "%"PRIuMAX, n);
392 1.49 christos }
393 1.12 kleink pt = _add(buf, pt, ptlim);
394 1.12 kleink }
395 1.1 mrg continue;
396 1.1 mrg case 'T':
397 1.25 joerg pt = _fmt(sp, "%H:%M:%S", t, pt, ptlim, warnp,
398 1.25 joerg loc);
399 1.1 mrg continue;
400 1.1 mrg case 't':
401 1.12 kleink pt = _add("\t", pt, ptlim);
402 1.1 mrg continue;
403 1.1 mrg case 'U':
404 1.12 kleink pt = _conv((t->tm_yday + DAYSPERWEEK -
405 1.44 christos t->tm_wday) / DAYSPERWEEK,
406 1.44 christos fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex],
407 1.44 christos pt, ptlim, loc);
408 1.1 mrg continue;
409 1.1 mrg case 'u':
410 1.12 kleink /*
411 1.12 kleink ** From Arnold Robbins' strftime version 3.0:
412 1.12 kleink ** "ISO 8601: Weekday as a decimal number
413 1.12 kleink ** [1 (Monday) - 7]"
414 1.12 kleink ** (ado, 1993-05-24)
415 1.12 kleink */
416 1.12 kleink pt = _conv((t->tm_wday == 0) ?
417 1.12 kleink DAYSPERWEEK : t->tm_wday,
418 1.44 christos "%d", pt, ptlim, loc);
419 1.1 mrg continue;
420 1.8 augustss case 'V': /* ISO 8601 week number */
421 1.8 augustss case 'G': /* ISO 8601 year (four digits) */
422 1.8 augustss case 'g': /* ISO 8601 year (two digits) */
423 1.8 augustss /*
424 1.20 mlelstv ** From Arnold Robbins' strftime version 3.0: "the week number of the
425 1.8 augustss ** year (the first Monday as the first day of week 1) as a decimal number
426 1.8 augustss ** (01-53)."
427 1.8 augustss ** (ado, 1993-05-24)
428 1.8 augustss **
429 1.40 christos ** From <https://www.cl.cam.ac.uk/~mgk25/iso-time.html> by Markus Kuhn:
430 1.8 augustss ** "Week 01 of a year is per definition the first week which has the
431 1.8 augustss ** Thursday in this year, which is equivalent to the week which contains
432 1.8 augustss ** the fourth day of January. In other words, the first week of a new year
433 1.8 augustss ** is the week which has the majority of its days in the new year. Week 01
434 1.8 augustss ** might also contain days from the previous year and the week before week
435 1.8 augustss ** 01 of a year is the last week (52 or 53) of the previous year even if
436 1.8 augustss ** it contains days from the new year. A week starts with Monday (day 1)
437 1.20 mlelstv ** and ends with Sunday (day 7). For example, the first week of the year
438 1.8 augustss ** 1997 lasts from 1996-12-30 to 1997-01-05..."
439 1.8 augustss ** (ado, 1996-01-02)
440 1.8 augustss */
441 1.1 mrg {
442 1.8 augustss int year;
443 1.20 mlelstv int base;
444 1.8 augustss int yday;
445 1.8 augustss int wday;
446 1.8 augustss int w;
447 1.8 augustss
448 1.20 mlelstv year = t->tm_year;
449 1.20 mlelstv base = TM_YEAR_BASE;
450 1.8 augustss yday = t->tm_yday;
451 1.8 augustss wday = t->tm_wday;
452 1.8 augustss for ( ; ; ) {
453 1.8 augustss int len;
454 1.8 augustss int bot;
455 1.8 augustss int top;
456 1.8 augustss
457 1.20 mlelstv len = isleap_sum(year, base) ?
458 1.8 augustss DAYSPERLYEAR :
459 1.8 augustss DAYSPERNYEAR;
460 1.8 augustss /*
461 1.8 augustss ** What yday (-3 ... 3) does
462 1.8 augustss ** the ISO year begin on?
463 1.8 augustss */
464 1.8 augustss bot = ((yday + 11 - wday) %
465 1.8 augustss DAYSPERWEEK) - 3;
466 1.8 augustss /*
467 1.8 augustss ** What yday does the NEXT
468 1.8 augustss ** ISO year begin on?
469 1.8 augustss */
470 1.8 augustss top = bot -
471 1.8 augustss (len % DAYSPERWEEK);
472 1.8 augustss if (top < -3)
473 1.8 augustss top += DAYSPERWEEK;
474 1.8 augustss top += len;
475 1.8 augustss if (yday >= top) {
476 1.20 mlelstv ++base;
477 1.8 augustss w = 1;
478 1.8 augustss break;
479 1.8 augustss }
480 1.8 augustss if (yday >= bot) {
481 1.8 augustss w = 1 + ((yday - bot) /
482 1.8 augustss DAYSPERWEEK);
483 1.8 augustss break;
484 1.8 augustss }
485 1.20 mlelstv --base;
486 1.20 mlelstv yday += isleap_sum(year, base) ?
487 1.8 augustss DAYSPERLYEAR :
488 1.8 augustss DAYSPERNYEAR;
489 1.8 augustss }
490 1.8 augustss #ifdef XPG4_1994_04_09
491 1.20 mlelstv if ((w == 52 &&
492 1.20 mlelstv t->tm_mon == TM_JANUARY) ||
493 1.20 mlelstv (w == 1 &&
494 1.20 mlelstv t->tm_mon == TM_DECEMBER))
495 1.20 mlelstv w = 53;
496 1.8 augustss #endif /* defined XPG4_1994_04_09 */
497 1.12 kleink if (*format == 'V')
498 1.44 christos pt = _conv(w,
499 1.44 christos fmt_padding[
500 1.44 christos PAD_FMT_WEEKOFYEAR][
501 1.44 christos PadIndex], pt, ptlim, loc);
502 1.12 kleink else if (*format == 'g') {
503 1.12 kleink *warnp = IN_ALL;
504 1.33 christos pt = _yconv(year, base,
505 1.33 christos false, true,
506 1.44 christos pt, ptlim, loc);
507 1.33 christos } else pt = _yconv(year, base,
508 1.33 christos true, true,
509 1.44 christos pt, ptlim, loc);
510 1.1 mrg }
511 1.1 mrg continue;
512 1.12 kleink case 'v':
513 1.12 kleink /*
514 1.12 kleink ** From Arnold Robbins' strftime version 3.0:
515 1.12 kleink ** "date as dd-bbb-YYYY"
516 1.12 kleink ** (ado, 1993-05-24)
517 1.12 kleink */
518 1.25 joerg pt = _fmt(sp, "%e-%b-%Y", t, pt, ptlim, warnp,
519 1.25 joerg loc);
520 1.12 kleink continue;
521 1.1 mrg case 'W':
522 1.12 kleink pt = _conv((t->tm_yday + DAYSPERWEEK -
523 1.44 christos (t->tm_wday ?
524 1.44 christos (t->tm_wday - 1) :
525 1.44 christos (DAYSPERWEEK - 1))) / DAYSPERWEEK,
526 1.44 christos fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex],
527 1.44 christos pt, ptlim, loc);
528 1.1 mrg continue;
529 1.1 mrg case 'w':
530 1.44 christos pt = _conv(t->tm_wday, "%d", pt, ptlim, loc);
531 1.12 kleink continue;
532 1.12 kleink case 'X':
533 1.44 christos pt = _fmt(sp, tptr->t_fmt, t, pt,
534 1.25 joerg ptlim, warnp, loc);
535 1.1 mrg continue;
536 1.1 mrg case 'x':
537 1.12 kleink {
538 1.40 christos enum warn warn2 = IN_SOME;
539 1.12 kleink
540 1.44 christos pt = _fmt(sp, tptr->d_fmt, t, pt,
541 1.25 joerg ptlim, &warn2, loc);
542 1.12 kleink if (warn2 == IN_ALL)
543 1.12 kleink warn2 = IN_THIS;
544 1.12 kleink if (warn2 > *warnp)
545 1.12 kleink *warnp = warn2;
546 1.12 kleink }
547 1.1 mrg continue;
548 1.1 mrg case 'y':
549 1.12 kleink *warnp = IN_ALL;
550 1.33 christos pt = _yconv(t->tm_year, TM_YEAR_BASE,
551 1.33 christos false, true,
552 1.44 christos pt, ptlim, loc);
553 1.1 mrg continue;
554 1.1 mrg case 'Y':
555 1.33 christos pt = _yconv(t->tm_year, TM_YEAR_BASE,
556 1.33 christos true, true,
557 1.44 christos pt, ptlim, loc);
558 1.1 mrg continue;
559 1.1 mrg case 'Z':
560 1.11 taca #ifdef TM_ZONE
561 1.35 christos pt = _add(t->TM_ZONE, pt, ptlim);
562 1.40 christos #elif HAVE_TZNAME
563 1.45 christos if (t->tm_isdst >= 0) {
564 1.45 christos int oerrno = errno, dst = t->tm_isdst;
565 1.45 christos const char *z =
566 1.45 christos tzgetname(sp, dst);
567 1.45 christos if (z == NULL)
568 1.45 christos z = tzgetname(sp, !dst);
569 1.46 christos if (z != NULL)
570 1.46 christos pt = _add(z, pt, ptlim);
571 1.45 christos errno = oerrno;
572 1.45 christos }
573 1.40 christos #endif
574 1.12 kleink /*
575 1.40 christos ** C99 and later say that %Z must be
576 1.40 christos ** replaced by the empty string if the
577 1.42 christos ** time zone abbreviation is not
578 1.42 christos ** determinable.
579 1.12 kleink */
580 1.12 kleink continue;
581 1.12 kleink case 'z':
582 1.47 christos #if defined TM_GMTOFF || USG_COMPAT || ALTZONE
583 1.12 kleink {
584 1.30 christos long diff;
585 1.12 kleink char const * sign;
586 1.39 christos bool negative;
587 1.12 kleink
588 1.12 kleink if (t->tm_isdst < 0)
589 1.12 kleink continue;
590 1.38 christos # ifdef TM_GMTOFF
591 1.12 kleink diff = (int)t->TM_GMTOFF;
592 1.38 christos # else
593 1.12 kleink /*
594 1.40 christos ** C99 and later say that the UT offset must
595 1.12 kleink ** be computed by looking only at
596 1.20 mlelstv ** tm_isdst. This requirement is
597 1.12 kleink ** incorrect, since it means the code
598 1.12 kleink ** must rely on magic (in this case
599 1.12 kleink ** altzone and timezone), and the
600 1.12 kleink ** magic might not have the correct
601 1.20 mlelstv ** offset. Doing things correctly is
602 1.40 christos ** tricky and requires disobeying the standard;
603 1.12 kleink ** see GNU C strftime for details.
604 1.12 kleink ** For now, punt and conform to the
605 1.12 kleink ** standard, even though it's incorrect.
606 1.12 kleink **
607 1.40 christos ** C99 and later say that %z must be replaced by
608 1.40 christos ** the empty string if the time zone is not
609 1.12 kleink ** determinable, so output nothing if the
610 1.12 kleink ** appropriate variables are not available.
611 1.12 kleink */
612 1.38 christos # ifndef STD_INSPIRED
613 1.12 kleink if (t->tm_isdst == 0)
614 1.40 christos # if USG_COMPAT
615 1.12 kleink diff = -timezone;
616 1.38 christos # else
617 1.12 kleink continue;
618 1.38 christos # endif
619 1.12 kleink else
620 1.47 christos # if ALTZONE
621 1.12 kleink diff = -altzone;
622 1.38 christos # else
623 1.12 kleink continue;
624 1.38 christos # endif
625 1.38 christos # else
626 1.16 kleink {
627 1.16 kleink struct tm tmp;
628 1.16 kleink time_t lct, gct;
629 1.16 kleink
630 1.16 kleink /*
631 1.16 kleink ** Get calendar time from t
632 1.16 kleink ** being treated as local.
633 1.16 kleink */
634 1.16 kleink tmp = *t; /* mktime discards const */
635 1.43 christos lct = mktime_z(sp, &tmp);
636 1.16 kleink
637 1.16 kleink if (lct == (time_t)-1)
638 1.16 kleink continue;
639 1.16 kleink
640 1.16 kleink /*
641 1.16 kleink ** Get calendar time from t
642 1.16 kleink ** being treated as GMT.
643 1.16 kleink **/
644 1.16 kleink tmp = *t; /* mktime discards const */
645 1.16 kleink gct = timegm(&tmp);
646 1.16 kleink
647 1.16 kleink if (gct == (time_t)-1)
648 1.16 kleink continue;
649 1.16 kleink
650 1.16 kleink /* LINTED difference will fit int */
651 1.16 kleink diff = (intmax_t)gct - (intmax_t)lct;
652 1.16 kleink }
653 1.38 christos # endif
654 1.38 christos # endif
655 1.39 christos negative = diff < 0;
656 1.39 christos if (diff == 0) {
657 1.50 christos # ifdef TM_ZONE
658 1.39 christos negative = t->TM_ZONE[0] == '-';
659 1.50 christos # else
660 1.40 christos negative = t->tm_isdst < 0;
661 1.50 christos # if HAVE_TZNAME
662 1.40 christos if (tzname[t->tm_isdst != 0][0] == '-')
663 1.40 christos negative = true;
664 1.50 christos # endif
665 1.40 christos # endif
666 1.39 christos }
667 1.39 christos if (negative) {
668 1.12 kleink sign = "-";
669 1.12 kleink diff = -diff;
670 1.12 kleink } else sign = "+";
671 1.12 kleink pt = _add(sign, pt, ptlim);
672 1.20 mlelstv diff /= SECSPERMIN;
673 1.20 mlelstv diff = (diff / MINSPERHOUR) * 100 +
674 1.20 mlelstv (diff % MINSPERHOUR);
675 1.30 christos _DIAGASSERT(__type_fit(int, diff));
676 1.44 christos pt = _conv((int)diff,
677 1.44 christos fmt_padding[PAD_FMT_YEAR][PadIndex],
678 1.44 christos pt, ptlim, loc);
679 1.12 kleink }
680 1.38 christos #endif
681 1.1 mrg continue;
682 1.12 kleink case '+':
683 1.44 christos #ifdef notyet
684 1.44 christos /* XXX: no date_fmt in _TimeLocale */
685 1.44 christos pt = _fmt(sp, tptr->date_fmt, t,
686 1.25 joerg pt, ptlim, warnp, loc);
687 1.44 christos #else
688 1.44 christos pt = _fmt(sp, "%a %b %e %H:%M:%S %Z %Y", t,
689 1.44 christos pt, ptlim, warnp, loc);
690 1.44 christos #endif
691 1.12 kleink continue;
692 1.44 christos case '-':
693 1.44 christos if (PadIndex != PAD_DEFAULT)
694 1.44 christos break;
695 1.44 christos PadIndex = PAD_LESS;
696 1.44 christos goto label;
697 1.44 christos case '_':
698 1.44 christos if (PadIndex != PAD_DEFAULT)
699 1.44 christos break;
700 1.44 christos PadIndex = PAD_SPACE;
701 1.44 christos goto label;
702 1.44 christos case '0':
703 1.44 christos if (PadIndex != PAD_DEFAULT)
704 1.44 christos break;
705 1.44 christos PadIndex = PAD_ZERO;
706 1.44 christos goto label;
707 1.1 mrg case '%':
708 1.1 mrg /*
709 1.12 kleink ** X311J/88-090 (4.12.3.5): if conversion char is
710 1.20 mlelstv ** undefined, behavior is undefined. Print out the
711 1.12 kleink ** character itself as printf(3) also does.
712 1.12 kleink */
713 1.1 mrg default:
714 1.1 mrg break;
715 1.1 mrg }
716 1.1 mrg }
717 1.12 kleink if (pt == ptlim)
718 1.12 kleink break;
719 1.12 kleink *pt++ = *format;
720 1.1 mrg }
721 1.12 kleink return pt;
722 1.1 mrg }
723 1.1 mrg
724 1.21 christos size_t
725 1.34 christos strftime(char *s, size_t maxsize, const char *format, const struct tm *t)
726 1.21 christos {
727 1.43 christos size_t r;
728 1.43 christos
729 1.43 christos rwlock_wrlock(&__lcl_lock);
730 1.43 christos tzset_unlocked();
731 1.43 christos r = strftime_z(__lclptr, s, maxsize, format, t);
732 1.43 christos rwlock_unlock(&__lcl_lock);
733 1.43 christos
734 1.43 christos return r;
735 1.21 christos }
736 1.21 christos
737 1.25 joerg size_t
738 1.25 joerg strftime_l(char * __restrict s, size_t maxsize, const char * __restrict format,
739 1.25 joerg const struct tm * __restrict t, locale_t loc)
740 1.25 joerg {
741 1.43 christos size_t r;
742 1.43 christos
743 1.43 christos rwlock_wrlock(&__lcl_lock);
744 1.43 christos tzset_unlocked();
745 1.43 christos r = strftime_lz(__lclptr, s, maxsize, format, t, loc);
746 1.43 christos rwlock_unlock(&__lcl_lock);
747 1.43 christos
748 1.43 christos return r;
749 1.25 joerg }
750 1.25 joerg
751 1.12 kleink static char *
752 1.44 christos _conv(int n, const char *format, char *pt, const char *ptlim, locale_t loc)
753 1.1 mrg {
754 1.12 kleink char buf[INT_STRLEN_MAXIMUM(int) + 1];
755 1.1 mrg
756 1.44 christos (void) snprintf_l(buf, sizeof(buf), loc, format, n);
757 1.12 kleink return _add(buf, pt, ptlim);
758 1.1 mrg }
759 1.1 mrg
760 1.12 kleink static char *
761 1.34 christos _add(const char *str, char *pt, const char *ptlim)
762 1.1 mrg {
763 1.12 kleink while (pt < ptlim && (*pt = *str++) != '\0')
764 1.12 kleink ++pt;
765 1.12 kleink return pt;
766 1.1 mrg }
767 1.20 mlelstv
768 1.20 mlelstv /*
769 1.20 mlelstv ** POSIX and the C Standard are unclear or inconsistent about
770 1.20 mlelstv ** what %C and %y do if the year is negative or exceeds 9999.
771 1.20 mlelstv ** Use the convention that %C concatenated with %y yields the
772 1.20 mlelstv ** same output as %Y, and that %Y contains at least 4 bytes,
773 1.20 mlelstv ** with more only if necessary.
774 1.20 mlelstv */
775 1.20 mlelstv
776 1.20 mlelstv static char *
777 1.33 christos _yconv(int a, int b, bool convert_top, bool convert_yy,
778 1.44 christos char *pt, const char * ptlim, locale_t loc)
779 1.20 mlelstv {
780 1.28 christos int lead;
781 1.28 christos int trail;
782 1.20 mlelstv
783 1.50 christos int DIVISOR = 100;
784 1.20 mlelstv trail = a % DIVISOR + b % DIVISOR;
785 1.20 mlelstv lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
786 1.20 mlelstv trail %= DIVISOR;
787 1.20 mlelstv if (trail < 0 && lead > 0) {
788 1.20 mlelstv trail += DIVISOR;
789 1.20 mlelstv --lead;
790 1.20 mlelstv } else if (lead < 0 && trail > 0) {
791 1.20 mlelstv trail -= DIVISOR;
792 1.20 mlelstv ++lead;
793 1.20 mlelstv }
794 1.20 mlelstv if (convert_top) {
795 1.20 mlelstv if (lead == 0 && trail < 0)
796 1.20 mlelstv pt = _add("-0", pt, ptlim);
797 1.44 christos else pt = _conv(lead, "%02d", pt, ptlim, loc);
798 1.20 mlelstv }
799 1.20 mlelstv if (convert_yy)
800 1.44 christos pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim,
801 1.44 christos loc);
802 1.20 mlelstv return pt;
803 1.20 mlelstv }
804