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