1 /* $NetBSD: strftime.c,v 1.19 2009/01/11 02:46:30 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.19 2009/01/11 02:46:30 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 */
76 #include "sys/localedef.h"
77 #define Locale _CurrentTimeLocale
80 static char * _add(const char *, char *, const char *);
81 static char * _conv(int, const char *, char *, const char *);
82 static char * _fmt(const char *, const struct tm
*, char *, const char *,
84 static char * _yconv(int, int, int, int, char *, const char *);
86 extern char * tzname
[];
88 #ifndef YEAR_2000_NAME
89 #define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
90 #endif /* !defined YEAR_2000_NAME */
98 strftime(s
, maxsize
, format
, t
)
100 const size_t maxsize
;
101 const char * const format
;
102 const struct tm
* const t
;
109 p
= _fmt(((format
== NULL
) ? "%c" : format
), t
, s
, s
+ maxsize
, &warn
);
110 #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
111 if (warn
!= IN_NONE
&& getenv(YEAR_2000_NAME
) != NULL
) {
112 (void) fprintf(stderr
, "\n");
114 (void) fprintf(stderr
, "NULL strftime format ");
115 else (void) fprintf(stderr
, "strftime format \"%s\" ",
117 (void) fprintf(stderr
, "yields only two digits of years in ");
119 (void) fprintf(stderr
, "some locales");
120 else if (warn
== IN_THIS
)
121 (void) fprintf(stderr
, "the current locale");
122 else (void) fprintf(stderr
, "all locales");
123 (void) fprintf(stderr
, "\n");
125 #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
126 if (p
== s
+ maxsize
)
133 _fmt(format
, t
, pt
, ptlim
, warnp
)
135 const struct tm
* const t
;
137 const char * const ptlim
;
140 for ( ; *format
; ++format
) {
141 if (*format
== '%') {
148 pt
= _add((t
->tm_wday
< 0 ||
149 t
->tm_wday
>= DAYSPERWEEK
) ?
150 "?" : Locale
->day
[t
->tm_wday
],
154 pt
= _add((t
->tm_wday
< 0 ||
155 t
->tm_wday
>= DAYSPERWEEK
) ?
156 "?" : Locale
->abday
[t
->tm_wday
],
160 pt
= _add((t
->tm_mon
< 0 ||
161 t
->tm_mon
>= MONSPERYEAR
) ?
162 "?" : Locale
->mon
[t
->tm_mon
],
167 pt
= _add((t
->tm_mon
< 0 ||
168 t
->tm_mon
>= MONSPERYEAR
) ?
169 "?" : Locale
->abmon
[t
->tm_mon
],
174 ** %C used to do a...
175 ** _fmt("%a %b %e %X %Y", t);
176 ** ...whereas now POSIX 1003.2 calls for
177 ** something completely different.
180 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
, 1, 0,
187 pt
= _fmt(Locale
->c_fmt
, t
, pt
, ptlim
, &warn2
);
195 pt
= _fmt("%m/%d/%y", t
, pt
, ptlim
, warnp
);
198 pt
= _conv(t
->tm_mday
, "%02d", pt
, ptlim
);
203 ** C99 locale modifiers.
205 ** %Ec %EC %Ex %EX %Ey %EY
206 ** %Od %oe %OH %OI %Om %OM
207 ** %OS %Ou %OU %OV %Ow %OW %Oy
208 ** are supposed to provide alternate
213 pt
= _conv(t
->tm_mday
, "%2d", pt
, ptlim
);
216 pt
= _fmt("%Y-%m-%d", t
, pt
, ptlim
, warnp
);
219 pt
= _conv(t
->tm_hour
, "%02d", pt
, ptlim
);
222 pt
= _conv((t
->tm_hour
% 12) ?
223 (t
->tm_hour
% 12) : 12,
227 pt
= _conv(t
->tm_yday
+ 1, "%03d", pt
, ptlim
);
231 ** This used to be...
232 ** _conv(t->tm_hour % 12 ?
233 ** t->tm_hour % 12 : 12, 2, ' ');
234 ** ...and has been changed to the below to
235 ** match SunOS 4.1.1 and Arnold Robbins'
236 ** strftime version 3.0. That is, "%k" and
237 ** "%l" have been swapped.
240 pt
= _conv(t
->tm_hour
, "%2d", pt
, ptlim
);
245 ** After all this time, still unclaimed!
247 pt
= _add("kitchen sink", pt
, ptlim
);
249 #endif /* defined KITCHEN_SINK */
252 ** This used to be...
253 ** _conv(t->tm_hour, 2, ' ');
254 ** ...and has been changed to the below to
255 ** match SunOS 4.1.1 and Arnold Robbin's
256 ** strftime version 3.0. That is, "%k" and
257 ** "%l" have been swapped.
260 pt
= _conv((t
->tm_hour
% 12) ?
261 (t
->tm_hour
% 12) : 12,
265 pt
= _conv(t
->tm_min
, "%02d", pt
, ptlim
);
268 pt
= _conv(t
->tm_mon
+ 1, "%02d", pt
, ptlim
);
271 pt
= _add("\n", pt
, ptlim
);
274 pt
= _add((t
->tm_hour
>= (HOURSPERDAY
/ 2)) ?
280 pt
= _fmt("%H:%M", t
, pt
, ptlim
, warnp
);
283 pt
= _fmt(Locale
->t_fmt_ampm
, t
, pt
, ptlim
,
287 pt
= _conv(t
->tm_sec
, "%02d", pt
, ptlim
);
292 char buf
[INT_STRLEN_MAXIMUM(
299 if (TYPE_SIGNED(time_t))
300 (void) snprintf(buf
, sizeof(buf
),
301 "%lld", (long long) mkt
);
302 else (void) snprintf(buf
, sizeof(buf
),
303 "%llu", (unsigned long long)
305 pt
= _add(buf
, pt
, ptlim
);
309 pt
= _fmt("%H:%M:%S", t
, pt
, ptlim
, warnp
);
312 pt
= _add("\t", pt
, ptlim
);
315 pt
= _conv((t
->tm_yday
+ DAYSPERWEEK
-
316 t
->tm_wday
) / DAYSPERWEEK
,
321 ** From Arnold Robbins' strftime version 3.0:
322 ** "ISO 8601: Weekday as a decimal number
326 pt
= _conv((t
->tm_wday
== 0) ?
327 DAYSPERWEEK
: t
->tm_wday
,
330 case 'V': /* ISO 8601 week number */
331 case 'G': /* ISO 8601 year (four digits) */
332 case 'g': /* ISO 8601 year (two digits) */
334 ** From Arnold Robbins' strftime version 3.0: "the week number of the
335 ** year (the first Monday as the first day of week 1) as a decimal number
339 ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
340 ** "Week 01 of a year is per definition the first week which has the
341 ** Thursday in this year, which is equivalent to the week which contains
342 ** the fourth day of January. In other words, the first week of a new year
343 ** is the week which has the majority of its days in the new year. Week 01
344 ** might also contain days from the previous year and the week before week
345 ** 01 of a year is the last week (52 or 53) of the previous year even if
346 ** it contains days from the new year. A week starts with Monday (day 1)
347 ** and ends with Sunday (day 7). For example, the first week of the year
348 ** 1997 lasts from 1996-12-30 to 1997-01-05..."
367 len
= isleap_sum(year
, base
) ?
371 ** What yday (-3 ... 3) does
372 ** the ISO year begin on?
374 bot
= ((yday
+ 11 - wday
) %
377 ** What yday does the NEXT
378 ** ISO year begin on?
391 w
= 1 + ((yday
- bot
) /
396 yday
+= isleap_sum(year
, base
) ?
400 #ifdef XPG4_1994_04_09
402 t
->tm_mon
== TM_JANUARY
) ||
404 t
->tm_mon
== TM_DECEMBER
))
406 #endif /* defined XPG4_1994_04_09 */
408 pt
= _conv(w
, "%02d",
410 else if (*format
== 'g') {
412 pt
= _yconv(year
, base
, 0, 1,
414 } else pt
= _yconv(year
, base
, 1, 1,
420 ** From Arnold Robbins' strftime version 3.0:
421 ** "date as dd-bbb-YYYY"
424 pt
= _fmt("%e-%b-%Y", t
, pt
, ptlim
, warnp
);
427 pt
= _conv((t
->tm_yday
+ DAYSPERWEEK
-
430 (DAYSPERWEEK
- 1))) / DAYSPERWEEK
,
434 pt
= _conv(t
->tm_wday
, "%d", pt
, ptlim
);
437 pt
= _fmt(Locale
->t_fmt
, t
, pt
, ptlim
, warnp
);
443 pt
= _fmt(Locale
->d_fmt
, t
, pt
, ptlim
, &warn2
);
452 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
, 0, 1,
456 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
, 1, 1,
461 if (t
->TM_ZONE
!= NULL
)
462 pt
= _add(t
->TM_ZONE
, pt
, ptlim
);
464 #endif /* defined TM_ZONE */
465 if (t
->tm_isdst
>= 0)
466 pt
= _add(tzname
[t
->tm_isdst
!= 0],
469 ** C99 says that %Z must be replaced by the
470 ** empty string if the time zone is not
482 diff
= (int)t
->TM_GMTOFF
;
483 #else /* !defined TM_GMTOFF */
485 ** C99 says that the UTC offset must
486 ** be computed by looking only at
487 ** tm_isdst. This requirement is
488 ** incorrect, since it means the code
489 ** must rely on magic (in this case
490 ** altzone and timezone), and the
491 ** magic might not have the correct
492 ** offset. Doing things correctly is
493 ** tricky and requires disobeying C99;
494 ** see GNU C strftime for details.
495 ** For now, punt and conform to the
496 ** standard, even though it's incorrect.
498 ** C99 says that %z must be replaced by the
499 ** empty string if the time zone is not
500 ** determinable, so output nothing if the
501 ** appropriate variables are not available.
504 if (t
->tm_isdst
== 0)
507 #else /* !defined USG_COMPAT */
509 #endif /* !defined USG_COMPAT */
513 #else /* !defined ALTZONE */
515 #endif /* !defined ALTZONE */
516 #else /* defined STD_INSPIRED */
522 ** Get calendar time from t
523 ** being treated as local.
525 tmp
= *t
; /* mktime discards const */
528 if (lct
== (time_t)-1)
532 ** Get calendar time from t
533 ** being treated as GMT.
535 tmp
= *t
; /* mktime discards const */
538 if (gct
== (time_t)-1)
541 /* LINTED difference will fit int */
542 diff
= (intmax_t)gct
- (intmax_t)lct
;
544 #endif /* defined STD_INSPIRED */
545 #endif /* !defined TM_GMTOFF */
550 pt
= _add(sign
, pt
, ptlim
);
552 diff
= (diff
/ MINSPERHOUR
) * 100 +
553 (diff
% MINSPERHOUR
);
554 pt
= _conv(diff
, "%04d", pt
, ptlim
);
559 pt
= _fmt(Locale
->date_fmt
, t
, pt
, ptlim
,
565 ** X311J/88-090 (4.12.3.5): if conversion char is
566 ** undefined, behavior is undefined. Print out the
567 ** character itself as printf(3) also does.
581 _conv(n
, format
, pt
, ptlim
)
583 const char * const format
;
585 const char * const ptlim
;
587 char buf
[INT_STRLEN_MAXIMUM(int) + 1];
589 (void) snprintf(buf
, sizeof(buf
), format
, n
);
590 return _add(buf
, pt
, ptlim
);
597 const char * const ptlim
;
599 while (pt
< ptlim
&& (*pt
= *str
++) != '\0')
605 ** POSIX and the C Standard are unclear or inconsistent about
606 ** what %C and %y do if the year is negative or exceeds 9999.
607 ** Use the convention that %C concatenated with %y yields the
608 ** same output as %Y, and that %Y contains at least 4 bytes,
609 ** with more only if necessary.
613 _yconv(a
, b
, convert_top
, convert_yy
, pt
, ptlim
)
616 const int convert_top
;
617 const int convert_yy
;
619 const char * const ptlim
;
625 trail
= a
% DIVISOR
+ b
% DIVISOR
;
626 lead
= a
/ DIVISOR
+ b
/ DIVISOR
+ trail
/ DIVISOR
;
628 if (trail
< 0 && lead
> 0) {
631 } else if (lead
< 0 && trail
> 0) {
636 if (lead
== 0 && trail
< 0)
637 pt
= _add("-0", pt
, ptlim
);
638 else pt
= _conv(lead
, "%02d", pt
, ptlim
);
641 pt
= _conv(((trail
< 0) ? -trail
: trail
), "%02d", pt
, ptlim
);
646 static struct lc_time_T
*
649 static const char locale_home
[] = LOCALE_HOME
;
650 static const char lc_time
[] = "LC_TIME";
651 static char * locale_buf
;
654 int oldsun
; /* "...ain't got nothin' to do..." */
660 char filename
[FILENAME_MAX
];
666 ** Use localebuf.mon[0] to signal whether locale is already set up.
668 if (localebuf
.mon
[0])
670 name
= setlocale(LC_TIME
, (char *) NULL
);
671 if (name
== NULL
|| *name
== '\0')
674 ** If the locale name is the same as our cache, use the cache.
677 if (lbuf
!= NULL
&& strcmp(name
, lbuf
) == 0) {
679 for (ap
= (const char **) &localebuf
;
680 ap
< (const char **) (&localebuf
+ 1);
682 *ap
= p
+= strlen(p
) + 1;
686 ** Slurp the locale file into the cache.
688 namesize
= strlen(name
) + 1;
689 if (sizeof filename
<
690 ((sizeof locale_home
) + namesize
+ (sizeof lc_time
)))
693 (void) sprintf(filename
, "%s/%s/%s", locale_home
, name
, lc_time
);
694 fd
= open(filename
, O_RDONLY
);
697 ** Old Sun systems have a different naming and data convention.
700 (void) sprintf(filename
, "%s/%s/%s", locale_home
,
702 fd
= open(filename
, O_RDONLY
);
706 if (fstat(fd
, &st
) != 0)
710 bufsize
= namesize
+ st
.st_size
;
712 lbuf
= (lbuf
== NULL
) ? malloc(bufsize
) : realloc(lbuf
, bufsize
);
715 (void) strcpy(lbuf
, name
);
717 plim
= p
+ st
.st_size
;
718 if (read(fd
, p
, (size_t) st
.st_size
) != st
.st_size
)
723 ** Parse the locale file into localebuf.
725 if (plim
[-1] != '\n')
727 for (ap
= (const char **) &localebuf
;
728 ap
< (const char **) (&localebuf
+ 1);
739 ** SunOS 4 used an obsolescent format; see localdtconv(3).
740 ** c_fmt had the ``short format for dates and times together''
741 ** (SunOS 4 date, "%a %b %e %T %Z %Y" in the C locale);
742 ** date_fmt had the ``long format for dates''
743 ** (SunOS 4 strftime %C, "%A, %B %e, %Y" in the C locale).
744 ** Discard the latter in favor of the former.
746 localebuf
.date_fmt
= localebuf
.c_fmt
;
749 ** Record the successful parse in the cache.
760 localebuf
= C_time_locale
;
764 #endif /* defined LOCALE_HOME */