Debian Codegraph @

missing_d/strftime.c

raw
1 /* Copyright (C) 1991-1999, 2000, 2001, 2002, 2003
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301 USA. */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #ifdef _LIBC
25 # define HAVE_LIMITS_H 1
26 # define HAVE_MBLEN 1
27 # define HAVE_MBRLEN 1
28 # define HAVE_STRUCT_ERA_ENTRY 1
29 # define HAVE_TM_GMTOFF 1
30 # define HAVE_TM_ZONE 1
31 # define HAVE_TZNAME 1
32 # define HAVE_TZSET 1
33 # define MULTIBYTE_IS_FORMAT_SAFE 1
34 # define STDC_HEADERS 1
35 # include "../locale/localeinfo.h"
36 #endif
37
38 #if defined emacs && !defined HAVE_BCOPY
39 # define HAVE_MEMCPY 1
40 #endif
41
42 #include <ctype.h>
43 #ifdef TIME_T_IN_SYS_TYPES
44 #include <sys/types.h> /* Some systems define `time_t' here. */
45 #endif
46
47 #ifdef TIME_WITH_SYS_TIME
48 # include <sys/time.h>
49 # include <time.h>
50 #else
51 # ifdef HAVE_SYS_TIME_H
52 # include <sys/time.h>
53 # else
54 # include <time.h>
55 # endif
56 #endif
57 #if HAVE_TZNAME
58 #ifndef __MINGW32__
59 extern char *tzname[];
60 #endif
61 #endif
62
63 /* Do multibyte processing if multibytes are supported, unless
64 multibyte sequences are safe in formats. Multibyte sequences are
65 safe if they cannot contain byte sequences that look like format
66 conversion specifications. The GNU C Library uses UTF8 multibyte
67 encoding, which is safe for formats, but strftime.c can be used
68 with other C libraries that use unsafe encodings. */
69 #define DO_MULTIBYTE (HAVE_MBLEN && ! MULTIBYTE_IS_FORMAT_SAFE)
70
71 #if DO_MULTIBYTE
72 # if HAVE_MBRLEN
73 # include <wchar.h>
74 # else
75 /* Simulate mbrlen with mblen as best we can. */
76 # define mbstate_t int
77 # define mbrlen(s, n, ps) mblen (s, n)
78 # define mbsinit(ps) (*(ps) == 0)
79 # endif
80 static const mbstate_t mbstate_zero;
81 #endif
82
83 #if HAVE_LIMITS_H
84 # include <limits.h>
85 #endif
86
87 #if STDC_HEADERS
88 # include <stddef.h>
89 # include <stdlib.h>
90 # include <string.h>
91 #else
92 # ifndef HAVE_MEMCPY
93 # define memcpy(d, s, n) bcopy ((s), (d), (n))
94 # endif
95 #endif
96
97 #ifdef COMPILE_WIDE
98 # include <endian.h>
99 # define CHAR_T wchar_t
100 # define UCHAR_T unsigned int
101 # define L_(Str) L##Str
102 # define NLW(Sym) _NL_W##Sym
103
104 # define MEMCPY(d, s, n) __wmemcpy (d, s, n)
105 # define STRLEN(s) __wcslen (s)
106
107 #else
108 # define CHAR_T char
109 # define UCHAR_T unsigned char
110 # define L_(Str) Str
111 # define NLW(Sym) Sym
112
113 # if !defined STDC_HEADERS && !defined HAVE_MEMCPY
114 # define MEMCPY(d, s, n) bcopy ((s), (d), (n))
115 # else
116 # define MEMCPY(d, s, n) memcpy ((d), (s), (n))
117 # endif
118 # define STRLEN(s) strlen (s)
119
120 # ifdef _LIBC
121 # define MEMPCPY(d, s, n) __mempcpy (d, s, n)
122 # else
123 # ifndef HAVE_MEMPCPY
124 # define MEMPCPY(d, s, n) ((void *) ((char *) memcpy (d, s, n) + (n)))
125 # endif
126 # endif
127 #endif
128
129 #ifndef __P
130 # if defined __GNUC__ || (defined __STDC__ && __STDC__)
131 # define __P(args) args
132 # else
133 # define __P(args) ()
134 # endif /* GCC. */
135 #endif /* Not __P. */
136
137 #ifndef PTR
138 # ifdef __STDC__
139 # define PTR void *
140 # else
141 # define PTR char *
142 # endif
143 #endif
144
145 #ifndef CHAR_BIT
146 # define CHAR_BIT 8
147 #endif
148
149 #ifndef NULL
150 # define NULL 0
151 #endif
152
153 /* Test for checking whether a given type is signed or not.
154 Some compilers issue a diagnostic about suspicious construct for
155 a test that will always fail when comparing a value that can't be
156 negative against 0 using `<' or `<=' operator. */
157 /* #define TYPE_SIGNED(t) ((t) -1 < 0) */
158 #define TYPE_SIGNED(t) ((t) -1 < 1)
159
160 #ifndef INT_STRLEN_BOUND
161 /* Bound on length of the string representing an integer value of type t.
162 Subtract one for the sign bit if t is signed;
163 302 / 1000 is log10 (2) rounded up;
164 add one for integer division truncation;
165 add one more for a minus sign if t is signed. */
166 #define INT_STRLEN_BOUND(t) \
167 ((sizeof (t) * CHAR_BIT - TYPE_SIGNED (t)) * 302 / 1000 + 1 + TYPE_SIGNED (t))
168 #endif
169
170 #define TM_YEAR_BASE 1900
171
172 #ifndef __isleap
173 /* Nonzero if YEAR is a leap year (every 4 years,
174 except every 100th isn't, and every 400th is). */
175 # define __isleap(year) \
176 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
177 #endif
178
179
180 #ifdef _LIBC
181 # define my_strftime_gmtime_r __gmtime_r
182 # define my_strftime_localtime_r __localtime_r
183 # define tzname __tzname
184 # define tzset __tzset
185 #else
186
187 /* If we're a strftime substitute in a GNU program, then prefer gmtime
188 to gmtime_r, since many gmtime_r implementations are buggy.
189 Similarly for localtime_r. */
190
191 # if ! HAVE_TM_GMTOFF
192 static struct tm *my_strftime_gmtime_r __P ((const time_t *, struct tm *));
193 static struct tm *
194 my_strftime_gmtime_r (t, tp)
195 const time_t *t;
196 struct tm *tp;
197 {
198 struct tm *l = gmtime (t);
199 if (! l)
200 return 0;
201 *tp = *l;
202 return tp;
203 }
204 # endif /* ! HAVE_TM_GMTOFF */
205
206 static struct tm *my_strftime_localtime_r __P ((const time_t *, struct tm *));
207 static struct tm *
208 my_strftime_localtime_r (t, tp)
209 const time_t *t;
210 struct tm *tp;
211 {
212 struct tm *l = localtime (t);
213 if (! l)
214 return 0;
215 *tp = *l;
216 return tp;
217 }
218 #endif /* ! defined _LIBC */
219
220
221 #if !defined memset && !defined HAVE_MEMSET && !defined _LIBC
222 /* Some systems lack the `memset' function and we don't want to
223 introduce additional dependencies. */
224 /* The SGI compiler reportedly barfs on the trailing null
225 if we use a string constant as the initializer. 28 June 1997, rms. */
226 static const CHAR_T spaces[16] = /* " " */
227 {
228 L_(' '),L_(' '),L_(' '),L_(' '),L_(' '),L_(' '),L_(' '),L_(' '),
229 L_(' '),L_(' '),L_(' '),L_(' '),L_(' '),L_(' '),L_(' '),L_(' ')
230 };
231 static const CHAR_T zeroes[16] = /* "0000000000000000" */
232 {
233 L_('0'),L_('0'),L_('0'),L_('0'),L_('0'),L_('0'),L_('0'),L_('0'),
234 L_('0'),L_('0'),L_('0'),L_('0'),L_('0'),L_('0'),L_('0'),L_('0')
235 };
236
237 # define memset_space(P, Len) \
238 do { \
239 int _len = (Len); \
240 \
241 do \
242 { \
243 int _this = _len > 16 ? 16 : _len; \
244 (P) = MEMPCPY ((P), spaces, _this * sizeof (CHAR_T)); \
245 _len -= _this; \
246 } \
247 while (_len > 0); \
248 } while (0)
249
250 # define memset_zero(P, Len) \
251 do { \
252 int _len = (Len); \
253 \
254 do \
255 { \
256 int _this = _len > 16 ? 16 : _len; \
257 (P) = MEMPCPY ((P), zeroes, _this * sizeof (CHAR_T)); \
258 _len -= _this; \
259 } \
260 while (_len > 0); \
261 } while (0)
262 #else
263 # ifdef COMPILE_WIDE
264 # define memset_space(P, Len) (wmemset ((P), L' ', (Len)), (P) += (Len))
265 # define memset_zero(P, Len) (wmemset ((P), L'0', (Len)), (P) += (Len))
266 # else
267 # define memset_space(P, Len) (memset ((P), ' ', (Len)), (P) += (Len))
268 # define memset_zero(P, Len) (memset ((P), '0', (Len)), (P) += (Len))
269 # endif
270 #endif
271
272 #define add(n, f) \
273 do \
274 { \
275 int _n = (n); \
276 int _delta = width - _n; \
277 int _incr = _n + (_delta > 0 ? _delta : 0); \
278 if ((size_t) _incr >= maxsize - i) \
279 return 0; \
280 if (p) \
281 { \
282 if (_delta > 0) \
283 { \
284 if (pad == L_('0')) \
285 memset_zero (p, _delta); \
286 else \
287 memset_space (p, _delta); \
288 } \
289 f; \
290 p += _n; \
291 } \
292 i += _incr; \
293 } while (0)
294
295 #define cpy(n, s) \
296 add ((n), \
297 if (to_lowcase) \
298 memcpy_lowcase (p, (s), _n LOCALE_ARG); \
299 else if (to_uppcase) \
300 memcpy_uppcase (p, (s), _n LOCALE_ARG); \
301 else \
302 MEMCPY ((PTR) p, (const PTR) (s), _n))
303
304 #ifdef COMPILE_WIDE
305 # ifndef USE_IN_EXTENDED_LOCALE_MODEL
306 # undef __mbsrtowcs_l
307 # define __mbsrtowcs_l(d, s, l, st, loc) __mbsrtowcs (d, s, l, st)
308 # endif
309 # define widen(os, ws, l) \
310 { \
311 mbstate_t __st; \
312 const char *__s = os; \
313 memset (&__st, '\0', sizeof (__st)); \
314 l = __mbsrtowcs_l (NULL, &__s, 0, &__st, loc); \
315 ws = alloca ((l + 1) * sizeof (wchar_t)); \
316 (void) __mbsrtowcs_l (ws, &__s, l, &__st, loc); \
317 }
318 #endif
319
320 /* For gawk */
321 #undef TOLOWER
322 #undef TOUPPER
323 #undef ISDIGIT
324
325 #if defined _LIBC && defined USE_IN_EXTENDED_LOCALE_MODEL
326 /* We use this code also for the extended locale handling where the
327 function gets as an additional argument the locale which has to be
328 used. To access the values we have to redefine the _NL_CURRENT
329 macro. */
330 # define strftime __strftime_l
331 # define wcsftime __wcsftime_l
332 # undef _NL_CURRENT
333 # define _NL_CURRENT(category, item) \
334 (current->values[_NL_ITEM_INDEX (item)].string)
335 # define LOCALE_PARAM , loc
336 # define LOCALE_ARG , loc
337 # define LOCALE_PARAM_DECL __locale_t loc;
338 # define LOCALE_PARAM_PROTO , __locale_t loc
339 # define HELPER_LOCALE_ARG , current
340 #else
341 # define LOCALE_PARAM
342 # define LOCALE_PARAM_PROTO
343 # define LOCALE_ARG
344 # define LOCALE_PARAM_DECL
345 # ifdef _LIBC
346 # define HELPER_LOCALE_ARG , _NL_CURRENT_DATA (LC_TIME)
347 # else
348 # define HELPER_LOCALE_ARG
349 # endif
350 #endif
351
352 #ifdef COMPILE_WIDE
353 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
354 # define TOUPPER(Ch, L) __towupper_l (Ch, L)
355 # define TOLOWER(Ch, L) __towlower_l (Ch, L)
356 # else
357 # define TOUPPER(Ch, L) towupper (Ch)
358 # define TOLOWER(Ch, L) towlower (Ch)
359 # endif
360 #else
361 # ifdef _LIBC
362 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
363 # define TOUPPER(Ch, L) __toupper_l (Ch, L)
364 # define TOLOWER(Ch, L) __tolower_l (Ch, L)
365 # else
366 # define TOUPPER(Ch, L) toupper (Ch)
367 # define TOLOWER(Ch, L) tolower (Ch)
368 # endif
369 # else
370 # define TOUPPER(Ch, L) (islower (Ch) ? toupper (Ch) : (Ch))
371 # define TOLOWER(Ch, L) (isupper (Ch) ? tolower (Ch) : (Ch))
372 # endif
373 #endif
374 /* We don't use `isdigit' here since the locale dependent
375 interpretation is not what we want here. We only need to accept
376 the arabic digits in the ASCII range. One day there is perhaps a
377 more reliable way to accept other sets of digits. */
378 #define ISDIGIT(Ch) ((unsigned int) (Ch) - L_('0') <= 9)
379
380 static CHAR_T *memcpy_lowcase __P ((CHAR_T *dest, const CHAR_T *src,
381 size_t len LOCALE_PARAM_PROTO));
382
383 static CHAR_T *
384 memcpy_lowcase (dest, src, len LOCALE_PARAM)
385 CHAR_T *dest;
386 const CHAR_T *src;
387 size_t len;
388 LOCALE_PARAM_DECL
389 {
390 while (len-- > 0)
391 dest[len] = TOLOWER ((UCHAR_T) src[len], loc);
392 return dest;
393 }
394
395 static CHAR_T *memcpy_uppcase __P ((CHAR_T *dest, const CHAR_T *src,
396 size_t len LOCALE_PARAM_PROTO));
397
398 static CHAR_T *
399 memcpy_uppcase (dest, src, len LOCALE_PARAM)
400 CHAR_T *dest;
401 const CHAR_T *src;
402 size_t len;
403 LOCALE_PARAM_DECL
404 {
405 while (len-- > 0)
406 dest[len] = TOUPPER ((UCHAR_T) src[len], loc);
407 return dest;
408 }
409
410
411 #if ! HAVE_TM_GMTOFF
412 /* Yield the difference between *A and *B,
413 measured in seconds, ignoring leap seconds. */
414 # define tm_diff ftime_tm_diff
415 static int tm_diff __P ((const struct tm *, const struct tm *));
416 static int
417 tm_diff (a, b)
418 const struct tm *a;
419 const struct tm *b;
420 {
421 /* Compute intervening leap days correctly even if year is negative.
422 Take care to avoid int overflow in leap day calculations,
423 but it's OK to assume that A and B are close to each other. */
424 int a4 = (a->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (a->tm_year & 3);
425 int b4 = (b->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (b->tm_year & 3);
426 int a100 = a4 / 25 - (a4 % 25 < 0);
427 int b100 = b4 / 25 - (b4 % 25 < 0);
428 int a400 = a100 >> 2;
429 int b400 = b100 >> 2;
430 int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
431 int years = a->tm_year - b->tm_year;
432 int days = (365 * years + intervening_leap_days
433 + (a->tm_yday - b->tm_yday));
434 return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
435 + (a->tm_min - b->tm_min))
436 + (a->tm_sec - b->tm_sec));
437 }
438 #endif /* ! HAVE_TM_GMTOFF */
439
440
441
442 /* The number of days from the first day of the first ISO week of this
443 year to the year day YDAY with week day WDAY. ISO weeks start on
444 Monday; the first ISO week has the year's first Thursday. YDAY may
445 be as small as YDAY_MINIMUM. */
446 #define ISO_WEEK_START_WDAY 1 /* Monday */
447 #define ISO_WEEK1_WDAY 4 /* Thursday */
448 #define YDAY_MINIMUM (-366)
449 static int iso_week_days __P ((int, int));
450 #ifdef __GNUC__
451 __inline__
452 #endif
453 static int
454 iso_week_days (yday, wday)
455 int yday;
456 int wday;
457 {
458 /* Add enough to the first operand of % to make it nonnegative. */
459 int big_enough_multiple_of_7 = (-YDAY_MINIMUM / 7 + 2) * 7;
460 return (yday
461 - (yday - wday + ISO_WEEK1_WDAY + big_enough_multiple_of_7) % 7
462 + ISO_WEEK1_WDAY - ISO_WEEK_START_WDAY);
463 }
464
465
466 #if !(defined _NL_CURRENT || HAVE_STRFTIME)
467 static CHAR_T const weekday_name[][10] =
468 {
469 L_("Sunday"), L_("Monday"), L_("Tuesday"), L_("Wednesday"),
470 L_("Thursday"), L_("Friday"), L_("Saturday")
471 };
472 static CHAR_T const month_name[][10] =
473 {
474 L_("January"), L_("February"), L_("March"), L_("April"), L_("May"),
475 L_("June"), L_("July"), L_("August"), L_("September"), L_("October"),
476 L_("November"), L_("December")
477 };
478 #endif
479
480
481 #ifdef emacs
482 # define my_strftime emacs_strftimeu
483 # define ut_argument , ut
484 # define ut_argument_spec int ut;
485 # define ut_argument_spec_iso , int ut
486 #else
487 # ifdef COMPILE_WIDE
488 # define my_strftime wcsftime
489 # define nl_get_alt_digit _nl_get_walt_digit
490 # else
491 # define my_strftime strftime
492 # define nl_get_alt_digit _nl_get_alt_digit
493 # endif
494 # define ut_argument
495 # define ut_argument_spec
496 # define ut_argument_spec_iso
497 /* We don't have this information in general. */
498 # define ut 0
499 #endif
500
501 #if !defined _LIBC && HAVE_TZNAME && HAVE_TZSET
502 /* Solaris 2.5 tzset sometimes modifies the storage returned by localtime.
503 Work around this bug by copying *tp before it might be munged. */
504 size_t _strftime_copytm __P ((char *, size_t, const char *,
505 const struct tm * ut_argument_spec_iso));
506 size_t
507 my_strftime (s, maxsize, format, tp ut_argument)
508 CHAR_T *s;
509 size_t maxsize;
510 const CHAR_T *format;
511 const struct tm *tp;
512 ut_argument_spec
513 {
514 struct tm tmcopy;
515 tmcopy = *tp;
516 return _strftime_copytm (s, maxsize, format, &tmcopy ut_argument);
517 }
518 # undef my_strftime
519 # define my_strftime _strftime_copytm
520 #endif
521
522
523 /* Write information from TP into S according to the format
524 string FORMAT, writing no more that MAXSIZE characters
525 (including the terminating '\0') and returning number of
526 characters written. If S is NULL, nothing will be written
527 anywhere, so to determine how many characters would be
528 written, use NULL for S and (size_t) UINT_MAX for MAXSIZE. */
529 size_t
530 my_strftime (s, maxsize, format, tp ut_argument LOCALE_PARAM)
531 CHAR_T *s;
532 size_t maxsize;
533 const CHAR_T *format;
534 const struct tm *tp;
535 ut_argument_spec
536 LOCALE_PARAM_DECL
537 {
538 #if defined _LIBC && defined USE_IN_EXTENDED_LOCALE_MODEL
539 struct locale_data *const current = loc->__locales[LC_TIME];
540 #endif
541
542 int hour12 = tp->tm_hour;
543 #ifdef _NL_CURRENT
544 /* We cannot make the following values variables since we must delay
545 the evaluation of these values until really needed since some
546 expressions might not be valid in every situation. The `struct tm'
547 might be generated by a strptime() call that initialized
548 only a few elements. Dereference the pointers only if the format
549 requires this. Then it is ok to fail if the pointers are invalid. */
550 # define a_wkday \
551 ((const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(ABDAY_1) + tp->tm_wday))
552 # define f_wkday \
553 ((const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(DAY_1) + tp->tm_wday))
554 # define a_month \
555 ((const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(ABMON_1) + tp->tm_mon))
556 # define f_month \
557 ((const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(MON_1) + tp->tm_mon))
558 # define ampm \
559 ((const CHAR_T *) _NL_CURRENT (LC_TIME, tp->tm_hour > 11 \
560 ? NLW(PM_STR) : NLW(AM_STR)))
561
562 # define aw_len STRLEN (a_wkday)
563 # define am_len STRLEN (a_month)
564 # define ap_len STRLEN (ampm)
565 #else
566 # if !HAVE_STRFTIME
567 # define f_wkday (weekday_name[tp->tm_wday])
568 # define f_month (month_name[tp->tm_mon])
569 # define a_wkday f_wkday
570 # define a_month f_month
571 # define ampm (L_("AMPM") + 2 * (tp->tm_hour > 11))
572
573 size_t aw_len = 3;
574 size_t am_len = 3;
575 size_t ap_len = 2;
576 # endif
577 #endif
578 const char *zone;
579 size_t i = 0;
580 CHAR_T *p = s;
581 const CHAR_T *f;
582 #if DO_MULTIBYTE && !defined COMPILE_WIDE
583 const char *format_end = NULL;
584 #endif
585
586 zone = NULL;
587 #if HAVE_TM_ZONE
588 /* The POSIX test suite assumes that setting
589 the environment variable TZ to a new value before calling strftime()
590 will influence the result (the %Z format) even if the information in
591 TP is computed with a totally different time zone.
592 This is bogus: though POSIX allows bad behavior like this,
593 POSIX does not require it. Do the right thing instead. */
594 zone = (const char *) tp->tm_zone;
595 #endif
596 #if HAVE_TZNAME
597 if (ut)
598 {
599 if (! (zone && *zone))
600 zone = "GMT";
601 }
602 else
603 {
604 /* POSIX.1 requires that local time zone information is used as
605 though strftime called tzset. */
606 # if HAVE_TZSET
607 tzset ();
608 # endif
609 }
610 #endif
611
612 if (hour12 > 12)
613 hour12 -= 12;
614 else
615 if (hour12 == 0)
616 hour12 = 12;
617
618 for (f = format; *f != '\0'; ++f)
619 {
620 int pad = 0; /* Padding for number ('-', '_', or 0). */
621 int modifier; /* Field modifier ('E', 'O', or 0). */
622 int digits; /* Max digits for numeric format. */
623 int number_value; /* Numeric value to be printed. */
624 int negative_number; /* 1 if the number is negative. */
625 const CHAR_T *subfmt;
626 CHAR_T *bufp;
627 CHAR_T buf[1 + (sizeof (int) < sizeof (time_t)
628 ? INT_STRLEN_BOUND (time_t)
629 : INT_STRLEN_BOUND (int))];
630 int width = -1;
631 int to_lowcase = 0;
632 int to_uppcase = 0;
633 int change_case = 0;
634 int format_char;
635
636 #if DO_MULTIBYTE && !defined COMPILE_WIDE
637 switch (*f)
638 {
639 case L_('%'):
640 break;
641
642 case L_('\b'): case L_('\t'): case L_('\n'):
643 case L_('\v'): case L_('\f'): case L_('\r'):
644 case L_(' '): case L_('!'): case L_('"'): case L_('#'): case L_('&'):
645 case L_('\''): case L_('('): case L_(')'): case L_('*'): case L_('+'):
646 case L_(','): case L_('-'): case L_('.'): case L_('/'): case L_('0'):
647 case L_('1'): case L_('2'): case L_('3'): case L_('4'): case L_('5'):
648 case L_('6'): case L_('7'): case L_('8'): case L_('9'): case L_(':'):
649 case L_(';'): case L_('<'): case L_('='): case L_('>'): case L_('?'):
650 case L_('A'): case L_('B'): case L_('C'): case L_('D'): case L_('E'):
651 case L_('F'): case L_('G'): case L_('H'): case L_('I'): case L_('J'):
652 case L_('K'): case L_('L'): case L_('M'): case L_('N'): case L_('O'):
653 case L_('P'): case L_('Q'): case L_('R'): case L_('S'): case L_('T'):
654 case L_('U'): case L_('V'): case L_('W'): case L_('X'): case L_('Y'):
655 case L_('Z'): case L_('['): case L_('\\'): case L_(']'): case L_('^'):
656 case L_('_'): case L_('a'): case L_('b'): case L_('c'): case L_('d'):
657 case L_('e'): case L_('f'): case L_('g'): case L_('h'): case L_('i'):
658 case L_('j'): case L_('k'): case L_('l'): case L_('m'): case L_('n'):
659 case L_('o'): case L_('p'): case L_('q'): case L_('r'): case L_('s'):
660 case L_('t'): case L_('u'): case L_('v'): case L_('w'): case L_('x'):
661 case L_('y'): case L_('z'): case L_('{'): case L_('|'): case L_('}'):
662 case L_('~'):
663 /* The C Standard requires these 98 characters (plus '%') to
664 be in the basic execution character set. None of these
665 characters can start a multibyte sequence, so they need
666 not be analyzed further. */
667 add (1, *p = *f);
668 continue;
669
670 default:
671 /* Copy this multibyte sequence until we reach its end, find
672 an error, or come back to the initial shift state. */
673 {
674 mbstate_t mbstate = mbstate_zero;
675 size_t len = 0;
676 size_t fsize;
677
678 if (! format_end)
679 format_end = f + strlen (f) + 1;
680 fsize = format_end - f;
681
682 do
683 {
684 size_t bytes = mbrlen (f + len, fsize - len, &mbstate);
685
686 if (bytes == 0)
687 break;
688
689 if (bytes == (size_t) -2)
690 {
691 len += strlen (f + len);
692 break;
693 }
694
695 if (bytes == (size_t) -1)
696 {
697 len++;
698 break;
699 }
700
701 len += bytes;
702 }
703 while (! mbsinit (&mbstate));
704
705 cpy (len, f);
706 f += len - 1;
707 continue;
708 }
709 }
710
711 #else /* ! DO_MULTIBYTE */
712
713 /* Either multibyte encodings are not supported, they are
714 safe for formats, so any non-'%' byte can be copied through,
715 or this is the wide character version. */
716 if (*f != L_('%'))
717 {
718 add (1, *p = *f);
719 continue;
720 }
721
722 #endif /* ! DO_MULTIBYTE */
723
724 /* Check for flags that can modify a format. */
725 while (1)
726 {
727 switch (*++f)
728 {
729 /* This influences the number formats. */
730 case L_('_'):
731 case L_('-'):
732 case L_('0'):
733 pad = *f;
734 continue;
735
736 /* This changes textual output. */
737 case L_('^'):
738 to_uppcase = 1;
739 continue;
740 case L_('#'):
741 change_case = 1;
742 continue;
743
744 default:
745 break;
746 }
747 break;
748 }
749
750 /* As a GNU extension we allow to specify the field width. */
751 if (ISDIGIT (*f))
752 {
753 width = 0;
754 do
755 {
756 if (width > INT_MAX / 10
757 || (width == INT_MAX / 10 && *f - L_('0') > INT_MAX % 10))
758 /* Avoid overflow. */
759 width = INT_MAX;
760 else
761 {
762 width *= 10;
763 width += *f - L_('0');
764 }
765 ++f;
766 }
767 while (ISDIGIT (*f));
768 }
769
770 /* Check for modifiers. */
771 switch (*f)
772 {
773 case L_('E'):
774 case L_('O'):
775 modifier = *f++;
776 break;
777
778 default:
779 modifier = 0;
780 break;
781 }
782
783 /* Now do the specified format. */
784 format_char = *f;
785 switch (format_char)
786 {
787 #define DO_NUMBER(d, v) \
788 digits = d > width ? d : width; \
789 number_value = v; goto do_number
790 #define DO_NUMBER_SPACEPAD(d, v) \
791 digits = d > width ? d : width; \
792 number_value = v; goto do_number_spacepad
793
794 case L_('%'):
795 if (modifier != 0)
796 goto bad_format;
797 add (1, *p = *f);
798 break;
799
800 case L_('a'):
801 if (modifier != 0)
802 goto bad_format;
803 if (change_case)
804 {
805 to_uppcase = 1;
806 to_lowcase = 0;
807 }
808 #if defined _NL_CURRENT || !HAVE_STRFTIME
809 cpy (aw_len, a_wkday);
810 break;
811 #else
812 goto underlying_strftime;
813 #endif
814
815 case 'A':
816 if (modifier != 0)
817 goto bad_format;
818 if (change_case)
819 {
820 to_uppcase = 1;
821 to_lowcase = 0;
822 }
823 #if defined _NL_CURRENT || !HAVE_STRFTIME
824 cpy (STRLEN (f_wkday), f_wkday);
825 break;
826 #else
827 goto underlying_strftime;
828 #endif
829
830 case L_('b'):
831 case L_('h'):
832 if (change_case)
833 {
834 to_uppcase = 1;
835 to_lowcase = 0;
836 }
837 if (modifier != 0)
838 goto bad_format;
839 #if defined _NL_CURRENT || !HAVE_STRFTIME
840 cpy (am_len, a_month);
841 break;
842 #else
843 goto underlying_strftime;
844 #endif
845
846 case L_('B'):
847 if (modifier != 0)
848 goto bad_format;
849 if (change_case)
850 {
851 to_uppcase = 1;
852 to_lowcase = 0;
853 }
854 #if defined _NL_CURRENT || !HAVE_STRFTIME
855 cpy (STRLEN (f_month), f_month);
856 break;
857 #else
858 goto underlying_strftime;
859 #endif
860
861 case L_('c'):
862 if (modifier == L_('O'))
863 goto bad_format;
864 #ifdef _NL_CURRENT
865 if (! (modifier == 'E'
866 && (*(subfmt =
867 (const CHAR_T *) _NL_CURRENT (LC_TIME,
868 NLW(ERA_D_T_FMT)))
869 != '\0')))
870 subfmt = (const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(D_T_FMT));
871 #else
872 # if HAVE_STRFTIME
873 goto underlying_strftime;
874 # else
875 subfmt = L_("%a %b %e %H:%M:%S %Y");
876 # endif
877 #endif
878
879 subformat:
880 {
881 CHAR_T *old_start = p;
882 size_t len = my_strftime (NULL, (size_t) -1, subfmt,
883 tp ut_argument LOCALE_ARG);
884 add (len, my_strftime (p, maxsize - i, subfmt,
885 tp ut_argument LOCALE_ARG));
886
887 if (to_uppcase)
888 while (old_start < p)
889 {
890 *old_start = TOUPPER ((UCHAR_T) *old_start, loc);
891 ++old_start;
892 }
893 }
894 break;
895
896 #if HAVE_STRFTIME && ! (defined _NL_CURRENT && HAVE_STRUCT_ERA_ENTRY)
897 underlying_strftime:
898 {
899 /* The relevant information is available only via the
900 underlying strftime implementation, so use that. */
901 char ufmt[4];
902 char *u = ufmt;
903 char ubuf[1024]; /* enough for any single format in practice */
904 size_t len;
905 /* Make sure we're calling the actual underlying strftime.
906 In some cases, config.h contains something like
907 "#define strftime rpl_strftime". */
908 # ifdef strftime
909 # undef strftime
910 size_t strftime ();
911 # endif
912
913 *u++ = '%';
914 if (modifier != 0)
915 *u++ = modifier;
916 *u++ = format_char;
917 *u = '\0';
918 len = strftime (ubuf, sizeof ubuf, ufmt, tp);
919 if (len == 0 && ubuf[0] != '\0')
920 return 0;
921 cpy (len, ubuf);
922 }
923 break;
924 #endif
925
926 case L_('C'):
927 if (modifier == L_('O'))
928 goto bad_format;
929 if (modifier == L_('E'))
930 {
931 #if HAVE_STRUCT_ERA_ENTRY
932 struct era_entry *era = _nl_get_era_entry (tp HELPER_LOCALE_ARG);
933 if (era)
934 {
935 # ifdef COMPILE_WIDE
936 size_t len = __wcslen (era->era_wname);
937 cpy (len, era->era_wname);
938 # else
939 size_t len = strlen (era->era_name);
940 cpy (len, era->era_name);
941 # endif
942 break;
943 }
944 #else
945 # if HAVE_STRFTIME
946 goto underlying_strftime;
947 # endif
948 #endif
949 }
950
951 {
952 int year = tp->tm_year + TM_YEAR_BASE;
953 DO_NUMBER (1, year / 100 - (year % 100 < 0));
954 }
955
956 case L_('x'):
957 if (modifier == L_('O'))
958 goto bad_format;
959 #ifdef _NL_CURRENT
960 if (! (modifier == L_('E')
961 && (*(subfmt =
962 (const CHAR_T *)_NL_CURRENT (LC_TIME, NLW(ERA_D_FMT)))
963 != L_('\0'))))
964 subfmt = (const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(D_FMT));
965 goto subformat;
966 #else
967 # if HAVE_STRFTIME
968 goto underlying_strftime;
969 # else
970 /* Fall through. */
971 # endif
972 #endif
973 case L_('D'):
974 if (modifier != 0)
975 goto bad_format;
976 subfmt = L_("%m/%d/%y");
977 goto subformat;
978
979 case L_('d'):
980 if (modifier == L_('E'))
981 goto bad_format;
982
983 DO_NUMBER (2, tp->tm_mday);
984
985 case L_('e'):
986 if (modifier == L_('E'))
987 goto bad_format;
988
989 DO_NUMBER_SPACEPAD (2, tp->tm_mday);
990
991 /* All numeric formats set DIGITS and NUMBER_VALUE and then
992 jump to one of these two labels. */
993
994 do_number_spacepad:
995 /* Force `_' flag unless overwritten by `0' flag. */
996 if (pad != L_('0'))
997 pad = L_('_');
998
999 do_number:
1000 /* Format the number according to the MODIFIER flag. */
1001
1002 if (modifier == L_('O') && 0 <= number_value)
1003 {
1004 #ifdef _NL_CURRENT
1005 /* Get the locale specific alternate representation of
1006 the number NUMBER_VALUE. If none exist NULL is returned. */
1007 const CHAR_T *cp = nl_get_alt_digit (number_value
1008 HELPER_LOCALE_ARG);
1009
1010 if (cp != NULL)
1011 {
1012 size_t digitlen = STRLEN (cp);
1013 if (digitlen != 0)
1014 {
1015 cpy (digitlen, cp);
1016 break;
1017 }
1018 }
1019 #else
1020 # if HAVE_STRFTIME
1021 goto underlying_strftime;
1022 # endif
1023 #endif
1024 }
1025 {
1026 unsigned int u = number_value;
1027
1028 bufp = buf + sizeof (buf) / sizeof (buf[0]);
1029 negative_number = number_value < 0;
1030
1031 if (negative_number)
1032 u = -u;
1033
1034 do
1035 *--bufp = u % 10 + L_('0');
1036 while ((u /= 10) != 0);
1037 }
1038
1039 do_number_sign_and_padding:
1040 if (negative_number)
1041 *--bufp = L_('-');
1042
1043 if (pad != L_('-'))
1044 {
1045 int padding = digits - (buf + (sizeof (buf) / sizeof (buf[0]))
1046 - bufp);
1047
1048 if (padding > 0)
1049 {
1050 if (pad == L_('_'))
1051 {
1052 if ((size_t) padding >= maxsize - i)
1053 return 0;
1054
1055 if (p)
1056 memset_space (p, padding);
1057 i += padding;
1058 width = width > padding ? width - padding : 0;
1059 }
1060 else
1061 {
1062 if ((size_t) digits >= maxsize - i)
1063 return 0;
1064
1065 if (negative_number)
1066 {
1067 ++bufp;
1068
1069 if (p)
1070 *p++ = L_('-');
1071 ++i;
1072 }
1073
1074 if (p)
1075 memset_zero (p, padding);
1076 i += padding;
1077 width = 0;
1078 }
1079 }
1080 }
1081
1082 cpy (buf + sizeof (buf) / sizeof (buf[0]) - bufp, bufp);
1083 break;
1084
1085 case L_('F'):
1086 if (modifier != 0)
1087 goto bad_format;
1088 subfmt = L_("%Y-%m-%d");
1089 goto subformat;
1090
1091 case L_('H'):
1092 if (modifier == L_('E'))
1093 goto bad_format;
1094
1095 DO_NUMBER (2, tp->tm_hour);
1096
1097 case L_('I'):
1098 if (modifier == L_('E'))
1099 goto bad_format;
1100
1101 DO_NUMBER (2, hour12);
1102
1103 case L_('k'): /* GNU extension. */
1104 if (modifier == L_('E'))
1105 goto bad_format;
1106
1107 DO_NUMBER_SPACEPAD (2, tp->tm_hour);
1108
1109 case L_('l'): /* GNU extension. */
1110 if (modifier == L_('E'))
1111 goto bad_format;
1112
1113 DO_NUMBER_SPACEPAD (2, hour12);
1114
1115 case L_('j'):
1116 if (modifier == L_('E'))
1117 goto bad_format;
1118
1119 DO_NUMBER (3, 1 + tp->tm_yday);
1120
1121 case L_('M'):
1122 if (modifier == L_('E'))
1123 goto bad_format;
1124
1125 DO_NUMBER (2, tp->tm_min);
1126
1127 case L_('m'):
1128 if (modifier == L_('E'))
1129 goto bad_format;
1130
1131 DO_NUMBER (2, tp->tm_mon + 1);
1132
1133 case L_('n'):
1134 add (1, *p = L_('\n'));
1135 break;
1136
1137 case L_('P'):
1138 to_lowcase = 1;
1139 #if !defined _NL_CURRENT && HAVE_STRFTIME
1140 format_char = L_('p');
1141 #endif
1142 /* FALLTHROUGH */
1143
1144 case L_('p'):
1145 if (change_case)
1146 {
1147 to_uppcase = 0;
1148 to_lowcase = 1;
1149 }
1150 #if defined _NL_CURRENT || !HAVE_STRFTIME
1151 cpy (ap_len, ampm);
1152 break;
1153 #else
1154 goto underlying_strftime;
1155 #endif
1156
1157 case L_('R'):
1158 subfmt = L_("%H:%M");
1159 goto subformat;
1160
1161 case L_('r'):
1162 #ifdef _NL_CURRENT
1163 if (*(subfmt = (const CHAR_T *) _NL_CURRENT (LC_TIME,
1164 NLW(T_FMT_AMPM)))
1165 == L_('\0'))
1166 #endif
1167 subfmt = L_("%I:%M:%S %p");
1168 goto subformat;
1169
1170 case L_('S'):
1171 if (modifier == L_('E'))
1172 goto bad_format;
1173
1174 DO_NUMBER (2, tp->tm_sec);
1175
1176 case L_('s'): /* GNU extension. */
1177 {
1178 struct tm ltm;
1179 time_t t;
1180
1181 ltm = *tp;
1182 t = mktime (&ltm);
1183
1184 /* Generate string value for T using time_t arithmetic;
1185 this works even if sizeof (long) < sizeof (time_t). */
1186
1187 bufp = buf + sizeof (buf) / sizeof (buf[0]);
1188 #ifndef TIME_T_UNSIGNED
1189 negative_number = t < 0;
1190 #endif
1191
1192 do
1193 {
1194 int d = t % 10;
1195 t /= 10;
1196
1197 #ifndef TIME_T_UNSIGNED
1198 if (negative_number)
1199 {
1200 d = -d;
1201
1202 /* Adjust if division truncates to minus infinity. */
1203 if (0 < -1 % 10 && d < 0)
1204 {
1205 t++;
1206 d += 10;
1207 }
1208 }
1209 #endif
1210
1211 *--bufp = d + L_('0');
1212 }
1213 while (t != 0);
1214
1215 digits = 1;
1216 goto do_number_sign_and_padding;
1217 }
1218
1219 case L_('X'):
1220 if (modifier == L_('O'))
1221 goto bad_format;
1222 #ifdef _NL_CURRENT
1223 if (! (modifier == L_('E')
1224 && (*(subfmt =
1225 (const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(ERA_T_FMT)))
1226 != L_('\0'))))
1227 subfmt = (const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(T_FMT));
1228 goto subformat;
1229 #else
1230 # if HAVE_STRFTIME
1231 goto underlying_strftime;
1232 # else
1233 /* Fall through. */
1234 # endif
1235 #endif
1236 case L_('T'):
1237 subfmt = L_("%H:%M:%S");
1238 goto subformat;
1239
1240 case L_('t'):
1241 add (1, *p = L_('\t'));
1242 break;
1243
1244 case L_('u'):
1245 DO_NUMBER (1, (tp->tm_wday - 1 + 7) % 7 + 1);
1246
1247 case L_('U'):
1248 if (modifier == L_('E'))
1249 goto bad_format;
1250
1251 DO_NUMBER (2, (tp->tm_yday - tp->tm_wday + 7) / 7);
1252
1253 case L_('V'):
1254 case L_('g'):
1255 case L_('G'):
1256 if (modifier == L_('E'))
1257 goto bad_format;
1258 {
1259 int year = tp->tm_year + TM_YEAR_BASE;
1260 int days = iso_week_days (tp->tm_yday, tp->tm_wday);
1261
1262 if (days < 0)
1263 {
1264 /* This ISO week belongs to the previous year. */
1265 year--;
1266 days = iso_week_days (tp->tm_yday + (365 + __isleap (year)),
1267 tp->tm_wday);
1268 }
1269 else
1270 {
1271 int d = iso_week_days (tp->tm_yday - (365 + __isleap (year)),
1272 tp->tm_wday);
1273 if (0 <= d)
1274 {
1275 /* This ISO week belongs to the next year. */
1276 year++;
1277 days = d;
1278 }
1279 }
1280
1281 switch (*f)
1282 {
1283 case L_('g'):
1284 DO_NUMBER (2, (year % 100 + 100) % 100);
1285
1286 case L_('G'):
1287 DO_NUMBER (1, year);
1288
1289 default:
1290 DO_NUMBER (2, days / 7 + 1);
1291 }
1292 }
1293
1294 case L_('W'):
1295 if (modifier == L_('E'))
1296 goto bad_format;
1297
1298 DO_NUMBER (2, (tp->tm_yday - (tp->tm_wday - 1 + 7) % 7 + 7) / 7);
1299
1300 case L_('w'):
1301 if (modifier == L_('E'))
1302 goto bad_format;
1303
1304 DO_NUMBER (1, tp->tm_wday);
1305
1306 case L_('Y'):
1307 if (modifier == 'E')
1308 {
1309 #if HAVE_STRUCT_ERA_ENTRY
1310 struct era_entry *era = _nl_get_era_entry (tp HELPER_LOCALE_ARG);
1311 if (era)
1312 {
1313 # ifdef COMPILE_WIDE
1314 subfmt = era->era_wformat;
1315 # else
1316 subfmt = era->era_format;
1317 # endif
1318 goto subformat;
1319 }
1320 #else
1321 # if HAVE_STRFTIME
1322 goto underlying_strftime;
1323 # endif
1324 #endif
1325 }
1326 if (modifier == L_('O'))
1327 goto bad_format;
1328 else
1329 DO_NUMBER (1, tp->tm_year + TM_YEAR_BASE);
1330
1331 case L_('y'):
1332 if (modifier == L_('E'))
1333 {
1334 #if HAVE_STRUCT_ERA_ENTRY
1335 struct era_entry *era = _nl_get_era_entry (tp HELPER_LOCALE_ARG);
1336 if (era)
1337 {
1338 int delta = tp->tm_year - era->start_date[0];
1339 DO_NUMBER (1, (era->offset
1340 + delta * era->absolute_direction));
1341 }
1342 #else
1343 # if HAVE_STRFTIME
1344 goto underlying_strftime;
1345 # endif
1346 #endif
1347 }
1348 DO_NUMBER (2, (tp->tm_year % 100 + 100) % 100);
1349
1350 case L_('Z'):
1351 if (change_case)
1352 {
1353 to_uppcase = 0;
1354 to_lowcase = 1;
1355 }
1356
1357 #if HAVE_TZNAME
1358 /* The tzset() call might have changed the value. */
1359 if (!(zone && *zone) && tp->tm_isdst >= 0)
1360 zone = tzname[tp->tm_isdst];
1361 #endif
1362 if (! zone)
1363 zone = "";
1364
1365 #ifdef COMPILE_WIDE
1366 {
1367 /* The zone string is always given in multibyte form. We have
1368 to transform it first. */
1369 wchar_t *wczone;
1370 size_t len;
1371 widen (zone, wczone, len);
1372 cpy (len, wczone);
1373 }
1374 #else
1375 cpy (strlen (zone), zone);
1376 #endif
1377 break;
1378
1379 case L_('z'):
1380 if (tp->tm_isdst < 0)
1381 break;
1382
1383 {
1384 int diff;
1385 #if HAVE_TM_GMTOFF
1386 diff = tp->tm_gmtoff;
1387 #else
1388 if (ut)
1389 diff = 0;
1390 else
1391 {
1392 struct tm gtm;
1393 struct tm ltm;
1394 time_t lt;
1395
1396 ltm = *tp;
1397 lt = mktime (&ltm);
1398
1399 if (lt == (time_t) -1)
1400 {
1401 /* mktime returns -1 for errors, but -1 is also a
1402 valid time_t value. Check whether an error really
1403 occurred. */
1404 struct tm tm;
1405
1406 if (! my_strftime_localtime_r (&lt, &tm)
1407 || ((ltm.tm_sec ^ tm.tm_sec)
1408 | (ltm.tm_min ^ tm.tm_min)
1409 | (ltm.tm_hour ^ tm.tm_hour)
1410 | (ltm.tm_mday ^ tm.tm_mday)
1411 | (ltm.tm_mon ^ tm.tm_mon)
1412 | (ltm.tm_year ^ tm.tm_year)))
1413 break;
1414 }
1415
1416 if (! my_strftime_gmtime_r (&lt, &gtm))
1417 break;
1418
1419 diff = tm_diff (&ltm, &gtm);
1420 }
1421 #endif
1422
1423 if (diff < 0)
1424 {
1425 add (1, *p = L_('-'));
1426 diff = -diff;
1427 }
1428 else
1429 add (1, *p = L_('+'));
1430
1431 diff /= 60;
1432 DO_NUMBER (4, (diff / 60) * 100 + diff % 60);
1433 }
1434
1435 case L_('\0'): /* GNU extension: % at end of format. */
1436 --f;
1437 /* Fall through. */
1438 default:
1439 /* Unknown format; output the format, including the '%',
1440 since this is most likely the right thing to do if a
1441 multibyte string has been misparsed. */
1442 bad_format:
1443 {
1444 int flen;
1445 for (flen = 1; f[1 - flen] != L_('%'); flen++)
1446 continue;
1447 cpy (flen, &f[1 - flen]);
1448 }
1449 break;
1450 }
1451 }
1452
1453 if (p && maxsize != 0)
1454 *p = L_('\0');
1455 return i;
1456 }
1457 #ifdef _LIBC
1458 libc_hidden_def (my_strftime)
1459 #endif
1460
1461
1462 #ifdef emacs
1463 /* For Emacs we have a separate interface which corresponds to the normal
1464 strftime function and does not have the extra information whether the
1465 TP arguments comes from a `gmtime' call or not. */
1466 size_t
1467 emacs_strftime (s, maxsize, format, tp)
1468 char *s;
1469 size_t maxsize;
1470 const char *format;
1471 const struct tm *tp;
1472 {
1473 return my_strftime (s, maxsize, format, tp, 0);
1474 }
1475 #endif