1 /* $NetBSD: strftime.c,v 1.29 2013/09/20 19:06:54 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.29 2013/09/20 19:06:54 christos Exp $");
11 #endif /* LIBC_SCCS and not lint */
13 #include "namespace.h"
17 #include "setlocale_local.h"
20 ** Based on the UCB version with the copyright notice and sccsid
23 ** This is ANSIish only when "multibyte character == plain character".
29 ** We don't use these extensions in strftime operation even when
30 ** supported by the local tzcode configuration. A strictly
31 ** conforming C application may leave them in undefined state.
40 ** Copyright (c) 1989, 1993
41 ** The Regents of the University of California. All rights reserved.
43 ** Redistribution and use in source and binary forms, with or without
44 ** modification, are permitted provided that the following conditions
46 ** 1. Redistributions of source code must retain the above copyright
47 ** notice, this list of conditions and the following disclaimer.
48 ** 2. Redistributions in binary form must reproduce the above copyright
49 ** notice, this list of conditions and the following disclaimer in the
50 ** documentation and/or other materials provided with the distribution.
51 ** 3. All advertising materials mentioning features or use of this software
52 ** must display the following acknowledgement:
53 ** This product includes software developed by the University of
54 ** California, Berkeley and its contributors.
55 ** 4. Neither the name of the University nor the names of its contributors
56 ** may be used to endorse or promote products derived from this software
57 ** without specific prior written permission.
59 ** THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 ** ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
74 static const char sccsid
[] = "@(#)strftime.c 5.4 (Berkeley) 3/14/89";
75 #endif /* !defined lint */
76 #endif /* !defined LIBC_SCCS */
83 __weak_alias(strftime_l
, _strftime_l
)
84 __weak_alias(strftime_lz
, _strftime_lz
)
85 __weak_alias(strftime_z
, _strftime_z
)
88 #include "sys/localedef.h"
89 #define _TIME_LOCALE(loc) \
90 ((_TimeLocale *)((loc)->part_impl[(size_t)LC_TIME]))
93 static char * _add(const char *, char *, const char *);
94 static char * _conv(int, const char *, char *, const char *);
95 static char * _fmt(const timezone_t
, const char *, const struct tm
*, char *,
96 const char *, int *, locale_t
);
97 static char * _yconv(int, int, int, int, char *, const char *);
99 extern char * tzname
[];
101 #ifndef YEAR_2000_NAME
102 #define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
103 #endif /* !defined YEAR_2000_NAME */
111 strftime_z(const timezone_t sp
, char * __restrict s
, size_t maxsize
,
112 const char * __restrict format
, const struct tm
* __restrict t
)
114 return strftime_lz(sp
, s
, maxsize
, format
, t
, _current_locale());
118 strftime_lz(const timezone_t sp
, char *const s
, const size_t maxsize
,
119 const char *const format
, const struct tm
*const t
, locale_t loc
)
125 p
= _fmt(sp
, ((format
== NULL
) ? "%c" : format
), t
, s
, s
+ maxsize
,
127 #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
128 if (warn
!= IN_NONE
&& getenv(YEAR_2000_NAME
) != NULL
) {
129 (void) fprintf(stderr
, "\n");
131 (void) fprintf(stderr
, "NULL strftime format ");
132 else (void) fprintf(stderr
, "strftime format \"%s\" ",
134 (void) fprintf(stderr
, "yields only two digits of years in ");
136 (void) fprintf(stderr
, "some locales");
137 else if (warn
== IN_THIS
)
138 (void) fprintf(stderr
, "the current locale");
139 else (void) fprintf(stderr
, "all locales");
140 (void) fprintf(stderr
, "\n");
142 #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
143 if (p
== s
+ maxsize
)
150 _fmt(const timezone_t sp
, const char *format
, const struct tm
*const t
,
151 char *pt
, const char *const ptlim
, int *warnp
, locale_t loc
)
153 for ( ; *format
; ++format
) {
154 if (*format
== '%') {
161 pt
= _add((t
->tm_wday
< 0 ||
162 t
->tm_wday
>= DAYSPERWEEK
) ?
163 "?" : _TIME_LOCALE(loc
)->day
[t
->tm_wday
],
167 pt
= _add((t
->tm_wday
< 0 ||
168 t
->tm_wday
>= DAYSPERWEEK
) ?
169 "?" : _TIME_LOCALE(loc
)->abday
[t
->tm_wday
],
173 pt
= _add((t
->tm_mon
< 0 ||
174 t
->tm_mon
>= MONSPERYEAR
) ?
175 "?" : _TIME_LOCALE(loc
)->mon
[t
->tm_mon
],
180 pt
= _add((t
->tm_mon
< 0 ||
181 t
->tm_mon
>= MONSPERYEAR
) ?
182 "?" : _TIME_LOCALE(loc
)->abmon
[t
->tm_mon
],
187 ** %C used to do a...
188 ** _fmt("%a %b %e %X %Y", t);
189 ** ...whereas now POSIX 1003.2 calls for
190 ** something completely different.
193 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
, 1, 0,
200 pt
= _fmt(sp
, _TIME_LOCALE(loc
)->c_fmt
, t
, pt
,
209 pt
= _fmt(sp
, "%m/%d/%y", t
, pt
, ptlim
, warnp
,
213 pt
= _conv(t
->tm_mday
, "%02d", pt
, ptlim
);
218 ** C99 locale modifiers.
220 ** %Ec %EC %Ex %EX %Ey %EY
221 ** %Od %oe %OH %OI %Om %OM
222 ** %OS %Ou %OU %OV %Ow %OW %Oy
223 ** are supposed to provide alternate
228 pt
= _conv(t
->tm_mday
, "%2d", pt
, ptlim
);
231 pt
= _fmt(sp
, "%Y-%m-%d", t
, pt
, ptlim
, warnp
,
235 pt
= _conv(t
->tm_hour
, "%02d", pt
, ptlim
);
238 pt
= _conv((t
->tm_hour
% 12) ?
239 (t
->tm_hour
% 12) : 12,
243 pt
= _conv(t
->tm_yday
+ 1, "%03d", pt
, ptlim
);
247 ** This used to be...
248 ** _conv(t->tm_hour % 12 ?
249 ** t->tm_hour % 12 : 12, 2, ' ');
250 ** ...and has been changed to the below to
251 ** match SunOS 4.1.1 and Arnold Robbins'
252 ** strftime version 3.0. That is, "%k" and
253 ** "%l" have been swapped.
256 pt
= _conv(t
->tm_hour
, "%2d", pt
, ptlim
);
261 ** After all this time, still unclaimed!
263 pt
= _add("kitchen sink", pt
, ptlim
);
265 #endif /* defined KITCHEN_SINK */
268 ** This used to be...
269 ** _conv(t->tm_hour, 2, ' ');
270 ** ...and has been changed to the below to
271 ** match SunOS 4.1.1 and Arnold Robbin's
272 ** strftime version 3.0. That is, "%k" and
273 ** "%l" have been swapped.
276 pt
= _conv((t
->tm_hour
% 12) ?
277 (t
->tm_hour
% 12) : 12,
281 pt
= _conv(t
->tm_min
, "%02d", pt
, ptlim
);
284 pt
= _conv(t
->tm_mon
+ 1, "%02d", pt
, ptlim
);
287 pt
= _add("\n", pt
, ptlim
);
290 pt
= _add((t
->tm_hour
>= (HOURSPERDAY
/ 2)) ?
291 _TIME_LOCALE(loc
)->am_pm
[1] :
292 _TIME_LOCALE(loc
)->am_pm
[0],
296 pt
= _fmt(sp
, "%H:%M", t
, pt
, ptlim
, warnp
,
300 pt
= _fmt(sp
, _TIME_LOCALE(loc
)->t_fmt_ampm
, t
,
301 pt
, ptlim
, warnp
, loc
);
304 pt
= _conv(t
->tm_sec
, "%02d", pt
, ptlim
);
309 char buf
[INT_STRLEN_MAXIMUM(
316 if (TYPE_SIGNED(time_t))
317 (void)snprintf(buf
, sizeof(buf
),
318 "%jd", (intmax_t) mkt
);
319 else (void)snprintf(buf
, sizeof(buf
),
320 "%ju", (uintmax_t) mkt
);
321 pt
= _add(buf
, pt
, ptlim
);
325 pt
= _fmt(sp
, "%H:%M:%S", t
, pt
, ptlim
, warnp
,
329 pt
= _add("\t", pt
, ptlim
);
332 pt
= _conv((t
->tm_yday
+ DAYSPERWEEK
-
333 t
->tm_wday
) / DAYSPERWEEK
,
338 ** From Arnold Robbins' strftime version 3.0:
339 ** "ISO 8601: Weekday as a decimal number
343 pt
= _conv((t
->tm_wday
== 0) ?
344 DAYSPERWEEK
: t
->tm_wday
,
347 case 'V': /* ISO 8601 week number */
348 case 'G': /* ISO 8601 year (four digits) */
349 case 'g': /* ISO 8601 year (two digits) */
351 ** From Arnold Robbins' strftime version 3.0: "the week number of the
352 ** year (the first Monday as the first day of week 1) as a decimal number
356 ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
357 ** "Week 01 of a year is per definition the first week which has the
358 ** Thursday in this year, which is equivalent to the week which contains
359 ** the fourth day of January. In other words, the first week of a new year
360 ** is the week which has the majority of its days in the new year. Week 01
361 ** might also contain days from the previous year and the week before week
362 ** 01 of a year is the last week (52 or 53) of the previous year even if
363 ** it contains days from the new year. A week starts with Monday (day 1)
364 ** and ends with Sunday (day 7). For example, the first week of the year
365 ** 1997 lasts from 1996-12-30 to 1997-01-05..."
384 len
= isleap_sum(year
, base
) ?
388 ** What yday (-3 ... 3) does
389 ** the ISO year begin on?
391 bot
= ((yday
+ 11 - wday
) %
394 ** What yday does the NEXT
395 ** ISO year begin on?
408 w
= 1 + ((yday
- bot
) /
413 yday
+= isleap_sum(year
, base
) ?
417 #ifdef XPG4_1994_04_09
419 t
->tm_mon
== TM_JANUARY
) ||
421 t
->tm_mon
== TM_DECEMBER
))
423 #endif /* defined XPG4_1994_04_09 */
425 pt
= _conv(w
, "%02d",
427 else if (*format
== 'g') {
429 pt
= _yconv(year
, base
, 0, 1,
431 } else pt
= _yconv(year
, base
, 1, 1,
437 ** From Arnold Robbins' strftime version 3.0:
438 ** "date as dd-bbb-YYYY"
441 pt
= _fmt(sp
, "%e-%b-%Y", t
, pt
, ptlim
, warnp
,
445 pt
= _conv((t
->tm_yday
+ DAYSPERWEEK
-
448 (DAYSPERWEEK
- 1))) / DAYSPERWEEK
,
452 pt
= _conv(t
->tm_wday
, "%d", pt
, ptlim
);
455 pt
= _fmt(sp
, _TIME_LOCALE(loc
)->t_fmt
, t
, pt
,
462 pt
= _fmt(sp
, _TIME_LOCALE(loc
)->d_fmt
, t
, pt
,
472 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
, 0, 1,
476 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
, 1, 1,
481 if (t
->TM_ZONE
!= NULL
)
482 pt
= _add(t
->TM_ZONE
, pt
, ptlim
);
484 #endif /* defined TM_ZONE */
485 if (t
->tm_isdst
>= 0)
487 tzgetname(sp
, t
->tm_isdst
) :
488 tzname
[t
->tm_isdst
!= 0]),
491 ** C99 says that %Z must be replaced by the
492 ** empty string if the time zone is not
504 diff
= (int)t
->TM_GMTOFF
;
505 #else /* !defined TM_GMTOFF */
507 ** C99 says that the UT offset must
508 ** be computed by looking only at
509 ** tm_isdst. This requirement is
510 ** incorrect, since it means the code
511 ** must rely on magic (in this case
512 ** altzone and timezone), and the
513 ** magic might not have the correct
514 ** offset. Doing things correctly is
515 ** tricky and requires disobeying C99;
516 ** see GNU C strftime for details.
517 ** For now, punt and conform to the
518 ** standard, even though it's incorrect.
520 ** C99 says that %z must be replaced by the
521 ** empty string if the time zone is not
522 ** determinable, so output nothing if the
523 ** appropriate variables are not available.
526 if (t
->tm_isdst
== 0)
529 #else /* !defined USG_COMPAT */
531 #endif /* !defined USG_COMPAT */
535 #else /* !defined ALTZONE */
537 #endif /* !defined ALTZONE */
538 #else /* defined STD_INSPIRED */
544 ** Get calendar time from t
545 ** being treated as local.
547 tmp
= *t
; /* mktime discards const */
550 if (lct
== (time_t)-1)
554 ** Get calendar time from t
555 ** being treated as GMT.
557 tmp
= *t
; /* mktime discards const */
560 if (gct
== (time_t)-1)
563 /* LINTED difference will fit int */
564 diff
= (intmax_t)gct
- (intmax_t)lct
;
566 #endif /* defined STD_INSPIRED */
567 #endif /* !defined TM_GMTOFF */
572 pt
= _add(sign
, pt
, ptlim
);
574 diff
= (diff
/ MINSPERHOUR
) * 100 +
575 (diff
% MINSPERHOUR
);
576 pt
= _conv(diff
, "%04d", pt
, ptlim
);
581 pt
= _fmt(sp
, _TIME_LOCALE(loc
)->c_fmt
, t
, pt
, ptlim
, warnp
, loc
);
583 #endif /* defined(__minix) */
586 pt
= _fmt(sp
, _TIME_LOCALE(loc
)->date_fmt
, t
,
587 pt
, ptlim
, warnp
, loc
);
592 ** X311J/88-090 (4.12.3.5): if conversion char is
593 ** undefined, behavior is undefined. Print out the
594 ** character itself as printf(3) also does.
608 strftime(char * const s
, const size_t maxsize
,
609 const char * const format
, const struct tm
* const t
)
612 return strftime_z(NULL
, s
, maxsize
, format
, t
);
616 strftime_l(char * __restrict s
, size_t maxsize
, const char * __restrict format
,
617 const struct tm
* __restrict t
, locale_t loc
)
620 return strftime_lz(NULL
, s
, maxsize
, format
, t
, loc
);
624 _conv(const int n
, const char *const format
, char *const pt
,
625 const char *const ptlim
)
627 char buf
[INT_STRLEN_MAXIMUM(int) + 1];
629 (void) snprintf(buf
, sizeof(buf
), format
, n
);
630 return _add(buf
, pt
, ptlim
);
634 _add(const char *str
, char *pt
, const char *const ptlim
)
636 while (pt
< ptlim
&& (*pt
= *str
++) != '\0')
642 ** POSIX and the C Standard are unclear or inconsistent about
643 ** what %C and %y do if the year is negative or exceeds 9999.
644 ** Use the convention that %C concatenated with %y yields the
645 ** same output as %Y, and that %Y contains at least 4 bytes,
646 ** with more only if necessary.
650 _yconv(const int a
, const int b
, const int convert_top
, const int convert_yy
,
651 char *pt
, const char *const ptlim
)
657 trail
= a
% DIVISOR
+ b
% DIVISOR
;
658 lead
= a
/ DIVISOR
+ b
/ DIVISOR
+ trail
/ DIVISOR
;
660 if (trail
< 0 && lead
> 0) {
663 } else if (lead
< 0 && trail
> 0) {
668 if (lead
== 0 && trail
< 0)
669 pt
= _add("-0", pt
, ptlim
);
670 else pt
= _conv(lead
, "%02d", pt
, ptlim
);
673 pt
= _conv(((trail
< 0) ? -trail
: trail
), "%02d", pt
, ptlim
);
678 static struct lc_time_T
*
681 static const char locale_home
[] = LOCALE_HOME
;
682 static const char lc_time
[] = "LC_TIME";
683 static char * locale_buf
;
686 int oldsun
; /* "...ain't got nothin' to do..." */
692 char filename
[FILENAME_MAX
];
698 ** Use localebuf.mon[0] to signal whether locale is already set up.
700 if (localebuf
.mon
[0])
702 name
= setlocale(LC_TIME
, NULL
);
703 if (name
== NULL
|| *name
== '\0')
706 ** If the locale name is the same as our cache, use the cache.
709 if (lbuf
!= NULL
&& strcmp(name
, lbuf
) == 0) {
711 for (ap
= (const char **) &localebuf
;
712 ap
< (const char **) (&localebuf
+ 1);
714 *ap
= p
+= strlen(p
) + 1;
718 ** Slurp the locale file into the cache.
720 namesize
= strlen(name
) + 1;
721 if (sizeof filename
<
722 ((sizeof locale_home
) + namesize
+ (sizeof lc_time
)))
725 (void) sprintf(filename
, "%s/%s/%s", locale_home
, name
, lc_time
);
726 fd
= open(filename
, O_RDONLY
);
729 ** Old Sun systems have a different naming and data convention.
732 (void) sprintf(filename
, "%s/%s/%s", locale_home
,
734 fd
= open(filename
, O_RDONLY
);
738 if (fstat(fd
, &st
) != 0)
742 bufsize
= namesize
+ st
.st_size
;
744 lbuf
= (lbuf
== NULL
) ? malloc(bufsize
) : realloc(lbuf
, bufsize
);
747 (void) strcpy(lbuf
, name
);
749 plim
= p
+ st
.st_size
;
750 if (read(fd
, p
, (size_t) st
.st_size
) != st
.st_size
)
755 ** Parse the locale file into localebuf.
757 if (plim
[-1] != '\n')
759 for (ap
= (const char **) &localebuf
;
760 ap
< (const char **) (&localebuf
+ 1);
771 ** SunOS 4 used an obsolescent format; see localdtconv(3).
772 ** c_fmt had the ``short format for dates and times together''
773 ** (SunOS 4 date, "%a %b %e %T %Z %Y" in the C locale);
774 ** date_fmt had the ``long format for dates''
775 ** (SunOS 4 strftime %C, "%A, %B %e, %Y" in the C locale).
776 ** Discard the latter in favor of the former.
778 localebuf
.date_fmt
= localebuf
.c_fmt
;
781 ** Record the successful parse in the cache.
792 localebuf
= C_time_locale
;
796 #endif /* defined LOCALE_HOME */