1 /* $NetBSD: src/lib/libc/time/strftime.c,v 1.16 2004/05/12 23:03:11 kleink Exp $ */
2 /* $DragonFly: src/lib/libc/stdtime/strftime.c,v 1.6 2005/12/04 23:25:40 swildner Exp $ */
5 ** Based on the UCB version with the ID appearing below.
6 ** This is ANSIish only when "multibyte character == plain character".
10 ** We don't use these extensions in strftime operation even when
11 ** supported by the local tzcode configuration. A strictly
12 ** conforming C application may leave them in undefined state.
21 ** Copyright (c) 1989, 1993
22 ** The Regents of the University of California. All rights reserved.
24 ** Redistribution and use in source and binary forms, with or without
25 ** modification, are permitted provided that the following conditions
27 ** 1. Redistributions of source code must retain the above copyright
28 ** notice, this list of conditions and the following disclaimer.
29 ** 2. Redistributions in binary form must reproduce the above copyright
30 ** notice, this list of conditions and the following disclaimer in the
31 ** documentation and/or other materials provided with the distribution.
32 ** 3. All advertising materials mentioning features or use of this software
33 ** must display the following acknowledgement:
34 ** This product includes software developed by the University of
35 ** California, Berkeley and its contributors.
36 ** 4. Neither the name of the University nor the names of its contributors
37 ** may be used to endorse or promote products derived from this software
38 ** without specific prior written permission.
40 ** THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
41 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 ** ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
44 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 #include <sys/localedef.h>
61 #define Locale _CurrentTimeLocale
63 static char * _add(const char *, char *, const char *);
64 static char * _conv(int, const char *, char *, const char *);
65 static char * _fmt(const char *, const struct tm
*, char *,
68 #define NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
70 #ifndef YEAR_2000_NAME
71 #define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
72 #endif /* !defined YEAR_2000_NAME */
81 strftime(char * const s
, const size_t maxsize
, const char * const format
,
82 const struct tm
* const t
)
89 p
= _fmt(((format
== NULL
) ? "%c" : format
), t
, s
, s
+ maxsize
, &warn
);
90 #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
91 if (warn
!= IN_NONE
&& getenv(YEAR_2000_NAME
) != NULL
) {
92 fprintf(stderr
, "\n");
94 fprintf(stderr
, "NULL strftime format ");
95 else fprintf(stderr
, "strftime format \"%s\" ",
97 fprintf(stderr
, "yields only two digits of years in ");
99 fprintf(stderr
, "some locales");
100 else if (warn
== IN_THIS
)
101 fprintf(stderr
, "the current locale");
102 else fprintf(stderr
, "all locales");
103 fprintf(stderr
, "\n");
105 #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
106 if (p
== s
+ maxsize
)
113 _fmt(const char *format
, const struct tm
* const t
, char *pt
,
114 const char * const ptlim
, int * warnp
)
116 for ( ; *format
; ++format
) {
117 if (*format
== '%') {
124 pt
= _add((t
->tm_wday
< 0 ||
125 t
->tm_wday
>= DAYSPERWEEK
) ?
126 "?" : Locale
->day
[t
->tm_wday
],
130 pt
= _add((t
->tm_wday
< 0 ||
131 t
->tm_wday
>= DAYSPERWEEK
) ?
132 "?" : Locale
->abday
[t
->tm_wday
],
136 pt
= _add((t
->tm_mon
< 0 ||
137 t
->tm_mon
>= MONSPERYEAR
) ?
138 "?" : Locale
->mon
[t
->tm_mon
],
143 pt
= _add((t
->tm_mon
< 0 ||
144 t
->tm_mon
>= MONSPERYEAR
) ?
145 "?" : Locale
->abmon
[t
->tm_mon
],
150 ** %C used to do a...
151 ** _fmt("%a %b %e %X %Y", t);
152 ** ...whereas now POSIX 1003.2 calls for
153 ** something completely different.
156 pt
= _conv((t
->tm_year
+ TM_YEAR_BASE
) / 100,
163 pt
= _fmt(Locale
->d_t_fmt
, t
, pt
, ptlim
, warnp
);
171 pt
= _fmt("%m/%d/%y", t
, pt
, ptlim
, warnp
);
174 pt
= _conv(t
->tm_mday
, "%02d", pt
, ptlim
);
179 ** C99 locale modifiers.
181 ** %Ec %EC %Ex %EX %Ey %EY
182 ** %Od %oe %OH %OI %Om %OM
183 ** %OS %Ou %OU %OV %Ow %OW %Oy
184 ** are supposed to provide alternate
189 pt
= _conv(t
->tm_mday
, "%2d", pt
, ptlim
);
192 pt
= _fmt("%Y-%m-%d", t
, pt
, ptlim
, warnp
);
195 pt
= _conv(t
->tm_hour
, "%02d", pt
, ptlim
);
198 pt
= _conv((t
->tm_hour
% 12) ?
199 (t
->tm_hour
% 12) : 12,
203 pt
= _conv(t
->tm_yday
+ 1, "%03d", pt
, ptlim
);
207 ** This used to be...
208 ** _conv(t->tm_hour % 12 ?
209 ** t->tm_hour % 12 : 12, 2, ' ');
210 ** ...and has been changed to the below to
211 ** match SunOS 4.1.1 and Arnold Robbins'
212 ** strftime version 3.0. That is, "%k" and
213 ** "%l" have been swapped.
216 pt
= _conv(t
->tm_hour
, "%2d", pt
, ptlim
);
221 ** After all this time, still unclaimed!
223 pt
= _add("kitchen sink", pt
, ptlim
);
225 #endif /* defined KITCHEN_SINK */
228 ** This used to be...
229 ** _conv(t->tm_hour, 2, ' ');
230 ** ...and has been changed to the below to
231 ** match SunOS 4.1.1 and Arnold Robbin's
232 ** strftime version 3.0. That is, "%k" and
233 ** "%l" have been swapped.
236 pt
= _conv((t
->tm_hour
% 12) ?
237 (t
->tm_hour
% 12) : 12,
241 pt
= _conv(t
->tm_min
, "%02d", pt
, ptlim
);
244 pt
= _conv(t
->tm_mon
+ 1, "%02d", pt
, ptlim
);
247 pt
= _add("\n", pt
, ptlim
);
250 pt
= _add((t
->tm_hour
>= (HOURSPERDAY
/ 2)) ?
256 pt
= _fmt("%H:%M", t
, pt
, ptlim
, warnp
);
259 pt
= _fmt("%I:%M:%S %p", t
, pt
, ptlim
, warnp
);
262 pt
= _conv(t
->tm_sec
, "%02d", pt
, ptlim
);
267 char buf
[INT_STRLEN_MAXIMUM(
274 if (TYPE_SIGNED(time_t))
277 else sprintf(buf
, "%lu",
278 (unsigned long) mkt
);
279 pt
= _add(buf
, pt
, ptlim
);
283 pt
= _fmt("%H:%M:%S", t
, pt
, ptlim
, warnp
);
286 pt
= _add("\t", pt
, ptlim
);
289 pt
= _conv((t
->tm_yday
+ DAYSPERWEEK
-
290 t
->tm_wday
) / DAYSPERWEEK
,
295 ** From Arnold Robbins' strftime version 3.0:
296 ** "ISO 8601: Weekday as a decimal number
300 pt
= _conv((t
->tm_wday
== 0) ?
301 DAYSPERWEEK
: t
->tm_wday
,
304 case 'V': /* ISO 8601 week number */
305 case 'G': /* ISO 8601 year (four digits) */
306 case 'g': /* ISO 8601 year (two digits) */
308 ** From Arnold Robbins' strftime version 3.0: "the week number of the
309 ** year (the first Monday as the first day of week 1) as a decimal number
313 ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
314 ** "Week 01 of a year is per definition the first week which has the
315 ** Thursday in this year, which is equivalent to the week which contains
316 ** the fourth day of January. In other words, the first week of a new year
317 ** is the week which has the majority of its days in the new year. Week 01
318 ** might also contain days from the previous year and the week before week
319 ** 01 of a year is the last week (52 or 53) of the previous year even if
320 ** it contains days from the new year. A week starts with Monday (day 1)
321 ** and ends with Sunday (day 7). For example, the first week of the year
322 ** 1997 lasts from 1996-12-30 to 1997-01-05..."
331 year
= t
->tm_year
+ TM_YEAR_BASE
;
343 ** What yday (-3 ... 3) does
344 ** the ISO year begin on?
346 bot
= ((yday
+ 11 - wday
) %
349 ** What yday does the NEXT
350 ** ISO year begin on?
363 w
= 1 + ((yday
- bot
) /
368 yday
+= isleap(year
) ?
372 #ifdef XPG4_1994_04_09
374 && t
->tm_mon
== TM_JANUARY
)
376 && t
->tm_mon
== TM_DECEMBER
))
378 #endif /* defined XPG4_1994_04_09 */
380 pt
= _conv(w
, "%02d",
382 else if (*format
== 'g') {
384 pt
= _conv(year
% 100, "%02d",
386 } else pt
= _conv(year
, "%04d",
392 ** From Arnold Robbins' strftime version 3.0:
393 ** "date as dd-bbb-YYYY"
396 pt
= _fmt("%e-%b-%Y", t
, pt
, ptlim
, warnp
);
399 pt
= _conv((t
->tm_yday
+ DAYSPERWEEK
-
402 (DAYSPERWEEK
- 1))) / DAYSPERWEEK
,
406 pt
= _conv(t
->tm_wday
, "%d", pt
, ptlim
);
409 pt
= _fmt(Locale
->t_fmt
, t
, pt
, ptlim
, warnp
);
415 pt
= _fmt(Locale
->d_fmt
, t
, pt
, ptlim
, &warn2
);
424 pt
= _conv((t
->tm_year
+ TM_YEAR_BASE
) % 100,
428 pt
= _conv(t
->tm_year
+ TM_YEAR_BASE
, "%04d",
433 if (t
->TM_ZONE
!= NULL
)
434 pt
= _add(t
->TM_ZONE
, pt
, ptlim
);
436 #endif /* defined TM_ZONE */
437 if (t
->tm_isdst
>= 0)
438 pt
= _add(tzname
[t
->tm_isdst
!= 0],
441 ** C99 says that %Z must be replaced by the
442 ** empty string if the time zone is not
454 diff
= (int)t
->TM_GMTOFF
;
455 #else /* !defined TM_GMTOFF */
457 ** C99 says that the UTC offset must
458 ** be computed by looking only at
459 ** tm_isdst. This requirement is
460 ** incorrect, since it means the code
461 ** must rely on magic (in this case
462 ** altzone and timezone), and the
463 ** magic might not have the correct
464 ** offset. Doing things correctly is
465 ** tricky and requires disobeying C99;
466 ** see GNU C strftime for details.
467 ** For now, punt and conform to the
468 ** standard, even though it's incorrect.
470 ** C99 says that %z must be replaced by the
471 ** empty string if the time zone is not
472 ** determinable, so output nothing if the
473 ** appropriate variables are not available.
476 if (t
->tm_isdst
== 0)
479 #else /* !defined USG_COMPAT */
481 #endif /* !defined USG_COMPAT */
485 #else /* !defined ALTZONE */
487 #endif /* !defined ALTZONE */
488 #else /* defined STD_INSPIRED */
494 ** Get calendar time from t
495 ** being treated as local.
497 tmp
= *t
; /* mktime discards const */
500 if (lct
== (time_t)-1)
504 ** Get calendar time from t
505 ** being treated as GMT.
507 tmp
= *t
; /* mktime discards const */
510 if (gct
== (time_t)-1)
513 /* LINTED difference will fit int */
514 diff
= (intmax_t)gct
- (intmax_t)lct
;
516 #endif /* defined STD_INSPIRED */
517 #endif /* !defined TM_GMTOFF */
522 pt
= _add(sign
, pt
, ptlim
);
524 pt
= _conv((diff
/60)*100 + diff
%60,
529 pt
= _fmt(Locale
->d_t_fmt
, t
, pt
, ptlim
,
534 ** X311J/88-090 (4.12.3.5): if conversion char is
535 ** undefined, behavior is undefined. Print out the
536 ** character itself as printf(3) also does.
550 _conv(const int n
, const char * const format
, char * const pt
,
551 const char * const ptlim
)
553 char buf
[INT_STRLEN_MAXIMUM(int) + 1];
555 sprintf(buf
, format
, n
);
556 return _add(buf
, pt
, ptlim
);
560 _add(const char *str
, char *pt
, const char * const ptlim
)
562 while (pt
< ptlim
&& (*pt
= *str
++) != '\0')