]> git.saurik.com Git - apple/libc.git/blob - stdtime/FreeBSD/strftime.c
Libc-1244.1.7.tar.gz
[apple/libc.git] / stdtime / FreeBSD / strftime.c
1 /*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17
18 #if 0
19 static const char elsieid[] = "@(#)strftime.3 8.3";
20 /*
21 ** Based on the UCB version with the ID appearing below.
22 ** This is ANSIish only when "multibyte character == plain character".
23 */
24 #endif
25
26 #include "xlocale_private.h"
27
28 #include "namespace.h"
29 #include "private.h"
30
31 #if defined(LIBC_SCCS) && !defined(lint)
32 static const char sccsid[] = "@(#)strftime.c 5.4 (Berkeley) 3/14/89";
33 #endif /* LIBC_SCCS and not lint */
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD: src/lib/libc/stdtime/strftime.c,v 1.44 2009/06/09 09:02:58 delphij Exp $");
36
37 #include "tzfile.h"
38 #include <time.h>
39 #include <fcntl.h>
40 #include <sys/stat.h>
41 #include "un-namespace.h"
42 #include "timelocal.h"
43
44 #if !BUILDING_VARIANT
45 static char * _add(const char *, char *, const char *);
46 static char * _conv(int, const char *, char *, const char *, locale_t);
47 static char * _yconv(int, int, int, int, char *, const char *, locale_t);
48 #endif
49 #define _fmt _st_fmt
50 __private_extern__ char * _fmt(const char *, const struct tm *, char *, const char *,
51 int *, struct lc_time_T *, locale_t);
52
53 extern char * tzname[];
54 extern long __darwin_altzone; /* DST timezone offset */
55 #define altzone __darwin_altzone
56 __private_extern__ long _st_get_timezone(void);
57
58 #ifndef YEAR_2000_NAME
59 #define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
60 #endif /* !defined YEAR_2000_NAME */
61
62 #define IN_NONE 0
63 #define IN_SOME 1
64 #define IN_THIS 2
65 #define IN_ALL 3
66
67 #define PAD_DEFAULT 0
68 #define PAD_LESS 1
69 #define PAD_SPACE 2
70 #define PAD_ZERO 3
71
72 #ifndef BUILDING_VARIANT
73 static const char * const fmt_padding[][4] = {
74 /* DEFAULT, LESS, SPACE, ZERO */
75 #define PAD_FMT_MONTHDAY 0
76 #define PAD_FMT_HMS 0
77 #define PAD_FMT_CENTURY 0
78 #define PAD_FMT_SHORTYEAR 0
79 #define PAD_FMT_MONTH 0
80 #define PAD_FMT_WEEKOFYEAR 0
81 #define PAD_FMT_DAYOFMONTH 0
82 { "%02d", "%d", "%2d", "%02d" },
83 #define PAD_FMT_SDAYOFMONTH 1
84 #define PAD_FMT_SHMS 1
85 { "%2d", "%d", "%2d", "%02d" },
86 #define PAD_FMT_DAYOFYEAR 2
87 { "%03d", "%d", "%3d", "%03d" },
88 #define PAD_FMT_YEAR 3
89 { "%04d", "%d", "%4d", "%04d" }
90 };
91 #endif
92
93 #define USG_COMPAT
94 #define ALTZONE
95
96 size_t
97 strftime_l(char * __restrict s, size_t maxsize, const char * __restrict format,
98 const struct tm * __restrict t, locale_t loc)
99 {
100 char * p;
101 int warn;
102
103 NORMALIZE_LOCALE(loc);
104 tzset();
105 warn = IN_NONE;
106 p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn, __get_current_time_locale(loc), loc);
107 #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
108 if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {
109 (void) fputs("\n", stderr);
110 if (format == NULL)
111 (void) fputs("NULL strftime format ", stderr);
112 else (void) fprintf_l(stderr, loc, "strftime format \"%s\" ",
113 format);
114 (void) fputs("yields only two digits of years in ", stderr);
115 if (warn == IN_SOME)
116 (void) fputs("some locales", stderr);
117 else if (warn == IN_THIS)
118 (void) fputs("the current locale", stderr);
119 else (void) fputs("all locales", stderr);
120 (void) fputs("\n", stderr);
121 }
122 #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
123 if (p == s + maxsize)
124 return 0;
125 *p = '\0';
126 return p - s;
127 }
128
129 size_t
130 strftime(char * __restrict s, size_t maxsize, const char * __restrict format,
131 const struct tm * __restrict t)
132 {
133 return strftime_l(s, maxsize, format, t, __current_locale());
134 }
135
136 #ifndef BUILDING_VARIANT
137 __private_extern__ char *
138 _fmt(format, t, pt, ptlim, warnp, tptr, loc)
139 const char * format;
140 const struct tm * const t;
141 char * pt;
142 const char * const ptlim;
143 int * warnp;
144 struct lc_time_T * tptr;
145 locale_t loc;
146 {
147 int Ealternative, Oalternative, PadIndex;
148
149 for ( ; *format; ++format) {
150 if (*format == '%') {
151 Ealternative = 0;
152 Oalternative = 0;
153 PadIndex = PAD_DEFAULT;
154 label:
155 switch (*++format) {
156 case '\0':
157 --format;
158 break;
159 case 'A':
160 pt = _add((t->tm_wday < 0 ||
161 t->tm_wday >= DAYSPERWEEK) ?
162 "?" : tptr->weekday[t->tm_wday],
163 pt, ptlim);
164 continue;
165 case 'a':
166 pt = _add((t->tm_wday < 0 ||
167 t->tm_wday >= DAYSPERWEEK) ?
168 "?" : tptr->wday[t->tm_wday],
169 pt, ptlim);
170 continue;
171 case 'B':
172 pt = _add((t->tm_mon < 0 ||
173 t->tm_mon >= MONSPERYEAR) ?
174 "?" : (Oalternative ? tptr->alt_month :
175 tptr->month)[t->tm_mon],
176 pt, ptlim);
177 continue;
178 case 'b':
179 case 'h':
180 pt = _add((t->tm_mon < 0 ||
181 t->tm_mon >= MONSPERYEAR) ?
182 "?" : tptr->mon[t->tm_mon],
183 pt, ptlim);
184 continue;
185 case 'C':
186 /*
187 ** %C used to do a...
188 ** _fmt("%a %b %e %X %Y", t, tptr, loc);
189 ** ...whereas now POSIX 1003.2 calls for
190 ** something completely different.
191 ** (ado, 1993-05-24)
192 */
193 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0,
194 pt, ptlim, loc);
195 continue;
196 case 'c':
197 {
198 int warn2 = IN_SOME;
199
200 pt = _fmt(tptr->c_fmt, t, pt, ptlim, &warn2, tptr, loc);
201 if (warn2 == IN_ALL)
202 warn2 = IN_THIS;
203 if (warn2 > *warnp)
204 *warnp = warn2;
205 }
206 continue;
207 case 'D':
208 pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp, tptr, loc);
209 continue;
210 case 'd':
211 pt = _conv(t->tm_mday, fmt_padding[PAD_FMT_DAYOFMONTH][PadIndex],
212 pt, ptlim, loc);
213 continue;
214 case 'E':
215 if (Ealternative || Oalternative)
216 break;
217 Ealternative++;
218 goto label;
219 case 'O':
220 /*
221 ** C99 locale modifiers.
222 ** The sequences
223 ** %Ec %EC %Ex %EX %Ey %EY
224 ** %Od %oe %OH %OI %Om %OM
225 ** %OS %Ou %OU %OV %Ow %OW %Oy
226 ** are supposed to provide alternate
227 ** representations.
228 **
229 ** FreeBSD extension
230 ** %OB
231 */
232 if (Ealternative || Oalternative)
233 break;
234 Oalternative++;
235 goto label;
236 case 'e':
237 pt = _conv(t->tm_mday,
238 fmt_padding[PAD_FMT_SDAYOFMONTH][PadIndex], pt, ptlim, loc);
239 continue;
240 case 'F':
241 pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp, tptr, loc);
242 continue;
243 case 'H':
244 pt = _conv(t->tm_hour, fmt_padding[PAD_FMT_HMS][PadIndex],
245 pt, ptlim, loc);
246 continue;
247 case 'I':
248 pt = _conv((t->tm_hour % 12) ?
249 (t->tm_hour % 12) : 12,
250 fmt_padding[PAD_FMT_HMS][PadIndex], pt, ptlim, loc);
251 continue;
252 case 'j':
253 pt = _conv(t->tm_yday + 1,
254 fmt_padding[PAD_FMT_DAYOFYEAR][PadIndex], pt, ptlim, loc);
255 continue;
256 case 'k':
257 /*
258 ** This used to be...
259 ** _conv(t->tm_hour % 12 ?
260 ** t->tm_hour % 12 : 12, 2, ' ', loc);
261 ** ...and has been changed to the below to
262 ** match SunOS 4.1.1 and Arnold Robbins'
263 ** strftime version 3.0. That is, "%k" and
264 ** "%l" have been swapped.
265 ** (ado, 1993-05-24)
266 */
267 pt = _conv(t->tm_hour, fmt_padding[PAD_FMT_SHMS][PadIndex],
268 pt, ptlim, loc);
269 continue;
270 #ifdef KITCHEN_SINK
271 case 'K':
272 /*
273 ** After all this time, still unclaimed!
274 */
275 pt = _add("kitchen sink", pt, ptlim);
276 continue;
277 #endif /* defined KITCHEN_SINK */
278 case 'l':
279 /*
280 ** This used to be...
281 ** _conv(t->tm_hour, 2, ' ');
282 ** ...and has been changed to the below to
283 ** match SunOS 4.1.1 and Arnold Robbin's
284 ** strftime version 3.0. That is, "%k" and
285 ** "%l" have been swapped.
286 ** (ado, 1993-05-24)
287 */
288 pt = _conv((t->tm_hour % 12) ?
289 (t->tm_hour % 12) : 12,
290 fmt_padding[PAD_FMT_SHMS][PadIndex], pt, ptlim, loc);
291 continue;
292 case 'M':
293 pt = _conv(t->tm_min, fmt_padding[PAD_FMT_HMS][PadIndex],
294 pt, ptlim, loc);
295 continue;
296 case 'm':
297 pt = _conv(t->tm_mon + 1,
298 fmt_padding[PAD_FMT_MONTH][PadIndex], pt, ptlim, loc);
299 continue;
300 case 'n':
301 pt = _add("\n", pt, ptlim);
302 continue;
303 case 'p':
304 pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
305 tptr->pm :
306 tptr->am,
307 pt, ptlim);
308 continue;
309 case 'R':
310 pt = _fmt("%H:%M", t, pt, ptlim, warnp, tptr, loc);
311 continue;
312 case 'r':
313 pt = _fmt(tptr->ampm_fmt, t, pt, ptlim,
314 warnp, tptr, loc);
315 continue;
316 case 'S':
317 pt = _conv(t->tm_sec, fmt_padding[PAD_FMT_HMS][PadIndex],
318 pt, ptlim, loc);
319 continue;
320 case 's':
321 {
322 struct tm tm;
323 char buf[INT_STRLEN_MAXIMUM(
324 time_t) + 1];
325 time_t mkt;
326
327 tm = *t;
328 mkt = mktime(&tm);
329 if (TYPE_SIGNED(time_t))
330 (void) sprintf_l(buf, loc, "%ld",
331 (long) mkt);
332 else (void) sprintf_l(buf, loc, "%lu",
333 (unsigned long) mkt);
334 pt = _add(buf, pt, ptlim);
335 }
336 continue;
337 case 'T':
338 pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp, tptr, loc);
339 continue;
340 case 't':
341 pt = _add("\t", pt, ptlim);
342 continue;
343 case 'U':
344 pt = _conv((t->tm_yday + DAYSPERWEEK -
345 t->tm_wday) / DAYSPERWEEK,
346 fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex], pt, ptlim, loc);
347 continue;
348 case 'u':
349 /*
350 ** From Arnold Robbins' strftime version 3.0:
351 ** "ISO 8601: Weekday as a decimal number
352 ** [1 (Monday) - 7]"
353 ** (ado, 1993-05-24)
354 */
355 pt = _conv((t->tm_wday == 0) ?
356 DAYSPERWEEK : t->tm_wday,
357 "%d", pt, ptlim, loc);
358 continue;
359 case 'V': /* ISO 8601 week number */
360 case 'G': /* ISO 8601 year (four digits) */
361 case 'g': /* ISO 8601 year (two digits) */
362 /*
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
365 ** (01-53)."
366 ** (ado, 1993-05-24)
367 **
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..."
378 ** (ado, 1996-01-02)
379 */
380 {
381 int year;
382 int base;
383 int yday;
384 int wday;
385 int w;
386
387 year = t->tm_year;
388 base = TM_YEAR_BASE;
389 yday = t->tm_yday;
390 wday = t->tm_wday;
391 for ( ; ; ) {
392 int len;
393 int bot;
394 int top;
395
396 len = isleap_sum(year, base) ?
397 DAYSPERLYEAR :
398 DAYSPERNYEAR;
399 /*
400 ** What yday (-3 ... 3) does
401 ** the ISO year begin on?
402 */
403 bot = ((yday + 11 - wday) %
404 DAYSPERWEEK) - 3;
405 /*
406 ** What yday does the NEXT
407 ** ISO year begin on?
408 */
409 top = bot -
410 (len % DAYSPERWEEK);
411 if (top < -3)
412 top += DAYSPERWEEK;
413 top += len;
414 if (yday >= top) {
415 ++base;
416 w = 1;
417 break;
418 }
419 if (yday >= bot) {
420 w = 1 + ((yday - bot) /
421 DAYSPERWEEK);
422 break;
423 }
424 --base;
425 yday += isleap_sum(year, base) ?
426 DAYSPERLYEAR :
427 DAYSPERNYEAR;
428 }
429 #ifdef XPG4_1994_04_09
430 if ((w == 52 &&
431 t->tm_mon == TM_JANUARY) ||
432 (w == 1 &&
433 t->tm_mon == TM_DECEMBER))
434 w = 53;
435 #endif /* defined XPG4_1994_04_09 */
436 if (*format == 'V')
437 pt = _conv(w, fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex],
438 pt, ptlim, loc);
439 else if (*format == 'g') {
440 *warnp = IN_ALL;
441 pt = _yconv(year, base, 0, 1,
442 pt, ptlim, loc);
443 } else pt = _yconv(year, base, 1, 1,
444 pt, ptlim, loc);
445 }
446 continue;
447 case 'v':
448 /*
449 ** From Arnold Robbins' strftime version 3.0:
450 ** "date as dd-bbb-YYYY"
451 ** (ado, 1993-05-24)
452 */
453 pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp, tptr, loc);
454 continue;
455 case 'W':
456 pt = _conv((t->tm_yday + DAYSPERWEEK -
457 (t->tm_wday ?
458 (t->tm_wday - 1) :
459 (DAYSPERWEEK - 1))) / DAYSPERWEEK,
460 fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex], pt, ptlim, loc);
461 continue;
462 case 'w':
463 pt = _conv(t->tm_wday, "%d", pt, ptlim, loc);
464 continue;
465 case 'X':
466 pt = _fmt(tptr->X_fmt, t, pt, ptlim, warnp, tptr, loc);
467 continue;
468 case 'x':
469 {
470 int warn2 = IN_SOME;
471
472 pt = _fmt(tptr->x_fmt, t, pt, ptlim, &warn2, tptr, loc);
473 if (warn2 == IN_ALL)
474 warn2 = IN_THIS;
475 if (warn2 > *warnp)
476 *warnp = warn2;
477 }
478 continue;
479 case 'y':
480 *warnp = IN_ALL;
481 pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1,
482 pt, ptlim, loc);
483 continue;
484 case 'Y':
485 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1,
486 pt, ptlim, loc);
487 continue;
488 case 'Z':
489 #ifdef TM_ZONE
490 if (t->TM_ZONE != NULL)
491 pt = _add(t->TM_ZONE, pt, ptlim);
492 else
493 #endif /* defined TM_ZONE */
494 if (t->tm_isdst >= 0)
495 pt = _add(tzname[t->tm_isdst != 0],
496 pt, ptlim);
497 /*
498 ** C99 says that %Z must be replaced by the
499 ** empty string if the time zone is not
500 ** determinable.
501 */
502 continue;
503 case 'z':
504 {
505 int diff;
506 char const * sign;
507
508 if (t->tm_isdst < 0)
509 continue;
510 #if defined(TM_GMTOFF) && !__DARWIN_UNIX03
511 diff = t->TM_GMTOFF;
512 #else /* !defined TM_GMTOFF || __DARWIN_UNIX03 */
513 /*
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.
526 **
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.
531 */
532 if (t->tm_isdst == 0)
533 #ifdef USG_COMPAT
534 diff = -_st_get_timezone();
535 #else /* !defined USG_COMPAT */
536 continue;
537 #endif /* !defined USG_COMPAT */
538 else
539 #ifdef ALTZONE
540 diff = -altzone;
541 #else /* !defined ALTZONE */
542 continue;
543 #endif /* !defined ALTZONE */
544 #endif /* !defined TM_GMTOFF || __DARWIN_UNIX03 */
545 if (diff < 0) {
546 sign = "-";
547 diff = -diff;
548 } else sign = "+";
549 pt = _add(sign, pt, ptlim);
550 diff /= SECSPERMIN;
551 diff = (diff / MINSPERHOUR) * 100 +
552 (diff % MINSPERHOUR);
553 pt = _conv(diff,
554 fmt_padding[PAD_FMT_YEAR][PadIndex], pt, ptlim, loc);
555 }
556 continue;
557 case '+':
558 pt = _fmt(tptr->date_fmt, t, pt, ptlim,
559 warnp, tptr, loc);
560 continue;
561 case '-':
562 if (PadIndex != PAD_DEFAULT)
563 break;
564 PadIndex = PAD_LESS;
565 goto label;
566 case '_':
567 if (PadIndex != PAD_DEFAULT)
568 break;
569 PadIndex = PAD_SPACE;
570 goto label;
571 case '0':
572 if (PadIndex != PAD_DEFAULT)
573 break;
574 PadIndex = PAD_ZERO;
575 goto label;
576 case '%':
577 /*
578 ** X311J/88-090 (4.12.3.5): if conversion char is
579 ** undefined, behavior is undefined. Print out the
580 ** character itself as printf(3) also does.
581 */
582 default:
583 break;
584 }
585 }
586 if (pt == ptlim)
587 break;
588 *pt++ = *format;
589 }
590 return pt;
591 }
592
593 static char *
594 _conv(n, format, pt, ptlim, loc)
595 const int n;
596 const char * const format;
597 char * const pt;
598 const char * const ptlim;
599 locale_t loc;
600 {
601 char buf[INT_STRLEN_MAXIMUM(int) + 1];
602
603 (void) sprintf_l(buf, loc, format, n);
604 return _add(buf, pt, ptlim);
605 }
606
607 static char *
608 _add(str, pt, ptlim)
609 const char * str;
610 char * pt;
611 const char * const ptlim;
612 {
613 while (pt < ptlim && (*pt = *str++) != '\0')
614 ++pt;
615 return pt;
616 }
617
618 /*
619 ** POSIX and the C Standard are unclear or inconsistent about
620 ** what %C and %y do if the year is negative or exceeds 9999.
621 ** Use the convention that %C concatenated with %y yields the
622 ** same output as %Y, and that %Y contains at least 4 bytes,
623 ** with more only if necessary.
624 */
625
626 static char *
627 _yconv(a, b, convert_top, convert_yy, pt, ptlim, loc)
628 const int a;
629 const int b;
630 const int convert_top;
631 const int convert_yy;
632 char * pt;
633 const char * const ptlim;
634 locale_t loc;
635 {
636 register int lead;
637 register int trail;
638
639 #define DIVISOR 100
640 trail = a % DIVISOR + b % DIVISOR;
641 lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
642 trail %= DIVISOR;
643 if (trail < 0 && lead > 0) {
644 trail += DIVISOR;
645 --lead;
646 } else if (lead < 0 && trail > 0) {
647 trail -= DIVISOR;
648 ++lead;
649 }
650 if (convert_top) {
651 if (lead == 0 && trail < 0)
652 pt = _add("-0", pt, ptlim);
653 else pt = _conv(lead, "%02d", pt, ptlim, loc);
654 }
655 if (convert_yy)
656 pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim, loc);
657 return pt;
658 }
659 #endif /* !BUILDING_VARIANT */