]>
git.saurik.com Git - apple/libc.git/blob - stdtime/FreeBSD/strftime.c
0300e2b45ba8af4d66ff4d0fc58d28bfce0bf8f9
2 * Copyright (c) 1989 The Regents of the University of California.
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.
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wunreachable-code"
22 static const char elsieid
[] = "@(#)strftime.3 8.3";
24 ** Based on the UCB version with the ID appearing below.
25 ** This is ANSIish only when "multibyte character == plain character".
29 #include "xlocale_private.h"
31 #include "namespace.h"
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static const char sccsid
[] = "@(#)strftime.c 5.4 (Berkeley) 3/14/89";
36 #endif /* LIBC_SCCS and not lint */
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD: src/lib/libc/stdtime/strftime.c,v 1.44 2009/06/09 09:02:58 delphij Exp $");
44 #include "un-namespace.h"
45 #include "timelocal.h"
48 static char * _add(const char *, char *, const char *);
49 static char * _conv(int, const char *, char *, const char *, locale_t
);
50 static char * _yconv(int, int, int, int, char *, const char *, locale_t
);
53 __private_extern__
char * _fmt(const char *, const struct tm
*, char *, const char *,
54 int *, struct lc_time_T
*, locale_t
);
56 extern char * tzname
[];
57 extern long __darwin_altzone
; /* DST timezone offset */
58 #define altzone __darwin_altzone
59 __private_extern__
long _st_get_timezone(void);
61 #ifndef YEAR_2000_NAME
62 #define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
63 #endif /* !defined YEAR_2000_NAME */
75 #ifndef BUILDING_VARIANT
76 static const char * const fmt_padding
[][4] = {
77 /* DEFAULT, LESS, SPACE, ZERO */
78 #define PAD_FMT_MONTHDAY 0
80 #define PAD_FMT_CENTURY 0
81 #define PAD_FMT_SHORTYEAR 0
82 #define PAD_FMT_MONTH 0
83 #define PAD_FMT_WEEKOFYEAR 0
84 #define PAD_FMT_DAYOFMONTH 0
85 { "%02d", "%d", "%2d", "%02d" },
86 #define PAD_FMT_SDAYOFMONTH 1
87 #define PAD_FMT_SHMS 1
88 { "%2d", "%d", "%2d", "%02d" },
89 #define PAD_FMT_DAYOFYEAR 2
90 { "%03d", "%d", "%3d", "%03d" },
91 #define PAD_FMT_YEAR 3
92 { "%04d", "%d", "%4d", "%04d" }
100 strftime_l(char * __restrict s
, size_t maxsize
, const char * __restrict format
,
101 const struct tm
* __restrict t
, locale_t loc
)
106 NORMALIZE_LOCALE(loc
);
109 p
= _fmt(((format
== NULL
) ? "%c" : format
), t
, s
, s
+ maxsize
, &warn
, __get_current_time_locale(loc
), loc
);
110 #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
111 if (warn
!= IN_NONE
&& getenv(YEAR_2000_NAME
) != NULL
) {
112 (void) fputs("\n", stderr
);
114 (void) fputs("NULL strftime format ", stderr
);
115 else (void) fprintf_l(stderr
, loc
, "strftime format \"%s\" ",
117 (void) fputs("yields only two digits of years in ", stderr
);
119 (void) fputs("some locales", stderr
);
120 else if (warn
== IN_THIS
)
121 (void) fputs("the current locale", stderr
);
122 else (void) fputs("all locales", stderr
);
123 (void) fputs("\n", stderr
);
125 #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
126 if (p
== s
+ maxsize
)
133 strftime(char * __restrict s
, size_t maxsize
, const char * __restrict format
,
134 const struct tm
* __restrict t
)
136 return strftime_l(s
, maxsize
, format
, t
, __current_locale());
139 #ifndef BUILDING_VARIANT
140 __private_extern__
char *
141 _fmt(format
, t
, pt
, ptlim
, warnp
, tptr
, loc
)
143 const struct tm
* const t
;
145 const char * const ptlim
;
147 struct lc_time_T
* tptr
;
150 int Ealternative
, Oalternative
, PadIndex
;
152 for ( ; *format
; ++format
) {
153 if (*format
== '%') {
156 PadIndex
= PAD_DEFAULT
;
163 pt
= _add((t
->tm_wday
< 0 ||
164 t
->tm_wday
>= DAYSPERWEEK
) ?
165 "?" : tptr
->weekday
[t
->tm_wday
],
169 pt
= _add((t
->tm_wday
< 0 ||
170 t
->tm_wday
>= DAYSPERWEEK
) ?
171 "?" : tptr
->wday
[t
->tm_wday
],
175 pt
= _add((t
->tm_mon
< 0 ||
176 t
->tm_mon
>= MONSPERYEAR
) ?
177 "?" : (Oalternative
? tptr
->alt_month
:
178 tptr
->month
)[t
->tm_mon
],
183 pt
= _add((t
->tm_mon
< 0 ||
184 t
->tm_mon
>= MONSPERYEAR
) ?
185 "?" : tptr
->mon
[t
->tm_mon
],
190 ** %C used to do a...
191 ** _fmt("%a %b %e %X %Y", t, tptr, loc);
192 ** ...whereas now POSIX 1003.2 calls for
193 ** something completely different.
196 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
, 1, 0,
203 pt
= _fmt(tptr
->c_fmt
, t
, pt
, ptlim
, &warn2
, tptr
, loc
);
211 pt
= _fmt("%m/%d/%y", t
, pt
, ptlim
, warnp
, tptr
, loc
);
214 pt
= _conv(t
->tm_mday
, fmt_padding
[PAD_FMT_DAYOFMONTH
][PadIndex
],
218 if (Ealternative
|| Oalternative
)
224 ** C99 locale modifiers.
226 ** %Ec %EC %Ex %EX %Ey %EY
227 ** %Od %oe %OH %OI %Om %OM
228 ** %OS %Ou %OU %OV %Ow %OW %Oy
229 ** are supposed to provide alternate
235 if (Ealternative
|| Oalternative
)
240 pt
= _conv(t
->tm_mday
,
241 fmt_padding
[PAD_FMT_SDAYOFMONTH
][PadIndex
], pt
, ptlim
, loc
);
244 pt
= _fmt("%Y-%m-%d", t
, pt
, ptlim
, warnp
, tptr
, loc
);
247 pt
= _conv(t
->tm_hour
, fmt_padding
[PAD_FMT_HMS
][PadIndex
],
251 pt
= _conv((t
->tm_hour
% 12) ?
252 (t
->tm_hour
% 12) : 12,
253 fmt_padding
[PAD_FMT_HMS
][PadIndex
], pt
, ptlim
, loc
);
256 pt
= _conv(t
->tm_yday
+ 1,
257 fmt_padding
[PAD_FMT_DAYOFYEAR
][PadIndex
], pt
, ptlim
, loc
);
261 ** This used to be...
262 ** _conv(t->tm_hour % 12 ?
263 ** t->tm_hour % 12 : 12, 2, ' ', loc);
264 ** ...and has been changed to the below to
265 ** match SunOS 4.1.1 and Arnold Robbins'
266 ** strftime version 3.0. That is, "%k" and
267 ** "%l" have been swapped.
270 pt
= _conv(t
->tm_hour
, fmt_padding
[PAD_FMT_SHMS
][PadIndex
],
276 ** After all this time, still unclaimed!
278 pt
= _add("kitchen sink", pt
, ptlim
);
280 #endif /* defined KITCHEN_SINK */
283 ** This used to be...
284 ** _conv(t->tm_hour, 2, ' ');
285 ** ...and has been changed to the below to
286 ** match SunOS 4.1.1 and Arnold Robbin's
287 ** strftime version 3.0. That is, "%k" and
288 ** "%l" have been swapped.
291 pt
= _conv((t
->tm_hour
% 12) ?
292 (t
->tm_hour
% 12) : 12,
293 fmt_padding
[PAD_FMT_SHMS
][PadIndex
], pt
, ptlim
, loc
);
296 pt
= _conv(t
->tm_min
, fmt_padding
[PAD_FMT_HMS
][PadIndex
],
300 pt
= _conv(t
->tm_mon
+ 1,
301 fmt_padding
[PAD_FMT_MONTH
][PadIndex
], pt
, ptlim
, loc
);
304 pt
= _add("\n", pt
, ptlim
);
307 pt
= _add((t
->tm_hour
>= (HOURSPERDAY
/ 2)) ?
313 pt
= _fmt("%H:%M", t
, pt
, ptlim
, warnp
, tptr
, loc
);
316 pt
= _fmt(tptr
->ampm_fmt
, t
, pt
, ptlim
,
320 pt
= _conv(t
->tm_sec
, fmt_padding
[PAD_FMT_HMS
][PadIndex
],
326 char buf
[INT_STRLEN_MAXIMUM(
332 if (TYPE_SIGNED(time_t))
333 (void) sprintf_l(buf
, loc
, "%ld",
335 else (void) sprintf_l(buf
, loc
, "%lu",
336 (unsigned long) mkt
);
337 pt
= _add(buf
, pt
, ptlim
);
341 pt
= _fmt("%H:%M:%S", t
, pt
, ptlim
, warnp
, tptr
, loc
);
344 pt
= _add("\t", pt
, ptlim
);
347 pt
= _conv((t
->tm_yday
+ DAYSPERWEEK
-
348 t
->tm_wday
) / DAYSPERWEEK
,
349 fmt_padding
[PAD_FMT_WEEKOFYEAR
][PadIndex
], pt
, ptlim
, loc
);
353 ** From Arnold Robbins' strftime version 3.0:
354 ** "ISO 8601: Weekday as a decimal number
358 pt
= _conv((t
->tm_wday
== 0) ?
359 DAYSPERWEEK
: t
->tm_wday
,
360 "%d", pt
, ptlim
, loc
);
362 case 'V': /* ISO 8601 week number */
363 case 'G': /* ISO 8601 year (four digits) */
364 case 'g': /* ISO 8601 year (two digits) */
366 ** From Arnold Robbins' strftime version 3.0: "the week number of the
367 ** year (the first Monday as the first day of week 1) as a decimal number
371 ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
372 ** "Week 01 of a year is per definition the first week which has the
373 ** Thursday in this year, which is equivalent to the week which contains
374 ** the fourth day of January. In other words, the first week of a new year
375 ** is the week which has the majority of its days in the new year. Week 01
376 ** might also contain days from the previous year and the week before week
377 ** 01 of a year is the last week (52 or 53) of the previous year even if
378 ** it contains days from the new year. A week starts with Monday (day 1)
379 ** and ends with Sunday (day 7). For example, the first week of the year
380 ** 1997 lasts from 1996-12-30 to 1997-01-05..."
399 len
= isleap_sum(year
, base
) ?
403 ** What yday (-3 ... 3) does
404 ** the ISO year begin on?
406 bot
= ((yday
+ 11 - wday
) %
409 ** What yday does the NEXT
410 ** ISO year begin on?
423 w
= 1 + ((yday
- bot
) /
428 yday
+= isleap_sum(year
, base
) ?
432 #ifdef XPG4_1994_04_09
434 t
->tm_mon
== TM_JANUARY
) ||
436 t
->tm_mon
== TM_DECEMBER
))
438 #endif /* defined XPG4_1994_04_09 */
440 pt
= _conv(w
, fmt_padding
[PAD_FMT_WEEKOFYEAR
][PadIndex
],
442 else if (*format
== 'g') {
444 pt
= _yconv(year
, base
, 0, 1,
446 } else pt
= _yconv(year
, base
, 1, 1,
452 ** From Arnold Robbins' strftime version 3.0:
453 ** "date as dd-bbb-YYYY"
456 pt
= _fmt("%e-%b-%Y", t
, pt
, ptlim
, warnp
, tptr
, loc
);
459 pt
= _conv((t
->tm_yday
+ DAYSPERWEEK
-
462 (DAYSPERWEEK
- 1))) / DAYSPERWEEK
,
463 fmt_padding
[PAD_FMT_WEEKOFYEAR
][PadIndex
], pt
, ptlim
, loc
);
466 pt
= _conv(t
->tm_wday
, "%d", pt
, ptlim
, loc
);
469 pt
= _fmt(tptr
->X_fmt
, t
, pt
, ptlim
, warnp
, tptr
, loc
);
475 pt
= _fmt(tptr
->x_fmt
, t
, pt
, ptlim
, &warn2
, tptr
, loc
);
484 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
, 0, 1,
488 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
, 1, 1,
493 if (t
->TM_ZONE
!= NULL
)
494 pt
= _add(t
->TM_ZONE
, pt
, ptlim
);
496 #endif /* defined TM_ZONE */
497 if (t
->tm_isdst
>= 0)
498 pt
= _add(tzname
[t
->tm_isdst
!= 0],
501 ** C99 says that %Z must be replaced by the
502 ** empty string if the time zone is not
513 #if defined(TM_GMTOFF) && !__DARWIN_UNIX03
515 #else /* !defined TM_GMTOFF || __DARWIN_UNIX03 */
517 ** C99 says that the UTC offset must
518 ** be computed by looking only at
519 ** tm_isdst. This requirement is
520 ** incorrect, since it means the code
521 ** must rely on magic (in this case
522 ** altzone and timezone), and the
523 ** magic might not have the correct
524 ** offset. Doing things correctly is
525 ** tricky and requires disobeying C99;
526 ** see GNU C strftime for details.
527 ** For now, punt and conform to the
528 ** standard, even though it's incorrect.
530 ** C99 says that %z must be replaced by the
531 ** empty string if the time zone is not
532 ** determinable, so output nothing if the
533 ** appropriate variables are not available.
535 if (t
->tm_isdst
== 0)
537 diff
= -_st_get_timezone();
538 #else /* !defined USG_COMPAT */
540 #endif /* !defined USG_COMPAT */
544 #else /* !defined ALTZONE */
546 #endif /* !defined ALTZONE */
547 #endif /* !defined TM_GMTOFF || __DARWIN_UNIX03 */
552 pt
= _add(sign
, pt
, ptlim
);
554 diff
= (diff
/ MINSPERHOUR
) * 100 +
555 (diff
% MINSPERHOUR
);
557 fmt_padding
[PAD_FMT_YEAR
][PadIndex
], pt
, ptlim
, loc
);
561 pt
= _fmt(tptr
->date_fmt
, t
, pt
, ptlim
,
565 if (PadIndex
!= PAD_DEFAULT
)
570 if (PadIndex
!= PAD_DEFAULT
)
572 PadIndex
= PAD_SPACE
;
575 if (PadIndex
!= PAD_DEFAULT
)
581 ** X311J/88-090 (4.12.3.5): if conversion char is
582 ** undefined, behavior is undefined. Print out the
583 ** character itself as printf(3) also does.
597 _conv(n
, format
, pt
, ptlim
, loc
)
599 const char * const format
;
601 const char * const ptlim
;
604 char buf
[INT_STRLEN_MAXIMUM(int) + 1];
606 (void) sprintf_l(buf
, loc
, format
, n
);
607 return _add(buf
, pt
, ptlim
);
614 const char * const ptlim
;
616 while (pt
< ptlim
&& (*pt
= *str
++) != '\0')
622 ** POSIX and the C Standard are unclear or inconsistent about
623 ** what %C and %y do if the year is negative or exceeds 9999.
624 ** Use the convention that %C concatenated with %y yields the
625 ** same output as %Y, and that %Y contains at least 4 bytes,
626 ** with more only if necessary.
630 _yconv(a
, b
, convert_top
, convert_yy
, pt
, ptlim
, loc
)
633 const int convert_top
;
634 const int convert_yy
;
636 const char * const ptlim
;
643 trail
= a
% DIVISOR
+ b
% DIVISOR
;
644 lead
= a
/ DIVISOR
+ b
/ DIVISOR
+ trail
/ DIVISOR
;
646 if (trail
< 0 && lead
> 0) {
649 } else if (lead
< 0 && trail
> 0) {
654 if (lead
== 0 && trail
< 0)
655 pt
= _add("-0", pt
, ptlim
);
656 else pt
= _conv(lead
, "%02d", pt
, ptlim
, loc
);
659 pt
= _conv(((trail
< 0) ? -trail
: trail
), "%02d", pt
, ptlim
, loc
);
662 #endif /* !BUILDING_VARIANT */
663 #pragma clang diagnostic pop