1 /* NOTE: This file defines both strftime() and wcsftime(). Take care when
2 * making changes. See also wcsftime.c, and note the (small) overlap in the
3 * manual description, taking care to edit both as needed. */
6 * Original Author: G. Haley
7 * Additions from: Eric Blake, Corinna Vinschen
8 * Changes to allow dual use as wcstime, also: Craig Howland
10 * Places characters into the array pointed to by s as controlled by the string
11 * pointed to by format. If the total number of resulting characters including
12 * the terminating null character is not more than maxsize, returns the number
13 * of characters placed into the array pointed to by s (not including the
14 * terminating null character); otherwise zero is returned and the contents of
15 * the array indeterminate.
20 <<strftime>>, <<strftime_l>>---convert date and time to a formatted string
30 size_t strftime(char *restrict <[s]>, size_t <[maxsize]>,
31 const char *restrict <[format]>,
32 const struct tm *restrict <[timp]>);
33 size_t strftime_l(char *restrict <[s]>, size_t <[maxsize]>,
34 const char *restrict <[format]>,
35 const struct tm *restrict <[timp]>,
39 <<strftime>> converts a <<struct tm>> representation of the time (at
40 <[timp]>) into a null-terminated string, starting at <[s]> and occupying
41 no more than <[maxsize]> characters.
43 <<strftime_l>> is like <<strftime>> but creates a string in a format
44 as expected in locale <[locale]>. If <[locale]> is LC_GLOBAL_LOCALE or
45 not a valid locale object, the behaviour is undefined.
47 You control the format of the output using the string at <[format]>.
48 <<*<[format]>>> can contain two kinds of specifications: text to be
49 copied literally into the formatted string, and time conversion
50 specifications. Time conversion specifications are two- and
51 three-character sequences beginning with `<<%>>' (use `<<%%>>' to
52 include a percent sign in the output). Each defined conversion
53 specification selects only the specified field(s) of calendar time
54 data from <<*<[timp]>>>, and converts it to a string in one of the
59 The abbreviated weekday name according to the current locale. [tm_wday]
62 The full weekday name according to the current locale.
63 In the default "C" locale, one of `<<Sunday>>', `<<Monday>>', `<<Tuesday>>',
64 `<<Wednesday>>', `<<Thursday>>', `<<Friday>>', `<<Saturday>>'. [tm_wday]
67 The abbreviated month name according to the current locale. [tm_mon]
70 The full month name according to the current locale.
71 In the default "C" locale, one of `<<January>>', `<<February>>',
72 `<<March>>', `<<April>>', `<<May>>', `<<June>>', `<<July>>',
73 `<<August>>', `<<September>>', `<<October>>', `<<November>>',
74 `<<December>>'. [tm_mon]
77 The preferred date and time representation for the current locale.
78 [tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday]
81 The century, that is, the year divided by 100 then truncated. For
82 4-digit years, the result is zero-padded and exactly two characters;
83 but for other years, there may a negative sign or more digits. In
84 this way, `<<%C%y>>' is equivalent to `<<%Y>>'. [tm_year]
87 The day of the month, formatted with two digits (from `<<01>>' to
91 A string representing the date, in the form `<<"%m/%d/%y">>'.
92 [tm_mday, tm_mon, tm_year]
95 The day of the month, formatted with leading space if single digit
96 (from `<<1>>' to `<<31>>'). [tm_mday]
99 In some locales, the E modifier selects alternative representations of
100 certain modifiers <<x>>. In newlib, it is ignored, and treated as %<<x>>.
103 A string representing the ISO 8601:2000 date format, in the form
104 `<<"%Y-%m-%d">>'. [tm_mday, tm_mon, tm_year]
107 The last two digits of the week-based year, see specifier %G (from
108 `<<00>>' to `<<99>>'). [tm_year, tm_wday, tm_yday]
111 The week-based year. In the ISO 8601:2000 calendar, week 1 of the year
112 includes January 4th, and begin on Mondays. Therefore, if January 1st,
113 2nd, or 3rd falls on a Sunday, that day and earlier belong to the last
114 week of the previous year; and if December 29th, 30th, or 31st falls
115 on Monday, that day and later belong to week 1 of the next year. For
116 consistency with %Y, it always has at least four characters.
117 Example: "%G" for Saturday 2nd January 1999 gives "1998", and for
118 Tuesday 30th December 1997 gives "1998". [tm_year, tm_wday, tm_yday]
121 Synonym for "%b". [tm_mon]
124 The hour (on a 24-hour clock), formatted with two digits (from
125 `<<00>>' to `<<23>>'). [tm_hour]
128 The hour (on a 12-hour clock), formatted with two digits (from
129 `<<01>>' to `<<12>>'). [tm_hour]
132 The count of days in the year, formatted with three digits
133 (from `<<001>>' to `<<366>>'). [tm_yday]
136 The hour (on a 24-hour clock), formatted with leading space if single
137 digit (from `<<0>>' to `<<23>>'). Non-POSIX extension (c.p. %I). [tm_hour]
140 The hour (on a 12-hour clock), formatted with leading space if single
141 digit (from `<<1>>' to `<<12>>'). Non-POSIX extension (c.p. %H). [tm_hour]
144 The month number, formatted with two digits (from `<<01>>' to `<<12>>').
148 The minute, formatted with two digits (from `<<00>>' to `<<59>>'). [tm_min]
151 A newline character (`<<\n>>').
154 In some locales, the O modifier selects alternative digit characters
155 for certain modifiers <<x>>. In newlib, it is ignored, and treated as %<<x>>.
158 Either `<<AM>>' or `<<PM>>' as appropriate, or the corresponding strings for
159 the current locale. [tm_hour]
162 Same as '<<%p>>', but in lowercase. This is a GNU extension. [tm_hour]
165 Quarter of the year (from `<<1>>' to `<<4>>'), with January starting
166 the first quarter. This is a GNU extension. [tm_mon]
169 Replaced by the time in a.m. and p.m. notation. In the "C" locale this
170 is equivalent to "%I:%M:%S %p". In locales which don't define a.m./p.m.
171 notations, the result is an empty string. [tm_sec, tm_min, tm_hour]
174 The 24-hour time, to the minute. Equivalent to "%H:%M". [tm_min, tm_hour]
177 The time elapsed, in seconds, since the start of the Unix epoch at
178 1970-01-01 00:00:00 UTC.
181 The second, formatted with two digits (from `<<00>>' to `<<60>>'). The
182 value 60 accounts for the occasional leap second. [tm_sec]
185 A tab character (`<<\t>>').
188 The 24-hour time, to the second. Equivalent to "%H:%M:%S". [tm_sec,
192 The weekday as a number, 1-based from Monday (from `<<1>>' to
196 The week number, where weeks start on Sunday, week 1 contains the first
197 Sunday in a year, and earlier days are in week 0. Formatted with two
198 digits (from `<<00>>' to `<<53>>'). See also <<%W>>. [tm_wday, tm_yday]
201 The week number, where weeks start on Monday, week 1 contains January 4th,
202 and earlier days are in the previous year. Formatted with two digits
203 (from `<<01>>' to `<<53>>'). See also <<%G>>. [tm_year, tm_wday, tm_yday]
206 A string representing the BSD/OSX/Ruby VMS/Oracle date format, in the form
207 "%e-%b-%Y". Non-POSIX extension. [tm_mday, tm_mon, tm_year]
210 The weekday as a number, 0-based from Sunday (from `<<0>>' to `<<6>>').
214 The week number, where weeks start on Monday, week 1 contains the first
215 Monday in a year, and earlier days are in week 0. Formatted with two
216 digits (from `<<00>>' to `<<53>>'). [tm_wday, tm_yday]
219 Replaced by the preferred date representation in the current locale.
220 In the "C" locale this is equivalent to "%m/%d/%y".
221 [tm_mon, tm_mday, tm_year]
224 Replaced by the preferred time representation in the current locale.
225 In the "C" locale this is equivalent to "%H:%M:%S". [tm_sec, tm_min, tm_hour]
228 The last two digits of the year (from `<<00>>' to `<<99>>'). [tm_year]
229 (Implementation interpretation: always positive, even for negative years.)
232 The full year, equivalent to <<%C%y>>. It will always have at least four
233 characters, but may have more. The year is accurate even when tm_year
234 added to the offset of 1900 overflows an int. [tm_year]
237 The offset from UTC. The format consists of a sign (negative is west of
238 Greewich), two characters for hour, then two characters for minutes
239 (-hhmm or +hhmm). If tm_isdst is negative, the offset is unknown and no
240 output is generated; if it is zero, the offset is the standard offset for
241 the current time zone; and if it is positive, the offset is the daylight
242 savings offset for the current timezone. The offset is determined from
243 the TZ environment variable, as if by calling tzset(). [tm_isdst]
246 The current time zone abbreviation. If tm_isdst is negative, no output
247 is generated. Otherwise, the time zone abbreviation is based on the TZ
248 environment variable, as if by calling tzset(). [tm_isdst]
251 A single character, `<<%>>'.
255 When the formatted time takes up no more than <[maxsize]> characters,
256 the result is the length of the formatted string. Otherwise, if the
257 formatting operation was abandoned due to lack of room, the result is
258 <<0>>, and the string starting at <[s]> corresponds to just those
259 parts of <<*<[format]>>> that could be completely filled in within the
263 ANSI C requires <<strftime>>, but does not specify the contents of
264 <<*<[s]>>> when the formatted string would require more than
265 <[maxsize]> characters. Unrecognized specifiers and fields of
266 <<timp>> that are out of range cause undefined results. Since some
267 formats expand to 0 bytes, it is wise to set <<*<[s]>>> to a nonzero
268 value beforehand to distinguish between failure and an empty string.
269 This implementation does not support <<s>> being NULL, nor overlapping
270 <<s>> and <<format>>.
272 <<strftime_l>> is POSIX-1.2008.
274 <<strftime>> and <<strftime_l>> require no supporting OS subroutines.
277 (NOT Cygwin:) <<strftime>> ignores the LC_TIME category of the current
278 locale, hard-coding the "C" locale settings.
282 #include <sys/config.h>
292 #include "../locale/setlocale.h"
294 /* Defines to make the file dual use for either strftime() or wcsftime().
295 * To get wcsftime, define MAKE_WCSFTIME.
296 * To get strftime, do not define MAKE_WCSFTIME.
297 * Names are kept friendly to strftime() usage. The biggest ugliness is the
298 * use of the CQ() macro to make either regular character constants and
299 * string literals or wide-character constants and wide-character-string
300 * literals, as appropriate. */
301 #if !defined(MAKE_WCSFTIME)
302 # define CHAR char /* string type basis */
303 # define CQ(a) a /* character constant qualifier */
304 # define SFLG /* %s flag (null for normal char) */
305 # define _ctloc(x) (ctloclen = strlen (ctloc = _CurrentTimeLocale->x))
306 # define snprintf sniprintf /* avoid to pull in FP functions. */
307 # define TOLOWER(c) tolower((int)(unsigned char)(c))
308 # define STRTOUL(c,p,b) strtoul((c),(p),(b))
309 # define STRCPY(a,b) strcpy((a),(b))
310 # define STRCHR(a,b) strchr((a),(b))
311 # define STRLEN(a) strlen(a)
313 # define strftime wcsftime /* Alternate function name */
314 # define strftime_l wcsftime_l /* Alternate function name */
315 # define CHAR wchar_t /* string type basis */
316 # define CQ(a) L##a /* character constant qualifier */
317 # define snprintf swprintf /* wide-char equivalent function name */
318 # define strncmp wcsncmp /* wide-char equivalent function name */
319 # define TOLOWER(c) towlower((wint_t)(c))
320 # define STRTOUL(c,p,b) wcstoul((c),(p),(b))
321 # define STRCPY(a,b) wcscpy((a),(b))
322 # define STRCHR(a,b) wcschr((a),(b))
323 # define STRLEN(a) wcslen(a)
324 # define SFLG "l" /* %s flag (l for wide char) */
325 # ifdef __HAVE_LOCALE_INFO_EXTENDED__
326 # define _ctloc(x) (ctloclen = wcslen (ctloc = _CurrentTimeLocale->w##x))
328 # define CTLOCBUFLEN 256 /* Arbitrary big buffer size */
330 __ctloc (wchar_t *buf
, const char *elem
, size_t *len_ret
)
332 buf
[CTLOCBUFLEN
- 1] = L
'\0';
333 *len_ret
= mbstowcs (buf
, elem
, CTLOCBUFLEN
- 1);
334 if (*len_ret
== (size_t) -1 )
338 # define _ctloc(x) (ctloc = __ctloc (ctlocbuf, _CurrentTimeLocale->x, \
341 #endif /* MAKE_WCSFTIME */
343 #define CHECK_LENGTH() if (len < 0 || (count += len) >= maxsize) \
346 /* Enforce the coding assumptions that YEAR_BASE is positive. (%C, %Y, etc.) */
348 # error "YEAR_BASE < 0"
351 /* Using the tm_year, tm_wday, and tm_yday components of TIM_P, return
352 -1, 0, or 1 as the adjustment to add to the year for the ISO week
353 numbering used in "%g%G%V", avoiding overflow. */
355 iso_year_adjust (const struct tm
*tim_p
)
357 /* Account for fact that tm_year==0 is year 1900. */
358 int leap
= isleap (tim_p
->tm_year
+ (YEAR_BASE
359 - (tim_p
->tm_year
< 0 ? 0 : 2000)));
361 /* Pack the yday, wday, and leap year into a single int since there are so
362 many disparate cases. */
363 #define PACK(yd, wd, lp) (((yd) << 4) + (wd << 1) + (lp))
364 switch (PACK (tim_p
->tm_yday
, tim_p
->tm_wday
, leap
))
366 case PACK (0, 5, 0): /* Jan 1 is Fri, not leap. */
367 case PACK (0, 6, 0): /* Jan 1 is Sat, not leap. */
368 case PACK (0, 0, 0): /* Jan 1 is Sun, not leap. */
369 case PACK (0, 5, 1): /* Jan 1 is Fri, leap year. */
370 case PACK (0, 6, 1): /* Jan 1 is Sat, leap year. */
371 case PACK (0, 0, 1): /* Jan 1 is Sun, leap year. */
372 case PACK (1, 6, 0): /* Jan 2 is Sat, not leap. */
373 case PACK (1, 0, 0): /* Jan 2 is Sun, not leap. */
374 case PACK (1, 6, 1): /* Jan 2 is Sat, leap year. */
375 case PACK (1, 0, 1): /* Jan 2 is Sun, leap year. */
376 case PACK (2, 0, 0): /* Jan 3 is Sun, not leap. */
377 case PACK (2, 0, 1): /* Jan 3 is Sun, leap year. */
378 return -1; /* Belongs to last week of previous year. */
379 case PACK (362, 1, 0): /* Dec 29 is Mon, not leap. */
380 case PACK (363, 1, 1): /* Dec 29 is Mon, leap year. */
381 case PACK (363, 1, 0): /* Dec 30 is Mon, not leap. */
382 case PACK (363, 2, 0): /* Dec 30 is Tue, not leap. */
383 case PACK (364, 1, 1): /* Dec 30 is Mon, leap year. */
384 case PACK (364, 2, 1): /* Dec 30 is Tue, leap year. */
385 case PACK (364, 1, 0): /* Dec 31 is Mon, not leap. */
386 case PACK (364, 2, 0): /* Dec 31 is Tue, not leap. */
387 case PACK (364, 3, 0): /* Dec 31 is Wed, not leap. */
388 case PACK (365, 1, 1): /* Dec 31 is Mon, leap year. */
389 case PACK (365, 2, 1): /* Dec 31 is Tue, leap year. */
390 case PACK (365, 3, 1): /* Dec 31 is Wed, leap year. */
391 return 1; /* Belongs to first week of next year. */
393 return 0; /* Belongs to specified year. */
397 #ifdef _WANT_C99_TIME_FORMATS
405 #if defined (MAKE_WCSFTIME) && defined (__HAVE_LOCALE_INFO_EXTENDED__)
406 get_era_info (const struct tm
*tim_p
, const wchar_t *era
)
408 get_era_info (const struct tm
*tim_p
, const char *era
)
411 #if defined (MAKE_WCSFTIME) && defined (__HAVE_LOCALE_INFO_EXTENDED__)
414 # define ERA_STRCHR(a,b) wcschr((a),(b))
415 # define ERA_STRNCPY(a,b,c) wcsncpy((a),(b),(c))
416 # define ERA_STRTOL(a,b,c) wcstol((a),(b),(c))
420 # define ERA_STRCHR(a,b) strchr((a),(b))
421 # define ERA_STRNCPY(a,b,c) strncpy((a),(b),(c))
422 # define ERA_STRTOL(a,b,c) strtol((a),(b),(c))
428 ei
= (era_info_t
*) calloc (1, sizeof (era_info_t
));
432 stm
.tm_isdst
= etm
.tm_isdst
= 0;
437 offset
= ERA_STRTOL (era
, &c
, 10);
439 stm
.tm_year
= ERA_STRTOL (era
, &c
, 10) - YEAR_BASE
;
440 /* Adjust offset for negative gregorian dates. */
441 if (stm
.tm_year
<= -YEAR_BASE
)
443 stm
.tm_mon
= ERA_STRTOL (c
+ 1, &c
, 10) - 1;
444 stm
.tm_mday
= ERA_STRTOL (c
+ 1, &c
, 10);
445 stm
.tm_hour
= stm
.tm_min
= stm
.tm_sec
= 0;
447 if (era
[0] == '-' && era
[1] == '*')
450 stm
.tm_year
= INT_MIN
;
451 stm
.tm_mon
= stm
.tm_mday
= stm
.tm_hour
= stm
.tm_min
= stm
.tm_sec
= 0;
454 else if (era
[0] == '+' && era
[1] == '*')
456 etm
.tm_year
= INT_MAX
;
460 etm
.tm_min
= etm
.tm_sec
= 59;
465 etm
.tm_year
= ERA_STRTOL (era
, &c
, 10) - YEAR_BASE
;
466 /* Adjust offset for negative gregorian dates. */
467 if (etm
.tm_year
<= -YEAR_BASE
)
469 etm
.tm_mon
= ERA_STRTOL (c
+ 1, &c
, 10) - 1;
470 etm
.tm_mday
= ERA_STRTOL (c
+ 1, &c
, 10);
473 etm
.tm_min
= etm
.tm_sec
= 59;
476 if ((tim_p
->tm_year
> stm
.tm_year
477 || (tim_p
->tm_year
== stm
.tm_year
478 && (tim_p
->tm_mon
> stm
.tm_mon
479 || (tim_p
->tm_mon
== stm
.tm_mon
480 && tim_p
->tm_mday
>= stm
.tm_mday
))))
481 && (tim_p
->tm_year
< etm
.tm_year
482 || (tim_p
->tm_year
== etm
.tm_year
483 && (tim_p
->tm_mon
< etm
.tm_mon
484 || (tim_p
->tm_mon
== etm
.tm_mon
485 && tim_p
->tm_mday
<= etm
.tm_mday
)))))
491 if (*dir
== '+' && stm
.tm_year
!= INT_MIN
)
492 ei
->year
= tim_p
->tm_year
- stm
.tm_year
+ offset
;
494 ei
->year
= etm
.tm_year
- tim_p
->tm_year
+ offset
;
496 c
= ERA_STRCHR (era
, ':');
497 #if defined (MAKE_WCSFTIME) && !defined (__HAVE_LOCALE_INFO_EXTENDED__)
498 len
= mbsnrtowcs (NULL
, &era
, c
- era
, 0, NULL
);
499 if (len
== (size_t) -1)
507 ei
->era_C
= (CHAR
*) malloc ((len
+ 1) * sizeof (CHAR
));
513 #if defined (MAKE_WCSFTIME) && !defined (__HAVE_LOCALE_INFO_EXTENDED__)
514 len
= mbsnrtowcs (ei
->era_C
, &era
, c
- era
, len
+ 1, NULL
);
516 ERA_STRNCPY (ei
->era_C
, era
, len
);
519 ei
->era_C
[len
] = CQ('\0');
522 c
= ERA_STRCHR (era
, ';');
524 c
= ERA_STRCHR (era
, '\0');
525 #if defined (MAKE_WCSFTIME) && !defined (__HAVE_LOCALE_INFO_EXTENDED__)
526 len
= mbsnrtowcs (NULL
, &era
, c
- era
, 0, NULL
);
527 if (len
== (size_t) -1)
536 ei
->era_Y
= (CHAR
*) malloc ((len
+ 1) * sizeof (CHAR
));
543 #if defined (MAKE_WCSFTIME) && !defined (__HAVE_LOCALE_INFO_EXTENDED__)
544 len
= mbsnrtowcs (ei
->era_Y
, &era
, c
- era
, len
+ 1, NULL
);
546 ERA_STRNCPY (ei
->era_Y
, era
, len
);
549 ei
->era_Y
[len
] = CQ('\0');
553 era
= ERA_STRCHR (era
, ';');
561 free_era_info (era_info_t
*ei
)
574 static alt_digits_t
*
575 #if defined (MAKE_WCSFTIME) && defined (__HAVE_LOCALE_INFO_EXTENDED__)
576 get_alt_digits (const wchar_t *alt_digits
)
578 get_alt_digits (const char *alt_digits
)
582 #if defined (MAKE_WCSFTIME) && defined (__HAVE_LOCALE_INFO_EXTENDED__)
583 const wchar_t *a
, *e
;
584 # define ALT_STRCHR(a,b) wcschr((a),(b))
585 # define ALT_STRCPY(a,b) wcscpy((a),(b))
586 # define ALT_STRLEN(a) wcslen(a)
589 # define ALT_STRCHR(a,b) strchr((a),(b))
590 # define ALT_STRCPY(a,b) strcpy((a),(b))
591 # define ALT_STRLEN(a) strlen(a)
596 adi
= (alt_digits_t
*) calloc (1, sizeof (alt_digits_t
));
600 /* Compute number of alt_digits. */
602 for (a
= alt_digits
; (e
= ALT_STRCHR (a
, ';')) != NULL
; a
= e
+ 1)
604 /* Allocate the `digit' array, which is an array of `num' pointers into
606 adi
->digit
= (CHAR
**) calloc (adi
->num
, sizeof (CHAR
*));
612 /* Compute memory required for `buffer'. */
613 #if defined (MAKE_WCSFTIME) && !defined (__HAVE_LOCALE_INFO_EXTENDED__)
614 len
= mbstowcs (NULL
, alt_digits
, 0);
615 if (len
== (size_t) -1)
622 len
= ALT_STRLEN (alt_digits
);
625 adi
->buffer
= (CHAR
*) malloc ((len
+ 1) * sizeof (CHAR
));
632 /* Store digits in it. */
633 #if defined (MAKE_WCSFTIME) && !defined (__HAVE_LOCALE_INFO_EXTENDED__)
634 mbstowcs (adi
->buffer
, alt_digits
, len
+ 1);
636 ALT_STRCPY (adi
->buffer
, alt_digits
);
638 /* Store the pointers into `buffer' into the appropriate `digit' slot. */
639 for (len
= 0, aa
= adi
->buffer
; (ae
= STRCHR (aa
, CQ(';'))) != NULL
;
643 adi
->digit
[len
] = aa
;
645 adi
->digit
[len
] = aa
;
650 free_alt_digits (alt_digits_t
*adi
)
657 /* Return 0 if no alt_digit is available for a number.
658 Return -1 if buffer size isn't sufficient to hold alternative digit.
659 Return length of new digit otherwise. */
661 conv_to_alt_digits (CHAR
*buf
, size_t bufsiz
, unsigned num
, alt_digits_t
*adi
)
665 size_t len
= STRLEN (adi
->digit
[num
]);
668 STRCPY (buf
, adi
->digit
[num
]);
675 __strftime (CHAR
*s
, size_t maxsize
, const CHAR
*format
,
676 const struct tm
*tim_p
, struct __locale_t
*locale
,
677 era_info_t
**era_info
, alt_digits_t
**alt_digits
)
678 #else /* !_WANT_C99_TIME_FORMATS */
680 __strftime (CHAR
*s
, size_t maxsize
, const CHAR
*format
,
681 const struct tm
*tim_p
, struct __locale_t
*locale
)
683 #define __strftime(s,m,f,t,l,e,a) __strftime((s),(m),(f),(t),(l))
684 #endif /* !_WANT_C99_TIME_FORMATS */
689 #if defined (MAKE_WCSFTIME) && !defined (__HAVE_LOCALE_INFO_EXTENDED__)
690 CHAR ctlocbuf
[CTLOCBUFLEN
];
696 int tzset_called
= 0;
698 const struct lc_time_T
*_CurrentTimeLocale
= __get_time_locale (locale
);
701 while (*format
&& *format
!= CQ('%'))
703 if (count
< maxsize
- 1)
704 s
[count
++] = *format
++;
708 if (*format
== CQ('\0'))
714 /* POSIX-1.2008 feature: '0' and '+' modifiers require 0-padding with
715 slightly different semantics. */
716 if (*format
== CQ('0') || *format
== CQ('+'))
719 /* POSIX-1.2008 feature: A minimum field width can be specified. */
720 if (*format
>= CQ('1') && *format
<= CQ('9'))
723 width
= STRTOUL (format
, &fp
, 10);
728 if (*format
== CQ('E'))
731 #ifdef _WANT_C99_TIME_FORMATS
732 #if defined (MAKE_WCSFTIME) && defined (__HAVE_LOCALE_INFO_EXTENDED__)
733 if (!*era_info
&& *_CurrentTimeLocale
->wera
)
734 *era_info
= get_era_info (tim_p
, _CurrentTimeLocale
->wera
);
736 if (!*era_info
&& *_CurrentTimeLocale
->era
)
737 *era_info
= get_era_info (tim_p
, _CurrentTimeLocale
->era
);
739 #endif /* _WANT_C99_TIME_FORMATS */
741 else if (*format
== CQ('O'))
744 #ifdef _WANT_C99_TIME_FORMATS
745 #if defined (MAKE_WCSFTIME) && defined (__HAVE_LOCALE_INFO_EXTENDED__)
746 if (!*alt_digits
&& *_CurrentTimeLocale
->walt_digits
)
747 *alt_digits
= get_alt_digits (_CurrentTimeLocale
->walt_digits
);
749 if (!*alt_digits
&& *_CurrentTimeLocale
->alt_digits
)
750 *alt_digits
= get_alt_digits (_CurrentTimeLocale
->alt_digits
);
752 #endif /* _WANT_C99_TIME_FORMATS */
758 _ctloc (wday
[tim_p
->tm_wday
]);
759 for (i
= 0; i
< ctloclen
; i
++)
761 if (count
< maxsize
- 1)
762 s
[count
++] = ctloc
[i
];
768 _ctloc (weekday
[tim_p
->tm_wday
]);
769 for (i
= 0; i
< ctloclen
; i
++)
771 if (count
< maxsize
- 1)
772 s
[count
++] = ctloc
[i
];
779 _ctloc (mon
[tim_p
->tm_mon
]);
780 for (i
= 0; i
< ctloclen
; i
++)
782 if (count
< maxsize
- 1)
783 s
[count
++] = ctloc
[i
];
789 _ctloc (month
[tim_p
->tm_mon
]);
790 for (i
= 0; i
< ctloclen
; i
++)
792 if (count
< maxsize
- 1)
793 s
[count
++] = ctloc
[i
];
799 #ifdef _WANT_C99_TIME_FORMATS
800 if (alt
== 'E' && *era_info
&& *_CurrentTimeLocale
->era_d_t_fmt
)
801 _ctloc (era_d_t_fmt
);
803 #endif /* _WANT_C99_TIME_FORMATS */
810 #ifdef _WANT_C99_TIME_FORMATS
811 if (alt
== 'E' && *era_info
&& *_CurrentTimeLocale
->era_d_fmt
)
814 #endif /* _WANT_C99_TIME_FORMATS */
818 #ifdef _WANT_C99_TIME_FORMATS
819 if (alt
== 'E' && *era_info
&& *_CurrentTimeLocale
->era_t_fmt
)
822 #endif /* _WANT_C99_TIME_FORMATS */
827 /* Recurse to avoid need to replicate %Y formation. */
828 len
= __strftime (&s
[count
], maxsize
- count
, ctloc
, tim_p
,
829 locale
, era_info
, alt_digits
);
838 /* Examples of (tm_year + YEAR_BASE) that show how %Y == %C%y
841 2147485547 21474855 47
853 -2147481748 -21474817 48
855 Be careful of both overflow and sign adjustment due to the
856 asymmetric range of years.
858 #ifdef _WANT_C99_TIME_FORMATS
859 if (alt
== 'E' && *era_info
)
860 len
= snprintf (&s
[count
], maxsize
- count
, CQ("%" SFLG
"s"),
863 #endif /* _WANT_C99_TIME_FORMATS */
865 CHAR
*fmt
= CQ("%s%.*d");
867 int neg
= tim_p
->tm_year
< -YEAR_BASE
;
868 int century
= tim_p
->tm_year
>= 0
869 ? tim_p
->tm_year
/ 100 + YEAR_BASE
/ 100
870 : abs (tim_p
->tm_year
+ YEAR_BASE
) / 100;
871 if (pad
) /* '0' or '+' */
874 if (century
>= 100 && pad
== CQ('+'))
879 len
= snprintf (&s
[count
], maxsize
- count
, fmt
,
880 neg
? "-" : pos
, width
- neg
, century
);
887 #ifdef _WANT_C99_TIME_FORMATS
888 if (alt
== CQ('O') && *alt_digits
)
890 if (tim_p
->tm_mday
< 10)
892 if (*format
== CQ('d'))
894 if (maxsize
- count
< 2) return 0;
895 len
= conv_to_alt_digits (&s
[count
], maxsize
- count
,
899 if (*format
== CQ('e') || len
== 0)
900 s
[count
++] = CQ(' ');
902 len
= conv_to_alt_digits (&s
[count
], maxsize
- count
,
903 tim_p
->tm_mday
, *alt_digits
);
908 #endif /* _WANT_C99_TIME_FORMATS */
909 len
= snprintf (&s
[count
], maxsize
- count
,
910 *format
== CQ('d') ? CQ("%.2d") : CQ("%2d"),
916 len
= snprintf (&s
[count
], maxsize
- count
,
917 CQ("%.2d/%.2d/%.2d"),
918 tim_p
->tm_mon
+ 1, tim_p
->tm_mday
,
919 tim_p
->tm_year
>= 0 ? tim_p
->tm_year
% 100
920 : abs (tim_p
->tm_year
+ YEAR_BASE
) % 100);
924 { /* %F is equivalent to "%+4Y-%m-%d", flags and width can change
925 that. Recurse to avoid need to replicate %Y formation. */
926 CHAR fmtbuf
[32], *fmt
= fmtbuf
;
929 if (pad
) /* '0' or '+' */
940 len
= snprintf (fmt
, fmtbuf
+ 32 - fmt
, CQ("%lu"), width
);
944 STRCPY (fmt
, CQ("Y-%m-%d"));
945 len
= __strftime (&s
[count
], maxsize
- count
, fmtbuf
, tim_p
,
946 locale
, era_info
, alt_digits
);
954 /* Be careful of both overflow and negative years, thanks to
955 the asymmetric range of years. */
957 int adjust
= iso_year_adjust (tim_p
);
958 int year
= tim_p
->tm_year
>= 0 ? tim_p
->tm_year
% 100
959 : abs (tim_p
->tm_year
+ YEAR_BASE
) % 100;
960 if (adjust
< 0 && tim_p
->tm_year
<= -YEAR_BASE
)
962 else if (adjust
> 0 && tim_p
->tm_year
< -YEAR_BASE
)
964 len
= snprintf (&s
[count
], maxsize
- count
, CQ("%.2d"),
965 ((year
+ adjust
) % 100 + 100) % 100);
971 /* See the comments for 'C' and 'Y'; this is a variable length
972 field. Although there is no requirement for a minimum number
973 of digits, we use 4 for consistency with 'Y'. */
974 int sign
= tim_p
->tm_year
< -YEAR_BASE
;
975 int adjust
= iso_year_adjust (tim_p
);
976 int century
= tim_p
->tm_year
>= 0
977 ? tim_p
->tm_year
/ 100 + YEAR_BASE
/ 100
978 : abs (tim_p
->tm_year
+ YEAR_BASE
) / 100;
979 int year
= tim_p
->tm_year
>= 0 ? tim_p
->tm_year
% 100
980 : abs (tim_p
->tm_year
+ YEAR_BASE
) % 100;
981 if (adjust
< 0 && tim_p
->tm_year
<= -YEAR_BASE
)
983 else if (adjust
> 0 && sign
)
991 else if (year
== 100)
996 CHAR fmtbuf
[10], *fmt
= fmtbuf
;
997 /* int potentially overflows, so use unsigned instead. */
998 unsigned p_year
= century
* 100 + year
;
1001 else if (pad
== CQ('+') && p_year
>= 10000)
1011 STRCPY (fmt
, CQ(".*u"));
1012 len
= snprintf (&s
[count
], maxsize
- count
, fmtbuf
, width
, p_year
);
1013 if (len
< 0 || (count
+=len
) >= maxsize
)
1018 #ifdef _WANT_C99_TIME_FORMATS
1019 if (alt
== CQ('O') && *alt_digits
)
1021 len
= conv_to_alt_digits (&s
[count
], maxsize
- count
,
1022 tim_p
->tm_hour
, *alt_digits
);
1027 #endif /* _WANT_C99_TIME_FORMATS */
1029 case CQ('k'): /* newlib extension */
1030 len
= snprintf (&s
[count
], maxsize
- count
,
1031 *format
== CQ('k') ? CQ("%2d") : CQ("%.2d"),
1035 case CQ('l'): /* newlib extension */
1042 h12
= (tim_p
->tm_hour
== 0 || tim_p
->tm_hour
== 12) ?
1043 12 : tim_p
->tm_hour
% 12;
1044 #ifdef _WANT_C99_TIME_FORMATS
1045 if (alt
!= CQ('O') || !*alt_digits
1046 || !(len
= conv_to_alt_digits (&s
[count
], maxsize
- count
,
1048 #endif /* _WANT_C99_TIME_FORMATS */
1049 len
= snprintf (&s
[count
], maxsize
- count
,
1050 *format
== CQ('I') ? CQ("%.2d") : CQ("%2d"), h12
);
1055 len
= snprintf (&s
[count
], maxsize
- count
, CQ("%.3d"),
1056 tim_p
->tm_yday
+ 1);
1060 #ifdef _WANT_C99_TIME_FORMATS
1061 if (alt
!= CQ('O') || !*alt_digits
1062 || !(len
= conv_to_alt_digits (&s
[count
], maxsize
- count
,
1063 tim_p
->tm_mon
+ 1, *alt_digits
)))
1064 #endif /* _WANT_C99_TIME_FORMATS */
1065 len
= snprintf (&s
[count
], maxsize
- count
, CQ("%.2d"),
1070 #ifdef _WANT_C99_TIME_FORMATS
1071 if (alt
!= CQ('O') || !*alt_digits
1072 || !(len
= conv_to_alt_digits (&s
[count
], maxsize
- count
,
1073 tim_p
->tm_min
, *alt_digits
)))
1074 #endif /* _WANT_C99_TIME_FORMATS */
1075 len
= snprintf (&s
[count
], maxsize
- count
, CQ("%.2d"),
1080 if (count
< maxsize
- 1)
1081 s
[count
++] = CQ('\n');
1087 _ctloc (am_pm
[tim_p
->tm_hour
< 12 ? 0 : 1]);
1088 for (i
= 0; i
< ctloclen
; i
++)
1090 if (count
< maxsize
- 1)
1091 s
[count
++] = (*format
== CQ('P') ? TOLOWER (ctloc
[i
])
1097 case CQ('q'): /* GNU quarter year */
1098 len
= snprintf (&s
[count
], maxsize
- count
, CQ("%.1d"),
1099 tim_p
->tm_mon
/ 3 + 1);
1103 len
= snprintf (&s
[count
], maxsize
- count
, CQ("%.2d:%.2d"),
1104 tim_p
->tm_hour
, tim_p
->tm_min
);
1110 * The Open Group Base Specifications Issue 7
1111 * IEEE Std 1003.1, 2013 Edition
1112 * Copyright (c) 2001-2013 The IEEE and The Open Group
1113 * XBD Base Definitions
1114 * 4. General Concepts
1115 * 4.15 Seconds Since the Epoch
1116 * A value that approximates the number of seconds that have elapsed since the
1117 * Epoch. A Coordinated Universal Time name (specified in terms of seconds
1118 * (tm_sec), minutes (tm_min), hours (tm_hour), days since January 1 of the year
1119 * (tm_yday), and calendar year minus 1900 (tm_year)) is related to a time
1120 * represented as seconds since the Epoch, according to the expression below.
1121 * If the year is <1970 or the value is negative, the relationship is undefined.
1122 * If the year is >=1970 and the value is non-negative, the value is related to a
1123 * Coordinated Universal Time name according to the C-language expression, where
1124 * tm_sec, tm_min, tm_hour, tm_yday, and tm_year are all integer types:
1125 * tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
1126 * (tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
1127 * ((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400
1129 * ((((tm_year-69)/4 - (tm_year-1)/100 + (tm_year+299)/400 +
1130 * (tm_year-70)*365 + tm_yday)*24 + tm_hour)*60 + tm_min)*60 + tm_sec
1132 /* modified from %z case by hoisting offset outside if block and initializing */
1134 long offset
= 0; /* offset < 0 => W of GMT, > 0 => E of GMT:
1135 subtract to get UTC */
1137 if (tim_p
->tm_isdst
>= 0)
1146 #if defined (__CYGWIN__)
1147 /* Cygwin must check if the application has been built with or
1148 without the extra tm members for backward compatibility, and
1149 then use either that or the old method fetching from tzinfo.
1150 Rather than pulling in the version check infrastructure, we
1151 just call a Cygwin function. */
1152 extern long __cygwin_gettzoffset (const struct tm
*tmp
);
1153 offset
= __cygwin_gettzoffset (tim_p
);
1154 #elif defined (__TM_GMTOFF)
1155 offset
= tim_p
->__TM_GMTOFF
;
1157 __tzinfo_type
*tz
= __gettzinfo ();
1158 /* The sign of this is exactly opposite the envvar TZ. We
1159 could directly use the global _timezone for tm_isdst==0,
1160 but have to use __tzrule for daylight savings. */
1161 offset
= -tz
->__tzrule
[tim_p
->tm_isdst
> 0].offset
;
1165 len
= snprintf (&s
[count
], maxsize
- count
, CQ("%lld"),
1166 (((((long long)tim_p
->tm_year
- 69)/4
1167 - (tim_p
->tm_year
- 1)/100
1168 + (tim_p
->tm_year
+ 299)/400
1169 + (tim_p
->tm_year
- 70)*365 + tim_p
->tm_yday
)*24
1170 + tim_p
->tm_hour
)*60 + tim_p
->tm_min
)*60
1171 + tim_p
->tm_sec
- offset
);
1176 #ifdef _WANT_C99_TIME_FORMATS
1177 if (alt
!= CQ('O') || !*alt_digits
1178 || !(len
= conv_to_alt_digits (&s
[count
], maxsize
- count
,
1179 tim_p
->tm_sec
, *alt_digits
)))
1180 #endif /* _WANT_C99_TIME_FORMATS */
1181 len
= snprintf (&s
[count
], maxsize
- count
, CQ("%.2d"),
1186 if (count
< maxsize
- 1)
1187 s
[count
++] = CQ('\t');
1192 len
= snprintf (&s
[count
], maxsize
- count
, CQ("%.2d:%.2d:%.2d"),
1193 tim_p
->tm_hour
, tim_p
->tm_min
, tim_p
->tm_sec
);
1197 #ifdef _WANT_C99_TIME_FORMATS
1198 if (alt
== CQ('O') && *alt_digits
)
1200 len
= conv_to_alt_digits (&s
[count
], maxsize
- count
,
1201 tim_p
->tm_wday
== 0 ? 7
1208 #endif /* _WANT_C99_TIME_FORMATS */
1209 if (count
< maxsize
- 1)
1211 if (tim_p
->tm_wday
== 0)
1212 s
[count
++] = CQ('7');
1214 s
[count
++] = CQ('0') + tim_p
->tm_wday
;
1220 #ifdef _WANT_C99_TIME_FORMATS
1221 if (alt
!= CQ('O') || !*alt_digits
1222 || !(len
= conv_to_alt_digits (&s
[count
], maxsize
- count
,
1223 (tim_p
->tm_yday
+ 7 -
1224 tim_p
->tm_wday
) / 7,
1226 #endif /* _WANT_C99_TIME_FORMATS */
1227 len
= snprintf (&s
[count
], maxsize
- count
, CQ("%.2d"),
1228 (tim_p
->tm_yday
+ 7 -
1229 tim_p
->tm_wday
) / 7);
1234 int adjust
= iso_year_adjust (tim_p
);
1235 int wday
= (tim_p
->tm_wday
) ? tim_p
->tm_wday
- 1 : 6;
1236 int week
= (tim_p
->tm_yday
+ 10 - wday
) / 7;
1239 else if (adjust
< 0)
1240 /* Previous year has 53 weeks if current year starts on
1241 Fri, and also if current year starts on Sat and
1242 previous year was leap year. */
1243 week
= 52 + (4 >= (wday
- tim_p
->tm_yday
1244 - isleap (tim_p
->tm_year
1246 - (tim_p
->tm_year
< 0
1248 #ifdef _WANT_C99_TIME_FORMATS
1249 if (alt
!= CQ('O') || !*alt_digits
1250 || !(len
= conv_to_alt_digits (&s
[count
], maxsize
- count
,
1251 week
, *alt_digits
)))
1252 #endif /* _WANT_C99_TIME_FORMATS */
1253 len
= snprintf (&s
[count
], maxsize
- count
, CQ("%.2d"), week
);
1257 case CQ('v'): /* BSD/OSX/Ruby extension VMS/Oracle date format
1258 from Arnold Robbins strftime version 3.0 */
1259 { /* %v is equivalent to "%e-%b-%Y", flags and width can change year
1260 format. Recurse to avoid need to replicate %b and %Y formation. */
1261 CHAR fmtbuf
[32], *fmt
= fmtbuf
;
1262 STRCPY (fmt
, CQ("%e-%b-%"));
1263 fmt
+= STRLEN (fmt
);
1264 if (pad
) /* '0' or '+' */
1275 len
= snprintf (fmt
, fmtbuf
+ 32 - fmt
, CQ("%lu"), width
);
1279 STRCPY (fmt
, CQ("Y"));
1280 len
= __strftime (&s
[count
], maxsize
- count
, fmtbuf
, tim_p
,
1281 locale
, era_info
, alt_digits
);
1289 #ifdef _WANT_C99_TIME_FORMATS
1290 if (alt
== CQ('O') && *alt_digits
)
1292 len
= conv_to_alt_digits (&s
[count
], maxsize
- count
,
1293 tim_p
->tm_wday
, *alt_digits
);
1298 #endif /* _WANT_C99_TIME_FORMATS */
1299 if (count
< maxsize
- 1)
1300 s
[count
++] = CQ('0') + tim_p
->tm_wday
;
1306 int wday
= (tim_p
->tm_wday
) ? tim_p
->tm_wday
- 1 : 6;
1307 wday
= (tim_p
->tm_yday
+ 7 - wday
) / 7;
1308 #ifdef _WANT_C99_TIME_FORMATS
1309 if (alt
!= CQ('O') || !*alt_digits
1310 || !(len
= conv_to_alt_digits (&s
[count
], maxsize
- count
,
1311 wday
, *alt_digits
)))
1312 #endif /* _WANT_C99_TIME_FORMATS */
1313 len
= snprintf (&s
[count
], maxsize
- count
, CQ("%.2d"), wday
);
1319 #ifdef _WANT_C99_TIME_FORMATS
1320 if (alt
== 'E' && *era_info
)
1321 len
= snprintf (&s
[count
], maxsize
- count
, CQ("%d"),
1324 #endif /* _WANT_C99_TIME_FORMATS */
1326 /* Be careful of both overflow and negative years, thanks to
1327 the asymmetric range of years. */
1328 int year
= tim_p
->tm_year
>= 0 ? tim_p
->tm_year
% 100
1329 : abs (tim_p
->tm_year
+ YEAR_BASE
) % 100;
1330 #ifdef _WANT_C99_TIME_FORMATS
1331 if (alt
!= CQ('O') || !*alt_digits
1332 || !(len
= conv_to_alt_digits (&s
[count
], maxsize
- count
,
1333 year
, *alt_digits
)))
1334 #endif /* _WANT_C99_TIME_FORMATS */
1335 len
= snprintf (&s
[count
], maxsize
- count
, CQ("%.2d"),
1342 #ifdef _WANT_C99_TIME_FORMATS
1343 if (alt
== 'E' && *era_info
)
1345 ctloc
= (*era_info
)->era_Y
;
1349 #endif /* _WANT_C99_TIME_FORMATS */
1351 CHAR fmtbuf
[10], *fmt
= fmtbuf
;
1352 int sign
= tim_p
->tm_year
< -YEAR_BASE
;
1353 /* int potentially overflows, so use unsigned instead. */
1354 register unsigned year
= (unsigned) tim_p
->tm_year
1355 + (unsigned) YEAR_BASE
;
1359 year
= UINT_MAX
- year
+ 1;
1361 else if (pad
== CQ('+') && year
>= 10000)
1371 STRCPY (fmt
, CQ(".*u"));
1372 len
= snprintf (&s
[count
], maxsize
- count
, fmtbuf
, width
,
1378 if (tim_p
->tm_isdst
>= 0)
1389 #if defined (__CYGWIN__)
1390 /* Cygwin must check if the application has been built with or
1391 without the extra tm members for backward compatibility, and
1392 then use either that or the old method fetching from tzinfo.
1393 Rather than pulling in the version check infrastructure, we
1394 just call a Cygwin function. */
1395 extern long __cygwin_gettzoffset (const struct tm
*tmp
);
1396 offset
= __cygwin_gettzoffset (tim_p
);
1397 #elif defined (__TM_GMTOFF)
1398 offset
= tim_p
->__TM_GMTOFF
;
1400 __tzinfo_type
*tz
= __gettzinfo ();
1401 /* The sign of this is exactly opposite the envvar TZ. We
1402 could directly use the global _timezone for tm_isdst==0,
1403 but have to use __tzrule for daylight savings. */
1404 offset
= -tz
->__tzrule
[tim_p
->tm_isdst
> 0].offset
;
1407 len
= snprintf (&s
[count
], maxsize
- count
, CQ("%+03ld%.2ld"),
1408 offset
/ SECSPERHOUR
,
1409 labs (offset
/ SECSPERMIN
) % 60L);
1414 if (tim_p
->tm_isdst
>= 0)
1417 const char *tznam
= NULL
;
1425 #if defined (__CYGWIN__)
1427 extern const char *__cygwin_gettzname (const struct tm
*tmp
);
1428 tznam
= __cygwin_gettzname (tim_p
);
1429 #elif defined (__TM_ZONE)
1430 tznam
= tim_p
->__TM_ZONE
;
1433 tznam
= _tzname
[tim_p
->tm_isdst
> 0];
1434 /* Note that in case of wcsftime this loop only works for
1435 timezone abbreviations using the portable codeset (aka ASCII).
1436 This seems to be the case, but if that ever changes, this
1437 loop needs revisiting. */
1438 size
= strlen (tznam
);
1439 for (i
= 0; i
< size
; i
++)
1441 if (count
< maxsize
- 1)
1442 s
[count
++] = tznam
[i
];
1453 if (count
< maxsize
- 1)
1454 s
[count
++] = CQ('%');
1467 s
[count
] = CQ('\0');
1473 strftime (CHAR
*__restrict s
,
1475 const CHAR
*__restrict format
,
1476 const struct tm
*__restrict tim_p
)
1478 #ifdef _WANT_C99_TIME_FORMATS
1479 era_info_t
*era_info
= NULL
;
1480 alt_digits_t
*alt_digits
= NULL
;
1481 size_t ret
= __strftime (s
, maxsize
, format
, tim_p
, __get_current_locale (),
1482 &era_info
, &alt_digits
);
1484 free_era_info (era_info
);
1486 free_alt_digits (alt_digits
);
1488 #else /* !_WANT_C99_TIME_FORMATS */
1489 return __strftime (s
, maxsize
, format
, tim_p
, __get_current_locale (),
1491 #endif /* !_WANT_C99_TIME_FORMATS */
1495 strftime_l (CHAR
*__restrict s
, size_t maxsize
, const CHAR
*__restrict format
,
1496 const struct tm
*__restrict tim_p
, struct __locale_t
*locale
)
1498 #ifdef _WANT_C99_TIME_FORMATS
1499 era_info_t
*era_info
= NULL
;
1500 alt_digits_t
*alt_digits
= NULL
;
1501 size_t ret
= __strftime (s
, maxsize
, format
, tim_p
, locale
,
1502 &era_info
, &alt_digits
);
1504 free_era_info (era_info
);
1506 free_alt_digits (alt_digits
);
1508 #else /* !_WANT_C99_TIME_FORMATS */
1509 return __strftime (s
, maxsize
, format
, tim_p
, locale
, NULL
, NULL
);
1510 #endif /* !_WANT_C99_TIME_FORMATS */
1513 /* The remainder of this file can serve as a regression test. Compile
1514 * with -D_REGRESSION_TEST. */
1515 #if defined(_REGRESSION_TEST) /* [Test code: */
1517 /* This test code relies on ANSI C features, in particular on the ability
1518 * of adjacent strings to be pasted together into one string. */
1520 /* Test output buffer size (should be larger than all expected results) */
1524 CHAR
*fmt
; /* Testing format */
1525 size_t max
; /* Testing maxsize */
1526 size_t ret
; /* Expected return value */
1527 CHAR
*out
; /* Expected output string */
1530 const struct tm
*tms
; /* Time used for these vectors */
1531 const struct test
*vec
; /* Test vectors */
1532 int cnt
; /* Number of vectors */
1535 const char TZ
[]="TZ=EST5EDT";
1537 /* Define list of test inputs and expected outputs, for the given time zone
1539 const struct tm tm0
= {
1540 /* Tue Dec 30 10:53:47 EST 2008 (time_t=1230648827) */
1551 const struct test Vec0
[] = {
1552 /* Testing fields one at a time, expecting to pass, using exact
1553 * allowed length as what is needed. */
1554 /* Using tm0 for time: */
1555 #define EXP(s) sizeof(s)/sizeof(CHAR)-1, s
1556 { CQ("%a"), 3+1, EXP(CQ("Tue")) },
1557 { CQ("%A"), 7+1, EXP(CQ("Tuesday")) },
1558 { CQ("%b"), 3+1, EXP(CQ("Dec")) },
1559 { CQ("%B"), 8+1, EXP(CQ("December")) },
1560 { CQ("%c"), 24+1, EXP(CQ("Tue Dec 30 09:53:47 2008")) },
1561 { CQ("%C"), 2+1, EXP(CQ("20")) },
1562 { CQ("%d"), 2+1, EXP(CQ("30")) },
1563 { CQ("%D"), 8+1, EXP(CQ("12/30/08")) },
1564 { CQ("%e"), 2+1, EXP(CQ("30")) },
1565 { CQ("%F"), 10+1, EXP(CQ("2008-12-30")) },
1566 { CQ("%g"), 2+1, EXP(CQ("09")) },
1567 { CQ("%G"), 4+1, EXP(CQ("2009")) },
1568 { CQ("%h"), 3+1, EXP(CQ("Dec")) },
1569 { CQ("%H"), 2+1, EXP(CQ("09")) },
1570 { CQ("%I"), 2+1, EXP(CQ("09")) },
1571 { CQ("%j"), 3+1, EXP(CQ("365")) },
1572 { CQ("%k"), 2+1, EXP(CQ(" 9")) },
1573 { CQ("%l"), 2+1, EXP(CQ(" 9")) },
1574 { CQ("%m"), 2+1, EXP(CQ("12")) },
1575 { CQ("%M"), 2+1, EXP(CQ("53")) },
1576 { CQ("%n"), 1+1, EXP(CQ("\n")) },
1577 { CQ("%p"), 2+1, EXP(CQ("AM")) },
1578 { CQ("%q"), 1+1, EXP(CQ("4")) },
1579 { CQ("%r"), 11+1, EXP(CQ("09:53:47 AM")) },
1580 { CQ("%R"), 5+1, EXP(CQ("09:53")) },
1581 { CQ("%s"), 2+1, EXP(CQ("1230648827")) },
1582 { CQ("%S"), 2+1, EXP(CQ("47")) },
1583 { CQ("%t"), 1+1, EXP(CQ("\t")) },
1584 { CQ("%T"), 8+1, EXP(CQ("09:53:47")) },
1585 { CQ("%u"), 1+1, EXP(CQ("2")) },
1586 { CQ("%U"), 2+1, EXP(CQ("52")) },
1587 { CQ("%V"), 2+1, EXP(CQ("01")) },
1588 { CQ("%v"), 11+1, EXP(CQ("30-Dec-2008")) },
1589 { CQ("%w"), 1+1, EXP(CQ("2")) },
1590 { CQ("%W"), 2+1, EXP(CQ("52")) },
1591 { CQ("%x"), 8+1, EXP(CQ("12/30/08")) },
1592 { CQ("%X"), 8+1, EXP(CQ("09:53:47")) },
1593 { CQ("%y"), 2+1, EXP(CQ("08")) },
1594 { CQ("%Y"), 4+1, EXP(CQ("2008")) },
1595 { CQ("%z"), 5+1, EXP(CQ("-0500")) },
1596 { CQ("%Z"), 3+1, EXP(CQ("EST")) },
1597 { CQ("%%"), 1+1, EXP(CQ("%")) },
1600 /* Define list of test inputs and expected outputs, for the given time zone
1602 const struct tm tm1
= {
1603 /* Wed Jul 2 23:01:13 EDT 2008 (time_t=1215054073) */
1614 const struct test Vec1
[] = {
1615 /* Testing fields one at a time, expecting to pass, using exact
1616 * allowed length as what is needed. */
1617 /* Using tm1 for time: */
1618 #define EXP(s) sizeof(s)/sizeof(CHAR)-1, s
1619 { CQ("%a"), 3+1, EXP(CQ("Wed")) },
1620 { CQ("%A"), 9+1, EXP(CQ("Wednesday")) },
1621 { CQ("%b"), 3+1, EXP(CQ("Jul")) },
1622 { CQ("%B"), 4+1, EXP(CQ("July")) },
1623 { CQ("%c"), 24+1, EXP(CQ("Wed Jul 2 23:01:13 2008")) },
1624 { CQ("%C"), 2+1, EXP(CQ("20")) },
1625 { CQ("%d"), 2+1, EXP(CQ("02")) },
1626 { CQ("%D"), 8+1, EXP(CQ("07/02/08")) },
1627 { CQ("%e"), 2+1, EXP(CQ(" 2")) },
1628 { CQ("%F"), 10+1, EXP(CQ("2008-07-02")) },
1629 { CQ("%g"), 2+1, EXP(CQ("08")) },
1630 { CQ("%G"), 4+1, EXP(CQ("2008")) },
1631 { CQ("%h"), 3+1, EXP(CQ("Jul")) },
1632 { CQ("%H"), 2+1, EXP(CQ("23")) },
1633 { CQ("%I"), 2+1, EXP(CQ("11")) },
1634 { CQ("%j"), 3+1, EXP(CQ("184")) },
1635 { CQ("%k"), 2+1, EXP(CQ("23")) },
1636 { CQ("%l"), 2+1, EXP(CQ("11")) },
1637 { CQ("%m"), 2+1, EXP(CQ("07")) },
1638 { CQ("%M"), 2+1, EXP(CQ("01")) },
1639 { CQ("%n"), 1+1, EXP(CQ("\n")) },
1640 { CQ("%p"), 2+1, EXP(CQ("PM")) },
1641 { CQ("%q"), 1+1, EXP(CQ("3")) },
1642 { CQ("%r"), 11+1, EXP(CQ("11:01:13 PM")) },
1643 { CQ("%R"), 5+1, EXP(CQ("23:01")) },
1644 { CQ("%s"), 2+1, EXP(CQ("1215054073")) },
1645 { CQ("%S"), 2+1, EXP(CQ("13")) },
1646 { CQ("%t"), 1+1, EXP(CQ("\t")) },
1647 { CQ("%T"), 8+1, EXP(CQ("23:01:13")) },
1648 { CQ("%u"), 1+1, EXP(CQ("3")) },
1649 { CQ("%U"), 2+1, EXP(CQ("26")) },
1650 { CQ("%V"), 2+1, EXP(CQ("27")) },
1651 { CQ("%v"), 11+1, EXP(CQ(" 2-Jul-2008")) },
1652 { CQ("%w"), 1+1, EXP(CQ("3")) },
1653 { CQ("%W"), 2+1, EXP(CQ("26")) },
1654 { CQ("%x"), 8+1, EXP(CQ("07/02/08")) },
1655 { CQ("%X"), 8+1, EXP(CQ("23:01:13")) },
1656 { CQ("%y"), 2+1, EXP(CQ("08")) },
1657 { CQ("%Y"), 4+1, EXP(CQ("2008")) },
1658 { CQ("%z"), 5+1, EXP(CQ("-0400")) },
1659 { CQ("%Z"), 3+1, EXP(CQ("EDT")) },
1660 { CQ("%%"), 1+1, EXP(CQ("%")) },
1662 #define VEC(s) s, sizeof(s)/sizeof(CHAR), sizeof(s)/sizeof(CHAR)-1, s
1663 #define EXP(s) sizeof(s)/sizeof(CHAR), sizeof(s)/sizeof(CHAR)-1, s
1664 { VEC(CQ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")) },
1665 { CQ("0123456789%%%h:`~"), EXP(CQ("0123456789%Jul:`~")) },
1666 { CQ("%R%h:`~ %x %w"), EXP(CQ("23:01Jul:`~ 07/02/08 3")) },
1671 #if YEAR_BASE == 1900 /* ( */
1672 /* Checks for very large years. YEAR_BASE value relied upon so that the
1673 * answer strings can be predetermined.
1674 * Years more than 4 digits are not mentioned in the standard for %C, so the
1675 * test for those cases are based on the design intent (which is to print the
1676 * whole number, being the century). */
1677 const struct tm tmyr0
= {
1678 /* Wed Jul 2 23:01:13 EDT [HUGE#] */
1684 .tm_year
= INT_MAX
- YEAR_BASE
/2,
1689 #if INT_MAX == 32767
1690 # define YEAR CQ("33717") /* INT_MAX + YEAR_BASE/2 */
1691 # define CENT CQ("337")
1692 # define Year CQ("17")
1693 # elif INT_MAX == 2147483647
1694 # define YEAR CQ("2147484597")
1695 # define CENT CQ("21474845")
1696 # define Year CQ("97")
1697 # elif INT_MAX == 9223372036854775807
1698 # define YEAR CQ("9223372036854776757")
1699 # define CENT CQ("92233720368547777")
1700 # define Year CQ("57")
1702 # error "Unrecognized INT_MAX value: enhance me to recognize what you have"
1704 const struct test Vecyr0
[] = {
1705 /* Testing fields one at a time, expecting to pass, using a larger
1706 * allowed length than what is needed. */
1707 /* Using tmyr0 for time: */
1708 #define EXP(s) sizeof(s)/sizeof(CHAR)-1, s
1709 { CQ("%C"), OUTSIZE
, EXP(CENT
) },
1710 { CQ("%c"), OUTSIZE
, EXP(CQ("Wed Jul 2 23:01:13 ")YEAR
) },
1711 { CQ("%D"), OUTSIZE
, EXP(CQ("07/02/")Year
) },
1712 { CQ("%F"), OUTSIZE
, EXP(YEAR
CQ("-07-02")) },
1713 { CQ("%v"), OUTSIZE
, EXP(CQ(" 2-Jul-")YEAR
) },
1714 { CQ("%x"), OUTSIZE
, EXP(CQ("07/02/")Year
) },
1715 { CQ("%y"), OUTSIZE
, EXP(Year
) },
1716 { CQ("%Y"), OUTSIZE
, EXP(YEAR
) },
1722 /* Checks for very large negative years. YEAR_BASE value relied upon so that
1723 * the answer strings can be predetermined. */
1724 const struct tm tmyr1
= {
1725 /* Wed Jul 2 23:01:13 EDT [HUGE#] */
1736 #if INT_MAX == 32767
1737 # define YEAR CQ("-30868") /* INT_MIN + YEAR_BASE */
1738 # define CENT CQ("-308")
1739 # define Year CQ("68")
1740 # elif INT_MAX == 2147483647
1741 # define YEAR CQ("-2147481748")
1742 # define CENT CQ("-21474817")
1743 # define Year CQ("48")
1744 # elif INT_MAX == 9223372036854775807
1745 # define YEAR CQ("-9223372036854773908")
1746 # define CENT CQ("-92233720368547739")
1747 # define Year CQ("08")
1749 # error "Unrecognized INT_MAX value: enhance me to recognize what you have"
1751 const struct test Vecyr1
[] = {
1752 /* Testing fields one at a time, expecting to pass, using a larger
1753 * allowed length than what is needed. */
1754 /* Using tmyr1 for time: */
1755 #define EXP(s) sizeof(s)/sizeof(CHAR)-1, s
1756 { CQ("%C"), OUTSIZE
, EXP(CENT
) },
1757 { CQ("%c"), OUTSIZE
, EXP(CQ("Wed Jul 2 23:01:13 ")YEAR
) },
1758 { CQ("%D"), OUTSIZE
, EXP(CQ("07/02/")Year
) },
1759 { CQ("%F"), OUTSIZE
, EXP(YEAR
CQ("-07-02")) },
1760 { CQ("%v"), OUTSIZE
, EXP(CQ(" 2-Jul-")YEAR
) },
1761 { CQ("%x"), OUTSIZE
, EXP(CQ("07/02/")Year
) },
1762 { CQ("%y"), OUTSIZE
, EXP(Year
) },
1763 { CQ("%Y"), OUTSIZE
, EXP(YEAR
) },
1769 #endif /* YEAR_BASE ) */
1771 /* Checks for years just over zero (also test for s=60).
1772 * Years less than 4 digits are not mentioned for %Y in the standard, so the
1773 * test for that case is based on the design intent. */
1774 const struct tm tmyrzp
= {
1775 /* Wed Jul 2 23:01:60 EDT 0007 */
1781 .tm_year
= 7-YEAR_BASE
,
1786 #define YEAR CQ("0007") /* Design intent: %Y=%C%y */
1787 #define CENT CQ("00")
1788 #define Year CQ("07")
1789 const struct test Vecyrzp
[] = {
1790 /* Testing fields one at a time, expecting to pass, using a larger
1791 * allowed length than what is needed. */
1792 /* Using tmyrzp for time: */
1793 #define EXP(s) sizeof(s)/sizeof(CHAR)-1, s
1794 { CQ("%C"), OUTSIZE
, EXP(CENT
) },
1795 { CQ("%c"), OUTSIZE
, EXP(CQ("Wed Jul 2 23:01:60 ")YEAR
) },
1796 { CQ("%D"), OUTSIZE
, EXP(CQ("07/02/")Year
) },
1797 { CQ("%F"), OUTSIZE
, EXP(YEAR
CQ("-07-02")) },
1798 { CQ("%v"), OUTSIZE
, EXP(CQ(" 2-Jul-")YEAR
) },
1799 { CQ("%x"), OUTSIZE
, EXP(CQ("07/02/")Year
) },
1800 { CQ("%y"), OUTSIZE
, EXP(Year
) },
1801 { CQ("%Y"), OUTSIZE
, EXP(YEAR
) },
1807 /* Checks for years just under zero.
1808 * Negative years are not handled by the standard, so the vectors here are
1809 * verifying the chosen implemtation. */
1810 const struct tm tmyrzn
= {
1811 /* Wed Jul 2 23:01:00 EDT -004 */
1817 .tm_year
= -4-YEAR_BASE
,
1822 #define YEAR CQ("-004")
1823 #define CENT CQ("-0")
1824 #define Year CQ("04")
1825 const struct test Vecyrzn
[] = {
1826 /* Testing fields one at a time, expecting to pass, using a larger
1827 * allowed length than what is needed. */
1828 /* Using tmyrzn for time: */
1829 #define EXP(s) sizeof(s)/sizeof(CHAR)-1, s
1830 { CQ("%C"), OUTSIZE
, EXP(CENT
) },
1831 { CQ("%c"), OUTSIZE
, EXP(CQ("Wed Jul 2 23:01:00 ")YEAR
) },
1832 { CQ("%D"), OUTSIZE
, EXP(CQ("07/02/")Year
) },
1833 { CQ("%F"), OUTSIZE
, EXP(YEAR
CQ("-07-02")) },
1834 { CQ("%v"), OUTSIZE
, EXP(CQ(" 2-Jul-")YEAR
) },
1835 { CQ("%x"), OUTSIZE
, EXP(CQ("07/02/")Year
) },
1836 { CQ("%y"), OUTSIZE
, EXP(Year
) },
1837 { CQ("%Y"), OUTSIZE
, EXP(YEAR
) },
1844 const struct list ListYr
[] = {
1845 { &tmyrzp
, Vecyrzp
, sizeof(Vecyrzp
)/sizeof(Vecyrzp
[0]) },
1846 { &tmyrzn
, Vecyrzn
, sizeof(Vecyrzn
)/sizeof(Vecyrzn
[0]) },
1847 #if YEAR_BASE == 1900
1848 { &tmyr0
, Vecyr0
, sizeof(Vecyr0
)/sizeof(Vecyr0
[0]) },
1849 { &tmyr1
, Vecyr1
, sizeof(Vecyr1
)/sizeof(Vecyr1
[0]) },
1854 /* List of tests to be run */
1855 const struct list List
[] = {
1856 { &tm0
, Vec0
, sizeof(Vec0
)/sizeof(Vec0
[0]) },
1857 { &tm1
, Vec1
, sizeof(Vec1
)/sizeof(Vec1
[0]) },
1860 #if defined(STUB_getenv_r)
1862 _getenv_r(struct _reent
*p
, const char *cp
) { return getenv(cp
); }
1868 int i
, l
, errr
=0, erro
=0, tot
=0;
1873 /* Set timezone so that %z and %Z tests come out right */
1875 if((i
=putenv(cp
))) {
1876 printf( "putenv(%s) FAILED, ret %d\n", cp
, i
);
1879 if(strcmp(getenv("TZ"),strchr(TZ
,'=')+1)) {
1880 printf( "TZ not set properly in environment\n");
1885 #if defined(VERBOSE)
1886 printf("_timezone=%d, _daylight=%d, _tzname[0]=%s, _tzname[1]=%s\n", _timezone
, _daylight
, _tzname
[0], _tzname
[1]);
1889 __tzinfo_type
*tz
= __gettzinfo ();
1890 /* The sign of this is exactly opposite the envvar TZ. We
1891 could directly use the global _timezone for tm_isdst==0,
1892 but have to use __tzrule for daylight savings. */
1893 printf("tz->__tzrule[0].offset=%d, tz->__tzrule[1].offset=%d\n", tz
->__tzrule
[0].offset
, tz
->__tzrule
[1].offset
);
1897 /* Run all of the exact-length tests as-given--results should match */
1898 for(l
=0; l
<sizeof(List
)/sizeof(List
[0]); l
++) {
1899 const struct list
*test
= &List
[l
];
1900 for(i
=0; i
<test
->cnt
; i
++) {
1901 tot
++; /* Keep track of number of tests */
1902 ret
= strftime(out
, test
->vec
[i
].max
, test
->vec
[i
].fmt
, test
->tms
);
1903 if(ret
!= test
->vec
[i
].ret
) {
1906 "ERROR: return %d != %d expected for List[%d].vec[%d]\n",
1907 ret
, test
->vec
[i
].ret
, l
, i
);
1909 if(strncmp(out
, test
->vec
[i
].out
, test
->vec
[i
].max
-1)) {
1912 "ERROR: \"%"SFLG
"s\" != \"%"SFLG
"s\" expected for List[%d].vec[%d]\n",
1913 out
, test
->vec
[i
].out
, l
, i
);
1918 /* Run all of the exact-length tests with the length made too short--expect to
1920 for(l
=0; l
<sizeof(List
)/sizeof(List
[0]); l
++) {
1921 const struct list
*test
= &List
[l
];
1922 for(i
=0; i
<test
->cnt
; i
++) {
1923 tot
++; /* Keep track of number of tests */
1924 ret
= strftime(out
, test
->vec
[i
].max
-1, test
->vec
[i
].fmt
, test
->tms
);
1928 "ERROR: return %d != %d expected for List[%d].vec[%d]\n",
1931 /* Almost every conversion puts out as many characters as possible, so
1932 * go ahead and test the output even though have failed. (The test
1933 * times chosen happen to not hit any of the cases that fail this, so it
1935 if(strncmp(out
, test
->vec
[i
].out
, test
->vec
[i
].max
-1-1)) {
1938 "ERROR: \"%"SFLG
"s\" != \"%"SFLG
"s\" expected for List[%d].vec[%d]\n",
1939 out
, test
->vec
[i
].out
, l
, i
);
1944 /* Run all of the special year test cases */
1945 for(l
=0; l
<sizeof(ListYr
)/sizeof(ListYr
[0]); l
++) {
1946 const struct list
*test
= &ListYr
[l
];
1947 for(i
=0; i
<test
->cnt
; i
++) {
1948 tot
++; /* Keep track of number of tests */
1949 ret
= strftime(out
, test
->vec
[i
].max
, test
->vec
[i
].fmt
, test
->tms
);
1950 if(ret
!= test
->vec
[i
].ret
) {
1953 "ERROR: return %d != %d expected for ListYr[%d].vec[%d]\n",
1954 ret
, test
->vec
[i
].ret
, l
, i
);
1956 if(strncmp(out
, test
->vec
[i
].out
, test
->vec
[i
].max
-1)) {
1959 "ERROR: \"%"SFLG
"s\" != \"%"SFLG
"s\" expected for ListYr[%d].vec[%d]\n",
1960 out
, test
->vec
[i
].out
, l
, i
);
1965 #define STRIZE(f) #f
1966 #define NAME(f) STRIZE(f)
1967 printf(NAME(strftime
) "() test ");
1968 if(errr
|| erro
) printf("FAILED %d/%d of", errr
, erro
);
1969 else printf("passed");
1970 printf(" %d test cases.\n", tot
);
1972 return(errr
|| erro
);
1974 #endif /* defined(_REGRESSION_TEST) ] */