]>
git.saurik.com Git - apple/libc.git/blob - stdtime/FreeBSD/strftime.c
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.
19 static const char elsieid
[] = "@(#)strftime.3 8.3";
21 ** Based on the UCB version with the ID appearing below.
22 ** This is ANSIish only when "multibyte character == plain character".
26 #include "xlocale_private.h"
28 #include "namespace.h"
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 $");
41 #include "un-namespace.h"
42 #include "timelocal.h"
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
);
50 __private_extern__
char * _fmt(const char *, const struct tm
*, char *, const char *,
51 int *, struct lc_time_T
*, locale_t
);
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);
58 #ifndef YEAR_2000_NAME
59 #define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
60 #endif /* !defined YEAR_2000_NAME */
72 #ifndef BUILDING_VARIANT
73 static const char * const fmt_padding
[][4] = {
74 /* DEFAULT, LESS, SPACE, ZERO */
75 #define PAD_FMT_MONTHDAY 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" }
97 strftime_l(char * __restrict s
, size_t maxsize
, const char * __restrict format
,
98 const struct tm
* __restrict t
, locale_t loc
)
103 NORMALIZE_LOCALE(loc
);
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
);
111 (void) fputs("NULL strftime format ", stderr
);
112 else (void) fprintf_l(stderr
, loc
, "strftime format \"%s\" ",
114 (void) fputs("yields only two digits of years in ", stderr
);
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
);
122 #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
123 if (p
== s
+ maxsize
)
130 strftime(char * __restrict s
, size_t maxsize
, const char * __restrict format
,
131 const struct tm
* __restrict t
)
133 return strftime_l(s
, maxsize
, format
, t
, __current_locale());
136 #ifndef BUILDING_VARIANT
137 __private_extern__
char *
138 _fmt(format
, t
, pt
, ptlim
, warnp
, tptr
, loc
)
140 const struct tm
* const t
;
142 const char * const ptlim
;
144 struct lc_time_T
* tptr
;
147 int Ealternative
, Oalternative
, PadIndex
;
149 for ( ; *format
; ++format
) {
150 if (*format
== '%') {
153 PadIndex
= PAD_DEFAULT
;
160 pt
= _add((t
->tm_wday
< 0 ||
161 t
->tm_wday
>= DAYSPERWEEK
) ?
162 "?" : tptr
->weekday
[t
->tm_wday
],
166 pt
= _add((t
->tm_wday
< 0 ||
167 t
->tm_wday
>= DAYSPERWEEK
) ?
168 "?" : tptr
->wday
[t
->tm_wday
],
172 pt
= _add((t
->tm_mon
< 0 ||
173 t
->tm_mon
>= MONSPERYEAR
) ?
174 "?" : (Oalternative
? tptr
->alt_month
:
175 tptr
->month
)[t
->tm_mon
],
180 pt
= _add((t
->tm_mon
< 0 ||
181 t
->tm_mon
>= MONSPERYEAR
) ?
182 "?" : tptr
->mon
[t
->tm_mon
],
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.
193 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
, 1, 0,
200 pt
= _fmt(tptr
->c_fmt
, t
, pt
, ptlim
, &warn2
, tptr
, loc
);
208 pt
= _fmt("%m/%d/%y", t
, pt
, ptlim
, warnp
, tptr
, loc
);
211 pt
= _conv(t
->tm_mday
, fmt_padding
[PAD_FMT_DAYOFMONTH
][PadIndex
],
215 if (Ealternative
|| Oalternative
)
221 ** C99 locale modifiers.
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
232 if (Ealternative
|| Oalternative
)
237 pt
= _conv(t
->tm_mday
,
238 fmt_padding
[PAD_FMT_SDAYOFMONTH
][PadIndex
], pt
, ptlim
, loc
);
241 pt
= _fmt("%Y-%m-%d", t
, pt
, ptlim
, warnp
, tptr
, loc
);
244 pt
= _conv(t
->tm_hour
, fmt_padding
[PAD_FMT_HMS
][PadIndex
],
248 pt
= _conv((t
->tm_hour
% 12) ?
249 (t
->tm_hour
% 12) : 12,
250 fmt_padding
[PAD_FMT_HMS
][PadIndex
], pt
, ptlim
, loc
);
253 pt
= _conv(t
->tm_yday
+ 1,
254 fmt_padding
[PAD_FMT_DAYOFYEAR
][PadIndex
], pt
, ptlim
, loc
);
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.
267 pt
= _conv(t
->tm_hour
, fmt_padding
[PAD_FMT_SHMS
][PadIndex
],
273 ** After all this time, still unclaimed!
275 pt
= _add("kitchen sink", pt
, ptlim
);
277 #endif /* defined KITCHEN_SINK */
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.
288 pt
= _conv((t
->tm_hour
% 12) ?
289 (t
->tm_hour
% 12) : 12,
290 fmt_padding
[PAD_FMT_SHMS
][PadIndex
], pt
, ptlim
, loc
);
293 pt
= _conv(t
->tm_min
, fmt_padding
[PAD_FMT_HMS
][PadIndex
],
297 pt
= _conv(t
->tm_mon
+ 1,
298 fmt_padding
[PAD_FMT_MONTH
][PadIndex
], pt
, ptlim
, loc
);
301 pt
= _add("\n", pt
, ptlim
);
304 pt
= _add((t
->tm_hour
>= (HOURSPERDAY
/ 2)) ?
310 pt
= _fmt("%H:%M", t
, pt
, ptlim
, warnp
, tptr
, loc
);
313 pt
= _fmt(tptr
->ampm_fmt
, t
, pt
, ptlim
,
317 pt
= _conv(t
->tm_sec
, fmt_padding
[PAD_FMT_HMS
][PadIndex
],
323 char buf
[INT_STRLEN_MAXIMUM(
329 if (TYPE_SIGNED(time_t))
330 (void) sprintf_l(buf
, loc
, "%ld",
332 else (void) sprintf_l(buf
, loc
, "%lu",
333 (unsigned long) mkt
);
334 pt
= _add(buf
, pt
, ptlim
);
338 pt
= _fmt("%H:%M:%S", t
, pt
, ptlim
, warnp
, tptr
, loc
);
341 pt
= _add("\t", pt
, ptlim
);
344 pt
= _conv((t
->tm_yday
+ DAYSPERWEEK
-
345 t
->tm_wday
) / DAYSPERWEEK
,
346 fmt_padding
[PAD_FMT_WEEKOFYEAR
][PadIndex
], pt
, ptlim
, loc
);
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
,
357 "%d", pt
, ptlim
, loc
);
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
, fmt_padding
[PAD_FMT_WEEKOFYEAR
][PadIndex
],
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
, tptr
, loc
);
456 pt
= _conv((t
->tm_yday
+ DAYSPERWEEK
-
459 (DAYSPERWEEK
- 1))) / DAYSPERWEEK
,
460 fmt_padding
[PAD_FMT_WEEKOFYEAR
][PadIndex
], pt
, ptlim
, loc
);
463 pt
= _conv(t
->tm_wday
, "%d", pt
, ptlim
, loc
);
466 pt
= _fmt(tptr
->X_fmt
, t
, pt
, ptlim
, warnp
, tptr
, loc
);
472 pt
= _fmt(tptr
->x_fmt
, t
, pt
, ptlim
, &warn2
, tptr
, loc
);
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
510 #if defined(TM_GMTOFF) && !__DARWIN_UNIX03
512 #else /* !defined TM_GMTOFF || __DARWIN_UNIX03 */
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)
534 diff
= -_st_get_timezone();
535 #else /* !defined USG_COMPAT */
537 #endif /* !defined USG_COMPAT */
541 #else /* !defined ALTZONE */
543 #endif /* !defined ALTZONE */
544 #endif /* !defined TM_GMTOFF || __DARWIN_UNIX03 */
549 pt
= _add(sign
, pt
, ptlim
);
551 diff
= (diff
/ MINSPERHOUR
) * 100 +
552 (diff
% MINSPERHOUR
);
554 fmt_padding
[PAD_FMT_YEAR
][PadIndex
], pt
, ptlim
, loc
);
558 pt
= _fmt(tptr
->date_fmt
, t
, pt
, ptlim
,
562 if (PadIndex
!= PAD_DEFAULT
)
567 if (PadIndex
!= PAD_DEFAULT
)
569 PadIndex
= PAD_SPACE
;
572 if (PadIndex
!= PAD_DEFAULT
)
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.
594 _conv(n
, format
, pt
, ptlim
, loc
)
596 const char * const format
;
598 const char * const ptlim
;
601 char buf
[INT_STRLEN_MAXIMUM(int) + 1];
603 (void) sprintf_l(buf
, loc
, format
, n
);
604 return _add(buf
, pt
, ptlim
);
611 const char * const ptlim
;
613 while (pt
< ptlim
&& (*pt
= *str
++) != '\0')
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.
627 _yconv(a
, b
, convert_top
, convert_yy
, pt
, ptlim
, loc
)
630 const int convert_top
;
631 const int convert_yy
;
633 const char * const ptlim
;
640 trail
= a
% DIVISOR
+ b
% DIVISOR
;
641 lead
= a
/ DIVISOR
+ b
/ DIVISOR
+ trail
/ DIVISOR
;
643 if (trail
< 0 && lead
> 0) {
646 } else if (lead
< 0 && trail
> 0) {
651 if (lead
== 0 && trail
< 0)
652 pt
= _add("-0", pt
, ptlim
);
653 else pt
= _conv(lead
, "%02d", pt
, ptlim
, loc
);
656 pt
= _conv(((trail
< 0) ? -trail
: trail
), "%02d", pt
, ptlim
, loc
);
659 #endif /* !BUILDING_VARIANT */