Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / time / strftime.c
blob56f227c5f3b8e4ebcb2f0dc25c4e26cf6e512bf6
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. */
4 /*
5 * strftime.c
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.
19 FUNCTION
20 <<strftime>>, <<strftime_l>>---convert date and time to a formatted string
22 INDEX
23 strftime
25 INDEX
26 strftime_l
28 SYNOPSIS
29 #include <time.h>
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]>,
36 locale_t <[locale]>);
38 DESCRIPTION
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
55 following ways:
58 o %a
59 The abbreviated weekday name according to the current locale. [tm_wday]
61 o %A
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]
66 o %b
67 The abbreviated month name according to the current locale. [tm_mon]
69 o %B
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]
76 o %c
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]
80 o %C
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]
86 o %d
87 The day of the month, formatted with two digits (from `<<01>>' to
88 `<<31>>'). [tm_mday]
90 o %D
91 A string representing the date, in the form `<<"%m/%d/%y">>'.
92 [tm_mday, tm_mon, tm_year]
94 o %e
95 The day of the month, formatted with leading space if single digit
96 (from `<<1>>' to `<<31>>'). [tm_mday]
98 o %E<<x>>
99 In some locales, the E modifier selects alternative representations of
100 certain modifiers <<x>>. In newlib, it is ignored, and treated as %<<x>>.
102 o %F
103 A string representing the ISO 8601:2000 date format, in the form
104 `<<"%Y-%m-%d">>'. [tm_mday, tm_mon, tm_year]
106 o %g
107 The last two digits of the week-based year, see specifier %G (from
108 `<<00>>' to `<<99>>'). [tm_year, tm_wday, tm_yday]
110 o %G
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]
120 o %h
121 Synonym for "%b". [tm_mon]
123 o %H
124 The hour (on a 24-hour clock), formatted with two digits (from
125 `<<00>>' to `<<23>>'). [tm_hour]
127 o %I
128 The hour (on a 12-hour clock), formatted with two digits (from
129 `<<01>>' to `<<12>>'). [tm_hour]
131 o %j
132 The count of days in the year, formatted with three digits
133 (from `<<001>>' to `<<366>>'). [tm_yday]
135 o %k
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]
139 o %l
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]
143 o %m
144 The month number, formatted with two digits (from `<<01>>' to `<<12>>').
145 [tm_mon]
147 o %M
148 The minute, formatted with two digits (from `<<00>>' to `<<59>>'). [tm_min]
150 o %n
151 A newline character (`<<\n>>').
153 o %O<<x>>
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>>.
157 o %p
158 Either `<<AM>>' or `<<PM>>' as appropriate, or the corresponding strings for
159 the current locale. [tm_hour]
161 o %P
162 Same as '<<%p>>', but in lowercase. This is a GNU extension. [tm_hour]
164 o %q
165 Quarter of the year (from `<<1>>' to `<<4>>'), with January starting
166 the first quarter. This is a GNU extension. [tm_mon]
168 o %r
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]
173 o %R
174 The 24-hour time, to the minute. Equivalent to "%H:%M". [tm_min, tm_hour]
176 o %s
177 The time elapsed, in seconds, since the start of the Unix epoch at
178 1970-01-01 00:00:00 UTC.
180 o %S
181 The second, formatted with two digits (from `<<00>>' to `<<60>>'). The
182 value 60 accounts for the occasional leap second. [tm_sec]
184 o %t
185 A tab character (`<<\t>>').
187 o %T
188 The 24-hour time, to the second. Equivalent to "%H:%M:%S". [tm_sec,
189 tm_min, tm_hour]
191 o %u
192 The weekday as a number, 1-based from Monday (from `<<1>>' to
193 `<<7>>'). [tm_wday]
195 o %U
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]
200 o %V
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]
205 o %v
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]
209 o %w
210 The weekday as a number, 0-based from Sunday (from `<<0>>' to `<<6>>').
211 [tm_wday]
213 o %W
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]
218 o %x
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]
223 o %X
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]
227 o %y
228 The last two digits of the year (from `<<00>>' to `<<99>>'). [tm_year]
229 (Implementation interpretation: always positive, even for negative years.)
231 o %Y
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]
236 o %z
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]
245 o %Z
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]
250 o %%
251 A single character, `<<%>>'.
254 RETURNS
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
260 <[maxsize]> limit.
262 PORTABILITY
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.
276 BUGS
277 (NOT Cygwin:) <<strftime>> ignores the LC_TIME category of the current
278 locale, hard-coding the "C" locale settings.
281 #include <newlib.h>
282 #include <sys/config.h>
283 #include <stddef.h>
284 #include <stdio.h>
285 #include <time.h>
286 #include <string.h>
287 #include <stdlib.h>
288 #include <limits.h>
289 #include <ctype.h>
290 #include <wctype.h>
291 #include "local.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)
312 # else
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))
327 # else
328 # define CTLOCBUFLEN 256 /* Arbitrary big buffer size */
329 const wchar_t *
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 )
335 *len_ret = 0;
336 return buf;
338 # define _ctloc(x) (ctloc = __ctloc (ctlocbuf, _CurrentTimeLocale->x, \
339 &ctloclen))
340 # endif
341 #endif /* MAKE_WCSFTIME */
343 #define CHECK_LENGTH() if (len < 0 || (count += len) >= maxsize) \
344 return 0
346 /* Enforce the coding assumptions that YEAR_BASE is positive. (%C, %Y, etc.) */
347 #if YEAR_BASE < 0
348 # error "YEAR_BASE < 0"
349 #endif
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. */
354 static int
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. */
394 #undef PACK
397 #ifdef _WANT_C99_TIME_FORMATS
398 typedef struct {
399 int year;
400 CHAR *era_C;
401 CHAR *era_Y;
402 } era_info_t;
404 static era_info_t *
405 #if defined (MAKE_WCSFTIME) && defined (__HAVE_LOCALE_INFO_EXTENDED__)
406 get_era_info (const struct tm *tim_p, const wchar_t *era)
407 #else
408 get_era_info (const struct tm *tim_p, const char *era)
409 #endif
411 #if defined (MAKE_WCSFTIME) && defined (__HAVE_LOCALE_INFO_EXTENDED__)
412 wchar_t *c;
413 const wchar_t *dir;
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))
417 #else
418 char *c;
419 const char *dir;
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))
423 #endif
424 long offset;
425 struct tm stm, etm;
426 era_info_t *ei;
428 ei = (era_info_t *) calloc (1, sizeof (era_info_t));
429 if (!ei)
430 return NULL;
432 stm.tm_isdst = etm.tm_isdst = 0;
433 while (era)
435 dir = era;
436 era += 2;
437 offset = ERA_STRTOL (era, &c, 10);
438 era = c + 1;
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)
442 ++stm.tm_year;
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;
446 era = c + 1;
447 if (era[0] == '-' && era[1] == '*')
449 etm = stm;
450 stm.tm_year = INT_MIN;
451 stm.tm_mon = stm.tm_mday = stm.tm_hour = stm.tm_min = stm.tm_sec = 0;
452 era += 3;
454 else if (era[0] == '+' && era[1] == '*')
456 etm.tm_year = INT_MAX;
457 etm.tm_mon = 11;
458 etm.tm_mday = 31;
459 etm.tm_hour = 23;
460 etm.tm_min = etm.tm_sec = 59;
461 era += 3;
463 else
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)
468 ++etm.tm_year;
469 etm.tm_mon = ERA_STRTOL (c + 1, &c, 10) - 1;
470 etm.tm_mday = ERA_STRTOL (c + 1, &c, 10);
471 etm.tm_mday = 31;
472 etm.tm_hour = 23;
473 etm.tm_min = etm.tm_sec = 59;
474 era = c + 1;
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)))))
487 /* Gotcha */
488 size_t len;
490 /* year */
491 if (*dir == '+' && stm.tm_year != INT_MIN)
492 ei->year = tim_p->tm_year - stm.tm_year + offset;
493 else
494 ei->year = etm.tm_year - tim_p->tm_year + offset;
495 /* era_C */
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)
501 free (ei);
502 return NULL;
504 #else
505 len = c - era;
506 #endif
507 ei->era_C = (CHAR *) malloc ((len + 1) * sizeof (CHAR));
508 if (!ei->era_C)
510 free (ei);
511 return NULL;
513 #if defined (MAKE_WCSFTIME) && !defined (__HAVE_LOCALE_INFO_EXTENDED__)
514 len = mbsnrtowcs (ei->era_C, &era, c - era, len + 1, NULL);
515 #else
516 ERA_STRNCPY (ei->era_C, era, len);
517 era += len;
518 #endif
519 ei->era_C[len] = CQ('\0');
520 /* era_Y */
521 ++era;
522 c = ERA_STRCHR (era, ';');
523 if (!c)
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)
529 free (ei->era_C);
530 free (ei);
531 return NULL;
533 #else
534 len = c - era;
535 #endif
536 ei->era_Y = (CHAR *) malloc ((len + 1) * sizeof (CHAR));
537 if (!ei->era_Y)
539 free (ei->era_C);
540 free (ei);
541 return NULL;
543 #if defined (MAKE_WCSFTIME) && !defined (__HAVE_LOCALE_INFO_EXTENDED__)
544 len = mbsnrtowcs (ei->era_Y, &era, c - era, len + 1, NULL);
545 #else
546 ERA_STRNCPY (ei->era_Y, era, len);
547 era += len;
548 #endif
549 ei->era_Y[len] = CQ('\0');
550 return ei;
552 else
553 era = ERA_STRCHR (era, ';');
554 if (era)
555 ++era;
557 return NULL;
560 static void
561 free_era_info (era_info_t *ei)
563 free (ei->era_C);
564 free (ei->era_Y);
565 free (ei);
568 typedef struct {
569 size_t num;
570 CHAR **digit;
571 CHAR *buffer;
572 } alt_digits_t;
574 static alt_digits_t *
575 #if defined (MAKE_WCSFTIME) && defined (__HAVE_LOCALE_INFO_EXTENDED__)
576 get_alt_digits (const wchar_t *alt_digits)
577 #else
578 get_alt_digits (const char *alt_digits)
579 #endif
581 alt_digits_t *adi;
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)
587 #else
588 const char *a, *e;
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)
592 #endif
593 CHAR *aa, *ae;
594 size_t len;
596 adi = (alt_digits_t *) calloc (1, sizeof (alt_digits_t));
597 if (!adi)
598 return NULL;
600 /* Compute number of alt_digits. */
601 adi->num = 1;
602 for (a = alt_digits; (e = ALT_STRCHR (a, ';')) != NULL; a = e + 1)
603 ++adi->num;
604 /* Allocate the `digit' array, which is an array of `num' pointers into
605 `buffer'. */
606 adi->digit = (CHAR **) calloc (adi->num, sizeof (CHAR *));
607 if (!adi->digit)
609 free (adi);
610 return NULL;
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)
617 free (adi->digit);
618 free (adi);
619 return NULL;
621 #else
622 len = ALT_STRLEN (alt_digits);
623 #endif
624 /* Allocate it. */
625 adi->buffer = (CHAR *) malloc ((len + 1) * sizeof (CHAR));
626 if (!adi->buffer)
628 free (adi->digit);
629 free (adi);
630 return NULL;
632 /* Store digits in it. */
633 #if defined (MAKE_WCSFTIME) && !defined (__HAVE_LOCALE_INFO_EXTENDED__)
634 mbstowcs (adi->buffer, alt_digits, len + 1);
635 #else
636 ALT_STRCPY (adi->buffer, alt_digits);
637 #endif
638 /* Store the pointers into `buffer' into the appropriate `digit' slot. */
639 for (len = 0, aa = adi->buffer; (ae = STRCHR (aa, CQ(';'))) != NULL;
640 ++len, aa = ae + 1)
642 *ae = '\0';
643 adi->digit[len] = aa;
645 adi->digit[len] = aa;
646 return adi;
649 static void
650 free_alt_digits (alt_digits_t *adi)
652 free (adi->digit);
653 free (adi->buffer);
654 free (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. */
660 static int
661 conv_to_alt_digits (CHAR *buf, size_t bufsiz, unsigned num, alt_digits_t *adi)
663 if (num < adi->num)
665 size_t len = STRLEN (adi->digit[num]);
666 if (bufsiz < len)
667 return -1;
668 STRCPY (buf, adi->digit[num]);
669 return (int) len;
671 return 0;
674 static size_t
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 */
679 static size_t
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 */
686 size_t count = 0;
687 int len = 0;
688 const CHAR *ctloc;
689 #if defined (MAKE_WCSFTIME) && !defined (__HAVE_LOCALE_INFO_EXTENDED__)
690 CHAR ctlocbuf[CTLOCBUFLEN];
691 #endif
692 size_t i, ctloclen;
693 CHAR alt;
694 CHAR pad;
695 unsigned long width;
696 int tzset_called = 0;
698 const struct lc_time_T *_CurrentTimeLocale = __get_time_locale (locale);
699 for (;;)
701 while (*format && *format != CQ('%'))
703 if (count < maxsize - 1)
704 s[count++] = *format++;
705 else
706 return 0;
708 if (*format == CQ('\0'))
709 break;
710 format++;
711 pad = '\0';
712 width = 0;
714 /* POSIX-1.2008 feature: '0' and '+' modifiers require 0-padding with
715 slightly different semantics. */
716 if (*format == CQ('0') || *format == CQ('+'))
717 pad = *format++;
719 /* POSIX-1.2008 feature: A minimum field width can be specified. */
720 if (*format >= CQ('1') && *format <= CQ('9'))
722 CHAR *fp;
723 width = STRTOUL (format, &fp, 10);
724 format = fp;
727 alt = CQ('\0');
728 if (*format == CQ('E'))
730 alt = *format++;
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);
735 #else
736 if (!*era_info && *_CurrentTimeLocale->era)
737 *era_info = get_era_info (tim_p, _CurrentTimeLocale->era);
738 #endif
739 #endif /* _WANT_C99_TIME_FORMATS */
741 else if (*format == CQ('O'))
743 alt = *format++;
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);
748 #else
749 if (!*alt_digits && *_CurrentTimeLocale->alt_digits)
750 *alt_digits = get_alt_digits (_CurrentTimeLocale->alt_digits);
751 #endif
752 #endif /* _WANT_C99_TIME_FORMATS */
755 switch (*format)
757 case CQ('a'):
758 _ctloc (wday[tim_p->tm_wday]);
759 for (i = 0; i < ctloclen; i++)
761 if (count < maxsize - 1)
762 s[count++] = ctloc[i];
763 else
764 return 0;
766 break;
767 case CQ('A'):
768 _ctloc (weekday[tim_p->tm_wday]);
769 for (i = 0; i < ctloclen; i++)
771 if (count < maxsize - 1)
772 s[count++] = ctloc[i];
773 else
774 return 0;
776 break;
777 case CQ('b'):
778 case CQ('h'):
779 _ctloc (mon[tim_p->tm_mon]);
780 for (i = 0; i < ctloclen; i++)
782 if (count < maxsize - 1)
783 s[count++] = ctloc[i];
784 else
785 return 0;
787 break;
788 case CQ('B'):
789 _ctloc (month[tim_p->tm_mon]);
790 for (i = 0; i < ctloclen; i++)
792 if (count < maxsize - 1)
793 s[count++] = ctloc[i];
794 else
795 return 0;
797 break;
798 case CQ('c'):
799 #ifdef _WANT_C99_TIME_FORMATS
800 if (alt == 'E' && *era_info && *_CurrentTimeLocale->era_d_t_fmt)
801 _ctloc (era_d_t_fmt);
802 else
803 #endif /* _WANT_C99_TIME_FORMATS */
804 _ctloc (c_fmt);
805 goto recurse;
806 case CQ('r'):
807 _ctloc (ampm_fmt);
808 goto recurse;
809 case CQ('x'):
810 #ifdef _WANT_C99_TIME_FORMATS
811 if (alt == 'E' && *era_info && *_CurrentTimeLocale->era_d_fmt)
812 _ctloc (era_d_fmt);
813 else
814 #endif /* _WANT_C99_TIME_FORMATS */
815 _ctloc (x_fmt);
816 goto recurse;
817 case CQ('X'):
818 #ifdef _WANT_C99_TIME_FORMATS
819 if (alt == 'E' && *era_info && *_CurrentTimeLocale->era_t_fmt)
820 _ctloc (era_t_fmt);
821 else
822 #endif /* _WANT_C99_TIME_FORMATS */
823 _ctloc (X_fmt);
824 recurse:
825 if (*ctloc)
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);
830 if (len > 0)
831 count += len;
832 else
833 return 0;
835 break;
836 case CQ('C'):
838 /* Examples of (tm_year + YEAR_BASE) that show how %Y == %C%y
839 with 32-bit int.
840 %Y %C %y
841 2147485547 21474855 47
842 10000 100 00
843 9999 99 99
844 0999 09 99
845 0099 00 99
846 0001 00 01
847 0000 00 00
848 -001 -0 01
849 -099 -0 99
850 -999 -9 99
851 -1000 -10 00
852 -10000 -100 00
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"),
861 (*era_info)->era_C);
862 else
863 #endif /* _WANT_C99_TIME_FORMATS */
865 CHAR *fmt = CQ("%s%.*d");
866 char *pos = "";
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 '+' */
873 fmt = CQ("%s%0.*d");
874 if (century >= 100 && pad == CQ('+'))
875 pos = "+";
877 if (width < 2)
878 width = 2;
879 len = snprintf (&s[count], maxsize - count, fmt,
880 neg ? "-" : pos, width - neg, century);
882 CHECK_LENGTH ();
884 break;
885 case CQ('d'):
886 case CQ('e'):
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,
896 0, *alt_digits);
897 CHECK_LENGTH ();
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);
904 CHECK_LENGTH ();
905 if (len > 0)
906 break;
908 #endif /* _WANT_C99_TIME_FORMATS */
909 len = snprintf (&s[count], maxsize - count,
910 *format == CQ('d') ? CQ("%.2d") : CQ("%2d"),
911 tim_p->tm_mday);
912 CHECK_LENGTH ();
913 break;
914 case CQ('D'):
915 /* %m/%d/%y */
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);
921 CHECK_LENGTH ();
922 break;
923 case CQ('F'):
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;
928 *fmt++ = CQ('%');
929 if (pad) /* '0' or '+' */
930 *fmt++ = pad;
931 else
932 *fmt++ = '+';
933 if (!pad)
934 width = 10;
935 if (width < 6)
936 width = 6;
937 width -= 6;
938 if (width)
940 len = snprintf (fmt, fmtbuf + 32 - fmt, CQ("%lu"), width);
941 if (len > 0)
942 fmt += len;
944 STRCPY (fmt, CQ("Y-%m-%d"));
945 len = __strftime (&s[count], maxsize - count, fmtbuf, tim_p,
946 locale, era_info, alt_digits);
947 if (len > 0)
948 count += len;
949 else
950 return 0;
952 break;
953 case CQ('g'):
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)
961 adjust = 1;
962 else if (adjust > 0 && tim_p->tm_year < -YEAR_BASE)
963 adjust = -1;
964 len = snprintf (&s[count], maxsize - count, CQ("%.2d"),
965 ((year + adjust) % 100 + 100) % 100);
966 CHECK_LENGTH ();
968 break;
969 case CQ('G'):
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)
982 sign = adjust = 1;
983 else if (adjust > 0 && sign)
984 adjust = -1;
985 year += adjust;
986 if (year == -1)
988 year = 99;
989 --century;
991 else if (year == 100)
993 year = 0;
994 ++century;
996 CHAR fmtbuf[10], *fmt = fmtbuf;
997 /* int potentially overflows, so use unsigned instead. */
998 unsigned p_year = century * 100 + year;
999 if (sign)
1000 *fmt++ = CQ('-');
1001 else if (pad == CQ('+') && p_year >= 10000)
1003 *fmt++ = CQ('+');
1004 sign = 1;
1006 if (width && sign)
1007 --width;
1008 *fmt++ = CQ('%');
1009 if (pad)
1010 *fmt++ = CQ('0');
1011 STRCPY (fmt, CQ(".*u"));
1012 len = snprintf (&s[count], maxsize - count, fmtbuf, width, p_year);
1013 if (len < 0 || (count+=len) >= maxsize)
1014 return 0;
1016 break;
1017 case CQ('H'):
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);
1023 CHECK_LENGTH ();
1024 if (len > 0)
1025 break;
1027 #endif /* _WANT_C99_TIME_FORMATS */
1028 /*FALLTHRU*/
1029 case CQ('k'): /* newlib extension */
1030 len = snprintf (&s[count], maxsize - count,
1031 *format == CQ('k') ? CQ("%2d") : CQ("%.2d"),
1032 tim_p->tm_hour);
1033 CHECK_LENGTH ();
1034 break;
1035 case CQ('l'): /* newlib extension */
1036 if (alt == CQ('O'))
1037 alt = CQ('\0');
1038 /*FALLTHRU*/
1039 case CQ('I'):
1041 register int h12;
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,
1047 h12, *alt_digits)))
1048 #endif /* _WANT_C99_TIME_FORMATS */
1049 len = snprintf (&s[count], maxsize - count,
1050 *format == CQ('I') ? CQ("%.2d") : CQ("%2d"), h12);
1051 CHECK_LENGTH ();
1053 break;
1054 case CQ('j'):
1055 len = snprintf (&s[count], maxsize - count, CQ("%.3d"),
1056 tim_p->tm_yday + 1);
1057 CHECK_LENGTH ();
1058 break;
1059 case CQ('m'):
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"),
1066 tim_p->tm_mon + 1);
1067 CHECK_LENGTH ();
1068 break;
1069 case CQ('M'):
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"),
1076 tim_p->tm_min);
1077 CHECK_LENGTH ();
1078 break;
1079 case CQ('n'):
1080 if (count < maxsize - 1)
1081 s[count++] = CQ('\n');
1082 else
1083 return 0;
1084 break;
1085 case CQ('p'):
1086 case CQ('P'):
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])
1092 : ctloc[i]);
1093 else
1094 return 0;
1096 break;
1097 case CQ('q'): /* GNU quarter year */
1098 len = snprintf (&s[count], maxsize - count, CQ("%.1d"),
1099 tim_p->tm_mon / 3 + 1);
1100 CHECK_LENGTH ();
1101 break;
1102 case CQ('R'):
1103 len = snprintf (&s[count], maxsize - count, CQ("%.2d:%.2d"),
1104 tim_p->tm_hour, tim_p->tm_min);
1105 CHECK_LENGTH ();
1106 break;
1107 case CQ('s'):
1109 * From:
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
1128 * OR
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)
1139 TZ_LOCK;
1140 if (!tzset_called)
1142 _tzset_unlocked ();
1143 tzset_called = 1;
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;
1156 #else
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;
1162 #endif
1163 TZ_UNLOCK;
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);
1172 CHECK_LENGTH ();
1174 break;
1175 case CQ('S'):
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"),
1182 tim_p->tm_sec);
1183 CHECK_LENGTH ();
1184 break;
1185 case CQ('t'):
1186 if (count < maxsize - 1)
1187 s[count++] = CQ('\t');
1188 else
1189 return 0;
1190 break;
1191 case 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);
1194 CHECK_LENGTH ();
1195 break;
1196 case CQ('u'):
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
1202 : tim_p->tm_wday,
1203 *alt_digits);
1204 CHECK_LENGTH ();
1205 if (len > 0)
1206 break;
1208 #endif /* _WANT_C99_TIME_FORMATS */
1209 if (count < maxsize - 1)
1211 if (tim_p->tm_wday == 0)
1212 s[count++] = CQ('7');
1213 else
1214 s[count++] = CQ('0') + tim_p->tm_wday;
1216 else
1217 return 0;
1218 break;
1219 case CQ('U'):
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,
1225 *alt_digits)))
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);
1230 CHECK_LENGTH ();
1231 break;
1232 case CQ('V'):
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;
1237 if (adjust > 0)
1238 week = 1;
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
1245 + (YEAR_BASE - 1
1246 - (tim_p->tm_year < 0
1247 ? 0 : 2000)))));
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);
1254 CHECK_LENGTH ();
1256 break;
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 '+' */
1265 *fmt++ = pad;
1266 else
1267 *fmt++ = '+';
1268 if (!pad)
1269 width = 10;
1270 if (width < 6)
1271 width = 6;
1272 width -= 6;
1273 if (width)
1275 len = snprintf (fmt, fmtbuf + 32 - fmt, CQ("%lu"), width);
1276 if (len > 0)
1277 fmt += len;
1279 STRCPY (fmt, CQ("Y"));
1280 len = __strftime (&s[count], maxsize - count, fmtbuf, tim_p,
1281 locale, era_info, alt_digits);
1282 if (len > 0)
1283 count += len;
1284 else
1285 return 0;
1287 break;
1288 case CQ('w'):
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);
1294 CHECK_LENGTH ();
1295 if (len > 0)
1296 break;
1298 #endif /* _WANT_C99_TIME_FORMATS */
1299 if (count < maxsize - 1)
1300 s[count++] = CQ('0') + tim_p->tm_wday;
1301 else
1302 return 0;
1303 break;
1304 case CQ('W'):
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);
1314 CHECK_LENGTH ();
1316 break;
1317 case CQ('y'):
1319 #ifdef _WANT_C99_TIME_FORMATS
1320 if (alt == 'E' && *era_info)
1321 len = snprintf (&s[count], maxsize - count, CQ("%d"),
1322 (*era_info)->year);
1323 else
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"),
1336 year);
1338 CHECK_LENGTH ();
1340 break;
1341 case CQ('Y'):
1342 #ifdef _WANT_C99_TIME_FORMATS
1343 if (alt == 'E' && *era_info)
1345 ctloc = (*era_info)->era_Y;
1346 goto recurse;
1348 else
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;
1356 if (sign)
1358 *fmt++ = CQ('-');
1359 year = UINT_MAX - year + 1;
1361 else if (pad == CQ('+') && year >= 10000)
1363 *fmt++ = CQ('+');
1364 sign = 1;
1366 if (width && sign)
1367 --width;
1368 *fmt++ = CQ('%');
1369 if (pad)
1370 *fmt++ = CQ('0');
1371 STRCPY (fmt, CQ(".*u"));
1372 len = snprintf (&s[count], maxsize - count, fmtbuf, width,
1373 year);
1374 CHECK_LENGTH ();
1376 break;
1377 case CQ('z'):
1378 if (tim_p->tm_isdst >= 0)
1380 long offset;
1382 TZ_LOCK;
1383 if (!tzset_called)
1385 _tzset_unlocked ();
1386 tzset_called = 1;
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;
1399 #else
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;
1405 #endif
1406 TZ_UNLOCK;
1407 len = snprintf (&s[count], maxsize - count, CQ("%+03ld%.2ld"),
1408 offset / SECSPERHOUR,
1409 labs (offset / SECSPERMIN) % 60L);
1410 CHECK_LENGTH ();
1412 break;
1413 case CQ('Z'):
1414 if (tim_p->tm_isdst >= 0)
1416 size_t size;
1417 const char *tznam = NULL;
1419 TZ_LOCK;
1420 if (!tzset_called)
1422 _tzset_unlocked ();
1423 tzset_called = 1;
1425 #if defined (__CYGWIN__)
1426 /* See above. */
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;
1431 #endif
1432 if (!tznam)
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];
1443 else
1445 TZ_UNLOCK;
1446 return 0;
1449 TZ_UNLOCK;
1451 break;
1452 case CQ('%'):
1453 if (count < maxsize - 1)
1454 s[count++] = CQ('%');
1455 else
1456 return 0;
1457 break;
1458 default:
1459 return 0;
1461 if (*format)
1462 format++;
1463 else
1464 break;
1466 if (maxsize)
1467 s[count] = CQ('\0');
1469 return count;
1472 size_t
1473 strftime (CHAR *__restrict s,
1474 size_t maxsize,
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);
1483 if (era_info)
1484 free_era_info (era_info);
1485 if (alt_digits)
1486 free_alt_digits (alt_digits);
1487 return ret;
1488 #else /* !_WANT_C99_TIME_FORMATS */
1489 return __strftime (s, maxsize, format, tim_p, __get_current_locale (),
1490 NULL, NULL);
1491 #endif /* !_WANT_C99_TIME_FORMATS */
1494 size_t
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);
1503 if (era_info)
1504 free_era_info (era_info);
1505 if (alt_digits)
1506 free_alt_digits (alt_digits);
1507 return ret;
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) */
1521 #define OUTSIZE 256
1523 struct test {
1524 CHAR *fmt; /* Testing format */
1525 size_t max; /* Testing maxsize */
1526 size_t ret; /* Expected return value */
1527 CHAR *out; /* Expected output string */
1529 struct list {
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
1538 * and time. */
1539 const struct tm tm0 = {
1540 /* Tue Dec 30 10:53:47 EST 2008 (time_t=1230648827) */
1541 .tm_sec = 47,
1542 .tm_min = 53,
1543 .tm_hour = 9,
1544 .tm_mday = 30,
1545 .tm_mon = 11,
1546 .tm_year = 108,
1547 .tm_wday = 2,
1548 .tm_yday = 364,
1549 .tm_isdst = 0
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("%")) },
1598 #undef EXP
1600 /* Define list of test inputs and expected outputs, for the given time zone
1601 * and time. */
1602 const struct tm tm1 = {
1603 /* Wed Jul 2 23:01:13 EDT 2008 (time_t=1215054073) */
1604 .tm_sec = 13,
1605 .tm_min = 1,
1606 .tm_hour = 23,
1607 .tm_mday = 2,
1608 .tm_mon = 6,
1609 .tm_year = 108,
1610 .tm_wday = 3,
1611 .tm_yday = 183,
1612 .tm_isdst = 1
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("%")) },
1661 #undef EXP
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")) },
1667 #undef VEC
1668 #undef EXP
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#] */
1679 .tm_sec = 13,
1680 .tm_min = 1,
1681 .tm_hour = 23,
1682 .tm_mday = 2,
1683 .tm_mon = 6,
1684 .tm_year = INT_MAX - YEAR_BASE/2,
1685 .tm_wday = 3,
1686 .tm_yday = 183,
1687 .tm_isdst = 1
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")
1701 # else
1702 # error "Unrecognized INT_MAX value: enhance me to recognize what you have"
1703 #endif
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) },
1717 #undef EXP
1719 #undef YEAR
1720 #undef CENT
1721 #undef 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#] */
1726 .tm_sec = 13,
1727 .tm_min = 1,
1728 .tm_hour = 23,
1729 .tm_mday = 2,
1730 .tm_mon = 6,
1731 .tm_year = INT_MIN,
1732 .tm_wday = 3,
1733 .tm_yday = 183,
1734 .tm_isdst = 1
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")
1748 # else
1749 # error "Unrecognized INT_MAX value: enhance me to recognize what you have"
1750 #endif
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) },
1764 #undef EXP
1766 #undef YEAR
1767 #undef CENT
1768 #undef 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 */
1776 .tm_sec = 60,
1777 .tm_min = 1,
1778 .tm_hour = 23,
1779 .tm_mday = 2,
1780 .tm_mon = 6,
1781 .tm_year = 7-YEAR_BASE,
1782 .tm_wday = 3,
1783 .tm_yday = 183,
1784 .tm_isdst = 1
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) },
1802 #undef EXP
1804 #undef YEAR
1805 #undef CENT
1806 #undef 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 */
1812 .tm_sec = 00,
1813 .tm_min = 1,
1814 .tm_hour = 23,
1815 .tm_mday = 2,
1816 .tm_mon = 6,
1817 .tm_year = -4-YEAR_BASE,
1818 .tm_wday = 3,
1819 .tm_yday = 183,
1820 .tm_isdst = 1
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) },
1838 #undef EXP
1840 #undef YEAR
1841 #undef CENT
1842 #undef 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]) },
1850 #endif
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)
1861 char *
1862 _getenv_r(struct _reent *p, const char *cp) { return getenv(cp); }
1863 #endif
1866 main(void)
1868 int i, l, errr=0, erro=0, tot=0;
1869 const char *cp;
1870 CHAR out[OUTSIZE];
1871 size_t ret;
1873 /* Set timezone so that %z and %Z tests come out right */
1874 cp = TZ;
1875 if((i=putenv(cp))) {
1876 printf( "putenv(%s) FAILED, ret %d\n", cp, i);
1877 return(-1);
1879 if(strcmp(getenv("TZ"),strchr(TZ,'=')+1)) {
1880 printf( "TZ not set properly in environment\n");
1881 return(-2);
1883 tzset();
1885 #if defined(VERBOSE)
1886 printf("_timezone=%d, _daylight=%d, _tzname[0]=%s, _tzname[1]=%s\n", _timezone, _daylight, _tzname[0], _tzname[1]);
1888 long offset;
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);
1895 #endif
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) {
1904 errr++;
1905 fprintf(stderr,
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)) {
1910 erro++;
1911 fprintf(stderr,
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
1919 * fail. */
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);
1925 if(ret != 0) {
1926 errr++;
1927 fprintf(stderr,
1928 "ERROR: return %d != %d expected for List[%d].vec[%d]\n",
1929 ret, 0, l, i);
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
1934 * works.) */
1935 if(strncmp(out, test->vec[i].out, test->vec[i].max-1-1)) {
1936 erro++;
1937 fprintf(stderr,
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) {
1951 errr++;
1952 fprintf(stderr,
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)) {
1957 erro++;
1958 fprintf(stderr,
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) ] */