strftime.c revision 1.1.1.1 1 #ifndef lint
2 #ifndef NOID
3 static char elsieid[] = "@(#)strftime.c 7.62";
4 /*
5 ** Based on the UCB version with the ID appearing below.
6 ** This is ANSIish only when "multibyte character == plain character".
7 */
8 #endif /* !defined NOID */
9 #endif /* !defined lint */
10
11 #include "private.h"
12
13 /*
14 ** Copyright (c) 1989 The Regents of the University of California.
15 ** All rights reserved.
16 **
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.
28 */
29
30 #ifndef LIBC_SCCS
31 #ifndef lint
32 static const char sccsid[] = "@(#)strftime.c 5.4 (Berkeley) 3/14/89";
33 #endif /* !defined lint */
34 #endif /* !defined LIBC_SCCS */
35
36 #include "tzfile.h"
37 #include "fcntl.h"
38 #include "locale.h"
39
40 struct lc_time_T {
41 const char * mon[MONSPERYEAR];
42 const char * month[MONSPERYEAR];
43 const char * wday[DAYSPERWEEK];
44 const char * weekday[DAYSPERWEEK];
45 const char * X_fmt;
46 const char * x_fmt;
47 const char * c_fmt;
48 const char * am;
49 const char * pm;
50 const char * date_fmt;
51 };
52
53 #ifdef LOCALE_HOME
54 #include "sys/stat.h"
55 static struct lc_time_T localebuf;
56 static struct lc_time_T * _loc P((void));
57 #define Locale _loc()
58 #endif /* defined LOCALE_HOME */
59 #ifndef LOCALE_HOME
60 #define Locale (&C_time_locale)
61 #endif /* !defined LOCALE_HOME */
62
63 static const struct lc_time_T C_time_locale = {
64 {
65 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
66 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
67 }, {
68 "January", "February", "March", "April", "May", "June",
69 "July", "August", "September", "October", "November", "December"
70 }, {
71 "Sun", "Mon", "Tue", "Wed",
72 "Thu", "Fri", "Sat"
73 }, {
74 "Sunday", "Monday", "Tuesday", "Wednesday",
75 "Thursday", "Friday", "Saturday"
76 },
77
78 /* X_fmt */
79 "%H:%M:%S",
80
81 /*
82 ** x_fmt
83 ** C99 requires this format.
84 ** Using just numbers (as here) makes Quakers happier;
85 ** it's also compatible with SVR4.
86 */
87 "%m/%d/%y",
88
89 /*
90 ** c_fmt
91 ** C99 requires this format.
92 ** Previously this code used "%D %X", but we now conform to C99.
93 ** Note that
94 ** "%a %b %d %H:%M:%S %Y"
95 ** is used by Solaris 2.3.
96 */
97 "%a %b %e %T %Y",
98
99 /* am */
100 "AM",
101
102 /* pm */
103 "PM",
104
105 /* date_fmt */
106 "%a %b %e %H:%M:%S %Z %Y"
107 };
108
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 *, int *));
112
113 size_t strftime P((char *, size_t, const char *, const struct tm *));
114
115 extern char * tzname[];
116
117 #ifndef YEAR_2000_NAME
118 #define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
119 #endif /* !defined YEAR_2000_NAME */
120
121
122 #define IN_NONE 0
123 #define IN_SOME 1
124 #define IN_THIS 2
125 #define IN_ALL 3
126
127 size_t
128 strftime(s, maxsize, format, t)
129 char * const s;
130 const size_t maxsize;
131 const char * const format;
132 const struct tm * const t;
133 {
134 char * p;
135 int warn;
136
137 tzset();
138 #ifdef LOCALE_HOME
139 localebuf.mon[0] = 0;
140 #endif /* defined LOCALE_HOME */
141 warn = IN_NONE;
142 p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn);
143 #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
144 if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {
145 (void) fprintf(stderr, "\n");
146 if (format == NULL)
147 (void) fprintf(stderr, "NULL strftime format ");
148 else (void) fprintf(stderr, "strftime format \"%s\" ",
149 format);
150 (void) fprintf(stderr, "yields only two digits of years in ");
151 if (warn == IN_SOME)
152 (void) fprintf(stderr, "some locales");
153 else if (warn == IN_THIS)
154 (void) fprintf(stderr, "the current locale");
155 else (void) fprintf(stderr, "all locales");
156 (void) fprintf(stderr, "\n");
157 }
158 #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
159 if (p == s + maxsize)
160 return 0;
161 *p = '\0';
162 return p - s;
163 }
164
165 static char *
166 _fmt(format, t, pt, ptlim, warnp)
167 const char * format;
168 const struct tm * const t;
169 char * pt;
170 const char * const ptlim;
171 int * warnp;
172 {
173 for ( ; *format; ++format) {
174 if (*format == '%') {
175 label:
176 switch (*++format) {
177 case '\0':
178 --format;
179 break;
180 case 'A':
181 pt = _add((t->tm_wday < 0 ||
182 t->tm_wday >= DAYSPERWEEK) ?
183 "?" : Locale->weekday[t->tm_wday],
184 pt, ptlim);
185 continue;
186 case 'a':
187 pt = _add((t->tm_wday < 0 ||
188 t->tm_wday >= DAYSPERWEEK) ?
189 "?" : Locale->wday[t->tm_wday],
190 pt, ptlim);
191 continue;
192 case 'B':
193 pt = _add((t->tm_mon < 0 ||
194 t->tm_mon >= MONSPERYEAR) ?
195 "?" : Locale->month[t->tm_mon],
196 pt, ptlim);
197 continue;
198 case 'b':
199 case 'h':
200 pt = _add((t->tm_mon < 0 ||
201 t->tm_mon >= MONSPERYEAR) ?
202 "?" : Locale->mon[t->tm_mon],
203 pt, ptlim);
204 continue;
205 case 'C':
206 /*
207 ** %C used to do a...
208 ** _fmt("%a %b %e %X %Y", t);
209 ** ...whereas now POSIX 1003.2 calls for
210 ** something completely different.
211 ** (ado, 1993-05-24)
212 */
213 pt = _conv((t->tm_year + TM_YEAR_BASE) / 100,
214 "%02d", pt, ptlim);
215 continue;
216 case 'c':
217 {
218 int warn2 = IN_SOME;
219
220 pt = _fmt(Locale->c_fmt, t, pt, ptlim, warnp);
221 if (warn2 == IN_ALL)
222 warn2 = IN_THIS;
223 if (warn2 > *warnp)
224 *warnp = warn2;
225 }
226 continue;
227 case 'D':
228 pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp);
229 continue;
230 case 'd':
231 pt = _conv(t->tm_mday, "%02d", pt, ptlim);
232 continue;
233 case 'E':
234 case 'O':
235 /*
236 ** C99 locale modifiers.
237 ** The sequences
238 ** %Ec %EC %Ex %EX %Ey %EY
239 ** %Od %oe %OH %OI %Om %OM
240 ** %OS %Ou %OU %OV %Ow %OW %Oy
241 ** are supposed to provide alternate
242 ** representations.
243 */
244 goto label;
245 case 'e':
246 pt = _conv(t->tm_mday, "%2d", pt, ptlim);
247 continue;
248 case 'F':
249 pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp);
250 continue;
251 case 'H':
252 pt = _conv(t->tm_hour, "%02d", pt, ptlim);
253 continue;
254 case 'I':
255 pt = _conv((t->tm_hour % 12) ?
256 (t->tm_hour % 12) : 12,
257 "%02d", pt, ptlim);
258 continue;
259 case 'j':
260 pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
261 continue;
262 case 'k':
263 /*
264 ** This used to be...
265 ** _conv(t->tm_hour % 12 ?
266 ** t->tm_hour % 12 : 12, 2, ' ');
267 ** ...and has been changed to the below to
268 ** match SunOS 4.1.1 and Arnold Robbins'
269 ** strftime version 3.0. That is, "%k" and
270 ** "%l" have been swapped.
271 ** (ado, 1993-05-24)
272 */
273 pt = _conv(t->tm_hour, "%2d", pt, ptlim);
274 continue;
275 #ifdef KITCHEN_SINK
276 case 'K':
277 /*
278 ** After all this time, still unclaimed!
279 */
280 pt = _add("kitchen sink", pt, ptlim);
281 continue;
282 #endif /* defined KITCHEN_SINK */
283 case 'l':
284 /*
285 ** This used to be...
286 ** _conv(t->tm_hour, 2, ' ');
287 ** ...and has been changed to the below to
288 ** match SunOS 4.1.1 and Arnold Robbin's
289 ** strftime version 3.0. That is, "%k" and
290 ** "%l" have been swapped.
291 ** (ado, 1993-05-24)
292 */
293 pt = _conv((t->tm_hour % 12) ?
294 (t->tm_hour % 12) : 12,
295 "%2d", pt, ptlim);
296 continue;
297 case 'M':
298 pt = _conv(t->tm_min, "%02d", pt, ptlim);
299 continue;
300 case 'm':
301 pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
302 continue;
303 case 'n':
304 pt = _add("\n", pt, ptlim);
305 continue;
306 case 'p':
307 pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
308 Locale->pm :
309 Locale->am,
310 pt, ptlim);
311 continue;
312 case 'R':
313 pt = _fmt("%H:%M", t, pt, ptlim, warnp);
314 continue;
315 case 'r':
316 pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp);
317 continue;
318 case 'S':
319 pt = _conv(t->tm_sec, "%02d", pt, ptlim);
320 continue;
321 case 's':
322 {
323 struct tm tm;
324 char buf[INT_STRLEN_MAXIMUM(
325 time_t) + 1];
326 time_t mkt;
327
328 tm = *t;
329 mkt = mktime(&tm);
330 if (TYPE_SIGNED(time_t))
331 (void) sprintf(buf, "%ld",
332 (long) mkt);
333 else (void) sprintf(buf, "%lu",
334 (unsigned long) mkt);
335 pt = _add(buf, pt, ptlim);
336 }
337 continue;
338 case 'T':
339 pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp);
340 continue;
341 case 't':
342 pt = _add("\t", pt, ptlim);
343 continue;
344 case 'U':
345 pt = _conv((t->tm_yday + DAYSPERWEEK -
346 t->tm_wday) / DAYSPERWEEK,
347 "%02d", pt, ptlim);
348 continue;
349 case 'u':
350 /*
351 ** From Arnold Robbins' strftime version 3.0:
352 ** "ISO 8601: Weekday as a decimal number
353 ** [1 (Monday) - 7]"
354 ** (ado, 1993-05-24)
355 */
356 pt = _conv((t->tm_wday == 0) ?
357 DAYSPERWEEK : t->tm_wday,
358 "%d", pt, ptlim);
359 continue;
360 case 'V': /* ISO 8601 week number */
361 case 'G': /* ISO 8601 year (four digits) */
362 case 'g': /* ISO 8601 year (two digits) */
363 /*
364 ** From Arnold Robbins' strftime version 3.0: "the week number of the
365 ** year (the first Monday as the first day of week 1) as a decimal number
366 ** (01-53)."
367 ** (ado, 1993-05-24)
368 **
369 ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
370 ** "Week 01 of a year is per definition the first week which has the
371 ** Thursday in this year, which is equivalent to the week which contains
372 ** the fourth day of January. In other words, the first week of a new year
373 ** is the week which has the majority of its days in the new year. Week 01
374 ** might also contain days from the previous year and the week before week
375 ** 01 of a year is the last week (52 or 53) of the previous year even if
376 ** it contains days from the new year. A week starts with Monday (day 1)
377 ** and ends with Sunday (day 7). For example, the first week of the year
378 ** 1997 lasts from 1996-12-30 to 1997-01-05..."
379 ** (ado, 1996-01-02)
380 */
381 {
382 int year;
383 int yday;
384 int wday;
385 int w;
386
387 year = t->tm_year + TM_YEAR_BASE;
388 yday = t->tm_yday;
389 wday = t->tm_wday;
390 for ( ; ; ) {
391 int len;
392 int bot;
393 int top;
394
395 len = isleap(year) ?
396 DAYSPERLYEAR :
397 DAYSPERNYEAR;
398 /*
399 ** What yday (-3 ... 3) does
400 ** the ISO year begin on?
401 */
402 bot = ((yday + 11 - wday) %
403 DAYSPERWEEK) - 3;
404 /*
405 ** What yday does the NEXT
406 ** ISO year begin on?
407 */
408 top = bot -
409 (len % DAYSPERWEEK);
410 if (top < -3)
411 top += DAYSPERWEEK;
412 top += len;
413 if (yday >= top) {
414 ++year;
415 w = 1;
416 break;
417 }
418 if (yday >= bot) {
419 w = 1 + ((yday - bot) /
420 DAYSPERWEEK);
421 break;
422 }
423 --year;
424 yday += isleap(year) ?
425 DAYSPERLYEAR :
426 DAYSPERNYEAR;
427 }
428 #ifdef XPG4_1994_04_09
429 if ((w == 52
430 && t->tm_mon == TM_JANUARY)
431 || (w == 1
432 && t->tm_mon == TM_DECEMBER))
433 w = 53;
434 #endif /* defined XPG4_1994_04_09 */
435 if (*format == 'V')
436 pt = _conv(w, "%02d",
437 pt, ptlim);
438 else if (*format == 'g') {
439 *warnp = IN_ALL;
440 pt = _conv(year % 100, "%02d",
441 pt, ptlim);
442 } else pt = _conv(year, "%04d",
443 pt, ptlim);
444 }
445 continue;
446 case 'v':
447 /*
448 ** From Arnold Robbins' strftime version 3.0:
449 ** "date as dd-bbb-YYYY"
450 ** (ado, 1993-05-24)
451 */
452 pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp);
453 continue;
454 case 'W':
455 pt = _conv((t->tm_yday + DAYSPERWEEK -
456 (t->tm_wday ?
457 (t->tm_wday - 1) :
458 (DAYSPERWEEK - 1))) / DAYSPERWEEK,
459 "%02d", pt, ptlim);
460 continue;
461 case 'w':
462 pt = _conv(t->tm_wday, "%d", pt, ptlim);
463 continue;
464 case 'X':
465 pt = _fmt(Locale->X_fmt, t, pt, ptlim, warnp);
466 continue;
467 case 'x':
468 {
469 int warn2 = IN_SOME;
470
471 pt = _fmt(Locale->x_fmt, t, pt, ptlim, &warn2);
472 if (warn2 == IN_ALL)
473 warn2 = IN_THIS;
474 if (warn2 > *warnp)
475 *warnp = warn2;
476 }
477 continue;
478 case 'y':
479 *warnp = IN_ALL;
480 pt = _conv((t->tm_year + TM_YEAR_BASE) % 100,
481 "%02d", pt, ptlim);
482 continue;
483 case 'Y':
484 pt = _conv(t->tm_year + TM_YEAR_BASE, "%04d",
485 pt, ptlim);
486 continue;
487 case 'Z':
488 #ifdef TM_ZONE
489 if (t->TM_ZONE != NULL)
490 pt = _add(t->TM_ZONE, pt, ptlim);
491 else
492 #endif /* defined TM_ZONE */
493 if (t->tm_isdst >= 0)
494 pt = _add(tzname[t->tm_isdst != 0],
495 pt, ptlim);
496 /*
497 ** C99 says that %Z must be replaced by the
498 ** empty string if the time zone is not
499 ** determinable.
500 */
501 continue;
502 case 'z':
503 {
504 int diff;
505 char const * sign;
506
507 if (t->tm_isdst < 0)
508 continue;
509 #ifdef TM_GMTOFF
510 diff = t->TM_GMTOFF;
511 #else /* !defined TM_GMTOFF */
512 /*
513 ** C99 says that the UTC offset must
514 ** be computed by looking only at
515 ** tm_isdst. This requirement is
516 ** incorrect, since it means the code
517 ** must rely on magic (in this case
518 ** altzone and timezone), and the
519 ** magic might not have the correct
520 ** offset. Doing things correctly is
521 ** tricky and requires disobeying C99;
522 ** see GNU C strftime for details.
523 ** For now, punt and conform to the
524 ** standard, even though it's incorrect.
525 **
526 ** C99 says that %z must be replaced by the
527 ** empty string if the time zone is not
528 ** determinable, so output nothing if the
529 ** appropriate variables are not available.
530 */
531 if (t->tm_isdst == 0)
532 #ifdef USG_COMPAT
533 diff = -timezone;
534 #else /* defined USG_COMPAT */
535 continue;
536 #endif /* !defined USG_COMPAT */
537 else
538 #ifdef ALTZONE
539 diff = -altzone;
540 #else /* !defined ALTZONE */
541 continue;
542 #endif /* !defined ALTZONE */
543 #endif /* !defined TM_GMTOFF */
544 if (diff < 0) {
545 sign = "-";
546 diff = -diff;
547 } else sign = "+";
548 pt = _add(sign, pt, ptlim);
549 diff /= 60;
550 pt = _conv((diff/60)*100 + diff%60,
551 "%04d", pt, ptlim);
552 }
553 continue;
554 case '+':
555 pt = _fmt(Locale->date_fmt, t, pt, ptlim,
556 warnp);
557 continue;
558 case '%':
559 /*
560 ** X311J/88-090 (4.12.3.5): if conversion char is
561 ** undefined, behavior is undefined. Print out the
562 ** character itself as printf(3) also does.
563 */
564 default:
565 break;
566 }
567 }
568 if (pt == ptlim)
569 break;
570 *pt++ = *format;
571 }
572 return pt;
573 }
574
575 static char *
576 _conv(n, format, pt, ptlim)
577 const int n;
578 const char * const format;
579 char * const pt;
580 const char * const ptlim;
581 {
582 char buf[INT_STRLEN_MAXIMUM(int) + 1];
583
584 (void) sprintf(buf, format, n);
585 return _add(buf, pt, ptlim);
586 }
587
588 static char *
589 _add(str, pt, ptlim)
590 const char * str;
591 char * pt;
592 const char * const ptlim;
593 {
594 while (pt < ptlim && (*pt = *str++) != '\0')
595 ++pt;
596 return pt;
597 }
598
599 #ifdef LOCALE_HOME
600 static struct lc_time_T *
601 _loc P((void))
602 {
603 static const char locale_home[] = LOCALE_HOME;
604 static const char lc_time[] = "LC_TIME";
605 static char * locale_buf;
606 static char locale_buf_C[] = "C";
607
608 int fd;
609 int oldsun; /* "...ain't got nothin' to do..." */
610 char * lbuf;
611 char * name;
612 char * p;
613 const char ** ap;
614 const char * plim;
615 char filename[FILENAME_MAX];
616 struct stat st;
617 size_t namesize;
618 size_t bufsize;
619
620 /*
621 ** Use localebuf.mon[0] to signal whether locale is already set up.
622 */
623 if (localebuf.mon[0])
624 return &localebuf;
625 name = setlocale(LC_TIME, (char *) NULL);
626 if (name == NULL || *name == '\0')
627 goto no_locale;
628 /*
629 ** If the locale name is the same as our cache, use the cache.
630 */
631 lbuf = locale_buf;
632 if (lbuf != NULL && strcmp(name, lbuf) == 0) {
633 p = lbuf;
634 for (ap = (const char **) &localebuf;
635 ap < (const char **) (&localebuf + 1);
636 ++ap)
637 *ap = p += strlen(p) + 1;
638 return &localebuf;
639 }
640 /*
641 ** Slurp the locale file into the cache.
642 */
643 namesize = strlen(name) + 1;
644 if (sizeof(filename) <
645 sizeof(locale_home) + namesize + sizeof(lc_time))
646 goto no_locale;
647 oldsun = 0;
648 (void) sprintf(filename, "%s/%s/%s", locale_home, name, lc_time);
649 fd = open(filename, O_RDONLY);
650 if (fd < 0) {
651 /*
652 ** Old Sun systems have a different naming and data convention.
653 */
654 oldsun = 1;
655 (void) sprintf(filename, "%s/%s/%s", locale_home,
656 lc_time, name);
657 fd = open(filename, O_RDONLY);
658 if (fd < 0)
659 goto no_locale;
660 }
661 if (fstat(fd, &st) != 0)
662 goto bad_locale;
663 if (st.st_size <= 0)
664 goto bad_locale;
665 bufsize = namesize + st.st_size;
666 locale_buf = NULL;
667 lbuf = (lbuf == NULL || lbuf == locale_buf_C) ?
668 malloc(bufsize) : realloc(lbuf, bufsize);
669 if (lbuf == NULL)
670 goto bad_locale;
671 (void) strcpy(lbuf, name);
672 p = lbuf + namesize;
673 plim = p + st.st_size;
674 if (read(fd, p, (size_t) st.st_size) != st.st_size)
675 goto bad_lbuf;
676 if (close(fd) != 0)
677 goto bad_lbuf;
678 /*
679 ** Parse the locale file into localebuf.
680 */
681 if (plim[-1] != '\n')
682 goto bad_lbuf;
683 for (ap = (const char **) &localebuf;
684 ap < (const char **) (&localebuf + 1);
685 ++ap) {
686 if (p == plim)
687 goto bad_lbuf;
688 *ap = p;
689 while (*p != '\n')
690 ++p;
691 *p++ = '\0';
692 }
693 if (oldsun) {
694 /*
695 ** SunOS 4 used an obsolescent format; see localdtconv(3).
696 ** c_fmt had the ``short format for dates and times together''
697 ** (SunOS 4 date, "%a %b %e %T %Z %Y" in the C locale);
698 ** date_fmt had the ``long format for dates''
699 ** (SunOS 4 strftime %C, "%A, %B %e, %Y" in the C locale).
700 ** Discard the latter in favor of the former.
701 */
702 localebuf.date_fmt = localebuf.c_fmt;
703 }
704 /*
705 ** Record the successful parse in the cache.
706 */
707 locale_buf = lbuf;
708
709 return &localebuf;
710
711 bad_lbuf:
712 free(lbuf);
713 bad_locale:
714 (void) close(fd);
715 no_locale:
716 localebuf = C_time_locale;
717 locale_buf = locale_buf_C;
718 return &localebuf;
719 }
720 #endif /* defined LOCALE_HOME */
721