1 /* $NetBSD: strftime.c,v 1.21 2010/12/16 18:38:07 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.21 2010/12/16 18:38:07 christos Exp $");
11 #endif /* LIBC_SCCS and not lint */
13 #include "namespace.h"
16 ** Based on the UCB version with the ID appearing below.
17 ** This is ANSIish only when "multibyte character == plain character".
23 ** We don't use these extensions in strftime operation even when
24 ** supported by the local tzcode configuration. A strictly
25 ** conforming C application may leave them in undefined state.
34 ** Copyright (c) 1989, 1993
35 ** The Regents of the University of California. All rights reserved.
37 ** Redistribution and use in source and binary forms, with or without
38 ** modification, are permitted provided that the following conditions
40 ** 1. Redistributions of source code must retain the above copyright
41 ** notice, this list of conditions and the following disclaimer.
42 ** 2. Redistributions in binary form must reproduce the above copyright
43 ** notice, this list of conditions and the following disclaimer in the
44 ** documentation and/or other materials provided with the distribution.
45 ** 3. All advertising materials mentioning features or use of this software
46 ** must display the following acknowledgement:
47 ** This product includes software developed by the University of
48 ** California, Berkeley and its contributors.
49 ** 4. Neither the name of the University nor the names of its contributors
50 ** may be used to endorse or promote products derived from this software
51 ** without specific prior written permission.
53 ** THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 ** ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 static const char sccsid
[] = "@(#)strftime.c 5.4 (Berkeley) 3/14/89";
69 #endif /* !defined lint */
70 #endif /* !defined LIBC_SCCS */
77 __weak_alias(strftime_z
, _strftime_z
)
80 #include "sys/localedef.h"
81 #define Locale _CurrentTimeLocale
84 static char * _add(const char *, char *, const char *);
85 static char * _conv(int, const char *, char *, const char *);
86 static char * _fmt(const timezone_t
, const char *, const struct tm
*, char *,
88 static char * _yconv(int, int, int, int, char *, const char *);
90 extern char * tzname
[];
92 #ifndef YEAR_2000_NAME
93 #define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
94 #endif /* !defined YEAR_2000_NAME */
102 strftime_z(const timezone_t sp
, char * const s
, const size_t maxsize
,
103 const char * const format
, const struct tm
* const t
)
109 p
= _fmt(sp
, ((format
== NULL
) ? "%c" : format
), t
, s
, s
+ maxsize
,
111 #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
112 if (warn
!= IN_NONE
&& getenv(YEAR_2000_NAME
) != NULL
) {
113 (void) fprintf(stderr
, "\n");
115 (void) fprintf(stderr
, "NULL strftime format ");
116 else (void) fprintf(stderr
, "strftime format \"%s\" ",
118 (void) fprintf(stderr
, "yields only two digits of years in ");
120 (void) fprintf(stderr
, "some locales");
121 else if (warn
== IN_THIS
)
122 (void) fprintf(stderr
, "the current locale");
123 else (void) fprintf(stderr
, "all locales");
124 (void) fprintf(stderr
, "\n");
126 #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
127 if (p
== s
+ maxsize
)
134 _fmt(const timezone_t sp
, const char *format
, const struct tm
* const t
,
135 char *pt
, const char *const ptlim
, int *warnp
)
137 for ( ; *format
; ++format
) {
138 if (*format
== '%') {
145 pt
= _add((t
->tm_wday
< 0 ||
146 t
->tm_wday
>= DAYSPERWEEK
) ?
147 "?" : Locale
->day
[t
->tm_wday
],
151 pt
= _add((t
->tm_wday
< 0 ||
152 t
->tm_wday
>= DAYSPERWEEK
) ?
153 "?" : Locale
->abday
[t
->tm_wday
],
157 pt
= _add((t
->tm_mon
< 0 ||
158 t
->tm_mon
>= MONSPERYEAR
) ?
159 "?" : Locale
->mon
[t
->tm_mon
],
164 pt
= _add((t
->tm_mon
< 0 ||
165 t
->tm_mon
>= MONSPERYEAR
) ?
166 "?" : Locale
->abmon
[t
->tm_mon
],
171 ** %C used to do a...
172 ** _fmt("%a %b %e %X %Y", t);
173 ** ...whereas now POSIX 1003.2 calls for
174 ** something completely different.
177 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
, 1, 0,
184 pt
= _fmt(sp
, Locale
->c_fmt
, t
, pt
, ptlim
, &warn2
);
192 pt
= _fmt(sp
, "%m/%d/%y", t
, pt
, ptlim
, warnp
);
195 pt
= _conv(t
->tm_mday
, "%02d", pt
, ptlim
);
200 ** C99 locale modifiers.
202 ** %Ec %EC %Ex %EX %Ey %EY
203 ** %Od %oe %OH %OI %Om %OM
204 ** %OS %Ou %OU %OV %Ow %OW %Oy
205 ** are supposed to provide alternate
210 pt
= _conv(t
->tm_mday
, "%2d", pt
, ptlim
);
213 pt
= _fmt(sp
, "%Y-%m-%d", t
, pt
, ptlim
, warnp
);
216 pt
= _conv(t
->tm_hour
, "%02d", pt
, ptlim
);
219 pt
= _conv((t
->tm_hour
% 12) ?
220 (t
->tm_hour
% 12) : 12,
224 pt
= _conv(t
->tm_yday
+ 1, "%03d", pt
, ptlim
);
228 ** This used to be...
229 ** _conv(t->tm_hour % 12 ?
230 ** t->tm_hour % 12 : 12, 2, ' ');
231 ** ...and has been changed to the below to
232 ** match SunOS 4.1.1 and Arnold Robbins'
233 ** strftime version 3.0. That is, "%k" and
234 ** "%l" have been swapped.
237 pt
= _conv(t
->tm_hour
, "%2d", pt
, ptlim
);
242 ** After all this time, still unclaimed!
244 pt
= _add("kitchen sink", pt
, ptlim
);
246 #endif /* defined KITCHEN_SINK */
249 ** This used to be...
250 ** _conv(t->tm_hour, 2, ' ');
251 ** ...and has been changed to the below to
252 ** match SunOS 4.1.1 and Arnold Robbin's
253 ** strftime version 3.0. That is, "%k" and
254 ** "%l" have been swapped.
257 pt
= _conv((t
->tm_hour
% 12) ?
258 (t
->tm_hour
% 12) : 12,
262 pt
= _conv(t
->tm_min
, "%02d", pt
, ptlim
);
265 pt
= _conv(t
->tm_mon
+ 1, "%02d", pt
, ptlim
);
268 pt
= _add("\n", pt
, ptlim
);
271 pt
= _add((t
->tm_hour
>= (HOURSPERDAY
/ 2)) ?
277 pt
= _fmt(sp
, "%H:%M", t
, pt
, ptlim
, warnp
);
280 pt
= _fmt(sp
, Locale
->t_fmt_ampm
, t
, pt
, ptlim
,
284 pt
= _conv(t
->tm_sec
, "%02d", pt
, ptlim
);
289 char buf
[INT_STRLEN_MAXIMUM(
296 if (TYPE_SIGNED(time_t))
297 (void) snprintf(buf
, sizeof(buf
),
298 "%lld", (long long) mkt
);
299 else (void) snprintf(buf
, sizeof(buf
),
300 "%llu", (unsigned long long)
302 pt
= _add(buf
, pt
, ptlim
);
306 pt
= _fmt(sp
, "%H:%M:%S", t
, pt
, ptlim
, warnp
);
309 pt
= _add("\t", pt
, ptlim
);
312 pt
= _conv((t
->tm_yday
+ DAYSPERWEEK
-
313 t
->tm_wday
) / DAYSPERWEEK
,
318 ** From Arnold Robbins' strftime version 3.0:
319 ** "ISO 8601: Weekday as a decimal number
323 pt
= _conv((t
->tm_wday
== 0) ?
324 DAYSPERWEEK
: t
->tm_wday
,
327 case 'V': /* ISO 8601 week number */
328 case 'G': /* ISO 8601 year (four digits) */
329 case 'g': /* ISO 8601 year (two digits) */
331 ** From Arnold Robbins' strftime version 3.0: "the week number of the
332 ** year (the first Monday as the first day of week 1) as a decimal number
336 ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
337 ** "Week 01 of a year is per definition the first week which has the
338 ** Thursday in this year, which is equivalent to the week which contains
339 ** the fourth day of January. In other words, the first week of a new year
340 ** is the week which has the majority of its days in the new year. Week 01
341 ** might also contain days from the previous year and the week before week
342 ** 01 of a year is the last week (52 or 53) of the previous year even if
343 ** it contains days from the new year. A week starts with Monday (day 1)
344 ** and ends with Sunday (day 7). For example, the first week of the year
345 ** 1997 lasts from 1996-12-30 to 1997-01-05..."
364 len
= isleap_sum(year
, base
) ?
368 ** What yday (-3 ... 3) does
369 ** the ISO year begin on?
371 bot
= ((yday
+ 11 - wday
) %
374 ** What yday does the NEXT
375 ** ISO year begin on?
388 w
= 1 + ((yday
- bot
) /
393 yday
+= isleap_sum(year
, base
) ?
397 #ifdef XPG4_1994_04_09
399 t
->tm_mon
== TM_JANUARY
) ||
401 t
->tm_mon
== TM_DECEMBER
))
403 #endif /* defined XPG4_1994_04_09 */
405 pt
= _conv(w
, "%02d",
407 else if (*format
== 'g') {
409 pt
= _yconv(year
, base
, 0, 1,
411 } else pt
= _yconv(year
, base
, 1, 1,
417 ** From Arnold Robbins' strftime version 3.0:
418 ** "date as dd-bbb-YYYY"
421 pt
= _fmt(sp
, "%e-%b-%Y", t
, pt
, ptlim
, warnp
);
424 pt
= _conv((t
->tm_yday
+ DAYSPERWEEK
-
427 (DAYSPERWEEK
- 1))) / DAYSPERWEEK
,
431 pt
= _conv(t
->tm_wday
, "%d", pt
, ptlim
);
434 pt
= _fmt(sp
, Locale
->t_fmt
, t
, pt
, ptlim
, warnp
);
440 pt
= _fmt(sp
, Locale
->d_fmt
, t
, pt
, ptlim
, &warn2
);
449 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
, 0, 1,
453 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
, 1, 1,
458 if (t
->TM_ZONE
!= NULL
)
459 pt
= _add(t
->TM_ZONE
, pt
, ptlim
);
461 #endif /* defined TM_ZONE */
462 if (t
->tm_isdst
>= 0)
464 tzgetname(sp
, t
->tm_isdst
) :
465 tzname
[t
->tm_isdst
!= 0]),
468 ** C99 says that %Z must be replaced by the
469 ** empty string if the time zone is not
481 diff
= (int)t
->TM_GMTOFF
;
482 #else /* !defined TM_GMTOFF */
484 ** C99 says that the UTC offset must
485 ** be computed by looking only at
486 ** tm_isdst. This requirement is
487 ** incorrect, since it means the code
488 ** must rely on magic (in this case
489 ** altzone and timezone), and the
490 ** magic might not have the correct
491 ** offset. Doing things correctly is
492 ** tricky and requires disobeying C99;
493 ** see GNU C strftime for details.
494 ** For now, punt and conform to the
495 ** standard, even though it's incorrect.
497 ** C99 says that %z must be replaced by the
498 ** empty string if the time zone is not
499 ** determinable, so output nothing if the
500 ** appropriate variables are not available.
503 if (t
->tm_isdst
== 0)
506 #else /* !defined USG_COMPAT */
508 #endif /* !defined USG_COMPAT */
512 #else /* !defined ALTZONE */
514 #endif /* !defined ALTZONE */
515 #else /* defined STD_INSPIRED */
521 ** Get calendar time from t
522 ** being treated as local.
524 tmp
= *t
; /* mktime discards const */
527 if (lct
== (time_t)-1)
531 ** Get calendar time from t
532 ** being treated as GMT.
534 tmp
= *t
; /* mktime discards const */
537 if (gct
== (time_t)-1)
540 /* LINTED difference will fit int */
541 diff
= (intmax_t)gct
- (intmax_t)lct
;
543 #endif /* defined STD_INSPIRED */
544 #endif /* !defined TM_GMTOFF */
549 pt
= _add(sign
, pt
, ptlim
);
551 diff
= (diff
/ MINSPERHOUR
) * 100 +
552 (diff
% MINSPERHOUR
);
553 pt
= _conv(diff
, "%04d", pt
, ptlim
);
558 pt
= _fmt(sp
, Locale
->c_fmt
, t
, pt
, ptlim
, warnp
);
560 #endif /* !__minix */
563 pt
= _fmt(sp
, Locale
->date_fmt
, t
, pt
, ptlim
,
569 ** X311J/88-090 (4.12.3.5): if conversion char is
570 ** undefined, behavior is undefined. Print out the
571 ** character itself as printf(3) also does.
585 strftime(char * const s
, const size_t maxsize
,
586 const char * const format
, const struct tm
* const t
)
589 return strftime_z(NULL
, s
, maxsize
, format
, t
);
593 _conv(n
, format
, pt
, ptlim
)
595 const char * const format
;
597 const char * const ptlim
;
599 char buf
[INT_STRLEN_MAXIMUM(int) + 1];
601 (void) snprintf(buf
, sizeof(buf
), format
, n
);
602 return _add(buf
, pt
, ptlim
);
609 const char * const ptlim
;
611 while (pt
< ptlim
&& (*pt
= *str
++) != '\0')
617 ** POSIX and the C Standard are unclear or inconsistent about
618 ** what %C and %y do if the year is negative or exceeds 9999.
619 ** Use the convention that %C concatenated with %y yields the
620 ** same output as %Y, and that %Y contains at least 4 bytes,
621 ** with more only if necessary.
625 _yconv(a
, b
, convert_top
, convert_yy
, pt
, ptlim
)
628 const int convert_top
;
629 const int convert_yy
;
631 const char * const ptlim
;
637 trail
= a
% DIVISOR
+ b
% DIVISOR
;
638 lead
= a
/ DIVISOR
+ b
/ DIVISOR
+ trail
/ DIVISOR
;
640 if (trail
< 0 && lead
> 0) {
643 } else if (lead
< 0 && trail
> 0) {
648 if (lead
== 0 && trail
< 0)
649 pt
= _add("-0", pt
, ptlim
);
650 else pt
= _conv(lead
, "%02d", pt
, ptlim
);
653 pt
= _conv(((trail
< 0) ? -trail
: trail
), "%02d", pt
, ptlim
);
658 static struct lc_time_T
*
661 static const char locale_home
[] = LOCALE_HOME
;
662 static const char lc_time
[] = "LC_TIME";
663 static char * locale_buf
;
666 int oldsun
; /* "...ain't got nothin' to do..." */
672 char filename
[FILENAME_MAX
];
678 ** Use localebuf.mon[0] to signal whether locale is already set up.
680 if (localebuf
.mon
[0])
682 name
= setlocale(LC_TIME
, (char *) NULL
);
683 if (name
== NULL
|| *name
== '\0')
686 ** If the locale name is the same as our cache, use the cache.
689 if (lbuf
!= NULL
&& strcmp(name
, lbuf
) == 0) {
691 for (ap
= (const char **) &localebuf
;
692 ap
< (const char **) (&localebuf
+ 1);
694 *ap
= p
+= strlen(p
) + 1;
698 ** Slurp the locale file into the cache.
700 namesize
= strlen(name
) + 1;
701 if (sizeof filename
<
702 ((sizeof locale_home
) + namesize
+ (sizeof lc_time
)))
705 (void) sprintf(filename
, "%s/%s/%s", locale_home
, name
, lc_time
);
706 fd
= open(filename
, O_RDONLY
);
709 ** Old Sun systems have a different naming and data convention.
712 (void) sprintf(filename
, "%s/%s/%s", locale_home
,
714 fd
= open(filename
, O_RDONLY
);
718 if (fstat(fd
, &st
) != 0)
722 bufsize
= namesize
+ st
.st_size
;
724 lbuf
= (lbuf
== NULL
) ? malloc(bufsize
) : realloc(lbuf
, bufsize
);
727 (void) strcpy(lbuf
, name
);
729 plim
= p
+ st
.st_size
;
730 if (read(fd
, p
, (size_t) st
.st_size
) != st
.st_size
)
735 ** Parse the locale file into localebuf.
737 if (plim
[-1] != '\n')
739 for (ap
= (const char **) &localebuf
;
740 ap
< (const char **) (&localebuf
+ 1);
751 ** SunOS 4 used an obsolescent format; see localdtconv(3).
752 ** c_fmt had the ``short format for dates and times together''
753 ** (SunOS 4 date, "%a %b %e %T %Z %Y" in the C locale);
754 ** date_fmt had the ``long format for dates''
755 ** (SunOS 4 strftime %C, "%A, %B %e, %Y" in the C locale).
756 ** Discard the latter in favor of the former.
758 localebuf
.date_fmt
= localebuf
.c_fmt
;
761 ** Record the successful parse in the cache.
772 localebuf
= C_time_locale
;
776 #endif /* defined LOCALE_HOME */