3 static char elsieid
[] = "@(#)strftime.c 8.1";
5 ** Based on the UCB version with the ID appearing below.
6 ** This is ANSIish only when "multibyte character == plain character".
8 #endif /* !defined NOID */
9 #endif /* !defined lint */
14 ** Copyright (c) 1989 The Regents of the University of California.
15 ** All rights reserved.
17 ** Redistribution and use in source and binary forms are permitted
18 ** provided that the above copyright notice and this paragraph are
19 ** duplicated in all such forms and that any documentation,
20 ** advertising materials, and other materials related to such
21 ** distribution and use acknowledge that the software was developed
22 ** by the University of California, Berkeley. The name of the
23 ** University may not be used to endorse or promote products derived
24 ** from this software without specific prior written permission.
25 ** THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
26 ** IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
27 ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32 static const char sccsid
[] = "@(#)strftime.c 5.4 (Berkeley) 3/14/89";
33 #endif /* !defined lint */
34 #endif /* !defined LIBC_SCCS */
39 #include "timelocal.h"
41 #define Locale __get_current_time_locale()
43 static char * _add
P((const char *, char *, const char *));
44 static char * _conv
P((int, const char *, char *, const char *));
45 static char * _fmt
P((const char *, const struct tm
*, char *, const char *,
47 static char * _yconv
P((int, int, int, int, char *, const char *));
49 extern char * tzname
[];
51 #ifndef YEAR_2000_NAME
52 #define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
53 #endif /* !defined YEAR_2000_NAME */
60 #define NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
63 strftime(s
, maxsize
, format
, t
)
66 const char * const format
;
67 const struct tm
* const t
;
74 p
= _fmt(((format
== NULL
) ? "%c" : format
), t
, s
, s
+ maxsize
, &warn
);
75 #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
76 if (warn
!= IN_NONE
&& getenv(YEAR_2000_NAME
) != NULL
) {
77 (void) fprintf(stderr
, "\n");
79 (void) fprintf(stderr
, "NULL strftime format ");
80 else (void) fprintf(stderr
, "strftime format \"%s\" ",
82 (void) fprintf(stderr
, "yields only two digits of years in ");
84 (void) fprintf(stderr
, "some locales");
85 else if (warn
== IN_THIS
)
86 (void) fprintf(stderr
, "the current locale");
87 else (void) fprintf(stderr
, "all locales");
88 (void) fprintf(stderr
, "\n");
90 #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
98 _fmt(format
, t
, pt
, ptlim
, warnp
)
100 const struct tm
* const t
;
102 const char * const ptlim
;
105 for ( ; *format
; ++format
) {
106 if (*format
== '%') {
113 // ignore '0'-modifier supported by glibc for compatibility
116 pt
= _add((t
->tm_wday
< 0 ||
117 t
->tm_wday
>= DAYSPERWEEK
) ?
118 "?" : Locale
->weekday
[t
->tm_wday
],
122 pt
= _add((t
->tm_wday
< 0 ||
123 t
->tm_wday
>= DAYSPERWEEK
) ?
124 "?" : Locale
->wday
[t
->tm_wday
],
128 pt
= _add((t
->tm_mon
< 0 ||
129 t
->tm_mon
>= MONSPERYEAR
) ?
130 "?" : Locale
->month
[t
->tm_mon
],
135 pt
= _add((t
->tm_mon
< 0 ||
136 t
->tm_mon
>= MONSPERYEAR
) ?
137 "?" : Locale
->mon
[t
->tm_mon
],
142 ** %C used to do a...
143 ** _fmt("%a %b %e %X %Y", t);
144 ** ...whereas now POSIX 1003.2 calls for
145 ** something completely different.
148 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
, 1, 0,
155 pt
= _fmt(Locale
->c_fmt
, t
, pt
, ptlim
, warnp
);
161 pt
= _fmt("%m/%d/%y", t
, pt
, ptlim
, warnp
);
164 pt
= _conv(t
->tm_mday
, "%02d", pt
, ptlim
);
169 ** C99 locale modifiers.
171 ** %Ec %EC %Ex %EX %Ey %EY
172 ** %Od %oe %OH %OI %Om %OM
173 ** %OS %Ou %OU %OV %Ow %OW %Oy
174 ** are supposed to provide alternate
179 pt
= _conv(t
->tm_mday
, "%2d", pt
, ptlim
);
182 pt
= _fmt("%Y-%m-%d", t
, pt
, ptlim
, warnp
);
185 pt
= _conv(t
->tm_hour
, "%02d", pt
, ptlim
);
188 pt
= _conv((t
->tm_hour
% 12) ?
189 (t
->tm_hour
% 12) : 12,
193 pt
= _conv(t
->tm_yday
+ 1, "%03d", pt
, ptlim
);
197 ** This used to be...
198 ** _conv(t->tm_hour % 12 ?
199 ** t->tm_hour % 12 : 12, 2, ' ');
200 ** ...and has been changed to the below to
201 ** match SunOS 4.1.1 and Arnold Robbins'
202 ** strftime version 3.0. That is, "%k" and
203 ** "%l" have been swapped.
206 pt
= _conv(t
->tm_hour
, "%2d", pt
, ptlim
);
211 ** After all this time, still unclaimed!
213 pt
= _add("kitchen sink", pt
, ptlim
);
215 #endif /* defined KITCHEN_SINK */
218 ** This used to be...
219 ** _conv(t->tm_hour, 2, ' ');
220 ** ...and has been changed to the below to
221 ** match SunOS 4.1.1 and Arnold Robbin's
222 ** strftime version 3.0. That is, "%k" and
223 ** "%l" have been swapped.
226 pt
= _conv((t
->tm_hour
% 12) ?
227 (t
->tm_hour
% 12) : 12,
231 pt
= _conv(t
->tm_min
, "%02d", pt
, ptlim
);
234 pt
= _conv(t
->tm_mon
+ 1, "%02d", pt
, ptlim
);
237 pt
= _add("\n", pt
, ptlim
);
240 pt
= _add((t
->tm_hour
>= (HOURSPERDAY
/ 2)) ?
246 pt
= _fmt("%H:%M", t
, pt
, ptlim
, warnp
);
249 pt
= _fmt("%I:%M:%S %p", t
, pt
, ptlim
, warnp
);
252 pt
= _conv(t
->tm_sec
, "%02d", pt
, ptlim
);
257 char buf
[INT_STRLEN_MAXIMUM(
263 if (TYPE_SIGNED(time_t))
264 (void) sprintf(buf
, "%ld",
266 else (void) sprintf(buf
, "%lu",
267 (unsigned long) mkt
);
268 pt
= _add(buf
, pt
, ptlim
);
272 pt
= _fmt("%H:%M:%S", t
, pt
, ptlim
, warnp
);
275 pt
= _add("\t", pt
, ptlim
);
278 pt
= _conv((t
->tm_yday
+ DAYSPERWEEK
-
279 t
->tm_wday
) / DAYSPERWEEK
,
284 ** From Arnold Robbins' strftime version 3.0:
285 ** "ISO 8601: Weekday as a decimal number
289 pt
= _conv((t
->tm_wday
== 0) ?
290 DAYSPERWEEK
: t
->tm_wday
,
293 case 'V': /* ISO 8601 week number */
294 case 'G': /* ISO 8601 year (four digits) */
295 case 'g': /* ISO 8601 year (two digits) */
297 ** From Arnold Robbins' strftime version 3.0: "the week number of the
298 ** year (the first Monday as the first day of week 1) as a decimal number
302 ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
303 ** "Week 01 of a year is per definition the first week which has the
304 ** Thursday in this year, which is equivalent to the week which contains
305 ** the fourth day of January. In other words, the first week of a new year
306 ** is the week which has the majority of its days in the new year. Week 01
307 ** might also contain days from the previous year and the week before week
308 ** 01 of a year is the last week (52 or 53) of the previous year even if
309 ** it contains days from the new year. A week starts with Monday (day 1)
310 ** and ends with Sunday (day 7). For example, the first week of the year
311 ** 1997 lasts from 1996-12-30 to 1997-01-05..."
330 len
= isleap_sum(year
, base
) ?
334 ** What yday (-3 ... 3) does
335 ** the ISO year begin on?
337 bot
= ((yday
+ 11 - wday
) %
340 ** What yday does the NEXT
341 ** ISO year begin on?
354 w
= 1 + ((yday
- bot
) /
359 yday
+= isleap_sum(year
, base
) ?
363 #ifdef XPG4_1994_04_09
365 t
->tm_mon
== TM_JANUARY
) ||
367 t
->tm_mon
== TM_DECEMBER
))
369 #endif /* defined XPG4_1994_04_09 */
371 pt
= _conv(w
, "%02d",
373 else if (*format
== 'g') {
375 pt
= _yconv(year
, base
, 0, 1,
377 } else pt
= _yconv(year
, base
, 1, 1,
383 ** From Arnold Robbins' strftime version 3.0:
384 ** "date as dd-bbb-YYYY"
387 pt
= _fmt("%e-%b-%Y", t
, pt
, ptlim
, warnp
);
390 pt
= _conv((t
->tm_yday
+ DAYSPERWEEK
-
393 (DAYSPERWEEK
- 1))) / DAYSPERWEEK
,
397 pt
= _conv(t
->tm_wday
, "%d", pt
, ptlim
);
400 pt
= _fmt(Locale
->X_fmt
, t
, pt
, ptlim
, warnp
);
406 pt
= _fmt(Locale
->x_fmt
, t
, pt
, ptlim
, &warn2
);
415 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
, 0, 1,
419 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
, 1, 1,
424 if (t
->TM_ZONE
!= NULL
)
425 pt
= _add(t
->TM_ZONE
, pt
, ptlim
);
427 #endif /* defined TM_ZONE */
428 if (t
->tm_isdst
>= 0)
429 pt
= _add(tzname
[t
->tm_isdst
!= 0],
432 ** C99 says that %Z must be replaced by the
433 ** empty string if the time zone is not
446 #else /* !defined TM_GMTOFF */
448 ** C99 says that the UTC offset must
449 ** be computed by looking only at
450 ** tm_isdst. This requirement is
451 ** incorrect, since it means the code
452 ** must rely on magic (in this case
453 ** altzone and timezone), and the
454 ** magic might not have the correct
455 ** offset. Doing things correctly is
456 ** tricky and requires disobeying C99;
457 ** see GNU C strftime for details.
458 ** For now, punt and conform to the
459 ** standard, even though it's incorrect.
461 ** C99 says that %z must be replaced by the
462 ** empty string if the time zone is not
463 ** determinable, so output nothing if the
464 ** appropriate variables are not available.
466 if (t
->tm_isdst
== 0)
469 #else /* !defined USG_COMPAT */
471 #endif /* !defined USG_COMPAT */
475 #else /* !defined ALTZONE */
477 #endif /* !defined ALTZONE */
478 #endif /* !defined TM_GMTOFF */
483 pt
= _add(sign
, pt
, ptlim
);
485 diff
= (diff
/ MINSPERHOUR
) * 100 +
486 (diff
% MINSPERHOUR
);
487 pt
= _conv(diff
, "%04d", pt
, ptlim
);
491 pt
= _fmt(Locale
->date_fmt
, t
, pt
, ptlim
,
496 ** X311J/88-090 (4.12.3.5): if conversion char is
497 ** undefined, behavior is undefined. Print out the
498 ** character itself as printf(3) also does.
512 _conv(n
, format
, pt
, ptlim
)
514 const char * const format
;
516 const char * const ptlim
;
518 char buf
[INT_STRLEN_MAXIMUM(int) + 1];
520 (void) sprintf(buf
, format
, n
);
521 return _add(buf
, pt
, ptlim
);
528 const char * const ptlim
;
530 while (pt
< ptlim
&& (*pt
= *str
++) != '\0')
536 ** POSIX and the C Standard are unclear or inconsistent about
537 ** what %C and %y do if the year is negative or exceeds 9999.
538 ** Use the convention that %C concatenated with %y yields the
539 ** same output as %Y, and that %Y contains at least 4 bytes,
540 ** with more only if necessary.
544 _yconv(a
, b
, convert_top
, convert_yy
, pt
, ptlim
)
547 const int convert_top
;
548 const int convert_yy
;
550 const char * const ptlim
;
556 trail
= a
% DIVISOR
+ b
% DIVISOR
;
557 lead
= a
/ DIVISOR
+ b
/ DIVISOR
+ trail
/ DIVISOR
;
559 if (trail
< 0 && lead
> 0) {
562 } else if (lead
< 0 && trail
> 0) {
567 if (lead
== 0 && trail
< 0)
568 pt
= _add("-0", pt
, ptlim
);
569 else pt
= _conv(lead
, "%02d", pt
, ptlim
);
572 pt
= _conv(((trail
< 0) ? -trail
: trail
), "%02d", pt
, ptlim
);