3 * $NetBSD: src/lib/libc/time/strftime.c,v 1.16 2004/05/12 23:03:11 kleink Exp $
4 * $DragonFly: src/lib/libc/stdtime/strftime.c,v 1.7 2008/10/19 20:15:58 swildner Exp $
8 ** Based on the UCB version with the ID appearing below.
9 ** This is ANSIish only when "multibyte character == plain character".
13 ** We don't use these extensions in strftime operation even when
14 ** supported by the local tzcode configuration. A strictly
15 ** conforming C application may leave them in undefined state.
24 ** Copyright (c) 1989, 1993
25 ** The Regents of the University of California. All rights reserved.
27 ** Redistribution and use in source and binary forms, with or without
28 ** modification, are permitted provided that the following conditions
30 ** 1. Redistributions of source code must retain the above copyright
31 ** notice, this list of conditions and the following disclaimer.
32 ** 2. Redistributions in binary form must reproduce the above copyright
33 ** notice, this list of conditions and the following disclaimer in the
34 ** documentation and/or other materials provided with the distribution.
35 ** 3. All advertising materials mentioning features or use of this software
36 ** must display the following acknowledgement:
37 ** This product includes software developed by the University of
38 ** California, Berkeley and its contributors.
39 ** 4. Neither the name of the University nor the names of its contributors
40 ** may be used to endorse or promote products derived from this software
41 ** without specific prior written permission.
43 ** THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 ** ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 #include <sys/localedef.h>
64 #define Locale _CurrentTimeLocale
66 static char * _add(const char *, char *, const char *);
67 static char * _conv(int, const char *, char *, const char *);
68 static char * _fmt(const char *, const struct tm *, char *,
70 static char * _yconv(int, int, int, int, char *, const char *);
72 #define NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
74 #ifndef YEAR_2000_NAME
75 #define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
76 #endif /* !defined YEAR_2000_NAME */
84 strftime(char * const s, const size_t maxsize, const char * const format,
85 const struct tm * const t)
92 p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn);
93 #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
94 if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {
95 fprintf(stderr, "\n");
97 fprintf(stderr, "NULL strftime format ");
98 else fprintf(stderr, "strftime format \"%s\" ",
100 fprintf(stderr, "yields only two digits of years in ");
102 fprintf(stderr, "some locales");
103 else if (warn == IN_THIS)
104 fprintf(stderr, "the current locale");
105 else fprintf(stderr, "all locales");
106 fprintf(stderr, "\n");
108 #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
109 if (p == s + maxsize)
116 _fmt(const char *format, const struct tm * const t, char *pt,
117 const char * const ptlim, int * warnp)
119 for ( ; *format; ++format) {
120 if (*format == '%') {
127 pt = _add((t->tm_wday < 0 ||
128 t->tm_wday >= DAYSPERWEEK) ?
129 "?" : Locale->day[t->tm_wday],
133 pt = _add((t->tm_wday < 0 ||
134 t->tm_wday >= DAYSPERWEEK) ?
135 "?" : Locale->abday[t->tm_wday],
139 pt = _add((t->tm_mon < 0 ||
140 t->tm_mon >= MONSPERYEAR) ?
141 "?" : Locale->mon[t->tm_mon],
146 pt = _add((t->tm_mon < 0 ||
147 t->tm_mon >= MONSPERYEAR) ?
148 "?" : Locale->abmon[t->tm_mon],
153 ** %C used to do a...
154 ** _fmt("%a %b %e %X %Y", t);
155 ** ...whereas now POSIX 1003.2 calls for
156 ** something completely different.
159 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0,
166 pt = _fmt(Locale->d_t_fmt, t, pt, ptlim, warnp);
174 pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp);
177 pt = _conv(t->tm_mday, "%02d", pt, ptlim);
182 ** C99 locale modifiers.
184 ** %Ec %EC %Ex %EX %Ey %EY
185 ** %Od %oe %OH %OI %Om %OM
186 ** %OS %Ou %OU %OV %Ow %OW %Oy
187 ** are supposed to provide alternate
192 pt = _conv(t->tm_mday, "%2d", pt, ptlim);
195 pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp);
198 pt = _conv(t->tm_hour, "%02d", pt, ptlim);
201 pt = _conv((t->tm_hour % 12) ?
202 (t->tm_hour % 12) : 12,
206 pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
210 ** This used to be...
211 ** _conv(t->tm_hour % 12 ?
212 ** t->tm_hour % 12 : 12, 2, ' ');
213 ** ...and has been changed to the below to
214 ** match SunOS 4.1.1 and Arnold Robbins'
215 ** strftime version 3.0. That is, "%k" and
216 ** "%l" have been swapped.
219 pt = _conv(t->tm_hour, "%2d", pt, ptlim);
224 ** After all this time, still unclaimed!
226 pt = _add("kitchen sink", pt, ptlim);
228 #endif /* defined KITCHEN_SINK */
231 ** This used to be...
232 ** _conv(t->tm_hour, 2, ' ');
233 ** ...and has been changed to the below to
234 ** match SunOS 4.1.1 and Arnold Robbin's
235 ** strftime version 3.0. That is, "%k" and
236 ** "%l" have been swapped.
239 pt = _conv((t->tm_hour % 12) ?
240 (t->tm_hour % 12) : 12,
244 pt = _conv(t->tm_min, "%02d", pt, ptlim);
247 pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
250 pt = _add("\n", pt, ptlim);
253 pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
259 pt = _fmt("%H:%M", t, pt, ptlim, warnp);
262 pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp);
265 pt = _conv(t->tm_sec, "%02d", pt, ptlim);
270 char buf[INT_STRLEN_MAXIMUM(
277 if (TYPE_SIGNED(time_t))
280 else sprintf(buf, "%lu",
281 (unsigned long) mkt);
282 pt = _add(buf, pt, ptlim);
286 pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp);
289 pt = _add("\t", pt, ptlim);
292 pt = _conv((t->tm_yday + DAYSPERWEEK -
293 t->tm_wday) / DAYSPERWEEK,
298 ** From Arnold Robbins' strftime version 3.0:
299 ** "ISO 8601: Weekday as a decimal number
303 pt = _conv((t->tm_wday == 0) ?
304 DAYSPERWEEK : t->tm_wday,
307 case 'V': /* ISO 8601 week number */
308 case 'G': /* ISO 8601 year (four digits) */
309 case 'g': /* ISO 8601 year (two digits) */
311 ** From Arnold Robbins' strftime version 3.0: "the week number of the
312 ** year (the first Monday as the first day of week 1) as a decimal number
316 ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
317 ** "Week 01 of a year is per definition the first week which has the
318 ** Thursday in this year, which is equivalent to the week which contains
319 ** the fourth day of January. In other words, the first week of a new year
320 ** is the week which has the majority of its days in the new year. Week 01
321 ** might also contain days from the previous year and the week before week
322 ** 01 of a year is the last week (52 or 53) of the previous year even if
323 ** it contains days from the new year. A week starts with Monday (day 1)
324 ** and ends with Sunday (day 7). For example, the first week of the year
325 ** 1997 lasts from 1996-12-30 to 1997-01-05..."
344 len = isleap_sum(year, base) ?
348 ** What yday (-3 ... 3) does
349 ** the ISO year begin on?
351 bot = ((yday + 11 - wday) %
354 ** What yday does the NEXT
355 ** ISO year begin on?
368 w = 1 + ((yday - bot) /
373 yday += isleap_sum(year, base) ?
377 #ifdef XPG4_1994_04_09
379 t->tm_mon == TM_JANUARY) ||
381 t->tm_mon == TM_DECEMBER))
383 #endif /* defined XPG4_1994_04_09 */
385 pt = _conv(w, "%02d",
387 else if (*format == 'g') {
389 pt = _yconv(year, base, 0, 1,
391 } else pt = _yconv(year, base, 1, 1,
397 ** From Arnold Robbins' strftime version 3.0:
398 ** "date as dd-bbb-YYYY"
401 pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp);
404 pt = _conv((t->tm_yday + DAYSPERWEEK -
407 (DAYSPERWEEK - 1))) / DAYSPERWEEK,
411 pt = _conv(t->tm_wday, "%d", pt, ptlim);
414 pt = _fmt(Locale->t_fmt, t, pt, ptlim, warnp);
420 pt = _fmt(Locale->d_fmt, t, pt, ptlim, &warn2);
429 pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1,
433 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1,
438 if (t->TM_ZONE != NULL)
439 pt = _add(t->TM_ZONE, pt, ptlim);
441 #endif /* defined TM_ZONE */
442 if (t->tm_isdst >= 0)
443 pt = _add(tzname[t->tm_isdst != 0],
446 ** C99 says that %Z must be replaced by the
447 ** empty string if the time zone is not
459 diff = (int)t->TM_GMTOFF;
460 #else /* !defined TM_GMTOFF */
462 ** C99 says that the UTC offset must
463 ** be computed by looking only at
464 ** tm_isdst. This requirement is
465 ** incorrect, since it means the code
466 ** must rely on magic (in this case
467 ** altzone and timezone), and the
468 ** magic might not have the correct
469 ** offset. Doing things correctly is
470 ** tricky and requires disobeying C99;
471 ** see GNU C strftime for details.
472 ** For now, punt and conform to the
473 ** standard, even though it's incorrect.
475 ** C99 says that %z must be replaced by the
476 ** empty string if the time zone is not
477 ** determinable, so output nothing if the
478 ** appropriate variables are not available.
481 if (t->tm_isdst == 0)
484 #else /* !defined USG_COMPAT */
486 #endif /* !defined USG_COMPAT */
490 #else /* !defined ALTZONE */
492 #endif /* !defined ALTZONE */
493 #else /* defined STD_INSPIRED */
499 ** Get calendar time from t
500 ** being treated as local.
502 tmp = *t; /* mktime discards const */
505 if (lct == (time_t)-1)
509 ** Get calendar time from t
510 ** being treated as GMT.
512 tmp = *t; /* mktime discards const */
515 if (gct == (time_t)-1)
518 /* LINTED difference will fit int */
519 diff = (intmax_t)gct - (intmax_t)lct;
521 #endif /* defined STD_INSPIRED */
522 #endif /* !defined TM_GMTOFF */
527 pt = _add(sign, pt, ptlim);
529 diff = (diff / MINSPERHOUR) * 100 +
530 (diff % MINSPERHOUR);
531 pt = _conv(diff, "%04d", pt, ptlim);
535 pt = _fmt(Locale->d_t_fmt, t, pt, ptlim,
540 ** X311J/88-090 (4.12.3.5): if conversion char is
541 ** undefined, behavior is undefined. Print out the
542 ** character itself as printf(3) also does.
556 _conv(const int n, const char * const format, char * const pt,
557 const char * const ptlim)
559 char buf[INT_STRLEN_MAXIMUM(int) + 1];
561 sprintf(buf, format, n);
562 return _add(buf, pt, ptlim);
566 _add(const char *str, char *pt, const char * const ptlim)
568 while (pt < ptlim && (*pt = *str++) != '\0')
574 ** POSIX and the C Standard are unclear or inconsistent about
575 ** what %C and %y do if the year is negative or exceeds 9999.
576 ** Use the convention that %C concatenated with %y yields the
577 ** same output as %Y, and that %Y contains at least 4 bytes,
578 ** with more only if necessary.
582 _yconv(const int a, const int b, const int convert_top, const int convert_yy,
583 char *pt, const char * const ptlim)
589 trail = a % DIVISOR + b % DIVISOR;
590 lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
592 if (trail < 0 && lead > 0) {
595 } else if (lead < 0 && trail > 0) {
600 if (lead == 0 && trail < 0)
601 pt = _add("-0", pt, ptlim);
602 else pt = _conv(lead, "%02d", pt, ptlim);
605 pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim);