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