]>
git.saurik.com Git - apple/libc.git/blob - stdtime/FreeBSD/strftime.c
0f0e524fda2892a742d750efe06c3a45b8323f55
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.
20 static const char elsieid
[] = "@(#)strftime.3 8.3";
22 ** Based on the UCB version with the ID appearing below.
23 ** This is ANSIish only when "multibyte character == plain character".
25 #endif /* !defined NOID */
26 #endif /* !defined lint */
28 #include "xlocale_private.h"
30 #include "namespace.h"
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static const char sccsid
[] = "@(#)strftime.c 5.4 (Berkeley) 3/14/89";
35 #endif /* LIBC_SCCS and not lint */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: src/lib/libc/stdtime/strftime.c,v 1.44 2009/06/09 09:02:58 delphij Exp $");
43 #include "un-namespace.h"
44 #include "timelocal.h"
47 static char * _add(const char *, char *, const char *);
48 static char * _conv(int, const char *, char *, const char *, locale_t
);
49 static char * _yconv(int, int, int, int, char *, const char *, locale_t
);
52 __private_extern__
char * _fmt(const char *, const struct tm
*, char *, const char *,
53 int *, struct lc_time_T
*, locale_t
);
55 extern char * tzname
[];
56 __private_extern__
long __darwin_altzone
; /* DST timezone offset */
57 #define altzone __darwin_altzone
58 __private_extern__
long _st_get_timezone(void);
60 #ifndef YEAR_2000_NAME
61 #define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
62 #endif /* !defined YEAR_2000_NAME */
74 #ifndef BUILDING_VARIANT
75 static const char* fmt_padding
[][4] = {
76 /* DEFAULT, LESS, SPACE, ZERO */
77 #define PAD_FMT_MONTHDAY 0
79 #define PAD_FMT_CENTURY 0
80 #define PAD_FMT_SHORTYEAR 0
81 #define PAD_FMT_MONTH 0
82 #define PAD_FMT_WEEKOFYEAR 0
83 #define PAD_FMT_DAYOFMONTH 0
84 { "%02d", "%d", "%2d", "%02d" },
85 #define PAD_FMT_SDAYOFMONTH 1
86 #define PAD_FMT_SHMS 1
87 { "%2d", "%d", "%2d", "%02d" },
88 #define PAD_FMT_DAYOFYEAR 2
89 { "%03d", "%d", "%3d", "%03d" },
90 #define PAD_FMT_YEAR 3
91 { "%04d", "%d", "%4d", "%04d" }
99 strftime_l(char * __restrict s
, size_t maxsize
, const char * __restrict format
,
100 const struct tm
* __restrict t
, locale_t loc
)
105 NORMALIZE_LOCALE(loc
);
108 p
= _fmt(((format
== NULL
) ? "%c" : format
), t
, s
, s
+ maxsize
, &warn
, __get_current_time_locale(loc
), loc
);
109 #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
110 if (warn
!= IN_NONE
&& getenv(YEAR_2000_NAME
) != NULL
) {
111 (void) fputs("\n", stderr
);
113 (void) fputs("NULL strftime format ", stderr
);
114 else (void) fprintf_l(stderr
, loc
, "strftime format \"%s\" ",
116 (void) fputs("yields only two digits of years in ", stderr
);
118 (void) fputs("some locales", stderr
);
119 else if (warn
== IN_THIS
)
120 (void) fputs("the current locale", stderr
);
121 else (void) fputs("all locales", stderr
);
122 (void) fputs("\n", stderr
);
124 #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
125 if (p
== s
+ maxsize
)
132 strftime(char * __restrict s
, size_t maxsize
, const char * __restrict format
,
133 const struct tm
* __restrict t
)
135 return strftime_l(s
, maxsize
, format
, t
, __current_locale());
138 #ifndef BUILDING_VARIANT
139 __private_extern__
char *
140 _fmt(format
, t
, pt
, ptlim
, warnp
, tptr
, loc
)
142 const struct tm
* const t
;
144 const char * const ptlim
;
146 struct lc_time_T
* tptr
;
149 int Ealternative
, Oalternative
, PadIndex
;
151 for ( ; *format
; ++format
) {
152 if (*format
== '%') {
155 PadIndex
= PAD_DEFAULT
;
162 pt
= _add((t
->tm_wday
< 0 ||
163 t
->tm_wday
>= DAYSPERWEEK
) ?
164 "?" : tptr
->weekday
[t
->tm_wday
],
168 pt
= _add((t
->tm_wday
< 0 ||
169 t
->tm_wday
>= DAYSPERWEEK
) ?
170 "?" : tptr
->wday
[t
->tm_wday
],
174 pt
= _add((t
->tm_mon
< 0 ||
175 t
->tm_mon
>= MONSPERYEAR
) ?
176 "?" : (Oalternative
? tptr
->alt_month
:
177 tptr
->month
)[t
->tm_mon
],
182 pt
= _add((t
->tm_mon
< 0 ||
183 t
->tm_mon
>= MONSPERYEAR
) ?
184 "?" : tptr
->mon
[t
->tm_mon
],
189 ** %C used to do a...
190 ** _fmt("%a %b %e %X %Y", t, tptr, loc);
191 ** ...whereas now POSIX 1003.2 calls for
192 ** something completely different.
195 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
, 1, 0,
202 pt
= _fmt(tptr
->c_fmt
, t
, pt
, ptlim
, &warn2
, tptr
, loc
);
210 pt
= _fmt("%m/%d/%y", t
, pt
, ptlim
, warnp
, tptr
, loc
);
213 pt
= _conv(t
->tm_mday
, fmt_padding
[PAD_FMT_DAYOFMONTH
][PadIndex
],
217 if (Ealternative
|| Oalternative
)
223 ** C99 locale modifiers.
225 ** %Ec %EC %Ex %EX %Ey %EY
226 ** %Od %oe %OH %OI %Om %OM
227 ** %OS %Ou %OU %OV %Ow %OW %Oy
228 ** are supposed to provide alternate
234 if (Ealternative
|| Oalternative
)
239 pt
= _conv(t
->tm_mday
,
240 fmt_padding
[PAD_FMT_SDAYOFMONTH
][PadIndex
], pt
, ptlim
, loc
);
243 pt
= _fmt("%Y-%m-%d", t
, pt
, ptlim
, warnp
, tptr
, loc
);
246 pt
= _conv(t
->tm_hour
, fmt_padding
[PAD_FMT_HMS
][PadIndex
],
250 pt
= _conv((t
->tm_hour
% 12) ?
251 (t
->tm_hour
% 12) : 12,
252 fmt_padding
[PAD_FMT_HMS
][PadIndex
], pt
, ptlim
, loc
);
255 pt
= _conv(t
->tm_yday
+ 1,
256 fmt_padding
[PAD_FMT_DAYOFYEAR
][PadIndex
], pt
, ptlim
, loc
);
260 ** This used to be...
261 ** _conv(t->tm_hour % 12 ?
262 ** t->tm_hour % 12 : 12, 2, ' ', loc);
263 ** ...and has been changed to the below to
264 ** match SunOS 4.1.1 and Arnold Robbins'
265 ** strftime version 3.0. That is, "%k" and
266 ** "%l" have been swapped.
269 pt
= _conv(t
->tm_hour
, fmt_padding
[PAD_FMT_SHMS
][PadIndex
],
275 ** After all this time, still unclaimed!
277 pt
= _add("kitchen sink", pt
, ptlim
);
279 #endif /* defined KITCHEN_SINK */
282 ** This used to be...
283 ** _conv(t->tm_hour, 2, ' ');
284 ** ...and has been changed to the below to
285 ** match SunOS 4.1.1 and Arnold Robbin's
286 ** strftime version 3.0. That is, "%k" and
287 ** "%l" have been swapped.
290 pt
= _conv((t
->tm_hour
% 12) ?
291 (t
->tm_hour
% 12) : 12,
292 fmt_padding
[PAD_FMT_SHMS
][PadIndex
], pt
, ptlim
, loc
);
295 pt
= _conv(t
->tm_min
, fmt_padding
[PAD_FMT_HMS
][PadIndex
],
299 pt
= _conv(t
->tm_mon
+ 1,
300 fmt_padding
[PAD_FMT_MONTH
][PadIndex
], pt
, ptlim
, loc
);
303 pt
= _add("\n", pt
, ptlim
);
306 pt
= _add((t
->tm_hour
>= (HOURSPERDAY
/ 2)) ?
312 pt
= _fmt("%H:%M", t
, pt
, ptlim
, warnp
, tptr
, loc
);
315 pt
= _fmt(tptr
->ampm_fmt
, t
, pt
, ptlim
,
319 pt
= _conv(t
->tm_sec
, fmt_padding
[PAD_FMT_HMS
][PadIndex
],
325 char buf
[INT_STRLEN_MAXIMUM(
331 if (TYPE_SIGNED(time_t))
332 (void) sprintf_l(buf
, loc
, "%ld",
334 else (void) sprintf_l(buf
, loc
, "%lu",
335 (unsigned long) mkt
);
336 pt
= _add(buf
, pt
, ptlim
);
340 pt
= _fmt("%H:%M:%S", t
, pt
, ptlim
, warnp
, tptr
, loc
);
343 pt
= _add("\t", pt
, ptlim
);
346 pt
= _conv((t
->tm_yday
+ DAYSPERWEEK
-
347 t
->tm_wday
) / DAYSPERWEEK
,
348 fmt_padding
[PAD_FMT_WEEKOFYEAR
][PadIndex
], pt
, ptlim
, loc
);
352 ** From Arnold Robbins' strftime version 3.0:
353 ** "ISO 8601: Weekday as a decimal number
357 pt
= _conv((t
->tm_wday
== 0) ?
358 DAYSPERWEEK
: t
->tm_wday
,
359 "%d", pt
, ptlim
, loc
);
361 case 'V': /* ISO 8601 week number */
362 case 'G': /* ISO 8601 year (four digits) */
363 case 'g': /* ISO 8601 year (two digits) */
365 ** From Arnold Robbins' strftime version 3.0: "the week number of the
366 ** year (the first Monday as the first day of week 1) as a decimal number
370 ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
371 ** "Week 01 of a year is per definition the first week which has the
372 ** Thursday in this year, which is equivalent to the week which contains
373 ** the fourth day of January. In other words, the first week of a new year
374 ** is the week which has the majority of its days in the new year. Week 01
375 ** might also contain days from the previous year and the week before week
376 ** 01 of a year is the last week (52 or 53) of the previous year even if
377 ** it contains days from the new year. A week starts with Monday (day 1)
378 ** and ends with Sunday (day 7). For example, the first week of the year
379 ** 1997 lasts from 1996-12-30 to 1997-01-05..."
398 len
= isleap_sum(year
, base
) ?
402 ** What yday (-3 ... 3) does
403 ** the ISO year begin on?
405 bot
= ((yday
+ 11 - wday
) %
408 ** What yday does the NEXT
409 ** ISO year begin on?
422 w
= 1 + ((yday
- bot
) /
427 yday
+= isleap_sum(year
, base
) ?
431 #ifdef XPG4_1994_04_09
433 t
->tm_mon
== TM_JANUARY
) ||
435 t
->tm_mon
== TM_DECEMBER
))
437 #endif /* defined XPG4_1994_04_09 */
439 pt
= _conv(w
, fmt_padding
[PAD_FMT_WEEKOFYEAR
][PadIndex
],
441 else if (*format
== 'g') {
443 pt
= _yconv(year
, base
, 0, 1,
445 } else pt
= _yconv(year
, base
, 1, 1,
451 ** From Arnold Robbins' strftime version 3.0:
452 ** "date as dd-bbb-YYYY"
455 pt
= _fmt("%e-%b-%Y", t
, pt
, ptlim
, warnp
, tptr
, loc
);
458 pt
= _conv((t
->tm_yday
+ DAYSPERWEEK
-
461 (DAYSPERWEEK
- 1))) / DAYSPERWEEK
,
462 fmt_padding
[PAD_FMT_WEEKOFYEAR
][PadIndex
], pt
, ptlim
, loc
);
465 pt
= _conv(t
->tm_wday
, "%d", pt
, ptlim
, loc
);
468 pt
= _fmt(tptr
->X_fmt
, t
, pt
, ptlim
, warnp
, tptr
, loc
);
474 pt
= _fmt(tptr
->x_fmt
, t
, pt
, ptlim
, &warn2
, tptr
, loc
);
483 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
, 0, 1,
487 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
, 1, 1,
492 if (t
->TM_ZONE
!= NULL
)
493 pt
= _add(t
->TM_ZONE
, pt
, ptlim
);
495 #endif /* defined TM_ZONE */
496 if (t
->tm_isdst
>= 0)
497 pt
= _add(tzname
[t
->tm_isdst
!= 0],
500 ** C99 says that %Z must be replaced by the
501 ** empty string if the time zone is not
512 #if defined(TM_GMTOFF) && !__DARWIN_UNIX03
514 #else /* !defined TM_GMTOFF || __DARWIN_UNIX03 */
516 ** C99 says that the UTC offset must
517 ** be computed by looking only at
518 ** tm_isdst. This requirement is
519 ** incorrect, since it means the code
520 ** must rely on magic (in this case
521 ** altzone and timezone), and the
522 ** magic might not have the correct
523 ** offset. Doing things correctly is
524 ** tricky and requires disobeying C99;
525 ** see GNU C strftime for details.
526 ** For now, punt and conform to the
527 ** standard, even though it's incorrect.
529 ** C99 says that %z must be replaced by the
530 ** empty string if the time zone is not
531 ** determinable, so output nothing if the
532 ** appropriate variables are not available.
534 if (t
->tm_isdst
== 0)
536 diff
= -_st_get_timezone();
537 #else /* !defined USG_COMPAT */
539 #endif /* !defined USG_COMPAT */
543 #else /* !defined ALTZONE */
545 #endif /* !defined ALTZONE */
546 #endif /* !defined TM_GMTOFF || __DARWIN_UNIX03 */
551 pt
= _add(sign
, pt
, ptlim
);
553 diff
= (diff
/ MINSPERHOUR
) * 100 +
554 (diff
% MINSPERHOUR
);
556 fmt_padding
[PAD_FMT_YEAR
][PadIndex
], pt
, ptlim
, loc
);
560 pt
= _fmt(tptr
->date_fmt
, t
, pt
, ptlim
,
564 if (PadIndex
!= PAD_DEFAULT
)
569 if (PadIndex
!= PAD_DEFAULT
)
571 PadIndex
= PAD_SPACE
;
574 if (PadIndex
!= PAD_DEFAULT
)
580 ** X311J/88-090 (4.12.3.5): if conversion char is
581 ** undefined, behavior is undefined. Print out the
582 ** character itself as printf(3) also does.
596 _conv(n
, format
, pt
, ptlim
, loc
)
598 const char * const format
;
600 const char * const ptlim
;
603 char buf
[INT_STRLEN_MAXIMUM(int) + 1];
605 (void) sprintf_l(buf
, loc
, format
, n
);
606 return _add(buf
, pt
, ptlim
);
613 const char * const ptlim
;
615 while (pt
< ptlim
&& (*pt
= *str
++) != '\0')
621 ** POSIX and the C Standard are unclear or inconsistent about
622 ** what %C and %y do if the year is negative or exceeds 9999.
623 ** Use the convention that %C concatenated with %y yields the
624 ** same output as %Y, and that %Y contains at least 4 bytes,
625 ** with more only if necessary.
629 _yconv(a
, b
, convert_top
, convert_yy
, pt
, ptlim
, loc
)
632 const int convert_top
;
633 const int convert_yy
;
635 const char * const ptlim
;
642 trail
= a
% DIVISOR
+ b
% DIVISOR
;
643 lead
= a
/ DIVISOR
+ b
/ DIVISOR
+ trail
/ DIVISOR
;
645 if (trail
< 0 && lead
> 0) {
648 } else if (lead
< 0 && trail
> 0) {
653 if (lead
== 0 && trail
< 0)
654 pt
= _add("-0", pt
, ptlim
);
655 else pt
= _conv(lead
, "%02d", pt
, ptlim
, loc
);
658 pt
= _conv(((trail
< 0) ? -trail
: trail
), "%02d", pt
, ptlim
, loc
);
661 #endif /* !BUILDING_VARIANT */