1 /* $NetBSD: strftime.c,v 1.35 2015/10/09 17:21:45 christos Exp $ */
4 #if defined(LIBC_SCCS) && !defined(lint)
6 static char elsieid
[] = "@(#)strftime.c 7.64";
7 static char elsieid
[] = "@(#)strftime.c 8.3";
9 __RCSID("$NetBSD: strftime.c,v 1.35 2015/10/09 17:21:45 christos Exp $");
11 #endif /* LIBC_SCCS and not lint */
13 #include "namespace.h"
18 #include "setlocale_local.h"
21 ** Based on the UCB version with the copyright notice and sccsid
24 ** This is ANSIish only when "multibyte character == plain character".
30 ** We don't use these extensions in strftime operation even when
31 ** supported by the local tzcode configuration. A strictly
32 ** conforming C application may leave them in undefined state.
41 ** Copyright (c) 1989, 1993
42 ** The Regents of the University of California. All rights reserved.
44 ** Redistribution and use in source and binary forms, with or without
45 ** modification, are permitted provided that the following conditions
47 ** 1. Redistributions of source code must retain the above copyright
48 ** notice, this list of conditions and the following disclaimer.
49 ** 2. Redistributions in binary form must reproduce the above copyright
50 ** notice, this list of conditions and the following disclaimer in the
51 ** documentation and/or other materials provided with the distribution.
52 ** 3. All advertising materials mentioning features or use of this software
53 ** must display the following acknowledgement:
54 ** This product includes software developed by the University of
55 ** California, Berkeley and its contributors.
56 ** 4. Neither the name of the University nor the names of its contributors
57 ** may be used to endorse or promote products derived from this software
58 ** without specific prior written permission.
60 ** THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
61 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 ** ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
78 __weak_alias(strftime_l
, _strftime_l
)
79 __weak_alias(strftime_lz
, _strftime_lz
)
80 __weak_alias(strftime_z
, _strftime_z
)
83 #include "sys/localedef.h"
84 #define _TIME_LOCALE(loc) \
85 ((_TimeLocale *)((loc)->part_impl[(size_t)LC_TIME]))
88 static char * _add(const char *, char *, const char *);
89 static char * _conv(int, const char *, char *, const char *);
90 static char * _fmt(const timezone_t
, const char *, const struct tm
*, char *,
91 const char *, int *, locale_t
);
92 static char * _yconv(int, int, bool, bool, char *, const char *);
94 extern char * tzname
[];
96 #ifndef YEAR_2000_NAME
97 #define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
98 #endif /* !defined YEAR_2000_NAME */
106 strftime_z(const timezone_t sp
, char * __restrict s
, size_t maxsize
,
107 const char * __restrict format
, const struct tm
* __restrict t
)
109 return strftime_lz(sp
, s
, maxsize
, format
, t
, _current_locale());
114 strftime_l(char *s
, size_t maxsize
, char const *format
, struct tm
const *t
,
117 /* Just call strftime, as only the C locale is supported. */
118 return strftime(s
, maxsize
, format
, t
);
123 strftime_lz(const timezone_t sp
, char *const s
, const size_t maxsize
,
124 const char *const format
, const struct tm
*const t
, locale_t loc
)
130 p
= _fmt(sp
, ((format
== NULL
) ? "%c" : format
), t
, s
, s
+ maxsize
,
132 #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
133 if (warn
!= IN_NONE
&& getenv(YEAR_2000_NAME
) != NULL
) {
134 (void) fprintf(stderr
, "\n");
136 (void) fprintf(stderr
, "NULL strftime format ");
137 else (void) fprintf(stderr
, "strftime format \"%s\" ",
139 (void) fprintf(stderr
, "yields only two digits of years in ");
141 (void) fprintf(stderr
, "some locales");
142 else if (warn
== IN_THIS
)
143 (void) fprintf(stderr
, "the current locale");
144 else (void) fprintf(stderr
, "all locales");
145 (void) fprintf(stderr
, "\n");
147 #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
148 if (p
== s
+ maxsize
)
155 _fmt(const timezone_t sp
, const char *format
, const struct tm
*t
, char *pt
,
156 const char *ptlim
, int *warnp
, locale_t loc
)
158 for ( ; *format
; ++format
) {
159 if (*format
== '%') {
166 pt
= _add((t
->tm_wday
< 0 ||
167 t
->tm_wday
>= DAYSPERWEEK
) ?
168 "?" : _TIME_LOCALE(loc
)->day
[t
->tm_wday
],
172 pt
= _add((t
->tm_wday
< 0 ||
173 t
->tm_wday
>= DAYSPERWEEK
) ?
174 "?" : _TIME_LOCALE(loc
)->abday
[t
->tm_wday
],
178 pt
= _add((t
->tm_mon
< 0 ||
179 t
->tm_mon
>= MONSPERYEAR
) ?
180 "?" : _TIME_LOCALE(loc
)->mon
[t
->tm_mon
],
185 pt
= _add((t
->tm_mon
< 0 ||
186 t
->tm_mon
>= MONSPERYEAR
) ?
187 "?" : _TIME_LOCALE(loc
)->abmon
[t
->tm_mon
],
192 ** %C used to do a...
193 ** _fmt("%a %b %e %X %Y", t);
194 ** ...whereas now POSIX 1003.2 calls for
195 ** something completely different.
198 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
,
199 true, false, pt
, ptlim
);
205 pt
= _fmt(sp
, _TIME_LOCALE(loc
)->c_fmt
, t
, pt
,
214 pt
= _fmt(sp
, "%m/%d/%y", t
, pt
, ptlim
, warnp
,
218 pt
= _conv(t
->tm_mday
, "%02d", pt
, ptlim
);
223 ** C99 locale modifiers.
225 ** %Ec %EC %Ex %EX %Ey %EY
226 ** %Od %oe %OH %OI %Om %OM
227 ** %OS %Ou %OU %OV %Ow %OW %Oy
228 ** are supposed to provide alternate
233 pt
= _conv(t
->tm_mday
, "%2d", pt
, ptlim
);
236 pt
= _fmt(sp
, "%Y-%m-%d", t
, pt
, ptlim
, warnp
,
240 pt
= _conv(t
->tm_hour
, "%02d", pt
, ptlim
);
243 pt
= _conv((t
->tm_hour
% 12) ?
244 (t
->tm_hour
% 12) : 12,
248 pt
= _conv(t
->tm_yday
+ 1, "%03d", pt
, ptlim
);
252 ** This used to be...
253 ** _conv(t->tm_hour % 12 ?
254 ** t->tm_hour % 12 : 12, 2, ' ');
255 ** ...and has been changed to the below to
256 ** match SunOS 4.1.1 and Arnold Robbins'
257 ** strftime version 3.0. That is, "%k" and
258 ** "%l" have been swapped.
261 pt
= _conv(t
->tm_hour
, "%2d", pt
, ptlim
);
266 ** After all this time, still unclaimed!
268 pt
= _add("kitchen sink", pt
, ptlim
);
270 #endif /* defined KITCHEN_SINK */
273 ** This used to be...
274 ** _conv(t->tm_hour, 2, ' ');
275 ** ...and has been changed to the below to
276 ** match SunOS 4.1.1 and Arnold Robbin's
277 ** strftime version 3.0. That is, "%k" and
278 ** "%l" have been swapped.
281 pt
= _conv((t
->tm_hour
% 12) ?
282 (t
->tm_hour
% 12) : 12,
286 pt
= _conv(t
->tm_min
, "%02d", pt
, ptlim
);
289 pt
= _conv(t
->tm_mon
+ 1, "%02d", pt
, ptlim
);
292 pt
= _add("\n", pt
, ptlim
);
295 pt
= _add((t
->tm_hour
>= (HOURSPERDAY
/ 2)) ?
296 _TIME_LOCALE(loc
)->am_pm
[1] :
297 _TIME_LOCALE(loc
)->am_pm
[0],
301 pt
= _fmt(sp
, "%H:%M", t
, pt
, ptlim
, warnp
,
305 pt
= _fmt(sp
, _TIME_LOCALE(loc
)->t_fmt_ampm
, t
,
306 pt
, ptlim
, warnp
, loc
);
309 pt
= _conv(t
->tm_sec
, "%02d", pt
, ptlim
);
314 char buf
[INT_STRLEN_MAXIMUM(
321 if (TYPE_SIGNED(time_t))
322 (void)snprintf(buf
, sizeof(buf
),
323 "%jd", (intmax_t) mkt
);
324 else (void)snprintf(buf
, sizeof(buf
),
325 "%ju", (uintmax_t) mkt
);
326 pt
= _add(buf
, pt
, ptlim
);
330 pt
= _fmt(sp
, "%H:%M:%S", t
, pt
, ptlim
, warnp
,
334 pt
= _add("\t", pt
, ptlim
);
337 pt
= _conv((t
->tm_yday
+ DAYSPERWEEK
-
338 t
->tm_wday
) / DAYSPERWEEK
,
343 ** From Arnold Robbins' strftime version 3.0:
344 ** "ISO 8601: Weekday as a decimal number
348 pt
= _conv((t
->tm_wday
== 0) ?
349 DAYSPERWEEK
: t
->tm_wday
,
352 case 'V': /* ISO 8601 week number */
353 case 'G': /* ISO 8601 year (four digits) */
354 case 'g': /* ISO 8601 year (two digits) */
356 ** From Arnold Robbins' strftime version 3.0: "the week number of the
357 ** year (the first Monday as the first day of week 1) as a decimal number
361 ** From <http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html> by Markus Kuhn:
362 ** "Week 01 of a year is per definition the first week which has the
363 ** Thursday in this year, which is equivalent to the week which contains
364 ** the fourth day of January. In other words, the first week of a new year
365 ** is the week which has the majority of its days in the new year. Week 01
366 ** might also contain days from the previous year and the week before week
367 ** 01 of a year is the last week (52 or 53) of the previous year even if
368 ** it contains days from the new year. A week starts with Monday (day 1)
369 ** and ends with Sunday (day 7). For example, the first week of the year
370 ** 1997 lasts from 1996-12-30 to 1997-01-05..."
389 len
= isleap_sum(year
, base
) ?
393 ** What yday (-3 ... 3) does
394 ** the ISO year begin on?
396 bot
= ((yday
+ 11 - wday
) %
399 ** What yday does the NEXT
400 ** ISO year begin on?
413 w
= 1 + ((yday
- bot
) /
418 yday
+= isleap_sum(year
, base
) ?
422 #ifdef XPG4_1994_04_09
424 t
->tm_mon
== TM_JANUARY
) ||
426 t
->tm_mon
== TM_DECEMBER
))
428 #endif /* defined XPG4_1994_04_09 */
430 pt
= _conv(w
, "%02d",
432 else if (*format
== 'g') {
434 pt
= _yconv(year
, base
,
437 } else pt
= _yconv(year
, base
,
444 ** From Arnold Robbins' strftime version 3.0:
445 ** "date as dd-bbb-YYYY"
448 pt
= _fmt(sp
, "%e-%b-%Y", t
, pt
, ptlim
, warnp
,
452 pt
= _conv((t
->tm_yday
+ DAYSPERWEEK
-
455 (DAYSPERWEEK
- 1))) / DAYSPERWEEK
,
459 pt
= _conv(t
->tm_wday
, "%d", pt
, ptlim
);
462 pt
= _fmt(sp
, _TIME_LOCALE(loc
)->t_fmt
, t
, pt
,
469 pt
= _fmt(sp
, _TIME_LOCALE(loc
)->d_fmt
, t
, pt
,
479 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
,
484 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
,
490 pt
= _add(t
->TM_ZONE
, pt
, ptlim
);
491 #endif /* defined TM_ZONE */
492 if (t
->tm_isdst
>= 0)
494 tzgetname(sp
, t
->tm_isdst
) :
495 tzname
[t
->tm_isdst
!= 0]),
498 ** C99 says that %Z must be replaced by the
499 ** empty string if the time zone is not
511 diff
= (int)t
->TM_GMTOFF
;
512 #else /* !defined TM_GMTOFF */
514 ** C99 says that the UT offset must
515 ** be computed by looking only at
516 ** tm_isdst. This requirement is
517 ** incorrect, since it means the code
518 ** must rely on magic (in this case
519 ** altzone and timezone), and the
520 ** magic might not have the correct
521 ** offset. Doing things correctly is
522 ** tricky and requires disobeying C99;
523 ** see GNU C strftime for details.
524 ** For now, punt and conform to the
525 ** standard, even though it's incorrect.
527 ** C99 says that %z must be replaced by the
528 ** empty string if the time zone is not
529 ** determinable, so output nothing if the
530 ** appropriate variables are not available.
533 if (t
->tm_isdst
== 0)
536 #else /* !defined USG_COMPAT */
538 #endif /* !defined USG_COMPAT */
542 #else /* !defined ALTZONE */
544 #endif /* !defined ALTZONE */
545 #else /* defined STD_INSPIRED */
551 ** Get calendar time from t
552 ** being treated as local.
554 tmp
= *t
; /* mktime discards const */
557 if (lct
== (time_t)-1)
561 ** Get calendar time from t
562 ** being treated as GMT.
564 tmp
= *t
; /* mktime discards const */
567 if (gct
== (time_t)-1)
570 /* LINTED difference will fit int */
571 diff
= (intmax_t)gct
- (intmax_t)lct
;
573 #endif /* defined STD_INSPIRED */
574 #endif /* !defined TM_GMTOFF */
579 pt
= _add(sign
, pt
, ptlim
);
581 diff
= (diff
/ MINSPERHOUR
) * 100 +
582 (diff
% MINSPERHOUR
);
583 _DIAGASSERT(__type_fit(int, diff
));
584 pt
= _conv((int)diff
, "%04d", pt
, ptlim
);
589 pt
= _fmt(sp
, _TIME_LOCALE(loc
)->c_fmt
, t
, pt
, ptlim
, warnp
, loc
);
591 #endif /* defined(__minix) */
594 pt
= _fmt(sp
, _TIME_LOCALE(loc
)->date_fmt
, t
,
595 pt
, ptlim
, warnp
, loc
);
600 ** X311J/88-090 (4.12.3.5): if conversion char is
601 ** undefined, behavior is undefined. Print out the
602 ** character itself as printf(3) also does.
616 strftime(char *s
, size_t maxsize
, const char *format
, const struct tm
*t
)
619 return strftime_z(NULL
, s
, maxsize
, format
, t
);
623 strftime_l(char * __restrict s
, size_t maxsize
, const char * __restrict format
,
624 const struct tm
* __restrict t
, locale_t loc
)
627 return strftime_lz(NULL
, s
, maxsize
, format
, t
, loc
);
631 _conv(int n
, const char *format
, char *pt
, const char *ptlim
)
633 char buf
[INT_STRLEN_MAXIMUM(int) + 1];
635 (void) snprintf(buf
, sizeof(buf
), format
, n
);
636 return _add(buf
, pt
, ptlim
);
640 _add(const char *str
, char *pt
, const char *ptlim
)
642 while (pt
< ptlim
&& (*pt
= *str
++) != '\0')
648 ** POSIX and the C Standard are unclear or inconsistent about
649 ** what %C and %y do if the year is negative or exceeds 9999.
650 ** Use the convention that %C concatenated with %y yields the
651 ** same output as %Y, and that %Y contains at least 4 bytes,
652 ** with more only if necessary.
656 _yconv(int a
, int b
, bool convert_top
, bool convert_yy
,
657 char *pt
, const char * ptlim
)
663 trail
= a
% DIVISOR
+ b
% DIVISOR
;
664 lead
= a
/ DIVISOR
+ b
/ DIVISOR
+ trail
/ DIVISOR
;
666 if (trail
< 0 && lead
> 0) {
669 } else if (lead
< 0 && trail
> 0) {
674 if (lead
== 0 && trail
< 0)
675 pt
= _add("-0", pt
, ptlim
);
676 else pt
= _conv(lead
, "%02d", pt
, ptlim
);
679 pt
= _conv(((trail
< 0) ? -trail
: trail
), "%02d", pt
, ptlim
);