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
9 *
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.
16 */
17
18 /*
19 FUNCTION
20 <<strftime>>, <<strftime_l>>---convert date and time to a formatted string
21
22 INDEX
23 strftime
24
25 INDEX
26 strftime_l
27
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]>);
37
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.
42
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.
46
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:
56
57 o+
58 o %a
59 The abbreviated weekday name according to the current locale. [tm_wday]
60
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]
65
66 o %b
67 The abbreviated month name according to the current locale. [tm_mon]
68
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]
75
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]
79
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]
85
86 o %d
87 The day of the month, formatted with two digits (from `<<01>>' to
88 `<<31>>'). [tm_mday]
89
90 o %D
91 A string representing the date, in the form `<<"%m/%d/%y">>'.
92 [tm_mday, tm_mon, tm_year]
93
94 o %e
95 The day of the month, formatted with leading space if single digit
96 (from `<<1>>' to `<<31>>'). [tm_mday]
97
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>>.
101
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]
105
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]
109
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]
119
120 o %h
121 Synonym for "%b". [tm_mon]
122
123 o %H
124 The hour (on a 24-hour clock), formatted with two digits (from
125 `<<00>>' to `<<23>>'). [tm_hour]
126
127 o %I
128 The hour (on a 12-hour clock), formatted with two digits (from
129 `<<01>>' to `<<12>>'). [tm_hour]
130
131 o %j
132 The count of days in the year, formatted with three digits
133 (from `<<001>>' to `<<366>>'). [tm_yday]
134
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]
138
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]
142
143 o %m
144 The month number, formatted with two digits (from `<<01>>' to `<<12>>').
145 [tm_mon]
146
147 o %M
148 The minute, formatted with two digits (from `<<00>>' to `<<59>>'). [tm_min]
149
150 o %n
151 A newline character (`<<\n>>').
152
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>>.
156
157 o %p
158 Either `<<AM>>' or `<<PM>>' as appropriate, or the corresponding strings for
159 the current locale. [tm_hour]
160
161 o %P
162 Same as '<<%p>>', but in lowercase. This is a GNU extension. [tm_hour]
163
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]
167
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]
172
173 o %R
174 The 24-hour time, to the minute. Equivalent to "%H:%M". [tm_min, tm_hour]
175
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.
179
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]
183
184 o %t
185 A tab character (`<<\t>>').
186
187 o %T
188 The 24-hour time, to the second. Equivalent to "%H:%M:%S". [tm_sec,
189 tm_min, tm_hour]
190
191 o %u
192 The weekday as a number, 1-based from Monday (from `<<1>>' to
193 `<<7>>'). [tm_wday]
194
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]
199
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]
204
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]
208
209 o %w
210 The weekday as a number, 0-based from Sunday (from `<<0>>' to `<<6>>').
211 [tm_wday]
212
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]
217
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]
222
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]
226
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.)
230
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]
235
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]
244
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]
249
250 o %%
251 A single character, `<<%>>'.
252 o-
253
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.
261
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>>.
271
272 <<strftime_l>> is POSIX-1.2008.
273
274 <<strftime>> and <<strftime_l>> require no supporting OS subroutines.
275
276 BUGS
277 (NOT Cygwin:) <<strftime>> ignores the LC_TIME category of the current
278 locale, hard-coding the "C" locale settings.
279 */
280
281 #define _DEFAULT_SOURCE
282 #include <newlib.h>
283 #include <sys/config.h>
284 #include <stddef.h>
285 #include <stdio.h>
286 #include <time.h>
287 #include <string.h>
288 #include <stdlib.h>
289 #include <limits.h>
290 #include <ctype.h>
291 #include <wctype.h>
292 #include "local.h"
293 #include "../locale/setlocale.h"
294
295 /* Defines to make the file dual use for either strftime() or wcsftime().
296 * To get wcsftime, define MAKE_WCSFTIME.
297 * To get strftime, do not define MAKE_WCSFTIME.
298 * Names are kept friendly to strftime() usage. The biggest ugliness is the
299 * use of the CQ() macro to make either regular character constants and
300 * string literals or wide-character constants and wide-character-string
301 * literals, as appropriate. */
302 #if !defined(MAKE_WCSFTIME)
303 # define CHAR char /* string type basis */
304 # define CQ(a) a /* character constant qualifier */
305 # define t_snprintf snprintf /* char equivalent function name */
306 # define t_strncmp strncmp /* char equivalent function name */
307 # define SFLG /* %s flag (null for normal char) */
308 # define _ctloc(x) (ctloclen = strlen (ctloc = _CurrentTimeLocale->x))
309 # define TOLOWER(c) tolower((int)(unsigned char)(c))
310 # define STRTOUL(c,p,b) strtoul((c),(p),(b))
311 # define STRCPY(a,b) strcpy((a),(b))
312 # define STRCHR(a,b) strchr((a),(b))
313 # define STRLEN(a) strlen(a)
314 # else
315 # define strftime wcsftime /* Alternate function name */
316 # define strftime_l wcsftime_l /* Alternate function name */
317 # define CHAR wchar_t /* string type basis */
318 # define CQ(a) L##a /* character constant qualifier */
319 # define t_snprintf swprintf /* wide-char equivalent function name */
320 # define t_strncmp wcsncmp /* wide-char equivalent function name */
321 # define TOLOWER(c) towlower((wint_t)(c))
322 # define STRTOUL(c,p,b) wcstoul((c),(p),(b))
323 # define STRCPY(a,b) wcscpy((a),(b))
324 # define STRCHR(a,b) wcschr((a),(b))
325 # define STRLEN(a) wcslen(a)
326 # define SFLG "l" /* %s flag (l for wide char) */
327 # ifdef __HAVE_LOCALE_INFO_EXTENDED__
328 # define _ctloc(x) (ctloclen = wcslen (ctloc = _CurrentTimeLocale->w##x))
329 # else
330 # define CTLOCBUFLEN 256 /* Arbitrary big buffer size */
331 const wchar_t *
__ctloc(wchar_t * buf,const char * elem,size_t * len_ret)332 __ctloc (wchar_t *buf, const char *elem, size_t *len_ret)
333 {
334 buf[CTLOCBUFLEN - 1] = L'\0';
335 *len_ret = mbstowcs (buf, elem, CTLOCBUFLEN - 1);
336 if (*len_ret == (size_t) -1 )
337 *len_ret = 0;
338 return buf;
339 }
340 # define _ctloc(x) (ctloc = __ctloc (ctlocbuf, _CurrentTimeLocale->x, \
341 &ctloclen))
342 # endif
343 #endif /* MAKE_WCSFTIME */
344
345 #define CHECK_LENGTH() if (len < 0 || (count += len) >= maxsize) \
346 return 0
347
348 /* Enforce the coding assumptions that YEAR_BASE is positive. (%C, %Y, etc.) */
349 #if YEAR_BASE < 0
350 # error "YEAR_BASE < 0"
351 #endif
352
353 /* Using the tm_year, tm_wday, and tm_yday components of TIM_P, return
354 -1, 0, or 1 as the adjustment to add to the year for the ISO week
355 numbering used in "%g%G%V", avoiding overflow. */
356 static int
iso_year_adjust(const struct tm * tim_p)357 iso_year_adjust (const struct tm *tim_p)
358 {
359 /* Account for fact that tm_year==0 is year 1900. */
360 int leap = isleap (tim_p->tm_year + (YEAR_BASE
361 - (tim_p->tm_year < 0 ? 0 : 2000)));
362
363 /* Pack the yday, wday, and leap year into a single int since there are so
364 many disparate cases. */
365 #define PACK(yd, wd, lp) (((yd) << 4) + (wd << 1) + (lp))
366 switch (PACK (tim_p->tm_yday, tim_p->tm_wday, leap))
367 {
368 case PACK (0, 5, 0): /* Jan 1 is Fri, not leap. */
369 case PACK (0, 6, 0): /* Jan 1 is Sat, not leap. */
370 case PACK (0, 0, 0): /* Jan 1 is Sun, not leap. */
371 case PACK (0, 5, 1): /* Jan 1 is Fri, leap year. */
372 case PACK (0, 6, 1): /* Jan 1 is Sat, leap year. */
373 case PACK (0, 0, 1): /* Jan 1 is Sun, leap year. */
374 case PACK (1, 6, 0): /* Jan 2 is Sat, not leap. */
375 case PACK (1, 0, 0): /* Jan 2 is Sun, not leap. */
376 case PACK (1, 6, 1): /* Jan 2 is Sat, leap year. */
377 case PACK (1, 0, 1): /* Jan 2 is Sun, leap year. */
378 case PACK (2, 0, 0): /* Jan 3 is Sun, not leap. */
379 case PACK (2, 0, 1): /* Jan 3 is Sun, leap year. */
380 return -1; /* Belongs to last week of previous year. */
381 case PACK (362, 1, 0): /* Dec 29 is Mon, not leap. */
382 case PACK (363, 1, 1): /* Dec 29 is Mon, leap year. */
383 case PACK (363, 1, 0): /* Dec 30 is Mon, not leap. */
384 case PACK (363, 2, 0): /* Dec 30 is Tue, not leap. */
385 case PACK (364, 1, 1): /* Dec 30 is Mon, leap year. */
386 case PACK (364, 2, 1): /* Dec 30 is Tue, leap year. */
387 case PACK (364, 1, 0): /* Dec 31 is Mon, not leap. */
388 case PACK (364, 2, 0): /* Dec 31 is Tue, not leap. */
389 case PACK (364, 3, 0): /* Dec 31 is Wed, not leap. */
390 case PACK (365, 1, 1): /* Dec 31 is Mon, leap year. */
391 case PACK (365, 2, 1): /* Dec 31 is Tue, leap year. */
392 case PACK (365, 3, 1): /* Dec 31 is Wed, leap year. */
393 return 1; /* Belongs to first week of next year. */
394 }
395 return 0; /* Belongs to specified year. */
396 #undef PACK
397 }
398
399 #ifdef _WANT_C99_TIME_FORMATS
400 typedef struct {
401 int year;
402 CHAR *era_C;
403 CHAR *era_Y;
404 } era_info_t;
405
406 static era_info_t *
407 #if defined (MAKE_WCSFTIME) && defined (__HAVE_LOCALE_INFO_EXTENDED__)
get_era_info(const struct tm * tim_p,const wchar_t * era)408 get_era_info (const struct tm *tim_p, const wchar_t *era)
409 #else
410 get_era_info (const struct tm *tim_p, const char *era)
411 #endif
412 {
413 #if defined (MAKE_WCSFTIME) && defined (__HAVE_LOCALE_INFO_EXTENDED__)
414 wchar_t *c;
415 const wchar_t *dir;
416 # define ERA_STRCHR(a,b) wcschr((a),(b))
417 # define ERA_STRNCPY(a,b,c) wcsncpy((a),(b),(c))
418 # define ERA_STRTOL(a,b,c) wcstol((a),(b),(c))
419 #else
420 char *c;
421 const char *dir;
422 # define ERA_STRCHR(a,b) strchr((a),(b))
423 # define ERA_STRNCPY(a,b,c) strncpy((a),(b),(c))
424 # define ERA_STRTOL(a,b,c) strtol((a),(b),(c))
425 #endif
426 long offset;
427 struct tm stm, etm;
428 era_info_t *ei;
429
430 ei = (era_info_t *) calloc (1, sizeof (era_info_t));
431 if (!ei)
432 return NULL;
433
434 stm.tm_isdst = etm.tm_isdst = 0;
435 while (era)
436 {
437 dir = era;
438 era += 2;
439 offset = ERA_STRTOL (era, &c, 10);
440 era = c + 1;
441 stm.tm_year = ERA_STRTOL (era, &c, 10) - YEAR_BASE;
442 /* Adjust offset for negative gregorian dates. */
443 if (stm.tm_year <= -YEAR_BASE)
444 ++stm.tm_year;
445 stm.tm_mon = ERA_STRTOL (c + 1, &c, 10) - 1;
446 stm.tm_mday = ERA_STRTOL (c + 1, &c, 10);
447 stm.tm_hour = stm.tm_min = stm.tm_sec = 0;
448 era = c + 1;
449 if (era[0] == '-' && era[1] == '*')
450 {
451 etm = stm;
452 stm.tm_year = INT_MIN;
453 stm.tm_mon = stm.tm_mday = stm.tm_hour = stm.tm_min = stm.tm_sec = 0;
454 era += 3;
455 }
456 else if (era[0] == '+' && era[1] == '*')
457 {
458 etm.tm_year = INT_MAX;
459 etm.tm_mon = 11;
460 etm.tm_mday = 31;
461 etm.tm_hour = 23;
462 etm.tm_min = etm.tm_sec = 59;
463 era += 3;
464 }
465 else
466 {
467 etm.tm_year = ERA_STRTOL (era, &c, 10) - YEAR_BASE;
468 /* Adjust offset for negative gregorian dates. */
469 if (etm.tm_year <= -YEAR_BASE)
470 ++etm.tm_year;
471 etm.tm_mon = ERA_STRTOL (c + 1, &c, 10) - 1;
472 etm.tm_mday = ERA_STRTOL (c + 1, &c, 10);
473 etm.tm_mday = 31;
474 etm.tm_hour = 23;
475 etm.tm_min = etm.tm_sec = 59;
476 era = c + 1;
477 }
478 if ((tim_p->tm_year > stm.tm_year
479 || (tim_p->tm_year == stm.tm_year
480 && (tim_p->tm_mon > stm.tm_mon
481 || (tim_p->tm_mon == stm.tm_mon
482 && tim_p->tm_mday >= stm.tm_mday))))
483 && (tim_p->tm_year < etm.tm_year
484 || (tim_p->tm_year == etm.tm_year
485 && (tim_p->tm_mon < etm.tm_mon
486 || (tim_p->tm_mon == etm.tm_mon
487 && tim_p->tm_mday <= etm.tm_mday)))))
488 {
489 /* Gotcha */
490 size_t len;
491
492 /* year */
493 if (*dir == '+' && stm.tm_year != INT_MIN)
494 ei->year = tim_p->tm_year - stm.tm_year + offset;
495 else
496 ei->year = etm.tm_year - tim_p->tm_year + offset;
497 /* era_C */
498 c = ERA_STRCHR (era, ':');
499 #if defined (MAKE_WCSFTIME) && !defined (__HAVE_LOCALE_INFO_EXTENDED__)
500 len = mbsnrtowcs (NULL, &era, c - era, 0, NULL);
501 if (len == (size_t) -1)
502 {
503 free (ei);
504 return NULL;
505 }
506 #else
507 len = c - era;
508 #endif
509 ei->era_C = (CHAR *) malloc ((len + 1) * sizeof (CHAR));
510 if (!ei->era_C)
511 {
512 free (ei);
513 return NULL;
514 }
515 #if defined (MAKE_WCSFTIME) && !defined (__HAVE_LOCALE_INFO_EXTENDED__)
516 len = mbsnrtowcs (ei->era_C, &era, c - era, len + 1, NULL);
517 #else
518 ERA_STRNCPY (ei->era_C, era, len);
519 era += len;
520 #endif
521 ei->era_C[len] = CQ('\0');
522 /* era_Y */
523 ++era;
524 c = ERA_STRCHR (era, ';');
525 if (!c)
526 c = ERA_STRCHR (era, '\0');
527 #if defined (MAKE_WCSFTIME) && !defined (__HAVE_LOCALE_INFO_EXTENDED__)
528 len = mbsnrtowcs (NULL, &era, c - era, 0, NULL);
529 if (len == (size_t) -1)
530 {
531 free (ei->era_C);
532 free (ei);
533 return NULL;
534 }
535 #else
536 len = c - era;
537 #endif
538 ei->era_Y = (CHAR *) malloc ((len + 1) * sizeof (CHAR));
539 if (!ei->era_Y)
540 {
541 free (ei->era_C);
542 free (ei);
543 return NULL;
544 }
545 #if defined (MAKE_WCSFTIME) && !defined (__HAVE_LOCALE_INFO_EXTENDED__)
546 len = mbsnrtowcs (ei->era_Y, &era, c - era, len + 1, NULL);
547 #else
548 ERA_STRNCPY (ei->era_Y, era, len);
549 era += len;
550 #endif
551 ei->era_Y[len] = CQ('\0');
552 return ei;
553 }
554 else
555 era = ERA_STRCHR (era, ';');
556 if (era)
557 ++era;
558 }
559 return NULL;
560 }
561
562 static void
free_era_info(era_info_t * ei)563 free_era_info (era_info_t *ei)
564 {
565 free (ei->era_C);
566 free (ei->era_Y);
567 free (ei);
568 }
569
570 typedef struct {
571 size_t num;
572 CHAR **digit;
573 CHAR *buffer;
574 } alt_digits_t;
575
576 static alt_digits_t *
577 #if defined (MAKE_WCSFTIME) && defined (__HAVE_LOCALE_INFO_EXTENDED__)
get_alt_digits(const wchar_t * alt_digits)578 get_alt_digits (const wchar_t *alt_digits)
579 #else
580 get_alt_digits (const char *alt_digits)
581 #endif
582 {
583 alt_digits_t *adi;
584 #if defined (MAKE_WCSFTIME) && defined (__HAVE_LOCALE_INFO_EXTENDED__)
585 const wchar_t *a, *e;
586 # define ALT_STRCHR(a,b) wcschr((a),(b))
587 # define ALT_STRCPY(a,b) wcscpy((a),(b))
588 # define ALT_STRLEN(a) wcslen(a)
589 #else
590 const char *a, *e;
591 # define ALT_STRCHR(a,b) strchr((a),(b))
592 # define ALT_STRCPY(a,b) strcpy((a),(b))
593 # define ALT_STRLEN(a) strlen(a)
594 #endif
595 CHAR *aa, *ae;
596 size_t len;
597
598 adi = (alt_digits_t *) calloc (1, sizeof (alt_digits_t));
599 if (!adi)
600 return NULL;
601
602 /* Compute number of alt_digits. */
603 adi->num = 1;
604 for (a = alt_digits; (e = ALT_STRCHR (a, ';')) != NULL; a = e + 1)
605 ++adi->num;
606 /* Allocate the `digit' array, which is an array of `num' pointers into
607 `buffer'. */
608 adi->digit = (CHAR **) calloc (adi->num, sizeof (CHAR *));
609 if (!adi->digit)
610 {
611 free (adi);
612 return NULL;
613 }
614 /* Compute memory required for `buffer'. */
615 #if defined (MAKE_WCSFTIME) && !defined (__HAVE_LOCALE_INFO_EXTENDED__)
616 len = mbstowcs (NULL, alt_digits, 0);
617 if (len == (size_t) -1)
618 {
619 free (adi->digit);
620 free (adi);
621 return NULL;
622 }
623 #else
624 len = ALT_STRLEN (alt_digits);
625 #endif
626 /* Allocate it. */
627 adi->buffer = (CHAR *) malloc ((len + 1) * sizeof (CHAR));
628 if (!adi->buffer)
629 {
630 free (adi->digit);
631 free (adi);
632 return NULL;
633 }
634 /* Store digits in it. */
635 #if defined (MAKE_WCSFTIME) && !defined (__HAVE_LOCALE_INFO_EXTENDED__)
636 mbstowcs (adi->buffer, alt_digits, len + 1);
637 #else
638 ALT_STRCPY (adi->buffer, alt_digits);
639 #endif
640 /* Store the pointers into `buffer' into the appropriate `digit' slot. */
641 for (len = 0, aa = adi->buffer; (ae = STRCHR (aa, CQ(';'))) != NULL;
642 ++len, aa = ae + 1)
643 {
644 *ae = '\0';
645 adi->digit[len] = aa;
646 }
647 adi->digit[len] = aa;
648 return adi;
649 }
650
651 static void
free_alt_digits(alt_digits_t * adi)652 free_alt_digits (alt_digits_t *adi)
653 {
654 free (adi->digit);
655 free (adi->buffer);
656 free (adi);
657 }
658
659 /* Return 0 if no alt_digit is available for a number.
660 Return -1 if buffer size isn't sufficient to hold alternative digit.
661 Return length of new digit otherwise. */
662 static int
conv_to_alt_digits(CHAR * buf,size_t bufsiz,unsigned num,alt_digits_t * adi)663 conv_to_alt_digits (CHAR *buf, size_t bufsiz, unsigned num, alt_digits_t *adi)
664 {
665 if (num < adi->num)
666 {
667 size_t len = STRLEN (adi->digit[num]);
668 if (bufsiz < len)
669 return -1;
670 STRCPY (buf, adi->digit[num]);
671 return (int) len;
672 }
673 return 0;
674 }
675
676 static size_t
__strftime(CHAR * s,size_t maxsize,const CHAR * format,const struct tm * tim_p,struct __locale_t * locale,era_info_t ** era_info,alt_digits_t ** alt_digits)677 __strftime (CHAR *s, size_t maxsize, const CHAR *format,
678 const struct tm *tim_p, struct __locale_t *locale,
679 era_info_t **era_info, alt_digits_t **alt_digits)
680 #else /* !_WANT_C99_TIME_FORMATS */
681 static size_t
682 __strftime (CHAR *s, size_t maxsize, const CHAR *format,
683 const struct tm *tim_p, struct __locale_t *locale)
684
685 #define __strftime(s,m,f,t,l,e,a) __strftime((s),(m),(f),(t),(l))
686 #endif /* !_WANT_C99_TIME_FORMATS */
687 {
688 size_t count = 0;
689 int len = 0;
690 const CHAR *ctloc;
691 #if defined (MAKE_WCSFTIME) && !defined (__HAVE_LOCALE_INFO_EXTENDED__)
692 CHAR ctlocbuf[CTLOCBUFLEN];
693 #endif
694 size_t i, ctloclen;
695 CHAR alt;
696 CHAR pad;
697 unsigned long width;
698 int tzset_called = 0;
699
700 const struct lc_time_T *_CurrentTimeLocale = __get_time_locale (locale);
701 for (;;)
702 {
703 while (*format && *format != CQ('%'))
704 {
705 if (count < maxsize - 1)
706 s[count++] = *format++;
707 else
708 return 0;
709 }
710 if (*format == CQ('\0'))
711 break;
712 format++;
713 pad = '\0';
714 width = 0;
715
716 /* POSIX-1.2008 feature: '0' and '+' modifiers require 0-padding with
717 slightly different semantics. */
718 if (*format == CQ('0') || *format == CQ('+'))
719 pad = *format++;
720
721 /* POSIX-1.2008 feature: A minimum field width can be specified. */
722 if (*format >= CQ('1') && *format <= CQ('9'))
723 {
724 CHAR *fp;
725 width = STRTOUL (format, &fp, 10);
726 format = fp;
727 }
728
729 alt = CQ('\0');
730 if (*format == CQ('E'))
731 {
732 alt = *format++;
733 #ifdef _WANT_C99_TIME_FORMATS
734 #if defined (MAKE_WCSFTIME) && defined (__HAVE_LOCALE_INFO_EXTENDED__)
735 if (!*era_info && *_CurrentTimeLocale->wera)
736 *era_info = get_era_info (tim_p, _CurrentTimeLocale->wera);
737 #else
738 if (!*era_info && *_CurrentTimeLocale->era)
739 *era_info = get_era_info (tim_p, _CurrentTimeLocale->era);
740 #endif
741 #endif /* _WANT_C99_TIME_FORMATS */
742 }
743 else if (*format == CQ('O'))
744 {
745 alt = *format++;
746 #ifdef _WANT_C99_TIME_FORMATS
747 #if defined (MAKE_WCSFTIME) && defined (__HAVE_LOCALE_INFO_EXTENDED__)
748 if (!*alt_digits && *_CurrentTimeLocale->walt_digits)
749 *alt_digits = get_alt_digits (_CurrentTimeLocale->walt_digits);
750 #else
751 if (!*alt_digits && *_CurrentTimeLocale->alt_digits)
752 *alt_digits = get_alt_digits (_CurrentTimeLocale->alt_digits);
753 #endif
754 #endif /* _WANT_C99_TIME_FORMATS */
755 }
756
757 switch (*format)
758 {
759 case CQ('a'):
760 _ctloc (wday[tim_p->tm_wday]);
761 for (i = 0; i < ctloclen; i++)
762 {
763 if (count < maxsize - 1)
764 s[count++] = ctloc[i];
765 else
766 return 0;
767 }
768 break;
769 case CQ('A'):
770 _ctloc (weekday[tim_p->tm_wday]);
771 for (i = 0; i < ctloclen; i++)
772 {
773 if (count < maxsize - 1)
774 s[count++] = ctloc[i];
775 else
776 return 0;
777 }
778 break;
779 case CQ('b'):
780 case CQ('h'):
781 _ctloc (mon[tim_p->tm_mon]);
782 for (i = 0; i < ctloclen; i++)
783 {
784 if (count < maxsize - 1)
785 s[count++] = ctloc[i];
786 else
787 return 0;
788 }
789 break;
790 case CQ('B'):
791 _ctloc (month[tim_p->tm_mon]);
792 for (i = 0; i < ctloclen; i++)
793 {
794 if (count < maxsize - 1)
795 s[count++] = ctloc[i];
796 else
797 return 0;
798 }
799 break;
800 case CQ('c'):
801 #ifdef _WANT_C99_TIME_FORMATS
802 if (alt == 'E' && *era_info && *_CurrentTimeLocale->era_d_t_fmt)
803 _ctloc (era_d_t_fmt);
804 else
805 #endif /* _WANT_C99_TIME_FORMATS */
806 _ctloc (c_fmt);
807 goto recurse;
808 case CQ('r'):
809 _ctloc (ampm_fmt);
810 goto recurse;
811 case CQ('x'):
812 #ifdef _WANT_C99_TIME_FORMATS
813 if (alt == 'E' && *era_info && *_CurrentTimeLocale->era_d_fmt)
814 _ctloc (era_d_fmt);
815 else
816 #endif /* _WANT_C99_TIME_FORMATS */
817 _ctloc (x_fmt);
818 goto recurse;
819 case CQ('X'):
820 #ifdef _WANT_C99_TIME_FORMATS
821 if (alt == 'E' && *era_info && *_CurrentTimeLocale->era_t_fmt)
822 _ctloc (era_t_fmt);
823 else
824 #endif /* _WANT_C99_TIME_FORMATS */
825 _ctloc (X_fmt);
826 recurse:
827 if (*ctloc)
828 {
829 /* Recurse to avoid need to replicate %Y formation. */
830 len = __strftime (&s[count], maxsize - count, ctloc, tim_p,
831 locale, era_info, alt_digits);
832 if (len > 0)
833 count += len;
834 else
835 return 0;
836 }
837 break;
838 case CQ('C'):
839 {
840 /* Examples of (tm_year + YEAR_BASE) that show how %Y == %C%y
841 with 32-bit int.
842 %Y %C %y
843 2147485547 21474855 47
844 10000 100 00
845 9999 99 99
846 0999 09 99
847 0099 00 99
848 0001 00 01
849 0000 00 00
850 -001 -0 01
851 -099 -0 99
852 -999 -9 99
853 -1000 -10 00
854 -10000 -100 00
855 -2147481748 -21474817 48
856
857 Be careful of both overflow and sign adjustment due to the
858 asymmetric range of years.
859 */
860 #ifdef _WANT_C99_TIME_FORMATS
861 if (alt == 'E' && *era_info)
862 len = t_snprintf (&s[count], maxsize - count, CQ("%" SFLG "s"),
863 (*era_info)->era_C);
864 else
865 #endif /* _WANT_C99_TIME_FORMATS */
866 {
867 CHAR *fmt = CQ("%s%.*d");
868 char *pos = "";
869 int neg = tim_p->tm_year < -YEAR_BASE;
870 int century = tim_p->tm_year >= 0
871 ? tim_p->tm_year / 100 + YEAR_BASE / 100
872 : abs (tim_p->tm_year + YEAR_BASE) / 100;
873 if (pad) /* '0' or '+' */
874 {
875 fmt = CQ("%s%0.*d");
876 if (century >= 100 && pad == CQ('+'))
877 pos = "+";
878 }
879 if (width < 2)
880 width = 2;
881 len = t_snprintf (&s[count], maxsize - count, fmt,
882 neg ? "-" : pos, width - neg, century);
883 }
884 CHECK_LENGTH ();
885 }
886 break;
887 case CQ('d'):
888 case CQ('e'):
889 #ifdef _WANT_C99_TIME_FORMATS
890 if (alt == CQ('O') && *alt_digits)
891 {
892 if (tim_p->tm_mday < 10)
893 {
894 if (*format == CQ('d'))
895 {
896 if (maxsize - count < 2) return 0;
897 len = conv_to_alt_digits (&s[count], maxsize - count,
898 0, *alt_digits);
899 CHECK_LENGTH ();
900 }
901 if (*format == CQ('e') || len == 0)
902 s[count++] = CQ(' ');
903 }
904 len = conv_to_alt_digits (&s[count], maxsize - count,
905 tim_p->tm_mday, *alt_digits);
906 CHECK_LENGTH ();
907 if (len > 0)
908 break;
909 }
910 #endif /* _WANT_C99_TIME_FORMATS */
911 len = t_snprintf (&s[count], maxsize - count,
912 *format == CQ('d') ? CQ("%.2d") : CQ("%2d"),
913 tim_p->tm_mday);
914 CHECK_LENGTH ();
915 break;
916 case CQ('D'):
917 /* %m/%d/%y */
918 len = t_snprintf (&s[count], maxsize - count,
919 CQ("%.2d/%.2d/%.2d"),
920 tim_p->tm_mon + 1, tim_p->tm_mday,
921 tim_p->tm_year >= 0 ? tim_p->tm_year % 100
922 : abs (tim_p->tm_year + YEAR_BASE) % 100);
923 CHECK_LENGTH ();
924 break;
925 case CQ('F'):
926 { /* %F is equivalent to "%+4Y-%m-%d", flags and width can change
927 that. Recurse to avoid need to replicate %Y formation. */
928 CHAR fmtbuf[32], *fmt = fmtbuf;
929
930 *fmt++ = CQ('%');
931 if (pad) /* '0' or '+' */
932 *fmt++ = pad;
933 else
934 *fmt++ = '+';
935 if (!pad)
936 width = 10;
937 if (width < 6)
938 width = 6;
939 width -= 6;
940 if (width)
941 {
942 len = t_snprintf (fmt, fmtbuf + 32 - fmt, CQ("%lu"), width);
943 if (len > 0)
944 fmt += len;
945 }
946 STRCPY (fmt, CQ("Y-%m-%d"));
947 len = __strftime (&s[count], maxsize - count, fmtbuf, tim_p,
948 locale, era_info, alt_digits);
949 if (len > 0)
950 count += len;
951 else
952 return 0;
953 }
954 break;
955 case CQ('g'):
956 /* Be careful of both overflow and negative years, thanks to
957 the asymmetric range of years. */
958 {
959 int adjust = iso_year_adjust (tim_p);
960 int year = tim_p->tm_year >= 0 ? tim_p->tm_year % 100
961 : abs (tim_p->tm_year + YEAR_BASE) % 100;
962 if (adjust < 0 && tim_p->tm_year <= -YEAR_BASE)
963 adjust = 1;
964 else if (adjust > 0 && tim_p->tm_year < -YEAR_BASE)
965 adjust = -1;
966 len = t_snprintf (&s[count], maxsize - count, CQ("%.2d"),
967 ((year + adjust) % 100 + 100) % 100);
968 CHECK_LENGTH ();
969 }
970 break;
971 case CQ('G'):
972 {
973 /* See the comments for 'C' and 'Y'; this is a variable length
974 field. Although there is no requirement for a minimum number
975 of digits, we use 4 for consistency with 'Y'. */
976 int sign = tim_p->tm_year < -YEAR_BASE;
977 int adjust = iso_year_adjust (tim_p);
978 int century = tim_p->tm_year >= 0
979 ? tim_p->tm_year / 100 + YEAR_BASE / 100
980 : abs (tim_p->tm_year + YEAR_BASE) / 100;
981 int year = tim_p->tm_year >= 0 ? tim_p->tm_year % 100
982 : abs (tim_p->tm_year + YEAR_BASE) % 100;
983 if (adjust < 0 && tim_p->tm_year <= -YEAR_BASE)
984 sign = adjust = 1;
985 else if (adjust > 0 && sign)
986 adjust = -1;
987 year += adjust;
988 if (year == -1)
989 {
990 year = 99;
991 --century;
992 }
993 else if (year == 100)
994 {
995 year = 0;
996 ++century;
997 }
998 CHAR fmtbuf[10], *fmt = fmtbuf;
999 /* int potentially overflows, so use unsigned instead. */
1000 unsigned p_year = century * 100 + year;
1001 if (sign)
1002 *fmt++ = CQ('-');
1003 else if (pad == CQ('+') && p_year >= 10000)
1004 {
1005 *fmt++ = CQ('+');
1006 sign = 1;
1007 }
1008 if (width && sign)
1009 --width;
1010 *fmt++ = CQ('%');
1011 if (pad)
1012 *fmt++ = CQ('0');
1013 STRCPY (fmt, CQ(".*u"));
1014 len = t_snprintf (&s[count], maxsize - count, fmtbuf, width, p_year);
1015 if (len < 0 || (count+=len) >= maxsize)
1016 return 0;
1017 }
1018 break;
1019 case CQ('H'):
1020 #ifdef _WANT_C99_TIME_FORMATS
1021 if (alt == CQ('O') && *alt_digits)
1022 {
1023 len = conv_to_alt_digits (&s[count], maxsize - count,
1024 tim_p->tm_hour, *alt_digits);
1025 CHECK_LENGTH ();
1026 if (len > 0)
1027 break;
1028 }
1029 #endif /* _WANT_C99_TIME_FORMATS */
1030 /*FALLTHRU*/
1031 case CQ('k'): /* newlib extension */
1032 len = t_snprintf (&s[count], maxsize - count,
1033 *format == CQ('k') ? CQ("%2d") : CQ("%.2d"),
1034 tim_p->tm_hour);
1035 CHECK_LENGTH ();
1036 break;
1037 case CQ('l'): /* newlib extension */
1038 if (alt == CQ('O'))
1039 alt = CQ('\0');
1040 /*FALLTHRU*/
1041 case CQ('I'):
1042 {
1043 register int h12;
1044 h12 = (tim_p->tm_hour == 0 || tim_p->tm_hour == 12) ?
1045 12 : tim_p->tm_hour % 12;
1046 #ifdef _WANT_C99_TIME_FORMATS
1047 if (alt != CQ('O') || !*alt_digits
1048 || !(len = conv_to_alt_digits (&s[count], maxsize - count,
1049 h12, *alt_digits)))
1050 #endif /* _WANT_C99_TIME_FORMATS */
1051 len = t_snprintf (&s[count], maxsize - count,
1052 *format == CQ('I') ? CQ("%.2d") : CQ("%2d"), h12);
1053 CHECK_LENGTH ();
1054 }
1055 break;
1056 case CQ('j'):
1057 len = t_snprintf (&s[count], maxsize - count, CQ("%.3d"),
1058 tim_p->tm_yday + 1);
1059 CHECK_LENGTH ();
1060 break;
1061 case CQ('m'):
1062 #ifdef _WANT_C99_TIME_FORMATS
1063 if (alt != CQ('O') || !*alt_digits
1064 || !(len = conv_to_alt_digits (&s[count], maxsize - count,
1065 tim_p->tm_mon + 1, *alt_digits)))
1066 #endif /* _WANT_C99_TIME_FORMATS */
1067 len = t_snprintf (&s[count], maxsize - count, CQ("%.2d"),
1068 tim_p->tm_mon + 1);
1069 CHECK_LENGTH ();
1070 break;
1071 case CQ('M'):
1072 #ifdef _WANT_C99_TIME_FORMATS
1073 if (alt != CQ('O') || !*alt_digits
1074 || !(len = conv_to_alt_digits (&s[count], maxsize - count,
1075 tim_p->tm_min, *alt_digits)))
1076 #endif /* _WANT_C99_TIME_FORMATS */
1077 len = t_snprintf (&s[count], maxsize - count, CQ("%.2d"),
1078 tim_p->tm_min);
1079 CHECK_LENGTH ();
1080 break;
1081 case CQ('n'):
1082 if (count < maxsize - 1)
1083 s[count++] = CQ('\n');
1084 else
1085 return 0;
1086 break;
1087 case CQ('p'):
1088 case CQ('P'):
1089 _ctloc (am_pm[tim_p->tm_hour < 12 ? 0 : 1]);
1090 for (i = 0; i < ctloclen; i++)
1091 {
1092 if (count < maxsize - 1)
1093 s[count++] = (*format == CQ('P') ? (CHAR) TOLOWER (ctloc[i])
1094 : ctloc[i]);
1095 else
1096 return 0;
1097 }
1098 break;
1099 case CQ('q'): /* GNU quarter year */
1100 len = t_snprintf (&s[count], maxsize - count, CQ("%.1d"),
1101 tim_p->tm_mon / 3 + 1);
1102 CHECK_LENGTH ();
1103 break;
1104 case CQ('R'):
1105 len = t_snprintf (&s[count], maxsize - count, CQ("%.2d:%.2d"),
1106 tim_p->tm_hour, tim_p->tm_min);
1107 CHECK_LENGTH ();
1108 break;
1109 case CQ('s'):
1110 /*
1111 * From:
1112 * The Open Group Base Specifications Issue 7
1113 * IEEE Std 1003.1, 2013 Edition
1114 * Copyright (c) 2001-2013 The IEEE and The Open Group
1115 * XBD Base Definitions
1116 * 4. General Concepts
1117 * 4.15 Seconds Since the Epoch
1118 * A value that approximates the number of seconds that have elapsed since the
1119 * Epoch. A Coordinated Universal Time name (specified in terms of seconds
1120 * (tm_sec), minutes (tm_min), hours (tm_hour), days since January 1 of the year
1121 * (tm_yday), and calendar year minus 1900 (tm_year)) is related to a time
1122 * represented as seconds since the Epoch, according to the expression below.
1123 * If the year is <1970 or the value is negative, the relationship is undefined.
1124 * If the year is >=1970 and the value is non-negative, the value is related to a
1125 * Coordinated Universal Time name according to the C-language expression, where
1126 * tm_sec, tm_min, tm_hour, tm_yday, and tm_year are all integer types:
1127 * tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
1128 * (tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
1129 * ((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400
1130 * OR
1131 * ((((tm_year-69)/4 - (tm_year-1)/100 + (tm_year+299)/400 +
1132 * (tm_year-70)*365 + tm_yday)*24 + tm_hour)*60 + tm_min)*60 + tm_sec
1133 */
1134 /* modified from %z case by hoisting offset outside if block and initializing */
1135 {
1136 long offset = 0; /* offset < 0 => W of GMT, > 0 => E of GMT:
1137 subtract to get UTC */
1138
1139 if (tim_p->tm_isdst >= 0)
1140 {
1141 TZ_LOCK;
1142 if (!tzset_called)
1143 {
1144 _tzset_unlocked ();
1145 tzset_called = 1;
1146 }
1147
1148 #if defined (__CYGWIN__)
1149 /* Cygwin must check if the application has been built with or
1150 without the extra tm members for backward compatibility, and
1151 then use either that or the old method fetching from tzinfo.
1152 Rather than pulling in the version check infrastructure, we
1153 just call a Cygwin function. */
1154 extern long __cygwin_gettzoffset (const struct tm *tmp);
1155 offset = __cygwin_gettzoffset (tim_p);
1156 #elif defined (__TM_GMTOFF)
1157 offset = tim_p->__TM_GMTOFF;
1158 #else
1159 __tzinfo_type *tz = __gettzinfo ();
1160 /* The sign of this is exactly opposite the envvar TZ. We
1161 could directly use the global _timezone for tm_isdst==0,
1162 but have to use __tzrule for daylight savings. */
1163 offset = -tz->__tzrule[tim_p->tm_isdst > 0].offset;
1164 #endif
1165 TZ_UNLOCK;
1166 }
1167 len = t_snprintf (&s[count], maxsize - count, CQ("%lld"),
1168 (((((long long)tim_p->tm_year - 69)/4
1169 - (tim_p->tm_year - 1)/100
1170 + (tim_p->tm_year + 299)/400
1171 + (tim_p->tm_year - 70)*365 + tim_p->tm_yday)*24
1172 + tim_p->tm_hour)*60 + tim_p->tm_min)*60
1173 + tim_p->tm_sec - offset);
1174 CHECK_LENGTH ();
1175 }
1176 break;
1177 case CQ('S'):
1178 #ifdef _WANT_C99_TIME_FORMATS
1179 if (alt != CQ('O') || !*alt_digits
1180 || !(len = conv_to_alt_digits (&s[count], maxsize - count,
1181 tim_p->tm_sec, *alt_digits)))
1182 #endif /* _WANT_C99_TIME_FORMATS */
1183 len = t_snprintf (&s[count], maxsize - count, CQ("%.2d"),
1184 tim_p->tm_sec);
1185 CHECK_LENGTH ();
1186 break;
1187 case CQ('t'):
1188 if (count < maxsize - 1)
1189 s[count++] = CQ('\t');
1190 else
1191 return 0;
1192 break;
1193 case CQ('T'):
1194 len = t_snprintf (&s[count], maxsize - count, CQ("%.2d:%.2d:%.2d"),
1195 tim_p->tm_hour, tim_p->tm_min, tim_p->tm_sec);
1196 CHECK_LENGTH ();
1197 break;
1198 case CQ('u'):
1199 #ifdef _WANT_C99_TIME_FORMATS
1200 if (alt == CQ('O') && *alt_digits)
1201 {
1202 len = conv_to_alt_digits (&s[count], maxsize - count,
1203 tim_p->tm_wday == 0 ? 7
1204 : tim_p->tm_wday,
1205 *alt_digits);
1206 CHECK_LENGTH ();
1207 if (len > 0)
1208 break;
1209 }
1210 #endif /* _WANT_C99_TIME_FORMATS */
1211 if (count < maxsize - 1)
1212 {
1213 if (tim_p->tm_wday == 0)
1214 s[count++] = CQ('7');
1215 else
1216 s[count++] = CQ('0') + tim_p->tm_wday;
1217 }
1218 else
1219 return 0;
1220 break;
1221 case CQ('U'):
1222 #ifdef _WANT_C99_TIME_FORMATS
1223 if (alt != CQ('O') || !*alt_digits
1224 || !(len = conv_to_alt_digits (&s[count], maxsize - count,
1225 (tim_p->tm_yday + 7 -
1226 tim_p->tm_wday) / 7,
1227 *alt_digits)))
1228 #endif /* _WANT_C99_TIME_FORMATS */
1229 len = t_snprintf (&s[count], maxsize - count, CQ("%.2d"),
1230 (tim_p->tm_yday + 7 -
1231 tim_p->tm_wday) / 7);
1232 CHECK_LENGTH ();
1233 break;
1234 case CQ('V'):
1235 {
1236 int adjust = iso_year_adjust (tim_p);
1237 int wday = (tim_p->tm_wday) ? tim_p->tm_wday - 1 : 6;
1238 int week = (tim_p->tm_yday + 10 - wday) / 7;
1239 if (adjust > 0)
1240 week = 1;
1241 else if (adjust < 0)
1242 /* Previous year has 53 weeks if current year starts on
1243 Fri, and also if current year starts on Sat and
1244 previous year was leap year. */
1245 week = 52 + (4 >= (wday - tim_p->tm_yday
1246 - isleap (tim_p->tm_year
1247 + (YEAR_BASE - 1
1248 - (tim_p->tm_year < 0
1249 ? 0 : 2000)))));
1250 #ifdef _WANT_C99_TIME_FORMATS
1251 if (alt != CQ('O') || !*alt_digits
1252 || !(len = conv_to_alt_digits (&s[count], maxsize - count,
1253 week, *alt_digits)))
1254 #endif /* _WANT_C99_TIME_FORMATS */
1255 len = t_snprintf (&s[count], maxsize - count, CQ("%.2d"), week);
1256 CHECK_LENGTH ();
1257 }
1258 break;
1259 case CQ('v'): /* BSD/OSX/Ruby extension VMS/Oracle date format
1260 from Arnold Robbins strftime version 3.0 */
1261 { /* %v is equivalent to "%e-%b-%Y", flags and width can change year
1262 format. Recurse to avoid need to replicate %b and %Y formation. */
1263 CHAR fmtbuf[32], *fmt = fmtbuf;
1264 STRCPY (fmt, CQ("%e-%b-%"));
1265 fmt += STRLEN (fmt);
1266 if (pad) /* '0' or '+' */
1267 *fmt++ = pad;
1268 else
1269 *fmt++ = '+';
1270 if (!pad)
1271 width = 10;
1272 if (width < 6)
1273 width = 6;
1274 width -= 6;
1275 if (width)
1276 {
1277 len = t_snprintf (fmt, fmtbuf + 32 - fmt, CQ("%lu"), width);
1278 if (len > 0)
1279 fmt += len;
1280 }
1281 STRCPY (fmt, CQ("Y"));
1282 len = __strftime (&s[count], maxsize - count, fmtbuf, tim_p,
1283 locale, era_info, alt_digits);
1284 if (len > 0)
1285 count += len;
1286 else
1287 return 0;
1288 }
1289 break;
1290 case CQ('w'):
1291 #ifdef _WANT_C99_TIME_FORMATS
1292 if (alt == CQ('O') && *alt_digits)
1293 {
1294 len = conv_to_alt_digits (&s[count], maxsize - count,
1295 tim_p->tm_wday, *alt_digits);
1296 CHECK_LENGTH ();
1297 if (len > 0)
1298 break;
1299 }
1300 #endif /* _WANT_C99_TIME_FORMATS */
1301 if (count < maxsize - 1)
1302 s[count++] = CQ('0') + tim_p->tm_wday;
1303 else
1304 return 0;
1305 break;
1306 case CQ('W'):
1307 {
1308 int wday = (tim_p->tm_wday) ? tim_p->tm_wday - 1 : 6;
1309 wday = (tim_p->tm_yday + 7 - wday) / 7;
1310 #ifdef _WANT_C99_TIME_FORMATS
1311 if (alt != CQ('O') || !*alt_digits
1312 || !(len = conv_to_alt_digits (&s[count], maxsize - count,
1313 wday, *alt_digits)))
1314 #endif /* _WANT_C99_TIME_FORMATS */
1315 len = t_snprintf (&s[count], maxsize - count, CQ("%.2d"), wday);
1316 CHECK_LENGTH ();
1317 }
1318 break;
1319 case CQ('y'):
1320 {
1321 #ifdef _WANT_C99_TIME_FORMATS
1322 if (alt == 'E' && *era_info)
1323 len = t_snprintf (&s[count], maxsize - count, CQ("%d"),
1324 (*era_info)->year);
1325 else
1326 #endif /* _WANT_C99_TIME_FORMATS */
1327 {
1328 /* Be careful of both overflow and negative years, thanks to
1329 the asymmetric range of years. */
1330 int year = tim_p->tm_year >= 0 ? tim_p->tm_year % 100
1331 : abs (tim_p->tm_year + YEAR_BASE) % 100;
1332 #ifdef _WANT_C99_TIME_FORMATS
1333 if (alt != CQ('O') || !*alt_digits
1334 || !(len = conv_to_alt_digits (&s[count], maxsize - count,
1335 year, *alt_digits)))
1336 #endif /* _WANT_C99_TIME_FORMATS */
1337 len = t_snprintf (&s[count], maxsize - count, CQ("%.2d"),
1338 year);
1339 }
1340 CHECK_LENGTH ();
1341 }
1342 break;
1343 case CQ('Y'):
1344 #ifdef _WANT_C99_TIME_FORMATS
1345 if (alt == 'E' && *era_info)
1346 {
1347 ctloc = (*era_info)->era_Y;
1348 goto recurse;
1349 }
1350 else
1351 #endif /* _WANT_C99_TIME_FORMATS */
1352 {
1353 CHAR fmtbuf[10], *fmt = fmtbuf;
1354 int sign = tim_p->tm_year < -YEAR_BASE;
1355 /* int potentially overflows, so use unsigned instead. */
1356 register unsigned year = (unsigned) tim_p->tm_year
1357 + (unsigned) YEAR_BASE;
1358 if (sign)
1359 {
1360 *fmt++ = CQ('-');
1361 year = UINT_MAX - year + 1;
1362 }
1363 else if (pad == CQ('+') && year >= 10000)
1364 {
1365 *fmt++ = CQ('+');
1366 sign = 1;
1367 }
1368 if (width && sign)
1369 --width;
1370 *fmt++ = CQ('%');
1371 if (pad)
1372 *fmt++ = CQ('0');
1373 STRCPY (fmt, CQ(".*u"));
1374 len = t_snprintf (&s[count], maxsize - count, fmtbuf, width,
1375 year);
1376 CHECK_LENGTH ();
1377 }
1378 break;
1379 case CQ('z'):
1380 if (tim_p->tm_isdst >= 0)
1381 {
1382 long offset;
1383
1384 TZ_LOCK;
1385 if (!tzset_called)
1386 {
1387 _tzset_unlocked ();
1388 tzset_called = 1;
1389 }
1390
1391 #if defined (__CYGWIN__)
1392 /* Cygwin must check if the application has been built with or
1393 without the extra tm members for backward compatibility, and
1394 then use either that or the old method fetching from tzinfo.
1395 Rather than pulling in the version check infrastructure, we
1396 just call a Cygwin function. */
1397 extern long __cygwin_gettzoffset (const struct tm *tmp);
1398 offset = __cygwin_gettzoffset (tim_p);
1399 #elif defined (__TM_GMTOFF)
1400 offset = tim_p->__TM_GMTOFF;
1401 #else
1402 __tzinfo_type *tz = __gettzinfo ();
1403 /* The sign of this is exactly opposite the envvar TZ. We
1404 could directly use the global _timezone for tm_isdst==0,
1405 but have to use __tzrule for daylight savings. */
1406 offset = -tz->__tzrule[tim_p->tm_isdst > 0].offset;
1407 #endif
1408 TZ_UNLOCK;
1409 len = t_snprintf (&s[count], maxsize - count, CQ("%+03ld%.2ld"),
1410 offset / SECSPERHOUR,
1411 labs (offset / SECSPERMIN) % 60L);
1412 CHECK_LENGTH ();
1413 }
1414 break;
1415 case CQ('Z'):
1416 if (tim_p->tm_isdst >= 0)
1417 {
1418 size_t size;
1419 const char *tznam = NULL;
1420
1421 TZ_LOCK;
1422 if (!tzset_called)
1423 {
1424 _tzset_unlocked ();
1425 tzset_called = 1;
1426 }
1427 #if defined (__CYGWIN__)
1428 /* See above. */
1429 extern const char *__cygwin_gettzname (const struct tm *tmp);
1430 tznam = __cygwin_gettzname (tim_p);
1431 #elif defined (__TM_ZONE)
1432 tznam = tim_p->__TM_ZONE;
1433 #endif
1434 if (!tznam)
1435 tznam = _tzname[tim_p->tm_isdst > 0];
1436 /* Note that in case of wcsftime this loop only works for
1437 timezone abbreviations using the portable codeset (aka ASCII).
1438 This seems to be the case, but if that ever changes, this
1439 loop needs revisiting. */
1440 size = strlen (tznam);
1441 for (i = 0; i < size; i++)
1442 {
1443 if (count < maxsize - 1)
1444 s[count++] = tznam[i];
1445 else
1446 {
1447 TZ_UNLOCK;
1448 return 0;
1449 }
1450 }
1451 TZ_UNLOCK;
1452 }
1453 break;
1454 case CQ('%'):
1455 if (count < maxsize - 1)
1456 s[count++] = CQ('%');
1457 else
1458 return 0;
1459 break;
1460 default:
1461 return 0;
1462 }
1463 if (*format)
1464 format++;
1465 else
1466 break;
1467 }
1468 if (maxsize)
1469 s[count] = CQ('\0');
1470
1471 return count;
1472 }
1473
1474 size_t
strftime(CHAR * __restrict s,size_t maxsize,const CHAR * __restrict format,const struct tm * __restrict tim_p)1475 strftime (CHAR *__restrict s,
1476 size_t maxsize,
1477 const CHAR *__restrict format,
1478 const struct tm *__restrict tim_p)
1479 {
1480 #ifdef _WANT_C99_TIME_FORMATS
1481 era_info_t *era_info = NULL;
1482 alt_digits_t *alt_digits = NULL;
1483 size_t ret = __strftime (s, maxsize, format, tim_p, __get_current_locale (),
1484 &era_info, &alt_digits);
1485 if (era_info)
1486 free_era_info (era_info);
1487 if (alt_digits)
1488 free_alt_digits (alt_digits);
1489 return ret;
1490 #else /* !_WANT_C99_TIME_FORMATS */
1491 return __strftime (s, maxsize, format, tim_p, __get_current_locale (),
1492 NULL, NULL);
1493 #endif /* !_WANT_C99_TIME_FORMATS */
1494 }
1495
1496 size_t
strftime_l(CHAR * __restrict s,size_t maxsize,const CHAR * __restrict format,const struct tm * __restrict tim_p,struct __locale_t * locale)1497 strftime_l (CHAR *__restrict s, size_t maxsize, const CHAR *__restrict format,
1498 const struct tm *__restrict tim_p, struct __locale_t *locale)
1499 {
1500 #ifdef _WANT_C99_TIME_FORMATS
1501 era_info_t *era_info = NULL;
1502 alt_digits_t *alt_digits = NULL;
1503 size_t ret = __strftime (s, maxsize, format, tim_p, locale,
1504 &era_info, &alt_digits);
1505 if (era_info)
1506 free_era_info (era_info);
1507 if (alt_digits)
1508 free_alt_digits (alt_digits);
1509 return ret;
1510 #else /* !_WANT_C99_TIME_FORMATS */
1511 return __strftime (s, maxsize, format, tim_p, locale, NULL, NULL);
1512 #endif /* !_WANT_C99_TIME_FORMATS */
1513 }
1514
1515 /* The remainder of this file can serve as a regression test. Compile
1516 * with -D_REGRESSION_TEST. */
1517 #if defined(_REGRESSION_TEST) /* [Test code: */
1518
1519 /* This test code relies on ANSI C features, in particular on the ability
1520 * of adjacent strings to be pasted together into one string. */
1521
1522 /* Test output buffer size (should be larger than all expected results) */
1523 #define OUTSIZE 256
1524
1525 struct test {
1526 CHAR *fmt; /* Testing format */
1527 size_t max; /* Testing maxsize */
1528 size_t ret; /* Expected return value */
1529 CHAR *out; /* Expected output string */
1530 };
1531 struct list {
1532 const struct tm *tms; /* Time used for these vectors */
1533 const struct test *vec; /* Test vectors */
1534 int cnt; /* Number of vectors */
1535 };
1536
1537 const char TZ[]="TZ=EST5EDT";
1538
1539 /* Define list of test inputs and expected outputs, for the given time zone
1540 * and time. */
1541 const struct tm tm0 = {
1542 /* Tue Dec 30 10:53:47 EST 2008 (time_t=1230648827) */
1543 .tm_sec = 47,
1544 .tm_min = 53,
1545 .tm_hour = 9,
1546 .tm_mday = 30,
1547 .tm_mon = 11,
1548 .tm_year = 108,
1549 .tm_wday = 2,
1550 .tm_yday = 364,
1551 .tm_isdst = 0
1552 };
1553 const struct test Vec0[] = {
1554 /* Testing fields one at a time, expecting to pass, using exact
1555 * allowed length as what is needed. */
1556 /* Using tm0 for time: */
1557 #define EXP(s) sizeof(s)/sizeof(CHAR)-1, s
1558 { CQ("%a"), 3+1, EXP(CQ("Tue")) },
1559 { CQ("%A"), 7+1, EXP(CQ("Tuesday")) },
1560 { CQ("%b"), 3+1, EXP(CQ("Dec")) },
1561 { CQ("%B"), 8+1, EXP(CQ("December")) },
1562 { CQ("%c"), 24+1, EXP(CQ("Tue Dec 30 09:53:47 2008")) },
1563 { CQ("%C"), 2+1, EXP(CQ("20")) },
1564 { CQ("%d"), 2+1, EXP(CQ("30")) },
1565 { CQ("%D"), 8+1, EXP(CQ("12/30/08")) },
1566 { CQ("%e"), 2+1, EXP(CQ("30")) },
1567 { CQ("%F"), 10+1, EXP(CQ("2008-12-30")) },
1568 { CQ("%g"), 2+1, EXP(CQ("09")) },
1569 { CQ("%G"), 4+1, EXP(CQ("2009")) },
1570 { CQ("%h"), 3+1, EXP(CQ("Dec")) },
1571 { CQ("%H"), 2+1, EXP(CQ("09")) },
1572 { CQ("%I"), 2+1, EXP(CQ("09")) },
1573 { CQ("%j"), 3+1, EXP(CQ("365")) },
1574 { CQ("%k"), 2+1, EXP(CQ(" 9")) },
1575 { CQ("%l"), 2+1, EXP(CQ(" 9")) },
1576 { CQ("%m"), 2+1, EXP(CQ("12")) },
1577 { CQ("%M"), 2+1, EXP(CQ("53")) },
1578 { CQ("%n"), 1+1, EXP(CQ("\n")) },
1579 { CQ("%p"), 2+1, EXP(CQ("AM")) },
1580 { CQ("%q"), 1+1, EXP(CQ("4")) },
1581 { CQ("%r"), 11+1, EXP(CQ("09:53:47 AM")) },
1582 { CQ("%R"), 5+1, EXP(CQ("09:53")) },
1583 { CQ("%s"), 2+1, EXP(CQ("1230648827")) },
1584 { CQ("%S"), 2+1, EXP(CQ("47")) },
1585 { CQ("%t"), 1+1, EXP(CQ("\t")) },
1586 { CQ("%T"), 8+1, EXP(CQ("09:53:47")) },
1587 { CQ("%u"), 1+1, EXP(CQ("2")) },
1588 { CQ("%U"), 2+1, EXP(CQ("52")) },
1589 { CQ("%V"), 2+1, EXP(CQ("01")) },
1590 { CQ("%v"), 11+1, EXP(CQ("30-Dec-2008")) },
1591 { CQ("%w"), 1+1, EXP(CQ("2")) },
1592 { CQ("%W"), 2+1, EXP(CQ("52")) },
1593 { CQ("%x"), 8+1, EXP(CQ("12/30/08")) },
1594 { CQ("%X"), 8+1, EXP(CQ("09:53:47")) },
1595 { CQ("%y"), 2+1, EXP(CQ("08")) },
1596 { CQ("%Y"), 4+1, EXP(CQ("2008")) },
1597 { CQ("%z"), 5+1, EXP(CQ("-0500")) },
1598 { CQ("%Z"), 3+1, EXP(CQ("EST")) },
1599 { CQ("%%"), 1+1, EXP(CQ("%")) },
1600 #undef EXP
1601 };
1602 /* Define list of test inputs and expected outputs, for the given time zone
1603 * and time. */
1604 const struct tm tm1 = {
1605 /* Wed Jul 2 23:01:13 EDT 2008 (time_t=1215054073) */
1606 .tm_sec = 13,
1607 .tm_min = 1,
1608 .tm_hour = 23,
1609 .tm_mday = 2,
1610 .tm_mon = 6,
1611 .tm_year = 108,
1612 .tm_wday = 3,
1613 .tm_yday = 183,
1614 .tm_isdst = 1
1615 };
1616 const struct test Vec1[] = {
1617 /* Testing fields one at a time, expecting to pass, using exact
1618 * allowed length as what is needed. */
1619 /* Using tm1 for time: */
1620 #define EXP(s) sizeof(s)/sizeof(CHAR)-1, s
1621 { CQ("%a"), 3+1, EXP(CQ("Wed")) },
1622 { CQ("%A"), 9+1, EXP(CQ("Wednesday")) },
1623 { CQ("%b"), 3+1, EXP(CQ("Jul")) },
1624 { CQ("%B"), 4+1, EXP(CQ("July")) },
1625 { CQ("%c"), 24+1, EXP(CQ("Wed Jul 2 23:01:13 2008")) },
1626 { CQ("%C"), 2+1, EXP(CQ("20")) },
1627 { CQ("%d"), 2+1, EXP(CQ("02")) },
1628 { CQ("%D"), 8+1, EXP(CQ("07/02/08")) },
1629 { CQ("%e"), 2+1, EXP(CQ(" 2")) },
1630 { CQ("%F"), 10+1, EXP(CQ("2008-07-02")) },
1631 { CQ("%g"), 2+1, EXP(CQ("08")) },
1632 { CQ("%G"), 4+1, EXP(CQ("2008")) },
1633 { CQ("%h"), 3+1, EXP(CQ("Jul")) },
1634 { CQ("%H"), 2+1, EXP(CQ("23")) },
1635 { CQ("%I"), 2+1, EXP(CQ("11")) },
1636 { CQ("%j"), 3+1, EXP(CQ("184")) },
1637 { CQ("%k"), 2+1, EXP(CQ("23")) },
1638 { CQ("%l"), 2+1, EXP(CQ("11")) },
1639 { CQ("%m"), 2+1, EXP(CQ("07")) },
1640 { CQ("%M"), 2+1, EXP(CQ("01")) },
1641 { CQ("%n"), 1+1, EXP(CQ("\n")) },
1642 { CQ("%p"), 2+1, EXP(CQ("PM")) },
1643 { CQ("%q"), 1+1, EXP(CQ("3")) },
1644 { CQ("%r"), 11+1, EXP(CQ("11:01:13 PM")) },
1645 { CQ("%R"), 5+1, EXP(CQ("23:01")) },
1646 { CQ("%s"), 2+1, EXP(CQ("1215054073")) },
1647 { CQ("%S"), 2+1, EXP(CQ("13")) },
1648 { CQ("%t"), 1+1, EXP(CQ("\t")) },
1649 { CQ("%T"), 8+1, EXP(CQ("23:01:13")) },
1650 { CQ("%u"), 1+1, EXP(CQ("3")) },
1651 { CQ("%U"), 2+1, EXP(CQ("26")) },
1652 { CQ("%V"), 2+1, EXP(CQ("27")) },
1653 { CQ("%v"), 11+1, EXP(CQ(" 2-Jul-2008")) },
1654 { CQ("%w"), 1+1, EXP(CQ("3")) },
1655 { CQ("%W"), 2+1, EXP(CQ("26")) },
1656 { CQ("%x"), 8+1, EXP(CQ("07/02/08")) },
1657 { CQ("%X"), 8+1, EXP(CQ("23:01:13")) },
1658 { CQ("%y"), 2+1, EXP(CQ("08")) },
1659 { CQ("%Y"), 4+1, EXP(CQ("2008")) },
1660 { CQ("%z"), 5+1, EXP(CQ("-0400")) },
1661 { CQ("%Z"), 3+1, EXP(CQ("EDT")) },
1662 { CQ("%%"), 1+1, EXP(CQ("%")) },
1663 #undef EXP
1664 #define VEC(s) s, sizeof(s)/sizeof(CHAR), sizeof(s)/sizeof(CHAR)-1, s
1665 #define EXP(s) sizeof(s)/sizeof(CHAR), sizeof(s)/sizeof(CHAR)-1, s
1666 { VEC(CQ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")) },
1667 { CQ("0123456789%%%h:`~"), EXP(CQ("0123456789%Jul:`~")) },
1668 { CQ("%R%h:`~ %x %w"), EXP(CQ("23:01Jul:`~ 07/02/08 3")) },
1669 #undef VEC
1670 #undef EXP
1671 };
1672
1673 #if YEAR_BASE == 1900 /* ( */
1674 /* Checks for very large years. YEAR_BASE value relied upon so that the
1675 * answer strings can be predetermined.
1676 * Years more than 4 digits are not mentioned in the standard for %C, so the
1677 * test for those cases are based on the design intent (which is to print the
1678 * whole number, being the century). */
1679 const struct tm tmyr0 = {
1680 /* Wed Jul 2 23:01:13 EDT [HUGE#] */
1681 .tm_sec = 13,
1682 .tm_min = 1,
1683 .tm_hour = 23,
1684 .tm_mday = 2,
1685 .tm_mon = 6,
1686 .tm_year = INT_MAX - YEAR_BASE/2,
1687 .tm_wday = 3,
1688 .tm_yday = 183,
1689 .tm_isdst = 1
1690 };
1691 #if INT_MAX == 32767
1692 # define YEAR CQ("33717") /* INT_MAX + YEAR_BASE/2 */
1693 # define CENT CQ("337")
1694 # define Year CQ("17")
1695 # elif INT_MAX == 2147483647
1696 # define YEAR CQ("2147484597")
1697 # define CENT CQ("21474845")
1698 # define Year CQ("97")
1699 # elif INT_MAX == 9223372036854775807
1700 # define YEAR CQ("9223372036854776757")
1701 # define CENT CQ("92233720368547777")
1702 # define Year CQ("57")
1703 # else
1704 # error "Unrecognized INT_MAX value: enhance me to recognize what you have"
1705 #endif
1706 const struct test Vecyr0[] = {
1707 /* Testing fields one at a time, expecting to pass, using a larger
1708 * allowed length than what is needed. */
1709 /* Using tmyr0 for time: */
1710 #define EXP(s) sizeof(s)/sizeof(CHAR)-1, s
1711 { CQ("%C"), OUTSIZE, EXP(CENT) },
1712 { CQ("%c"), OUTSIZE, EXP(CQ("Wed Jul 2 23:01:13 ")YEAR) },
1713 { CQ("%D"), OUTSIZE, EXP(CQ("07/02/")Year) },
1714 { CQ("%F"), OUTSIZE, EXP(YEAR CQ("-07-02")) },
1715 { CQ("%v"), OUTSIZE, EXP(CQ(" 2-Jul-")YEAR) },
1716 { CQ("%x"), OUTSIZE, EXP(CQ("07/02/")Year) },
1717 { CQ("%y"), OUTSIZE, EXP(Year) },
1718 { CQ("%Y"), OUTSIZE, EXP(YEAR) },
1719 #undef EXP
1720 };
1721 #undef YEAR
1722 #undef CENT
1723 #undef Year
1724 /* Checks for very large negative years. YEAR_BASE value relied upon so that
1725 * the answer strings can be predetermined. */
1726 const struct tm tmyr1 = {
1727 /* Wed Jul 2 23:01:13 EDT [HUGE#] */
1728 .tm_sec = 13,
1729 .tm_min = 1,
1730 .tm_hour = 23,
1731 .tm_mday = 2,
1732 .tm_mon = 6,
1733 .tm_year = INT_MIN,
1734 .tm_wday = 3,
1735 .tm_yday = 183,
1736 .tm_isdst = 1
1737 };
1738 #if INT_MAX == 32767
1739 # define YEAR CQ("-30868") /* INT_MIN + YEAR_BASE */
1740 # define CENT CQ("-308")
1741 # define Year CQ("68")
1742 # elif INT_MAX == 2147483647
1743 # define YEAR CQ("-2147481748")
1744 # define CENT CQ("-21474817")
1745 # define Year CQ("48")
1746 # elif INT_MAX == 9223372036854775807
1747 # define YEAR CQ("-9223372036854773908")
1748 # define CENT CQ("-92233720368547739")
1749 # define Year CQ("08")
1750 # else
1751 # error "Unrecognized INT_MAX value: enhance me to recognize what you have"
1752 #endif
1753 const struct test Vecyr1[] = {
1754 /* Testing fields one at a time, expecting to pass, using a larger
1755 * allowed length than what is needed. */
1756 /* Using tmyr1 for time: */
1757 #define EXP(s) sizeof(s)/sizeof(CHAR)-1, s
1758 { CQ("%C"), OUTSIZE, EXP(CENT) },
1759 { CQ("%c"), OUTSIZE, EXP(CQ("Wed Jul 2 23:01:13 ")YEAR) },
1760 { CQ("%D"), OUTSIZE, EXP(CQ("07/02/")Year) },
1761 { CQ("%F"), OUTSIZE, EXP(YEAR CQ("-07-02")) },
1762 { CQ("%v"), OUTSIZE, EXP(CQ(" 2-Jul-")YEAR) },
1763 { CQ("%x"), OUTSIZE, EXP(CQ("07/02/")Year) },
1764 { CQ("%y"), OUTSIZE, EXP(Year) },
1765 { CQ("%Y"), OUTSIZE, EXP(YEAR) },
1766 #undef EXP
1767 };
1768 #undef YEAR
1769 #undef CENT
1770 #undef Year
1771 #endif /* YEAR_BASE ) */
1772
1773 /* Checks for years just over zero (also test for s=60).
1774 * Years less than 4 digits are not mentioned for %Y in the standard, so the
1775 * test for that case is based on the design intent. */
1776 const struct tm tmyrzp = {
1777 /* Wed Jul 2 23:01:60 EDT 0007 */
1778 .tm_sec = 60,
1779 .tm_min = 1,
1780 .tm_hour = 23,
1781 .tm_mday = 2,
1782 .tm_mon = 6,
1783 .tm_year = 7-YEAR_BASE,
1784 .tm_wday = 3,
1785 .tm_yday = 183,
1786 .tm_isdst = 1
1787 };
1788 #define YEAR CQ("0007") /* Design intent: %Y=%C%y */
1789 #define CENT CQ("00")
1790 #define Year CQ("07")
1791 const struct test Vecyrzp[] = {
1792 /* Testing fields one at a time, expecting to pass, using a larger
1793 * allowed length than what is needed. */
1794 /* Using tmyrzp for time: */
1795 #define EXP(s) sizeof(s)/sizeof(CHAR)-1, s
1796 { CQ("%C"), OUTSIZE, EXP(CENT) },
1797 { CQ("%c"), OUTSIZE, EXP(CQ("Wed Jul 2 23:01:60 ")YEAR) },
1798 { CQ("%D"), OUTSIZE, EXP(CQ("07/02/")Year) },
1799 { CQ("%F"), OUTSIZE, EXP(YEAR CQ("-07-02")) },
1800 { CQ("%v"), OUTSIZE, EXP(CQ(" 2-Jul-")YEAR) },
1801 { CQ("%x"), OUTSIZE, EXP(CQ("07/02/")Year) },
1802 { CQ("%y"), OUTSIZE, EXP(Year) },
1803 { CQ("%Y"), OUTSIZE, EXP(YEAR) },
1804 #undef EXP
1805 };
1806 #undef YEAR
1807 #undef CENT
1808 #undef Year
1809 /* Checks for years just under zero.
1810 * Negative years are not handled by the standard, so the vectors here are
1811 * verifying the chosen implemtation. */
1812 const struct tm tmyrzn = {
1813 /* Wed Jul 2 23:01:00 EDT -004 */
1814 .tm_sec = 00,
1815 .tm_min = 1,
1816 .tm_hour = 23,
1817 .tm_mday = 2,
1818 .tm_mon = 6,
1819 .tm_year = -4-YEAR_BASE,
1820 .tm_wday = 3,
1821 .tm_yday = 183,
1822 .tm_isdst = 1
1823 };
1824 #define YEAR CQ("-004")
1825 #define CENT CQ("-0")
1826 #define Year CQ("04")
1827 const struct test Vecyrzn[] = {
1828 /* Testing fields one at a time, expecting to pass, using a larger
1829 * allowed length than what is needed. */
1830 /* Using tmyrzn for time: */
1831 #define EXP(s) sizeof(s)/sizeof(CHAR)-1, s
1832 { CQ("%C"), OUTSIZE, EXP(CENT) },
1833 { CQ("%c"), OUTSIZE, EXP(CQ("Wed Jul 2 23:01:00 ")YEAR) },
1834 { CQ("%D"), OUTSIZE, EXP(CQ("07/02/")Year) },
1835 { CQ("%F"), OUTSIZE, EXP(YEAR CQ("-07-02")) },
1836 { CQ("%v"), OUTSIZE, EXP(CQ(" 2-Jul-")YEAR) },
1837 { CQ("%x"), OUTSIZE, EXP(CQ("07/02/")Year) },
1838 { CQ("%y"), OUTSIZE, EXP(Year) },
1839 { CQ("%Y"), OUTSIZE, EXP(YEAR) },
1840 #undef EXP
1841 };
1842 #undef YEAR
1843 #undef CENT
1844 #undef Year
1845
1846 const struct list ListYr[] = {
1847 { &tmyrzp, Vecyrzp, sizeof(Vecyrzp)/sizeof(Vecyrzp[0]) },
1848 { &tmyrzn, Vecyrzn, sizeof(Vecyrzn)/sizeof(Vecyrzn[0]) },
1849 #if YEAR_BASE == 1900
1850 { &tmyr0, Vecyr0, sizeof(Vecyr0)/sizeof(Vecyr0[0]) },
1851 { &tmyr1, Vecyr1, sizeof(Vecyr1)/sizeof(Vecyr1[0]) },
1852 #endif
1853 };
1854
1855
1856 /* List of tests to be run */
1857 const struct list List[] = {
1858 { &tm0, Vec0, sizeof(Vec0)/sizeof(Vec0[0]) },
1859 { &tm1, Vec1, sizeof(Vec1)/sizeof(Vec1[0]) },
1860 };
1861
1862 int
main(void)1863 main(void)
1864 {
1865 int i, l, errr=0, erro=0, tot=0;
1866 const char *cp;
1867 CHAR out[OUTSIZE];
1868 size_t ret;
1869
1870 /* Set timezone so that %z and %Z tests come out right */
1871 cp = TZ;
1872 if((i=putenv(cp))) {
1873 printf( "putenv(%s) FAILED, ret %d\n", cp, i);
1874 return(-1);
1875 }
1876 if(strcmp(getenv("TZ"),strchr(TZ,'=')+1)) {
1877 printf( "TZ not set properly in environment\n");
1878 return(-2);
1879 }
1880 tzset();
1881
1882 #if defined(VERBOSE)
1883 printf("_timezone=%d, _daylight=%d, _tzname[0]=%s, _tzname[1]=%s\n", _timezone, _daylight, _tzname[0], _tzname[1]);
1884 {
1885 long offset;
1886 __tzinfo_type *tz = __gettzinfo ();
1887 /* The sign of this is exactly opposite the envvar TZ. We
1888 could directly use the global _timezone for tm_isdst==0,
1889 but have to use __tzrule for daylight savings. */
1890 printf("tz->__tzrule[0].offset=%d, tz->__tzrule[1].offset=%d\n", tz->__tzrule[0].offset, tz->__tzrule[1].offset);
1891 }
1892 #endif
1893
1894 /* Run all of the exact-length tests as-given--results should match */
1895 for(l=0; l<sizeof(List)/sizeof(List[0]); l++) {
1896 const struct list *test = &List[l];
1897 for(i=0; i<test->cnt; i++) {
1898 tot++; /* Keep track of number of tests */
1899 ret = strftime(out, test->vec[i].max, test->vec[i].fmt, test->tms);
1900 if(ret != test->vec[i].ret) {
1901 errr++;
1902 fprintf(stderr,
1903 "ERROR: return %d != %d expected for List[%d].vec[%d]\n",
1904 ret, test->vec[i].ret, l, i);
1905 }
1906 if(t_strncmp(out, test->vec[i].out, test->vec[i].max-1)) {
1907 erro++;
1908 fprintf(stderr,
1909 "ERROR: \"%"SFLG"s\" != \"%"SFLG"s\" expected for List[%d].vec[%d]\n",
1910 out, test->vec[i].out, l, i);
1911 }
1912 }
1913 }
1914
1915 /* Run all of the exact-length tests with the length made too short--expect to
1916 * fail. */
1917 for(l=0; l<sizeof(List)/sizeof(List[0]); l++) {
1918 const struct list *test = &List[l];
1919 for(i=0; i<test->cnt; i++) {
1920 tot++; /* Keep track of number of tests */
1921 ret = strftime(out, test->vec[i].max-1, test->vec[i].fmt, test->tms);
1922 if(ret != 0) {
1923 errr++;
1924 fprintf(stderr,
1925 "ERROR: return %d != %d expected for List[%d].vec[%d]\n",
1926 ret, 0, l, i);
1927 }
1928 /* Almost every conversion puts out as many characters as possible, so
1929 * go ahead and test the output even though have failed. (The test
1930 * times chosen happen to not hit any of the cases that fail this, so it
1931 * works.) */
1932 if(t_strncmp(out, test->vec[i].out, test->vec[i].max-1-1)) {
1933 erro++;
1934 fprintf(stderr,
1935 "ERROR: \"%"SFLG"s\" != \"%"SFLG"s\" expected for List[%d].vec[%d]\n",
1936 out, test->vec[i].out, l, i);
1937 }
1938 }
1939 }
1940
1941 /* Run all of the special year test cases */
1942 for(l=0; l<sizeof(ListYr)/sizeof(ListYr[0]); l++) {
1943 const struct list *test = &ListYr[l];
1944 for(i=0; i<test->cnt; i++) {
1945 tot++; /* Keep track of number of tests */
1946 ret = strftime(out, test->vec[i].max, test->vec[i].fmt, test->tms);
1947 if(ret != test->vec[i].ret) {
1948 errr++;
1949 fprintf(stderr,
1950 "ERROR: return %d != %d expected for ListYr[%d].vec[%d]\n",
1951 ret, test->vec[i].ret, l, i);
1952 }
1953 if(t_strncmp(out, test->vec[i].out, test->vec[i].max-1)) {
1954 erro++;
1955 fprintf(stderr,
1956 "ERROR: \"%"SFLG"s\" != \"%"SFLG"s\" expected for ListYr[%d].vec[%d]\n",
1957 out, test->vec[i].out, l, i);
1958 }
1959 }
1960 }
1961
1962 #define STRIZE(f) #f
1963 #define NAME(f) STRIZE(f)
1964 printf(NAME(strftime) "() test ");
1965 if(errr || erro) printf("FAILED %d/%d of", errr, erro);
1966 else printf("passed");
1967 printf(" %d test cases.\n", tot);
1968
1969 return(errr || erro);
1970 }
1971 #endif /* defined(_REGRESSION_TEST) ] */
1972