]>
git.saurik.com Git - apple/libc.git/blob - stdtime/strftime-fbsd.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.
20 static const char elsieid
[] = "@(#)strftime.c 7.64";
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.40 2004/06/14 10:31:52 stefanf 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 #endif /* !BUILDING_VARIANT */
51 __private_extern__
char *_fmt(const char *, const struct tm
*, char *, const char *, int *, struct lc_time_T
*, locale_t
);
53 size_t strftime(char * __restrict
, size_t, const char * __restrict
,
54 const struct tm
* __restrict
);
56 extern char * tzname
[];
57 __private_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 */
72 strftime_l(char * __restrict s
, size_t maxsize
, const char * __restrict format
,
73 const struct tm
* __restrict t
, locale_t loc
)
79 #endif /* __DARWIN_UNIX03 */
81 NORMALIZE_LOCALE(loc
);
85 if (t
->tm_isdst
>= 0) {
87 t2
.tm_gmtoff
= t
->tm_isdst
? -__darwin_altzone
: -_st_get_timezone();
90 #endif /* __DARWIN_UNIX03 */
91 p
= _fmt(((format
== NULL
) ? "%c" : format
), t
, s
, s
+ maxsize
, &warn
, __get_current_time_locale(loc
), loc
);
92 #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
93 if (warn
!= IN_NONE
&& getenv(YEAR_2000_NAME
) != NULL
) {
94 (void) fputs("\n", stderr
);
96 (void) fputs("NULL strftime format ", stderr
);
97 else (void) fprintf_l(stderr
, loc
, "strftime format \"%s\" ",
99 (void) fputs("yields only two digits of years in ", stderr
);
101 (void) fputs("some locales", stderr
);
102 else if (warn
== IN_THIS
)
103 (void) fputs("the current locale", stderr
);
104 else (void) fputs("all locales", stderr
);
105 (void) fputs("\n", stderr
);
107 #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
108 if (p
== s
+ maxsize
)
115 strftime(char * __restrict s
, size_t maxsize
, const char * __restrict format
,
116 const struct tm
* __restrict t
)
118 return strftime_l(s
, maxsize
, format
, t
, __current_locale());
121 #ifndef BUILDING_VARIANT
122 __private_extern__
char *
123 _fmt(format
, t
, pt
, ptlim
, warnp
, tptr
, loc
)
125 const struct tm
* const t
;
127 const char * const ptlim
;
129 struct lc_time_T
* tptr
;
132 int Ealternative
, Oalternative
;
134 for ( ; *format
; ++format
) {
135 if (*format
== '%') {
144 pt
= _add((t
->tm_wday
< 0 ||
145 t
->tm_wday
>= DAYSPERWEEK
) ?
146 "?" : tptr
->weekday
[t
->tm_wday
],
150 pt
= _add((t
->tm_wday
< 0 ||
151 t
->tm_wday
>= DAYSPERWEEK
) ?
152 "?" : tptr
->wday
[t
->tm_wday
],
156 pt
= _add((t
->tm_mon
< 0 ||
157 t
->tm_mon
>= MONSPERYEAR
) ?
158 "?" : (Oalternative
? tptr
->alt_month
:
159 tptr
->month
)[t
->tm_mon
],
164 pt
= _add((t
->tm_mon
< 0 ||
165 t
->tm_mon
>= MONSPERYEAR
) ?
166 "?" : tptr
->mon
[t
->tm_mon
],
171 ** %C used to do a...
172 ** _fmt("%a %b %e %X %Y", t);
173 ** ...whereas now POSIX 1003.2 calls for
174 ** something completely different.
177 pt
= _conv((t
->tm_year
+ TM_YEAR_BASE
) / 100,
178 "%02d", pt
, ptlim
, loc
);
184 pt
= _fmt(tptr
->c_fmt
, t
, pt
, ptlim
, warnp
, tptr
, loc
);
192 pt
= _fmt("%m/%d/%y", t
, pt
, ptlim
, warnp
, tptr
, loc
);
195 pt
= _conv(t
->tm_mday
, "%02d", pt
, ptlim
, loc
);
198 if (Ealternative
|| Oalternative
)
204 ** C99 locale modifiers.
206 ** %Ec %EC %Ex %EX %Ey %EY
207 ** %Od %oe %OH %OI %Om %OM
208 ** %OS %Ou %OU %OV %Ow %OW %Oy
209 ** are supposed to provide alternate
215 if (Ealternative
|| Oalternative
)
220 pt
= _conv(t
->tm_mday
, "%2d", pt
, ptlim
, loc
);
223 pt
= _fmt("%Y-%m-%d", t
, pt
, ptlim
, warnp
, tptr
, loc
);
226 pt
= _conv(t
->tm_hour
, "%02d", pt
, ptlim
, loc
);
229 pt
= _conv((t
->tm_hour
% 12) ?
230 (t
->tm_hour
% 12) : 12,
231 "%02d", pt
, ptlim
, loc
);
234 pt
= _conv(t
->tm_yday
+ 1, "%03d", pt
, ptlim
, loc
);
238 ** This used to be...
239 ** _conv(t->tm_hour % 12 ?
240 ** t->tm_hour % 12 : 12, 2, ' ');
241 ** ...and has been changed to the below to
242 ** match SunOS 4.1.1 and Arnold Robbins'
243 ** strftime version 3.0. That is, "%k" and
244 ** "%l" have been swapped.
247 pt
= _conv(t
->tm_hour
, "%2d", pt
, ptlim
, loc
);
252 ** After all this time, still unclaimed!
254 pt
= _add("kitchen sink", pt
, ptlim
);
256 #endif /* defined KITCHEN_SINK */
259 ** This used to be...
260 ** _conv(t->tm_hour, 2, ' ');
261 ** ...and has been changed to the below to
262 ** match SunOS 4.1.1 and Arnold Robbin's
263 ** strftime version 3.0. That is, "%k" and
264 ** "%l" have been swapped.
267 pt
= _conv((t
->tm_hour
% 12) ?
268 (t
->tm_hour
% 12) : 12,
269 "%2d", pt
, ptlim
, loc
);
272 pt
= _conv(t
->tm_min
, "%02d", pt
, ptlim
, loc
);
275 pt
= _conv(t
->tm_mon
+ 1, "%02d", pt
, ptlim
, loc
);
278 pt
= _add("\n", pt
, ptlim
);
281 pt
= _add((t
->tm_hour
>= (HOURSPERDAY
/ 2)) ?
287 pt
= _fmt("%H:%M", t
, pt
, ptlim
, warnp
, tptr
, loc
);
290 pt
= _fmt(tptr
->ampm_fmt
, t
, pt
, ptlim
,
294 pt
= _conv(t
->tm_sec
, "%02d", pt
, ptlim
, loc
);
299 char buf
[INT_STRLEN_MAXIMUM(
305 if (TYPE_SIGNED(time_t))
306 (void) sprintf_l(buf
, loc
, "%ld",
308 else (void) sprintf_l(buf
, loc
, "%lu",
309 (unsigned long) mkt
);
310 pt
= _add(buf
, pt
, ptlim
);
314 pt
= _fmt("%H:%M:%S", t
, pt
, ptlim
, warnp
, tptr
, loc
);
317 pt
= _add("\t", pt
, ptlim
);
320 pt
= _conv((t
->tm_yday
+ DAYSPERWEEK
-
321 t
->tm_wday
) / DAYSPERWEEK
,
322 "%02d", pt
, ptlim
, loc
);
326 ** From Arnold Robbins' strftime version 3.0:
327 ** "ISO 8601: Weekday as a decimal number
331 pt
= _conv((t
->tm_wday
== 0) ?
332 DAYSPERWEEK
: t
->tm_wday
,
333 "%d", pt
, ptlim
, loc
);
335 case 'V': /* ISO 8601 week number */
336 case 'G': /* ISO 8601 year (four digits) */
337 case 'g': /* ISO 8601 year (two digits) */
339 ** From Arnold Robbins' strftime version 3.0: "the week number of the
340 ** year (the first Monday as the first day of week 1) as a decimal number
344 ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
345 ** "Week 01 of a year is per definition the first week which has the
346 ** Thursday in this year, which is equivalent to the week which contains
347 ** the fourth day of January. In other words, the first week of a new year
348 ** is the week which has the majority of its days in the new year. Week 01
349 ** might also contain days from the previous year and the week before week
350 ** 01 of a year is the last week (52 or 53) of the previous year even if
351 ** it contains days from the new year. A week starts with Monday (day 1)
352 ** and ends with Sunday (day 7). For example, the first week of the year
353 ** 1997 lasts from 1996-12-30 to 1997-01-05..."
362 year
= t
->tm_year
+ TM_YEAR_BASE
;
374 ** What yday (-3 ... 3) does
375 ** the ISO year begin on?
377 bot
= ((yday
+ 11 - wday
) %
380 ** What yday does the NEXT
381 ** ISO year begin on?
394 w
= 1 + ((yday
- bot
) /
399 yday
+= isleap(year
) ?
403 #ifdef XPG4_1994_04_09
405 && t
->tm_mon
== TM_JANUARY
)
407 && t
->tm_mon
== TM_DECEMBER
))
409 #endif /* defined XPG4_1994_04_09 */
411 pt
= _conv(w
, "%02d",
413 else if (*format
== 'g') {
415 pt
= _conv(year
% 100, "%02d",
417 } else pt
= _conv(year
, "%04d",
423 ** From Arnold Robbins' strftime version 3.0:
424 ** "date as dd-bbb-YYYY"
427 pt
= _fmt("%e-%b-%Y", t
, pt
, ptlim
, warnp
, tptr
, loc
);
430 pt
= _conv((t
->tm_yday
+ DAYSPERWEEK
-
433 (DAYSPERWEEK
- 1))) / DAYSPERWEEK
,
434 "%02d", pt
, ptlim
, loc
);
437 pt
= _conv(t
->tm_wday
, "%d", pt
, ptlim
, loc
);
440 pt
= _fmt(tptr
->X_fmt
, t
, pt
, ptlim
, warnp
, tptr
, loc
);
446 pt
= _fmt(tptr
->x_fmt
, t
, pt
, ptlim
, &warn2
, tptr
, loc
);
455 pt
= _conv((t
->tm_year
+ TM_YEAR_BASE
) % 100,
456 "%02d", pt
, ptlim
, loc
);
459 pt
= _conv(t
->tm_year
+ TM_YEAR_BASE
, "%04d",
464 if (t
->TM_ZONE
!= NULL
)
465 pt
= _add(t
->TM_ZONE
, pt
, ptlim
);
467 #endif /* defined TM_ZONE */
468 if (t
->tm_isdst
>= 0)
469 pt
= _add(tzname
[t
->tm_isdst
!= 0],
472 ** C99 says that %Z must be replaced by the
473 ** empty string if the time zone is not
486 #else /* !defined TM_GMTOFF */
488 ** C99 says that the UTC offset must
489 ** be computed by looking only at
490 ** tm_isdst. This requirement is
491 ** incorrect, since it means the code
492 ** must rely on magic (in this case
493 ** altzone and timezone), and the
494 ** magic might not have the correct
495 ** offset. Doing things correctly is
496 ** tricky and requires disobeying C99;
497 ** see GNU C strftime for details.
498 ** For now, punt and conform to the
499 ** standard, even though it's incorrect.
501 ** C99 says that %z must be replaced by the
502 ** empty string if the time zone is not
503 ** determinable, so output nothing if the
504 ** appropriate variables are not available.
506 if (t
->tm_isdst
== 0)
508 diff
= -_st_get_timezone();
509 #else /* !defined USG_COMPAT */
511 #endif /* !defined USG_COMPAT */
515 #else /* !defined ALTZONE */
517 #endif /* !defined ALTZONE */
518 #endif /* !defined TM_GMTOFF */
523 pt
= _add(sign
, pt
, ptlim
);
525 pt
= _conv((diff
/60)*100 + diff%60
,
526 "%04d", pt
, ptlim
, loc
);
530 pt
= _fmt(tptr
->date_fmt
, t
, pt
, ptlim
,
535 ** X311J/88-090 (4.12.3.5): if conversion char is
536 ** undefined, behavior is undefined. Print out the
537 ** character itself as printf(3) also does.
551 _conv(n
, format
, pt
, ptlim
, loc
)
553 const char * const format
;
555 const char * const ptlim
;
558 char buf
[INT_STRLEN_MAXIMUM(int) + 1];
560 (void) sprintf_l(buf
, loc
, format
, n
);
561 return _add(buf
, pt
, ptlim
);
568 const char * const ptlim
;
570 while (pt
< ptlim
&& (*pt
= *str
++) != '\0')
574 #endif /* !BUILDING_VARIANT */