3 static char elsieid
[] = "@(#)strftime.c 7.75";
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 */
41 const char * mon
[MONSPERYEAR
];
42 const char * month
[MONSPERYEAR
];
43 const char * wday
[DAYSPERWEEK
];
44 const char * weekday
[DAYSPERWEEK
];
50 const char * date_fmt
;
55 static struct lc_time_T localebuf
;
56 static struct lc_time_T
* _loc
P((void));
58 #endif /* defined LOCALE_HOME */
60 #define Locale (&C_time_locale)
61 #endif /* !defined LOCALE_HOME */
63 static const struct lc_time_T C_time_locale
= {
65 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
66 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
68 "January", "February", "March", "April", "May", "June",
69 "July", "August", "September", "October", "November", "December"
71 "Sun", "Mon", "Tue", "Wed",
74 "Sunday", "Monday", "Tuesday", "Wednesday",
75 "Thursday", "Friday", "Saturday"
83 ** C99 requires this format.
84 ** Using just numbers (as here) makes Quakers happier;
85 ** it's also compatible with SVR4.
91 ** C99 requires this format.
92 ** Previously this code used "%D %X", but we now conform to C99.
94 ** "%a %b %d %H:%M:%S %Y"
95 ** is used by Solaris 2.3.
106 "%a %b %e %H:%M:%S %Z %Y"
109 static char * _add
P((const char *, char *, const char *));
110 static char * _conv
P((int, const char *, char *, const char *));
111 static char * _fmt
P((const char *, const struct tm
*, char *, const char *,
113 static char * _yconv
P((int, int, int, int, char *, const char *));
115 extern char * tzname
[];
117 #ifndef YEAR_2000_NAME
118 #define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
119 #endif /* !defined YEAR_2000_NAME */
127 strftime(s
, maxsize
, format
, t
)
129 const size_t maxsize
;
130 const char * const format
;
131 const struct tm
* const t
;
138 localebuf
.mon
[0] = 0;
139 #endif /* defined LOCALE_HOME */
141 p
= _fmt(((format
== NULL
) ? "%c" : format
), t
, s
, s
+ maxsize
, &warn
);
142 #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
143 if (warn
!= IN_NONE
&& getenv(YEAR_2000_NAME
) != NULL
) {
144 (void) fprintf(stderr
, "\n");
146 (void) fprintf(stderr
, "NULL strftime format ");
147 else (void) fprintf(stderr
, "strftime format \"%s\" ",
149 (void) fprintf(stderr
, "yields only two digits of years in ");
151 (void) fprintf(stderr
, "some locales");
152 else if (warn
== IN_THIS
)
153 (void) fprintf(stderr
, "the current locale");
154 else (void) fprintf(stderr
, "all locales");
155 (void) fprintf(stderr
, "\n");
157 #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
158 if (p
== s
+ maxsize
)
165 _fmt(format
, t
, pt
, ptlim
, warnp
)
167 const struct tm
* const t
;
169 const char * const ptlim
;
172 for ( ; *format
; ++format
) {
173 if (*format
== '%') {
180 pt
= _add((t
->tm_wday
< 0 ||
181 t
->tm_wday
>= DAYSPERWEEK
) ?
182 "?" : Locale
->weekday
[t
->tm_wday
],
186 pt
= _add((t
->tm_wday
< 0 ||
187 t
->tm_wday
>= DAYSPERWEEK
) ?
188 "?" : Locale
->wday
[t
->tm_wday
],
192 pt
= _add((t
->tm_mon
< 0 ||
193 t
->tm_mon
>= MONSPERYEAR
) ?
194 "?" : Locale
->month
[t
->tm_mon
],
199 pt
= _add((t
->tm_mon
< 0 ||
200 t
->tm_mon
>= MONSPERYEAR
) ?
201 "?" : Locale
->mon
[t
->tm_mon
],
206 ** %C used to do a...
207 ** _fmt("%a %b %e %X %Y", t);
208 ** ...whereas now POSIX 1003.2 calls for
209 ** something completely different.
212 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
, 1, 0,
219 pt
= _fmt(Locale
->c_fmt
, t
, pt
, ptlim
, warnp
);
227 pt
= _fmt("%m/%d/%y", t
, pt
, ptlim
, warnp
);
230 pt
= _conv(t
->tm_mday
, "%02d", pt
, ptlim
);
235 ** C99 locale modifiers.
237 ** %Ec %EC %Ex %EX %Ey %EY
238 ** %Od %oe %OH %OI %Om %OM
239 ** %OS %Ou %OU %OV %Ow %OW %Oy
240 ** are supposed to provide alternate
245 pt
= _conv(t
->tm_mday
, "%2d", pt
, ptlim
);
248 pt
= _fmt("%Y-%m-%d", t
, pt
, ptlim
, warnp
);
251 pt
= _conv(t
->tm_hour
, "%02d", pt
, ptlim
);
254 pt
= _conv((t
->tm_hour
% 12) ?
255 (t
->tm_hour
% 12) : 12,
259 pt
= _conv(t
->tm_yday
+ 1, "%03d", pt
, ptlim
);
263 ** This used to be...
264 ** _conv(t->tm_hour % 12 ?
265 ** t->tm_hour % 12 : 12, 2, ' ');
266 ** ...and has been changed to the below to
267 ** match SunOS 4.1.1 and Arnold Robbins'
268 ** strftime version 3.0. That is, "%k" and
269 ** "%l" have been swapped.
272 pt
= _conv(t
->tm_hour
, "%2d", pt
, ptlim
);
277 ** After all this time, still unclaimed!
279 pt
= _add("kitchen sink", pt
, ptlim
);
281 #endif /* defined KITCHEN_SINK */
284 ** This used to be...
285 ** _conv(t->tm_hour, 2, ' ');
286 ** ...and has been changed to the below to
287 ** match SunOS 4.1.1 and Arnold Robbin's
288 ** strftime version 3.0. That is, "%k" and
289 ** "%l" have been swapped.
292 pt
= _conv((t
->tm_hour
% 12) ?
293 (t
->tm_hour
% 12) : 12,
297 pt
= _conv(t
->tm_min
, "%02d", pt
, ptlim
);
300 pt
= _conv(t
->tm_mon
+ 1, "%02d", pt
, ptlim
);
303 pt
= _add("\n", pt
, ptlim
);
306 pt
= _add((t
->tm_hour
>= (HOURSPERDAY
/ 2)) ?
312 pt
= _fmt("%H:%M", t
, pt
, ptlim
, warnp
);
315 pt
= _fmt("%I:%M:%S %p", t
, pt
, ptlim
, warnp
);
318 pt
= _conv(t
->tm_sec
, "%02d", pt
, ptlim
);
323 char buf
[INT_STRLEN_MAXIMUM(
329 if (TYPE_SIGNED(time_t))
330 (void) sprintf(buf
, "%ld",
332 else (void) sprintf(buf
, "%lu",
333 (unsigned long) mkt
);
334 pt
= _add(buf
, pt
, ptlim
);
338 pt
= _fmt("%H:%M:%S", t
, pt
, ptlim
, warnp
);
341 pt
= _add("\t", pt
, ptlim
);
344 pt
= _conv((t
->tm_yday
+ DAYSPERWEEK
-
345 t
->tm_wday
) / DAYSPERWEEK
,
350 ** From Arnold Robbins' strftime version 3.0:
351 ** "ISO 8601: Weekday as a decimal number
355 pt
= _conv((t
->tm_wday
== 0) ?
356 DAYSPERWEEK
: t
->tm_wday
,
359 case 'V': /* ISO 8601 week number */
360 case 'G': /* ISO 8601 year (four digits) */
361 case 'g': /* ISO 8601 year (two digits) */
363 ** From Arnold Robbins' strftime version 3.0: "the week number of the
364 ** year (the first Monday as the first day of week 1) as a decimal number
368 ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
369 ** "Week 01 of a year is per definition the first week which has the
370 ** Thursday in this year, which is equivalent to the week which contains
371 ** the fourth day of January. In other words, the first week of a new year
372 ** is the week which has the majority of its days in the new year. Week 01
373 ** might also contain days from the previous year and the week before week
374 ** 01 of a year is the last week (52 or 53) of the previous year even if
375 ** it contains days from the new year. A week starts with Monday (day 1)
376 ** and ends with Sunday (day 7). For example, the first week of the year
377 ** 1997 lasts from 1996-12-30 to 1997-01-05..."
396 len
= isleap_sum(year
, base
) ?
400 ** What yday (-3 ... 3) does
401 ** the ISO year begin on?
403 bot
= ((yday
+ 11 - wday
) %
406 ** What yday does the NEXT
407 ** ISO year begin on?
420 w
= 1 + ((yday
- bot
) /
425 yday
+= isleap_sum(year
, base
) ?
429 #ifdef XPG4_1994_04_09
431 t
->tm_mon
== TM_JANUARY
) ||
433 t
->tm_mon
== TM_DECEMBER
))
435 #endif /* defined XPG4_1994_04_09 */
437 pt
= _conv(w
, "%02d",
439 else if (*format
== 'g') {
441 pt
= _yconv(year
, base
, 0, 1,
443 } else pt
= _yconv(year
, base
, 1, 1,
449 ** From Arnold Robbins' strftime version 3.0:
450 ** "date as dd-bbb-YYYY"
453 pt
= _fmt("%e-%b-%Y", t
, pt
, ptlim
, warnp
);
456 pt
= _conv((t
->tm_yday
+ DAYSPERWEEK
-
459 (DAYSPERWEEK
- 1))) / DAYSPERWEEK
,
463 pt
= _conv(t
->tm_wday
, "%d", pt
, ptlim
);
466 pt
= _fmt(Locale
->X_fmt
, t
, pt
, ptlim
, warnp
);
472 pt
= _fmt(Locale
->x_fmt
, t
, pt
, ptlim
, &warn2
);
481 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
, 0, 1,
485 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
, 1, 1,
490 if (t
->TM_ZONE
!= NULL
)
491 pt
= _add(t
->TM_ZONE
, pt
, ptlim
);
493 #endif /* defined TM_ZONE */
494 if (t
->tm_isdst
>= 0)
495 pt
= _add(tzname
[t
->tm_isdst
!= 0],
498 ** C99 says that %Z must be replaced by the
499 ** empty string if the time zone is not
512 #else /* !defined TM_GMTOFF */
514 ** C99 says that the UTC offset must
515 ** be computed by looking only at
516 ** tm_isdst. This requirement is
517 ** incorrect, since it means the code
518 ** must rely on magic (in this case
519 ** altzone and timezone), and the
520 ** magic might not have the correct
521 ** offset. Doing things correctly is
522 ** tricky and requires disobeying C99;
523 ** see GNU C strftime for details.
524 ** For now, punt and conform to the
525 ** standard, even though it's incorrect.
527 ** C99 says that %z must be replaced by the
528 ** empty string if the time zone is not
529 ** determinable, so output nothing if the
530 ** appropriate variables are not available.
532 if (t
->tm_isdst
== 0)
535 #else /* !defined USG_COMPAT */
537 #endif /* !defined USG_COMPAT */
541 #else /* !defined ALTZONE */
543 #endif /* !defined ALTZONE */
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
);
557 pt
= _fmt(Locale
->date_fmt
, t
, pt
, ptlim
,
562 ** X311J/88-090 (4.12.3.5): if conversion char is
563 ** undefined, behavior is undefined. Print out the
564 ** character itself as printf(3) also does.
578 _conv(n
, format
, pt
, ptlim
)
580 const char * const format
;
582 const char * const ptlim
;
584 char buf
[INT_STRLEN_MAXIMUM(int) + 1];
586 (void) sprintf(buf
, format
, n
);
587 return _add(buf
, pt
, ptlim
);
594 const char * const ptlim
;
596 while (pt
< ptlim
&& (*pt
= *str
++) != '\0')
602 ** POSIX and the C Standard are unclear or inconsistent about
603 ** what %C and %y do if the year is negative or exceeds 9999.
604 ** Use the convention that %C concatenated with %y yields the
605 ** same output as %Y, and that %Y contains at least 4 bytes,
606 ** with more only if necessary.
610 _yconv(a
, b
, convert_top
, convert_yy
, pt
, ptlim
)
613 const int convert_top
;
614 const int convert_yy
;
616 const char * const ptlim
;
622 trail
= a
% DIVISOR
+ b
% DIVISOR
;
623 lead
= a
/ DIVISOR
+ b
/ DIVISOR
+ trail
/ DIVISOR
;
625 if (trail
< 0 && lead
> 0) {
628 } else if (lead
< 0 && trail
> 0) {
633 if (lead
== 0 && trail
< 0)
634 pt
= _add("-0", pt
, ptlim
);
635 else pt
= _conv(lead
, "%02d", pt
, ptlim
);
638 pt
= _conv(((trail
< 0) ? -trail
: trail
), "%02d", pt
, ptlim
);
643 static struct lc_time_T
*
646 static const char locale_home
[] = LOCALE_HOME
;
647 static const char lc_time
[] = "LC_TIME";
648 static char * locale_buf
;
651 int oldsun
; /* "...ain't got nothin' to do..." */
657 char filename
[FILENAME_MAX
];
663 ** Use localebuf.mon[0] to signal whether locale is already set up.
665 if (localebuf
.mon
[0])
667 name
= setlocale(LC_TIME
, (char *) NULL
);
668 if (name
== NULL
|| *name
== '\0')
671 ** If the locale name is the same as our cache, use the cache.
674 if (lbuf
!= NULL
&& strcmp(name
, lbuf
) == 0) {
676 for (ap
= (const char **) &localebuf
;
677 ap
< (const char **) (&localebuf
+ 1);
679 *ap
= p
+= strlen(p
) + 1;
683 ** Slurp the locale file into the cache.
685 namesize
= strlen(name
) + 1;
686 if (sizeof filename
<
687 ((sizeof locale_home
) + namesize
+ (sizeof lc_time
)))
690 (void) sprintf(filename
, "%s/%s/%s", locale_home
, name
, lc_time
);
691 fd
= open(filename
, O_RDONLY
);
694 ** Old Sun systems have a different naming and data convention.
697 (void) sprintf(filename
, "%s/%s/%s", locale_home
,
699 fd
= open(filename
, O_RDONLY
);
703 if (fstat(fd
, &st
) != 0)
707 bufsize
= namesize
+ st
.st_size
;
709 lbuf
= (lbuf
== NULL
) ? malloc(bufsize
) : realloc(lbuf
, bufsize
);
712 (void) strcpy(lbuf
, name
);
714 plim
= p
+ st
.st_size
;
715 if (read(fd
, p
, (size_t) st
.st_size
) != st
.st_size
)
720 ** Parse the locale file into localebuf.
722 if (plim
[-1] != '\n')
724 for (ap
= (const char **) &localebuf
;
725 ap
< (const char **) (&localebuf
+ 1);
736 ** SunOS 4 used an obsolescent format; see localdtconv(3).
737 ** c_fmt had the ``short format for dates and times together''
738 ** (SunOS 4 date, "%a %b %e %T %Z %Y" in the C locale);
739 ** date_fmt had the ``long format for dates''
740 ** (SunOS 4 strftime %C, "%A, %B %e, %Y" in the C locale).
741 ** Discard the latter in favor of the former.
743 localebuf
.date_fmt
= localebuf
.c_fmt
;
746 ** Record the successful parse in the cache.
757 localebuf
= C_time_locale
;
761 #endif /* defined LOCALE_HOME */