strftime.c revision 1.46 1 1.46 christos /* $NetBSD: strftime.c,v 1.46 2019/04/07 22:31:54 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.46 christos __RCSID("$NetBSD: strftime.c,v 1.46 2019/04/07 22:31:54 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.12 kleink #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.40 christos enum warn warn = IN_NONE;
151 1.5 kleink
152 1.37 christos p = _fmt(sp, format, t, s, s + maxsize, &warn, loc);
153 1.40 christos if (/*CONSTCOND*/DEPRECATE_TWO_DIGIT_YEARS
154 1.40 christos && warn != IN_NONE && getenv(YEAR_2000_NAME)) {
155 1.12 kleink (void) fprintf(stderr, "\n");
156 1.37 christos (void) fprintf(stderr, "strftime format \"%s\" ", format);
157 1.12 kleink (void) fprintf(stderr, "yields only two digits of years in ");
158 1.12 kleink if (warn == IN_SOME)
159 1.12 kleink (void) fprintf(stderr, "some locales");
160 1.12 kleink else if (warn == IN_THIS)
161 1.12 kleink (void) fprintf(stderr, "the current locale");
162 1.12 kleink else (void) fprintf(stderr, "all locales");
163 1.12 kleink (void) fprintf(stderr, "\n");
164 1.12 kleink }
165 1.12 kleink if (p == s + maxsize)
166 1.12 kleink return 0;
167 1.12 kleink *p = '\0';
168 1.12 kleink return p - s;
169 1.1 mrg }
170 1.1 mrg
171 1.12 kleink static char *
172 1.34 christos _fmt(const timezone_t sp, const char *format, const struct tm *t, char *pt,
173 1.40 christos const char *ptlim, enum warn *warnp, locale_t loc)
174 1.1 mrg {
175 1.44 christos int Ealternative, Oalternative, PadIndex;
176 1.44 christos _TimeLocale *tptr = _TIME_LOCALE(loc);
177 1.44 christos
178 1.12 kleink for ( ; *format; ++format) {
179 1.1 mrg if (*format == '%') {
180 1.44 christos Ealternative = 0;
181 1.44 christos Oalternative = 0;
182 1.44 christos PadIndex = PAD_DEFAULT;
183 1.12 kleink label:
184 1.12 kleink switch (*++format) {
185 1.1 mrg case '\0':
186 1.1 mrg --format;
187 1.1 mrg break;
188 1.1 mrg case 'A':
189 1.12 kleink pt = _add((t->tm_wday < 0 ||
190 1.12 kleink t->tm_wday >= DAYSPERWEEK) ?
191 1.44 christos "?" : tptr->day[t->tm_wday],
192 1.12 kleink pt, ptlim);
193 1.1 mrg continue;
194 1.1 mrg case 'a':
195 1.12 kleink pt = _add((t->tm_wday < 0 ||
196 1.12 kleink t->tm_wday >= DAYSPERWEEK) ?
197 1.44 christos "?" : tptr->abday[t->tm_wday],
198 1.12 kleink pt, ptlim);
199 1.1 mrg continue;
200 1.1 mrg case 'B':
201 1.12 kleink pt = _add((t->tm_mon < 0 ||
202 1.12 kleink t->tm_mon >= MONSPERYEAR) ?
203 1.44 christos "?" :
204 1.44 christos /* no alt_month in _TimeLocale */
205 1.44 christos (Oalternative ? tptr->mon/*alt_month*/:
206 1.44 christos tptr->mon)[t->tm_mon],
207 1.12 kleink pt, ptlim);
208 1.1 mrg continue;
209 1.1 mrg case 'b':
210 1.1 mrg case 'h':
211 1.12 kleink pt = _add((t->tm_mon < 0 ||
212 1.12 kleink t->tm_mon >= MONSPERYEAR) ?
213 1.44 christos "?" : tptr->abmon[t->tm_mon],
214 1.12 kleink pt, ptlim);
215 1.1 mrg continue;
216 1.1 mrg case 'C':
217 1.12 kleink /*
218 1.12 kleink ** %C used to do a...
219 1.12 kleink ** _fmt("%a %b %e %X %Y", t);
220 1.12 kleink ** ...whereas now POSIX 1003.2 calls for
221 1.12 kleink ** something completely different.
222 1.12 kleink ** (ado, 1993-05-24)
223 1.12 kleink */
224 1.33 christos pt = _yconv(t->tm_year, TM_YEAR_BASE,
225 1.44 christos true, false, pt, ptlim, loc);
226 1.1 mrg continue;
227 1.1 mrg case 'c':
228 1.12 kleink {
229 1.40 christos enum warn warn2 = IN_SOME;
230 1.12 kleink
231 1.44 christos pt = _fmt(sp, tptr->c_fmt, t, pt,
232 1.25 joerg ptlim, &warn2, loc);
233 1.12 kleink if (warn2 == IN_ALL)
234 1.12 kleink warn2 = IN_THIS;
235 1.12 kleink if (warn2 > *warnp)
236 1.12 kleink *warnp = warn2;
237 1.12 kleink }
238 1.1 mrg continue;
239 1.1 mrg case 'D':
240 1.25 joerg pt = _fmt(sp, "%m/%d/%y", t, pt, ptlim, warnp,
241 1.25 joerg loc);
242 1.1 mrg continue;
243 1.1 mrg case 'd':
244 1.44 christos pt = _conv(t->tm_mday,
245 1.44 christos fmt_padding[PAD_FMT_DAYOFMONTH][PadIndex],
246 1.44 christos pt, ptlim, loc);
247 1.1 mrg continue;
248 1.12 kleink case 'E':
249 1.44 christos if (Ealternative || Oalternative)
250 1.44 christos break;
251 1.44 christos Ealternative++;
252 1.44 christos goto label;
253 1.12 kleink case 'O':
254 1.12 kleink /*
255 1.40 christos ** Locale modifiers of C99 and later.
256 1.12 kleink ** The sequences
257 1.12 kleink ** %Ec %EC %Ex %EX %Ey %EY
258 1.12 kleink ** %Od %oe %OH %OI %Om %OM
259 1.12 kleink ** %OS %Ou %OU %OV %Ow %OW %Oy
260 1.41 christos ** are supposed to provide alternative
261 1.12 kleink ** representations.
262 1.12 kleink */
263 1.44 christos if (Ealternative || Oalternative)
264 1.44 christos break;
265 1.44 christos Oalternative++;
266 1.12 kleink goto label;
267 1.1 mrg case 'e':
268 1.44 christos pt = _conv(t->tm_mday,
269 1.44 christos fmt_padding[PAD_FMT_SDAYOFMONTH][PadIndex],
270 1.44 christos pt, ptlim, loc);
271 1.10 kleink continue;
272 1.10 kleink case 'F':
273 1.25 joerg pt = _fmt(sp, "%Y-%m-%d", t, pt, ptlim, warnp,
274 1.25 joerg loc);
275 1.1 mrg continue;
276 1.1 mrg case 'H':
277 1.44 christos pt = _conv(t->tm_hour,
278 1.44 christos fmt_padding[PAD_FMT_HMS][PadIndex],
279 1.44 christos pt, ptlim, loc);
280 1.1 mrg continue;
281 1.1 mrg case 'I':
282 1.12 kleink pt = _conv((t->tm_hour % 12) ?
283 1.44 christos (t->tm_hour % 12) : 12,
284 1.44 christos fmt_padding[PAD_FMT_HMS][PadIndex],
285 1.44 christos pt, ptlim, loc);
286 1.1 mrg continue;
287 1.1 mrg case 'j':
288 1.44 christos pt = _conv(t->tm_yday + 1,
289 1.44 christos fmt_padding[PAD_FMT_DAYOFYEAR][PadIndex],
290 1.44 christos pt, ptlim, loc);
291 1.1 mrg continue;
292 1.1 mrg case 'k':
293 1.12 kleink /*
294 1.12 kleink ** This used to be...
295 1.12 kleink ** _conv(t->tm_hour % 12 ?
296 1.12 kleink ** t->tm_hour % 12 : 12, 2, ' ');
297 1.12 kleink ** ...and has been changed to the below to
298 1.12 kleink ** match SunOS 4.1.1 and Arnold Robbins'
299 1.20 mlelstv ** strftime version 3.0. That is, "%k" and
300 1.12 kleink ** "%l" have been swapped.
301 1.12 kleink ** (ado, 1993-05-24)
302 1.12 kleink */
303 1.44 christos pt = _conv(t->tm_hour,
304 1.44 christos fmt_padding[PAD_FMT_SHMS][PadIndex],
305 1.44 christos pt, ptlim, loc);
306 1.12 kleink continue;
307 1.12 kleink #ifdef KITCHEN_SINK
308 1.12 kleink case 'K':
309 1.12 kleink /*
310 1.12 kleink ** After all this time, still unclaimed!
311 1.12 kleink */
312 1.12 kleink pt = _add("kitchen sink", pt, ptlim);
313 1.1 mrg continue;
314 1.12 kleink #endif /* defined KITCHEN_SINK */
315 1.1 mrg case 'l':
316 1.12 kleink /*
317 1.12 kleink ** This used to be...
318 1.12 kleink ** _conv(t->tm_hour, 2, ' ');
319 1.12 kleink ** ...and has been changed to the below to
320 1.12 kleink ** match SunOS 4.1.1 and Arnold Robbin's
321 1.20 mlelstv ** strftime version 3.0. That is, "%k" and
322 1.12 kleink ** "%l" have been swapped.
323 1.12 kleink ** (ado, 1993-05-24)
324 1.12 kleink */
325 1.12 kleink pt = _conv((t->tm_hour % 12) ?
326 1.12 kleink (t->tm_hour % 12) : 12,
327 1.44 christos fmt_padding[PAD_FMT_SHMS][PadIndex],
328 1.44 christos pt, ptlim, loc);
329 1.1 mrg continue;
330 1.1 mrg case 'M':
331 1.44 christos pt = _conv(t->tm_min,
332 1.44 christos fmt_padding[PAD_FMT_HMS][PadIndex],
333 1.44 christos pt, ptlim, loc);
334 1.1 mrg continue;
335 1.1 mrg case 'm':
336 1.44 christos pt = _conv(t->tm_mon + 1,
337 1.44 christos fmt_padding[PAD_FMT_MONTH][PadIndex],
338 1.44 christos pt, ptlim, loc);
339 1.1 mrg continue;
340 1.1 mrg case 'n':
341 1.12 kleink pt = _add("\n", pt, ptlim);
342 1.1 mrg continue;
343 1.1 mrg case 'p':
344 1.12 kleink pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
345 1.44 christos tptr->am_pm[1] :
346 1.44 christos tptr->am_pm[0],
347 1.12 kleink pt, ptlim);
348 1.1 mrg continue;
349 1.1 mrg case 'R':
350 1.25 joerg pt = _fmt(sp, "%H:%M", t, pt, ptlim, warnp,
351 1.25 joerg loc);
352 1.1 mrg continue;
353 1.1 mrg case 'r':
354 1.44 christos pt = _fmt(sp, tptr->t_fmt_ampm, t,
355 1.25 joerg pt, ptlim, warnp, loc);
356 1.1 mrg continue;
357 1.1 mrg case 'S':
358 1.44 christos pt = _conv(t->tm_sec,
359 1.44 christos fmt_padding[PAD_FMT_HMS][PadIndex],
360 1.44 christos pt, ptlim, loc);
361 1.1 mrg continue;
362 1.1 mrg case 's':
363 1.12 kleink {
364 1.12 kleink struct tm tm;
365 1.12 kleink char buf[INT_STRLEN_MAXIMUM(
366 1.12 kleink time_t) + 1];
367 1.12 kleink time_t mkt;
368 1.12 kleink
369 1.12 kleink tm = *t;
370 1.43 christos mkt = mktime_z(sp, &tm);
371 1.12 kleink /* CONSTCOND */
372 1.12 kleink if (TYPE_SIGNED(time_t))
373 1.27 christos (void)snprintf(buf, sizeof(buf),
374 1.27 christos "%jd", (intmax_t) mkt);
375 1.27 christos else (void)snprintf(buf, sizeof(buf),
376 1.27 christos "%ju", (uintmax_t) mkt);
377 1.12 kleink pt = _add(buf, pt, ptlim);
378 1.12 kleink }
379 1.1 mrg continue;
380 1.1 mrg case 'T':
381 1.25 joerg pt = _fmt(sp, "%H:%M:%S", t, pt, ptlim, warnp,
382 1.25 joerg loc);
383 1.1 mrg continue;
384 1.1 mrg case 't':
385 1.12 kleink pt = _add("\t", pt, ptlim);
386 1.1 mrg continue;
387 1.1 mrg case 'U':
388 1.12 kleink pt = _conv((t->tm_yday + DAYSPERWEEK -
389 1.44 christos t->tm_wday) / DAYSPERWEEK,
390 1.44 christos fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex],
391 1.44 christos pt, ptlim, loc);
392 1.1 mrg continue;
393 1.1 mrg case 'u':
394 1.12 kleink /*
395 1.12 kleink ** From Arnold Robbins' strftime version 3.0:
396 1.12 kleink ** "ISO 8601: Weekday as a decimal number
397 1.12 kleink ** [1 (Monday) - 7]"
398 1.12 kleink ** (ado, 1993-05-24)
399 1.12 kleink */
400 1.12 kleink pt = _conv((t->tm_wday == 0) ?
401 1.12 kleink DAYSPERWEEK : t->tm_wday,
402 1.44 christos "%d", pt, ptlim, loc);
403 1.1 mrg continue;
404 1.8 augustss case 'V': /* ISO 8601 week number */
405 1.8 augustss case 'G': /* ISO 8601 year (four digits) */
406 1.8 augustss case 'g': /* ISO 8601 year (two digits) */
407 1.8 augustss /*
408 1.20 mlelstv ** From Arnold Robbins' strftime version 3.0: "the week number of the
409 1.8 augustss ** year (the first Monday as the first day of week 1) as a decimal number
410 1.8 augustss ** (01-53)."
411 1.8 augustss ** (ado, 1993-05-24)
412 1.8 augustss **
413 1.40 christos ** From <https://www.cl.cam.ac.uk/~mgk25/iso-time.html> by Markus Kuhn:
414 1.8 augustss ** "Week 01 of a year is per definition the first week which has the
415 1.8 augustss ** Thursday in this year, which is equivalent to the week which contains
416 1.8 augustss ** the fourth day of January. In other words, the first week of a new year
417 1.8 augustss ** is the week which has the majority of its days in the new year. Week 01
418 1.8 augustss ** might also contain days from the previous year and the week before week
419 1.8 augustss ** 01 of a year is the last week (52 or 53) of the previous year even if
420 1.8 augustss ** it contains days from the new year. A week starts with Monday (day 1)
421 1.20 mlelstv ** and ends with Sunday (day 7). For example, the first week of the year
422 1.8 augustss ** 1997 lasts from 1996-12-30 to 1997-01-05..."
423 1.8 augustss ** (ado, 1996-01-02)
424 1.8 augustss */
425 1.1 mrg {
426 1.8 augustss int year;
427 1.20 mlelstv int base;
428 1.8 augustss int yday;
429 1.8 augustss int wday;
430 1.8 augustss int w;
431 1.8 augustss
432 1.20 mlelstv year = t->tm_year;
433 1.20 mlelstv base = TM_YEAR_BASE;
434 1.8 augustss yday = t->tm_yday;
435 1.8 augustss wday = t->tm_wday;
436 1.8 augustss for ( ; ; ) {
437 1.8 augustss int len;
438 1.8 augustss int bot;
439 1.8 augustss int top;
440 1.8 augustss
441 1.20 mlelstv len = isleap_sum(year, base) ?
442 1.8 augustss DAYSPERLYEAR :
443 1.8 augustss DAYSPERNYEAR;
444 1.8 augustss /*
445 1.8 augustss ** What yday (-3 ... 3) does
446 1.8 augustss ** the ISO year begin on?
447 1.8 augustss */
448 1.8 augustss bot = ((yday + 11 - wday) %
449 1.8 augustss DAYSPERWEEK) - 3;
450 1.8 augustss /*
451 1.8 augustss ** What yday does the NEXT
452 1.8 augustss ** ISO year begin on?
453 1.8 augustss */
454 1.8 augustss top = bot -
455 1.8 augustss (len % DAYSPERWEEK);
456 1.8 augustss if (top < -3)
457 1.8 augustss top += DAYSPERWEEK;
458 1.8 augustss top += len;
459 1.8 augustss if (yday >= top) {
460 1.20 mlelstv ++base;
461 1.8 augustss w = 1;
462 1.8 augustss break;
463 1.8 augustss }
464 1.8 augustss if (yday >= bot) {
465 1.8 augustss w = 1 + ((yday - bot) /
466 1.8 augustss DAYSPERWEEK);
467 1.8 augustss break;
468 1.8 augustss }
469 1.20 mlelstv --base;
470 1.20 mlelstv yday += isleap_sum(year, base) ?
471 1.8 augustss DAYSPERLYEAR :
472 1.8 augustss DAYSPERNYEAR;
473 1.8 augustss }
474 1.8 augustss #ifdef XPG4_1994_04_09
475 1.20 mlelstv if ((w == 52 &&
476 1.20 mlelstv t->tm_mon == TM_JANUARY) ||
477 1.20 mlelstv (w == 1 &&
478 1.20 mlelstv t->tm_mon == TM_DECEMBER))
479 1.20 mlelstv w = 53;
480 1.8 augustss #endif /* defined XPG4_1994_04_09 */
481 1.12 kleink if (*format == 'V')
482 1.44 christos pt = _conv(w,
483 1.44 christos fmt_padding[
484 1.44 christos PAD_FMT_WEEKOFYEAR][
485 1.44 christos PadIndex], pt, ptlim, loc);
486 1.12 kleink else if (*format == 'g') {
487 1.12 kleink *warnp = IN_ALL;
488 1.33 christos pt = _yconv(year, base,
489 1.33 christos false, true,
490 1.44 christos pt, ptlim, loc);
491 1.33 christos } else pt = _yconv(year, base,
492 1.33 christos true, true,
493 1.44 christos pt, ptlim, loc);
494 1.1 mrg }
495 1.1 mrg continue;
496 1.12 kleink case 'v':
497 1.12 kleink /*
498 1.12 kleink ** From Arnold Robbins' strftime version 3.0:
499 1.12 kleink ** "date as dd-bbb-YYYY"
500 1.12 kleink ** (ado, 1993-05-24)
501 1.12 kleink */
502 1.25 joerg pt = _fmt(sp, "%e-%b-%Y", t, pt, ptlim, warnp,
503 1.25 joerg loc);
504 1.12 kleink continue;
505 1.1 mrg case 'W':
506 1.12 kleink pt = _conv((t->tm_yday + DAYSPERWEEK -
507 1.44 christos (t->tm_wday ?
508 1.44 christos (t->tm_wday - 1) :
509 1.44 christos (DAYSPERWEEK - 1))) / DAYSPERWEEK,
510 1.44 christos fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex],
511 1.44 christos pt, ptlim, loc);
512 1.1 mrg continue;
513 1.1 mrg case 'w':
514 1.44 christos pt = _conv(t->tm_wday, "%d", pt, ptlim, loc);
515 1.12 kleink continue;
516 1.12 kleink case 'X':
517 1.44 christos pt = _fmt(sp, tptr->t_fmt, t, pt,
518 1.25 joerg ptlim, warnp, loc);
519 1.1 mrg continue;
520 1.1 mrg case 'x':
521 1.12 kleink {
522 1.40 christos enum warn warn2 = IN_SOME;
523 1.12 kleink
524 1.44 christos pt = _fmt(sp, tptr->d_fmt, t, pt,
525 1.25 joerg ptlim, &warn2, loc);
526 1.12 kleink if (warn2 == IN_ALL)
527 1.12 kleink warn2 = IN_THIS;
528 1.12 kleink if (warn2 > *warnp)
529 1.12 kleink *warnp = warn2;
530 1.12 kleink }
531 1.1 mrg continue;
532 1.1 mrg case 'y':
533 1.12 kleink *warnp = IN_ALL;
534 1.33 christos pt = _yconv(t->tm_year, TM_YEAR_BASE,
535 1.33 christos false, true,
536 1.44 christos pt, ptlim, loc);
537 1.1 mrg continue;
538 1.1 mrg case 'Y':
539 1.33 christos pt = _yconv(t->tm_year, TM_YEAR_BASE,
540 1.33 christos true, true,
541 1.44 christos pt, ptlim, loc);
542 1.1 mrg continue;
543 1.1 mrg case 'Z':
544 1.11 taca #ifdef TM_ZONE
545 1.35 christos pt = _add(t->TM_ZONE, pt, ptlim);
546 1.40 christos #elif HAVE_TZNAME
547 1.45 christos if (t->tm_isdst >= 0) {
548 1.45 christos int oerrno = errno, dst = t->tm_isdst;
549 1.45 christos const char *z =
550 1.45 christos tzgetname(sp, dst);
551 1.45 christos if (z == NULL)
552 1.45 christos z = tzgetname(sp, !dst);
553 1.46 christos if (z != NULL)
554 1.46 christos pt = _add(z, pt, ptlim);
555 1.45 christos errno = oerrno;
556 1.45 christos }
557 1.40 christos #endif
558 1.12 kleink /*
559 1.40 christos ** C99 and later say that %Z must be
560 1.40 christos ** replaced by the empty string if the
561 1.42 christos ** time zone abbreviation is not
562 1.42 christos ** determinable.
563 1.12 kleink */
564 1.12 kleink continue;
565 1.12 kleink case 'z':
566 1.40 christos #if defined TM_GMTOFF || USG_COMPAT || defined ALTZONE
567 1.12 kleink {
568 1.30 christos long diff;
569 1.12 kleink char const * sign;
570 1.39 christos bool negative;
571 1.12 kleink
572 1.12 kleink if (t->tm_isdst < 0)
573 1.12 kleink continue;
574 1.38 christos # ifdef TM_GMTOFF
575 1.12 kleink diff = (int)t->TM_GMTOFF;
576 1.38 christos # else
577 1.12 kleink /*
578 1.40 christos ** C99 and later say that the UT offset must
579 1.12 kleink ** be computed by looking only at
580 1.20 mlelstv ** tm_isdst. This requirement is
581 1.12 kleink ** incorrect, since it means the code
582 1.12 kleink ** must rely on magic (in this case
583 1.12 kleink ** altzone and timezone), and the
584 1.12 kleink ** magic might not have the correct
585 1.20 mlelstv ** offset. Doing things correctly is
586 1.40 christos ** tricky and requires disobeying the standard;
587 1.12 kleink ** see GNU C strftime for details.
588 1.12 kleink ** For now, punt and conform to the
589 1.12 kleink ** standard, even though it's incorrect.
590 1.12 kleink **
591 1.40 christos ** C99 and later say that %z must be replaced by
592 1.40 christos ** the empty string if the time zone is not
593 1.12 kleink ** determinable, so output nothing if the
594 1.12 kleink ** appropriate variables are not available.
595 1.12 kleink */
596 1.38 christos # ifndef STD_INSPIRED
597 1.12 kleink if (t->tm_isdst == 0)
598 1.40 christos # if USG_COMPAT
599 1.12 kleink diff = -timezone;
600 1.38 christos # else
601 1.12 kleink continue;
602 1.38 christos # endif
603 1.12 kleink else
604 1.38 christos # ifdef ALTZONE
605 1.12 kleink diff = -altzone;
606 1.38 christos # else
607 1.12 kleink continue;
608 1.38 christos # endif
609 1.38 christos # else
610 1.16 kleink {
611 1.16 kleink struct tm tmp;
612 1.16 kleink time_t lct, gct;
613 1.16 kleink
614 1.16 kleink /*
615 1.16 kleink ** Get calendar time from t
616 1.16 kleink ** being treated as local.
617 1.16 kleink */
618 1.16 kleink tmp = *t; /* mktime discards const */
619 1.43 christos lct = mktime_z(sp, &tmp);
620 1.16 kleink
621 1.16 kleink if (lct == (time_t)-1)
622 1.16 kleink continue;
623 1.16 kleink
624 1.16 kleink /*
625 1.16 kleink ** Get calendar time from t
626 1.16 kleink ** being treated as GMT.
627 1.16 kleink **/
628 1.16 kleink tmp = *t; /* mktime discards const */
629 1.16 kleink gct = timegm(&tmp);
630 1.16 kleink
631 1.16 kleink if (gct == (time_t)-1)
632 1.16 kleink continue;
633 1.16 kleink
634 1.16 kleink /* LINTED difference will fit int */
635 1.16 kleink diff = (intmax_t)gct - (intmax_t)lct;
636 1.16 kleink }
637 1.38 christos # endif
638 1.38 christos # endif
639 1.39 christos negative = diff < 0;
640 1.39 christos if (diff == 0) {
641 1.39 christos #ifdef TM_ZONE
642 1.39 christos negative = t->TM_ZONE[0] == '-';
643 1.39 christos #else
644 1.40 christos negative = t->tm_isdst < 0;
645 1.40 christos # if HAVE_TZNAME
646 1.40 christos if (tzname[t->tm_isdst != 0][0] == '-')
647 1.40 christos negative = true;
648 1.40 christos # endif
649 1.39 christos #endif
650 1.39 christos }
651 1.39 christos if (negative) {
652 1.12 kleink sign = "-";
653 1.12 kleink diff = -diff;
654 1.12 kleink } else sign = "+";
655 1.12 kleink pt = _add(sign, pt, ptlim);
656 1.20 mlelstv diff /= SECSPERMIN;
657 1.20 mlelstv diff = (diff / MINSPERHOUR) * 100 +
658 1.20 mlelstv (diff % MINSPERHOUR);
659 1.30 christos _DIAGASSERT(__type_fit(int, diff));
660 1.44 christos pt = _conv((int)diff,
661 1.44 christos fmt_padding[PAD_FMT_YEAR][PadIndex],
662 1.44 christos pt, ptlim, loc);
663 1.12 kleink }
664 1.38 christos #endif
665 1.1 mrg continue;
666 1.12 kleink case '+':
667 1.44 christos #ifdef notyet
668 1.44 christos /* XXX: no date_fmt in _TimeLocale */
669 1.44 christos pt = _fmt(sp, tptr->date_fmt, t,
670 1.25 joerg pt, ptlim, warnp, loc);
671 1.44 christos #else
672 1.44 christos pt = _fmt(sp, "%a %b %e %H:%M:%S %Z %Y", t,
673 1.44 christos pt, ptlim, warnp, loc);
674 1.44 christos #endif
675 1.12 kleink continue;
676 1.44 christos case '-':
677 1.44 christos if (PadIndex != PAD_DEFAULT)
678 1.44 christos break;
679 1.44 christos PadIndex = PAD_LESS;
680 1.44 christos goto label;
681 1.44 christos case '_':
682 1.44 christos if (PadIndex != PAD_DEFAULT)
683 1.44 christos break;
684 1.44 christos PadIndex = PAD_SPACE;
685 1.44 christos goto label;
686 1.44 christos case '0':
687 1.44 christos if (PadIndex != PAD_DEFAULT)
688 1.44 christos break;
689 1.44 christos PadIndex = PAD_ZERO;
690 1.44 christos goto label;
691 1.1 mrg case '%':
692 1.1 mrg /*
693 1.12 kleink ** X311J/88-090 (4.12.3.5): if conversion char is
694 1.20 mlelstv ** undefined, behavior is undefined. Print out the
695 1.12 kleink ** character itself as printf(3) also does.
696 1.12 kleink */
697 1.1 mrg default:
698 1.1 mrg break;
699 1.1 mrg }
700 1.1 mrg }
701 1.12 kleink if (pt == ptlim)
702 1.12 kleink break;
703 1.12 kleink *pt++ = *format;
704 1.1 mrg }
705 1.12 kleink return pt;
706 1.1 mrg }
707 1.1 mrg
708 1.21 christos size_t
709 1.34 christos strftime(char *s, size_t maxsize, const char *format, const struct tm *t)
710 1.21 christos {
711 1.43 christos size_t r;
712 1.43 christos
713 1.43 christos rwlock_wrlock(&__lcl_lock);
714 1.43 christos tzset_unlocked();
715 1.43 christos r = strftime_z(__lclptr, s, maxsize, format, t);
716 1.43 christos rwlock_unlock(&__lcl_lock);
717 1.43 christos
718 1.43 christos return r;
719 1.21 christos }
720 1.21 christos
721 1.25 joerg size_t
722 1.25 joerg strftime_l(char * __restrict s, size_t maxsize, const char * __restrict format,
723 1.25 joerg const struct tm * __restrict t, locale_t loc)
724 1.25 joerg {
725 1.43 christos size_t r;
726 1.43 christos
727 1.43 christos rwlock_wrlock(&__lcl_lock);
728 1.43 christos tzset_unlocked();
729 1.43 christos r = strftime_lz(__lclptr, s, maxsize, format, t, loc);
730 1.43 christos rwlock_unlock(&__lcl_lock);
731 1.43 christos
732 1.43 christos return r;
733 1.25 joerg }
734 1.25 joerg
735 1.12 kleink static char *
736 1.44 christos _conv(int n, const char *format, char *pt, const char *ptlim, locale_t loc)
737 1.1 mrg {
738 1.12 kleink char buf[INT_STRLEN_MAXIMUM(int) + 1];
739 1.1 mrg
740 1.44 christos (void) snprintf_l(buf, sizeof(buf), loc, format, n);
741 1.12 kleink return _add(buf, pt, ptlim);
742 1.1 mrg }
743 1.1 mrg
744 1.12 kleink static char *
745 1.34 christos _add(const char *str, char *pt, const char *ptlim)
746 1.1 mrg {
747 1.12 kleink while (pt < ptlim && (*pt = *str++) != '\0')
748 1.12 kleink ++pt;
749 1.12 kleink return pt;
750 1.1 mrg }
751 1.20 mlelstv
752 1.20 mlelstv /*
753 1.20 mlelstv ** POSIX and the C Standard are unclear or inconsistent about
754 1.20 mlelstv ** what %C and %y do if the year is negative or exceeds 9999.
755 1.20 mlelstv ** Use the convention that %C concatenated with %y yields the
756 1.20 mlelstv ** same output as %Y, and that %Y contains at least 4 bytes,
757 1.20 mlelstv ** with more only if necessary.
758 1.20 mlelstv */
759 1.20 mlelstv
760 1.20 mlelstv static char *
761 1.33 christos _yconv(int a, int b, bool convert_top, bool convert_yy,
762 1.44 christos char *pt, const char * ptlim, locale_t loc)
763 1.20 mlelstv {
764 1.28 christos int lead;
765 1.28 christos int trail;
766 1.20 mlelstv
767 1.20 mlelstv #define DIVISOR 100
768 1.20 mlelstv trail = a % DIVISOR + b % DIVISOR;
769 1.20 mlelstv lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
770 1.20 mlelstv trail %= DIVISOR;
771 1.20 mlelstv if (trail < 0 && lead > 0) {
772 1.20 mlelstv trail += DIVISOR;
773 1.20 mlelstv --lead;
774 1.20 mlelstv } else if (lead < 0 && trail > 0) {
775 1.20 mlelstv trail -= DIVISOR;
776 1.20 mlelstv ++lead;
777 1.20 mlelstv }
778 1.20 mlelstv if (convert_top) {
779 1.20 mlelstv if (lead == 0 && trail < 0)
780 1.20 mlelstv pt = _add("-0", pt, ptlim);
781 1.44 christos else pt = _conv(lead, "%02d", pt, ptlim, loc);
782 1.20 mlelstv }
783 1.20 mlelstv if (convert_yy)
784 1.44 christos pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim,
785 1.44 christos loc);
786 1.20 mlelstv return pt;
787 1.20 mlelstv }
788