3274
|
1 /* Copyright (C) 1991, 92, 93, 94, 95, 96 Free Software Foundation, Inc. |
|
2 |
|
3 NOTE: The canonical source of this file is maintained with the GNU C |
3285
|
4 Library. Bugs can be reported to bug-glibc@gnu.org. |
3274
|
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 Foundation, |
5307
|
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
3274
|
19 |
|
20 #ifdef HAVE_CONFIG_H |
|
21 # include <config.h> |
|
22 #endif |
|
23 |
|
24 #ifndef HAVE_STRFTIME |
|
25 |
|
26 #ifdef _LIBC |
|
27 # define HAVE_LIMITS_H 1 |
|
28 # define HAVE_MBLEN 1 |
|
29 # define HAVE_MBRLEN 1 |
|
30 # define HAVE_STRUCT_ERA_ENTRY 1 |
|
31 # define HAVE_TM_GMTOFF 1 |
3887
|
32 # define HAVE_STRUCT_TM_TM_ZONE 1 |
3274
|
33 # define MULTIBYTE_IS_FORMAT_SAFE 1 |
|
34 # define STDC_HEADERS 1 |
|
35 # include <ansidecl.h> |
|
36 # include "../locale/localeinfo.h" |
|
37 #endif |
|
38 |
|
39 #include <sys/types.h> /* Some systems define `time_t' here. */ |
|
40 |
|
41 #ifdef TIME_WITH_SYS_TIME |
|
42 # include <sys/time.h> |
|
43 # include <time.h> |
|
44 #else |
|
45 # ifdef HAVE_SYS_TIME_H |
|
46 # include <sys/time.h> |
|
47 # else |
|
48 # include <time.h> |
|
49 # endif |
|
50 #endif |
|
51 |
|
52 #if HAVE_TZNAME |
|
53 extern char *tzname[]; |
|
54 #endif |
|
55 |
|
56 /* Do multibyte processing if multibytes are supported, unless |
|
57 multibyte sequences are safe in formats. Multibyte sequences are |
|
58 safe if they cannot contain byte sequences that look like format |
|
59 conversion specifications. The GNU C Library uses UTF8 multibyte |
|
60 encoding, which is safe for formats, but strftime.c can be used |
|
61 with other C libraries that use unsafe encodings. */ |
|
62 #define DO_MULTIBYTE (HAVE_MBLEN && ! MULTIBYTE_IS_FORMAT_SAFE) |
|
63 |
|
64 #if DO_MULTIBYTE |
|
65 # if HAVE_MBRLEN |
|
66 # include <wchar.h> |
|
67 # else |
|
68 /* Simulate mbrlen with mblen as best we can. */ |
|
69 # define mbstate_t int |
|
70 # define mbrlen(s, n, ps) mblen (s, n) |
|
71 # define mbsinit(ps) (*(ps) == 0) |
|
72 # endif |
|
73 static const mbstate_t mbstate_zero; |
|
74 #endif |
|
75 |
|
76 #if HAVE_LIMITS_H |
|
77 # include <limits.h> |
|
78 #endif |
|
79 |
|
80 #if STDC_HEADERS |
|
81 # include <stddef.h> |
|
82 # include <stdlib.h> |
|
83 # include <string.h> |
|
84 #else |
|
85 # define memcpy(d, s, n) bcopy (s, d, n) |
|
86 #endif |
|
87 |
|
88 #ifndef __P |
|
89 #if defined (__GNUC__) || (defined (__STDC__) && __STDC__) |
|
90 #define __P(args) args |
|
91 #else |
|
92 #define __P(args) () |
|
93 #endif /* GCC. */ |
|
94 #endif /* Not __P. */ |
|
95 |
|
96 #ifndef PTR |
|
97 #ifdef __STDC__ |
|
98 #define PTR void * |
|
99 #else |
|
100 #define PTR char * |
|
101 #endif |
|
102 #endif |
|
103 |
|
104 #ifndef CHAR_BIT |
|
105 #define CHAR_BIT 8 |
|
106 #endif |
|
107 |
|
108 #define TYPE_SIGNED(t) ((t) -1 < 0) |
|
109 |
|
110 /* Bound on length of the string representing an integer value of type t. |
|
111 Subtract one for the sign bit if t is signed; |
|
112 302 / 1000 is log10 (2) rounded up; |
|
113 add one for integer division truncation; |
|
114 add one more for a minus sign if t is signed. */ |
|
115 #define INT_STRLEN_BOUND(t) \ |
|
116 ((sizeof (t) * CHAR_BIT - TYPE_SIGNED (t)) * 302 / 100 + 1 + TYPE_SIGNED (t)) |
|
117 |
|
118 #define TM_YEAR_BASE 1900 |
|
119 |
|
120 #ifndef __isleap |
|
121 /* Nonzero if YEAR is a leap year (every 4 years, |
|
122 except every 100th isn't, and every 400th is). */ |
|
123 #define __isleap(year) \ |
|
124 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0)) |
|
125 #endif |
|
126 |
|
127 |
|
128 #ifdef _LIBC |
|
129 # define gmtime_r __gmtime_r |
|
130 # define localtime_r __localtime_r |
|
131 #else |
|
132 # if ! HAVE_LOCALTIME_R |
|
133 # if ! HAVE_TM_GMTOFF |
|
134 /* Approximate gmtime_r as best we can in its absence. */ |
|
135 #define gmtime_r my_gmtime_r |
|
136 static struct tm *gmtime_r __P ((const time_t *, struct tm *)); |
|
137 static struct tm * |
|
138 gmtime_r (t, tp) |
|
139 const time_t *t; |
|
140 struct tm *tp; |
|
141 { |
|
142 struct tm *l = gmtime (t); |
|
143 if (! l) |
|
144 return 0; |
|
145 *tp = *l; |
|
146 return tp; |
|
147 } |
|
148 # endif /* ! HAVE_TM_GMTOFF */ |
|
149 |
|
150 /* Approximate localtime_r as best we can in its absence. */ |
|
151 #define localtime_r my_localtime_r |
|
152 static struct tm *localtime_r __P ((const time_t *, struct tm *)); |
|
153 static struct tm * |
|
154 localtime_r (t, tp) |
|
155 const time_t *t; |
|
156 struct tm *tp; |
|
157 { |
|
158 struct tm *l = localtime (t); |
|
159 if (! l) |
|
160 return 0; |
|
161 *tp = *l; |
|
162 return tp; |
|
163 } |
|
164 # endif /* ! HAVE_LOCALTIME_R */ |
|
165 #endif /* ! defined (_LIBC) */ |
|
166 |
|
167 |
|
168 #define add(n, f) \ |
|
169 do \ |
|
170 { \ |
|
171 i += (n); \ |
|
172 if (i >= maxsize) \ |
|
173 return 0; \ |
|
174 else \ |
|
175 if (p) \ |
|
176 { \ |
|
177 f; \ |
|
178 p += (n); \ |
|
179 } \ |
|
180 } while (0) |
|
181 #define cpy(n, s) add ((n), memcpy((PTR) p, (PTR) (s), (n))) |
|
182 |
|
183 #if ! HAVE_TM_GMTOFF |
|
184 /* Yield the difference between *A and *B, |
|
185 measured in seconds, ignoring leap seconds. */ |
|
186 static int tm_diff __P ((const struct tm *, const struct tm *)); |
|
187 static int |
|
188 tm_diff (a, b) |
|
189 const struct tm *a; |
|
190 const struct tm *b; |
|
191 { |
|
192 /* Compute intervening leap days correctly even if year is negative. |
|
193 Take care to avoid int overflow in leap day calculations, |
|
194 but it's OK to assume that A and B are close to each other. */ |
|
195 int a4 = (a->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (a->tm_year & 3); |
|
196 int b4 = (b->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (b->tm_year & 3); |
|
197 int a100 = a4 / 25 - (a4 % 25 < 0); |
|
198 int b100 = b4 / 25 - (b4 % 25 < 0); |
|
199 int a400 = a100 >> 2; |
|
200 int b400 = b100 >> 2; |
|
201 int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400); |
|
202 int years = a->tm_year - b->tm_year; |
|
203 int days = (365 * years + intervening_leap_days |
|
204 + (a->tm_yday - b->tm_yday)); |
|
205 return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour)) |
|
206 + (a->tm_min - b->tm_min)) |
|
207 + (a->tm_sec - b->tm_sec)); |
|
208 } |
|
209 #endif /* ! HAVE_TM_GMTOFF */ |
|
210 |
|
211 |
|
212 |
|
213 /* The number of days from the first day of the first ISO week of this |
|
214 year to the year day YDAY with week day WDAY. ISO weeks start on |
|
215 Monday; the first ISO week has the year's first Thursday. YDAY may |
|
216 be as small as YDAY_MINIMUM. */ |
|
217 #define ISO_WEEK_START_WDAY 1 /* Monday */ |
|
218 #define ISO_WEEK1_WDAY 4 /* Thursday */ |
|
219 #define YDAY_MINIMUM (-366) |
|
220 static int iso_week_days __P ((int, int)); |
|
221 #ifdef __GNUC__ |
|
222 inline |
|
223 #endif |
|
224 static int |
|
225 iso_week_days (yday, wday) |
|
226 int yday; |
|
227 int wday; |
|
228 { |
|
229 /* Add enough to the first operand of % to make it nonnegative. */ |
|
230 int big_enough_multiple_of_7 = (-YDAY_MINIMUM / 7 + 2) * 7; |
|
231 return (yday |
|
232 - (yday - wday + ISO_WEEK1_WDAY + big_enough_multiple_of_7) % 7 |
|
233 + ISO_WEEK1_WDAY - ISO_WEEK_START_WDAY); |
|
234 } |
|
235 |
|
236 |
|
237 #ifndef _NL_CURRENT |
|
238 static char const weekday_name[][10] = |
|
239 { |
|
240 "Sunday", "Monday", "Tuesday", "Wednesday", |
|
241 "Thursday", "Friday", "Saturday" |
|
242 }; |
|
243 static char const month_name[][10] = |
|
244 { |
|
245 "January", "February", "March", "April", "May", "June", |
|
246 "July", "August", "September", "October", "November", "December" |
|
247 }; |
|
248 #endif |
|
249 |
|
250 /* Write information from TP into S according to the format |
|
251 string FORMAT, writing no more that MAXSIZE characters |
|
252 (including the terminating '\0') and returning number of |
|
253 characters written. If S is NULL, nothing will be written |
|
254 anywhere, so to determine how many characters would be |
|
255 written, use NULL for S and (size_t) UINT_MAX for MAXSIZE. */ |
|
256 size_t |
|
257 strftime (s, maxsize, format, tp) |
|
258 char *s; |
|
259 size_t maxsize; |
|
260 const char *format; |
|
261 register const struct tm *tp; |
|
262 { |
|
263 int hour12 = tp->tm_hour; |
|
264 #ifdef _NL_CURRENT |
|
265 const char *const a_wkday = _NL_CURRENT (LC_TIME, ABDAY_1 + tp->tm_wday); |
|
266 const char *const f_wkday = _NL_CURRENT (LC_TIME, DAY_1 + tp->tm_wday); |
|
267 const char *const a_month = _NL_CURRENT (LC_TIME, ABMON_1 + tp->tm_mon); |
|
268 const char *const f_month = _NL_CURRENT (LC_TIME, MON_1 + tp->tm_mon); |
|
269 const char *const ampm = _NL_CURRENT (LC_TIME, |
|
270 hour12 > 11 ? PM_STR : AM_STR); |
|
271 size_t aw_len = strlen (a_wkday); |
|
272 size_t am_len = strlen (a_month); |
|
273 size_t ap_len = strlen (ampm); |
|
274 #else |
|
275 const char *const f_wkday = weekday_name[tp->tm_wday]; |
|
276 const char *const f_month = month_name[tp->tm_mon]; |
|
277 const char *const a_wkday = f_wkday; |
|
278 const char *const a_month = f_month; |
|
279 const char *const ampm = "AMPM" + 2 * (hour12 > 11); |
|
280 size_t aw_len = 3; |
|
281 size_t am_len = 3; |
|
282 size_t ap_len = 2; |
|
283 #endif |
|
284 size_t wkday_len = strlen (f_wkday); |
|
285 size_t month_len = strlen (f_month); |
|
286 const char *zone; |
|
287 size_t zonelen; |
|
288 register size_t i = 0; |
|
289 register char *p = s; |
|
290 register const char *f; |
|
291 |
|
292 zone = 0; |
3887
|
293 #if HAVE_STRUCT_TM_TM_ZONE |
3274
|
294 zone = (const char *) tp->tm_zone; |
|
295 #endif |
|
296 #if HAVE_TZNAME |
|
297 if (!(zone && *zone) && tp->tm_isdst >= 0) |
|
298 zone = tzname[tp->tm_isdst]; |
|
299 #endif |
|
300 if (! zone) |
|
301 zone = ""; /* POSIX.2 requires the empty string here. */ |
|
302 |
|
303 zonelen = strlen (zone); |
|
304 |
|
305 if (hour12 > 12) |
|
306 hour12 -= 12; |
|
307 else |
|
308 if (hour12 == 0) hour12 = 12; |
|
309 |
|
310 for (f = format; *f != '\0'; ++f) |
|
311 { |
|
312 int pad; /* Padding for number ('-', '_', or 0). */ |
|
313 int modifier; /* Field modifier ('E', 'O', or 0). */ |
|
314 int digits; /* Max digits for numeric format. */ |
|
315 int number_value; /* Numeric value to be printed. */ |
|
316 int negative_number; /* 1 if the number is negative. */ |
|
317 const char *subfmt; |
|
318 char *bufp; |
|
319 char buf[1 + (sizeof (int) < sizeof (time_t) |
|
320 ? INT_STRLEN_BOUND (time_t) |
|
321 : INT_STRLEN_BOUND (int))]; |
|
322 |
|
323 #if DO_MULTIBYTE |
|
324 |
|
325 switch (*f) |
|
326 { |
|
327 case '%': |
|
328 break; |
|
329 |
|
330 case '\a': case '\b': case '\t': case '\n': |
|
331 case '\v': case '\f': case '\r': |
|
332 case ' ': case '!': case '"': case '#': case '&': case'\'': |
|
333 case '(': case ')': case '*': case '+': case ',': case '-': |
|
334 case '.': case '/': case '0': case '1': case '2': case '3': |
|
335 case '4': case '5': case '6': case '7': case '8': case '9': |
|
336 case ':': case ';': case '<': case '=': case '>': case '?': |
|
337 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': |
|
338 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L': |
|
339 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': |
|
340 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': |
|
341 case 'Y': case 'Z': case '[': case'\\': case ']': case '^': |
|
342 case '_': case 'a': case 'b': case 'c': case 'd': case 'e': |
|
343 case 'f': case 'g': case 'h': case 'i': case 'j': case 'k': |
|
344 case 'l': case 'm': case 'n': case 'o': case 'p': case 'q': |
|
345 case 'r': case 's': case 't': case 'u': case 'v': case 'w': |
|
346 case 'x': case 'y': case 'z': case '{': case '|': case '}': |
|
347 case '~': |
|
348 /* The C Standard requires these 98 characters (plus '%') to |
|
349 be in the basic execution character set. None of these |
|
350 characters can start a multibyte sequence, so they need |
|
351 not be analyzed further. */ |
|
352 add (1, *p = *f); |
|
353 continue; |
|
354 |
|
355 default: |
|
356 /* Copy this multibyte sequence until we reach its end, find |
|
357 an error, or come back to the initial shift state. */ |
|
358 { |
|
359 mbstate_t mbstate = mbstate_zero; |
|
360 size_t len = 0; |
|
361 |
|
362 do |
|
363 { |
|
364 size_t bytes = mbrlen (f + len, (size_t) -1, &mbstate); |
|
365 |
|
366 if (bytes == 0) |
|
367 break; |
|
368 |
|
369 if (bytes == (size_t) -2 || bytes == (size_t) -1) |
|
370 { |
|
371 len++; |
|
372 break; |
|
373 } |
|
374 |
|
375 len += bytes; |
|
376 } |
|
377 while (! mbsinit (&mbstate)); |
|
378 |
|
379 cpy (len, f); |
|
380 continue; |
|
381 } |
|
382 } |
|
383 |
|
384 #else /* ! DO_MULTIBYTE */ |
|
385 |
|
386 /* Either multibyte encodings are not supported, or they are |
|
387 safe for formats, so any non-'%' byte can be copied through. */ |
|
388 if (*f != '%') |
|
389 { |
|
390 add (1, *p = *f); |
|
391 continue; |
|
392 } |
|
393 |
|
394 #endif /* ! DO_MULTIBYTE */ |
|
395 |
|
396 /* Check for flags that can modify a number format. */ |
|
397 ++f; |
|
398 switch (*f) |
|
399 { |
|
400 case '_': |
|
401 case '-': |
|
402 pad = *f++; |
|
403 break; |
|
404 |
|
405 default: |
|
406 pad = 0; |
|
407 break; |
|
408 } |
|
409 |
|
410 /* Check for modifiers. */ |
|
411 switch (*f) |
|
412 { |
|
413 case 'E': |
|
414 case 'O': |
|
415 modifier = *f++; |
|
416 break; |
|
417 |
|
418 default: |
|
419 modifier = 0; |
|
420 break; |
|
421 } |
|
422 |
|
423 /* Now do the specified format. */ |
|
424 switch (*f) |
|
425 { |
|
426 #define DO_NUMBER(d, v) \ |
|
427 digits = d; number_value = v; goto do_number |
|
428 #define DO_NUMBER_SPACEPAD(d, v) \ |
|
429 digits = d; number_value = v; goto do_number_spacepad |
|
430 |
|
431 case '%': |
|
432 if (modifier != 0) |
|
433 goto bad_format; |
|
434 add (1, *p = *f); |
|
435 break; |
|
436 |
|
437 case 'a': |
|
438 if (modifier != 0) |
|
439 goto bad_format; |
|
440 cpy (aw_len, a_wkday); |
|
441 break; |
|
442 |
|
443 case 'A': |
|
444 if (modifier != 0) |
|
445 goto bad_format; |
|
446 cpy (wkday_len, f_wkday); |
|
447 break; |
|
448 |
|
449 case 'b': |
|
450 case 'h': /* POSIX.2 extension. */ |
|
451 if (modifier != 0) |
|
452 goto bad_format; |
|
453 cpy (am_len, a_month); |
|
454 break; |
|
455 |
|
456 case 'B': |
|
457 if (modifier != 0) |
|
458 goto bad_format; |
|
459 cpy (month_len, f_month); |
|
460 break; |
|
461 |
|
462 case 'c': |
|
463 if (modifier == 'O') |
|
464 goto bad_format; |
|
465 #ifdef _NL_CURRENT |
|
466 if (! (modifier == 'E' |
|
467 && *(subfmt = _NL_CURRENT (LC_TIME, ERA_D_T_FMT)) != '\0')) |
|
468 subfmt = _NL_CURRENT (LC_TIME, D_T_FMT); |
|
469 #else |
|
470 subfmt = "%a %b %e %H:%M:%S %Y"; |
|
471 #endif |
|
472 |
|
473 subformat: |
|
474 { |
|
475 size_t len = strftime (p, maxsize - i, subfmt, tp); |
|
476 if (len == 0 && *subfmt) |
|
477 return 0; |
|
478 add (len, ;); |
|
479 } |
|
480 break; |
|
481 |
|
482 case 'C': /* POSIX.2 extension. */ |
|
483 if (modifier == 'O') |
|
484 goto bad_format; |
|
485 #if HAVE_STRUCT_ERA_ENTRY |
|
486 if (modifier == 'E') |
|
487 { |
|
488 struct era_entry *era = _nl_get_era_entry (tp); |
|
489 if (era) |
|
490 { |
|
491 size_t len = strlen (era->name_fmt); |
|
492 cpy (len, era->name_fmt); |
|
493 break; |
|
494 } |
|
495 } |
|
496 #endif |
|
497 { |
|
498 int year = tp->tm_year + TM_YEAR_BASE; |
|
499 DO_NUMBER (1, year / 100 - (year % 100 < 0)); |
|
500 } |
|
501 |
|
502 case 'x': |
|
503 if (modifier == 'O') |
|
504 goto bad_format; |
|
505 #ifdef _NL_CURRENT |
|
506 if (! (modifier == 'E' |
|
507 && *(subfmt = _NL_CURRENT (LC_TIME, ERA_D_FMT)) != '\0')) |
|
508 subfmt = _NL_CURRENT (LC_TIME, D_FMT); |
|
509 goto subformat; |
|
510 #endif |
|
511 /* Fall through. */ |
|
512 case 'D': /* POSIX.2 extension. */ |
|
513 if (modifier != 0) |
|
514 goto bad_format; |
|
515 subfmt = "%m/%d/%y"; |
|
516 goto subformat; |
|
517 |
|
518 case 'd': |
|
519 if (modifier == 'E') |
|
520 goto bad_format; |
|
521 |
|
522 DO_NUMBER (2, tp->tm_mday); |
|
523 |
|
524 case 'e': /* POSIX.2 extension. */ |
|
525 if (modifier == 'E') |
|
526 goto bad_format; |
|
527 |
|
528 DO_NUMBER_SPACEPAD (2, tp->tm_mday); |
|
529 |
|
530 /* All numeric formats set DIGITS and NUMBER_VALUE and then |
|
531 jump to one of these two labels. */ |
|
532 |
|
533 do_number_spacepad: |
|
534 /* Force `_' flag. */ |
|
535 pad = '_'; |
|
536 |
|
537 do_number: |
|
538 /* Format the number according to the MODIFIER flag. */ |
|
539 |
|
540 #ifdef _NL_CURRENT |
|
541 if (modifier == 'O' && 0 <= number_value) |
|
542 { |
|
543 /* Get the locale specific alternate representation of |
|
544 the number NUMBER_VALUE. If none exist NULL is returned. */ |
|
545 const char *cp = _nl_get_alt_digit (number_value); |
|
546 |
|
547 if (cp != NULL) |
|
548 { |
|
549 size_t digitlen = strlen (cp); |
|
550 if (digitlen != 0) |
|
551 { |
|
552 cpy (digitlen, cp); |
|
553 break; |
|
554 } |
|
555 } |
|
556 } |
|
557 #endif |
|
558 { |
|
559 unsigned int u = number_value; |
|
560 |
|
561 bufp = buf + sizeof (buf); |
|
562 negative_number = number_value < 0; |
|
563 |
|
564 if (negative_number) |
|
565 u = -u; |
|
566 |
|
567 do |
|
568 *--bufp = u % 10 + '0'; |
|
569 while ((u /= 10) != 0); |
|
570 } |
|
571 |
|
572 do_number_sign_and_padding: |
|
573 if (negative_number) |
|
574 *--bufp = '-'; |
|
575 |
|
576 if (pad != '-') |
|
577 { |
|
578 int padding = digits - (buf + sizeof (buf) - bufp); |
|
579 |
|
580 if (pad == '_') |
|
581 { |
|
582 while (0 < padding--) |
|
583 *--bufp = ' '; |
|
584 } |
|
585 else |
|
586 { |
|
587 bufp += negative_number; |
|
588 while (0 < padding--) |
|
589 *--bufp = '0'; |
|
590 if (negative_number) |
|
591 *--bufp = '-'; |
|
592 } |
|
593 } |
|
594 |
|
595 cpy (buf + sizeof (buf) - bufp, bufp); |
|
596 break; |
|
597 |
|
598 |
|
599 case 'H': |
|
600 if (modifier == 'E') |
|
601 goto bad_format; |
|
602 |
|
603 DO_NUMBER (2, tp->tm_hour); |
|
604 |
|
605 case 'I': |
|
606 if (modifier == 'E') |
|
607 goto bad_format; |
|
608 |
|
609 DO_NUMBER (2, hour12); |
|
610 |
|
611 case 'k': /* GNU extension. */ |
|
612 if (modifier == 'E') |
|
613 goto bad_format; |
|
614 |
|
615 DO_NUMBER_SPACEPAD (2, tp->tm_hour); |
|
616 |
|
617 case 'l': /* GNU extension. */ |
|
618 if (modifier == 'E') |
|
619 goto bad_format; |
|
620 |
|
621 DO_NUMBER_SPACEPAD (2, hour12); |
|
622 |
|
623 case 'j': |
|
624 if (modifier == 'E') |
|
625 goto bad_format; |
|
626 |
|
627 DO_NUMBER (3, 1 + tp->tm_yday); |
|
628 |
|
629 case 'M': |
|
630 if (modifier == 'E') |
|
631 goto bad_format; |
|
632 |
|
633 DO_NUMBER (2, tp->tm_min); |
|
634 |
|
635 case 'm': |
|
636 if (modifier == 'E') |
|
637 goto bad_format; |
|
638 |
|
639 DO_NUMBER (2, tp->tm_mon + 1); |
|
640 |
|
641 case 'n': /* POSIX.2 extension. */ |
|
642 add (1, *p = '\n'); |
|
643 break; |
|
644 |
|
645 case 'p': |
|
646 cpy (ap_len, ampm); |
|
647 break; |
|
648 |
|
649 case 'R': /* GNU extension. */ |
|
650 subfmt = "%H:%M"; |
|
651 goto subformat; |
|
652 |
|
653 case 'r': /* POSIX.2 extension. */ |
|
654 #ifdef _NL_CURRENT |
|
655 if (*(subfmt = _NL_CURRENT (LC_TIME, T_FMT_AMPM)) == '\0') |
|
656 #endif |
|
657 subfmt = "%I:%M:%S %p"; |
|
658 goto subformat; |
|
659 |
|
660 case 'S': |
|
661 if (modifier == 'E') |
|
662 goto bad_format; |
|
663 |
|
664 DO_NUMBER (2, tp->tm_sec); |
|
665 |
|
666 case 's': /* GNU extension. */ |
|
667 { |
|
668 struct tm ltm; |
|
669 time_t t; |
|
670 |
|
671 ltm = *tp; |
|
672 t = mktime (<m); |
|
673 |
|
674 /* Generate string value for T using time_t arithmetic; |
|
675 this works even if sizeof (long) < sizeof (time_t). */ |
|
676 |
|
677 bufp = buf + sizeof (buf); |
|
678 negative_number = t < 0; |
|
679 |
|
680 do |
|
681 { |
|
682 int d = t % 10; |
|
683 t /= 10; |
|
684 |
|
685 if (negative_number) |
|
686 { |
|
687 d = -d; |
|
688 |
|
689 /* Adjust if division truncates to minus infinity. */ |
|
690 if (0 < -1 % 10 && d < 0) |
|
691 { |
|
692 t++; |
|
693 d += 10; |
|
694 } |
|
695 } |
|
696 |
|
697 *--bufp = d + '0'; |
|
698 } |
|
699 while (t != 0); |
|
700 |
|
701 digits = 1; |
|
702 goto do_number_sign_and_padding; |
|
703 } |
|
704 |
|
705 case 'X': |
|
706 if (modifier == 'O') |
|
707 goto bad_format; |
|
708 #ifdef _NL_CURRENT |
|
709 if (! (modifier == 'E' |
|
710 && *(subfmt = _NL_CURRENT (LC_TIME, ERA_T_FMT)) != '\0')) |
|
711 subfmt = _NL_CURRENT (LC_TIME, T_FMT); |
|
712 goto subformat; |
|
713 #endif |
|
714 /* Fall through. */ |
|
715 case 'T': /* POSIX.2 extension. */ |
|
716 subfmt = "%H:%M:%S"; |
|
717 goto subformat; |
|
718 |
|
719 case 't': /* POSIX.2 extension. */ |
|
720 add (1, *p = '\t'); |
|
721 break; |
|
722 |
|
723 case 'u': /* POSIX.2 extension. */ |
|
724 DO_NUMBER (1, (tp->tm_wday - 1 + 7) % 7 + 1); |
|
725 |
|
726 case 'U': |
|
727 if (modifier == 'E') |
|
728 goto bad_format; |
|
729 |
|
730 DO_NUMBER (2, (tp->tm_yday - tp->tm_wday + 7) / 7); |
|
731 |
|
732 case 'V': |
|
733 case 'g': /* GNU extension. */ |
|
734 case 'G': /* GNU extension. */ |
|
735 if (modifier == 'E') |
|
736 goto bad_format; |
|
737 { |
|
738 int year = tp->tm_year + TM_YEAR_BASE; |
|
739 int days = iso_week_days (tp->tm_yday, tp->tm_wday); |
|
740 |
|
741 if (days < 0) |
|
742 { |
|
743 /* This ISO week belongs to the previous year. */ |
|
744 year--; |
|
745 days = iso_week_days (tp->tm_yday + (365 + __isleap (year)), |
|
746 tp->tm_wday); |
|
747 } |
|
748 else |
|
749 { |
|
750 int d = iso_week_days (tp->tm_yday - (365 + __isleap (year)), |
|
751 tp->tm_wday); |
|
752 if (0 <= d) |
|
753 { |
|
754 /* This ISO week belongs to the next year. */ |
|
755 year++; |
|
756 days = d; |
|
757 } |
|
758 } |
|
759 |
|
760 switch (*f) |
|
761 { |
|
762 case 'g': |
|
763 DO_NUMBER (2, (year % 100 + 100) % 100); |
|
764 |
|
765 case 'G': |
|
766 DO_NUMBER (1, year); |
|
767 |
|
768 default: |
|
769 DO_NUMBER (2, days / 7 + 1); |
|
770 } |
|
771 } |
|
772 |
|
773 case 'W': |
|
774 if (modifier == 'E') |
|
775 goto bad_format; |
|
776 |
|
777 DO_NUMBER (2, (tp->tm_yday - (tp->tm_wday - 1 + 7) % 7 + 7) / 7); |
|
778 |
|
779 case 'w': |
|
780 if (modifier == 'E') |
|
781 goto bad_format; |
|
782 |
|
783 DO_NUMBER (1, tp->tm_wday); |
|
784 |
|
785 case 'Y': |
|
786 #if HAVE_STRUCT_ERA_ENTRY |
|
787 if (modifier == 'E') |
|
788 { |
|
789 struct era_entry *era = _nl_get_era_entry (tp); |
|
790 if (era) |
|
791 { |
|
792 subfmt = strchr (era->name_fmt, '\0') + 1; |
|
793 goto subformat; |
|
794 } |
|
795 } |
|
796 #endif |
|
797 if (modifier == 'O') |
|
798 goto bad_format; |
|
799 else |
|
800 DO_NUMBER (1, tp->tm_year + TM_YEAR_BASE); |
|
801 |
|
802 case 'y': |
|
803 #if HAVE_STRUCT_ERA_ENTRY |
|
804 if (modifier == 'E') |
|
805 { |
|
806 struct era_entry *era = _nl_get_era_entry (tp); |
|
807 if (era) |
|
808 { |
|
809 int delta = tp->tm_year - era->start_date[0]; |
|
810 DO_NUMBER (1, (era->offset |
|
811 + (era->direction == '-' ? -delta : delta))); |
|
812 } |
|
813 } |
|
814 #endif |
|
815 DO_NUMBER (2, (tp->tm_year % 100 + 100) % 100); |
|
816 |
|
817 case 'Z': |
|
818 cpy (zonelen, zone); |
|
819 break; |
|
820 |
|
821 case 'z': /* GNU extension. */ |
|
822 if (tp->tm_isdst < 0) |
|
823 break; |
|
824 |
|
825 { |
|
826 int diff; |
|
827 #if HAVE_TM_GMTOFF |
|
828 diff = tp->tm_gmtoff; |
|
829 #else |
|
830 struct tm gtm; |
|
831 struct tm ltm; |
|
832 time_t lt; |
|
833 |
|
834 ltm = *tp; |
|
835 lt = mktime (<m); |
|
836 |
|
837 if (lt == (time_t) -1) |
|
838 { |
|
839 /* mktime returns -1 for errors, but -1 is also a |
|
840 valid time_t value. Check whether an error really |
|
841 occurred. */ |
|
842 struct tm tm; |
|
843 localtime_r (<, &tm); |
|
844 |
|
845 if ((ltm.tm_sec ^ tm.tm_sec) |
|
846 | (ltm.tm_min ^ tm.tm_min) |
|
847 | (ltm.tm_hour ^ tm.tm_hour) |
|
848 | (ltm.tm_mday ^ tm.tm_mday) |
|
849 | (ltm.tm_mon ^ tm.tm_mon) |
|
850 | (ltm.tm_year ^ tm.tm_year)) |
|
851 break; |
|
852 } |
|
853 |
|
854 if (! gmtime_r (<, >m)) |
|
855 break; |
|
856 |
|
857 diff = tm_diff (<m, >m); |
|
858 #endif |
|
859 |
|
860 if (diff < 0) |
|
861 { |
|
862 add (1, *p = '-'); |
|
863 diff = -diff; |
|
864 } |
|
865 else |
|
866 add (1, *p = '+'); |
|
867 |
|
868 diff /= 60; |
|
869 DO_NUMBER (4, (diff / 60) * 100 + diff % 60); |
|
870 } |
|
871 |
|
872 case '\0': /* GNU extension: % at end of format. */ |
|
873 --f; |
|
874 /* Fall through. */ |
|
875 default: |
|
876 /* Unknown format; output the format, including the '%', |
|
877 since this is most likely the right thing to do if a |
|
878 multibyte string has been misparsed. */ |
|
879 bad_format: |
|
880 { |
|
881 int flen; |
|
882 for (flen = 1; f[1 - flen] != '%'; flen++) |
|
883 continue; |
|
884 cpy (flen, &f[1 - flen]); |
|
885 } |
|
886 break; |
|
887 } |
|
888 } |
|
889 |
|
890 if (p) |
|
891 *p = '\0'; |
|
892 return i; |
|
893 } |
|
894 |
|
895 #endif |