blob: 75ab8ce3f3807c0679edf5c2885282724c73a024 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001#ifndef lint
2#ifndef NOID
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -07003static char elsieid[] = "@(#)strftime.c 8.1";
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004/*
5** Based on the UCB version with the ID appearing below.
6** This is ANSIish only when "multibyte character == plain character".
7*/
8#endif /* !defined NOID */
9#endif /* !defined lint */
10
11#include "private.h"
12
13/*
14** Copyright (c) 1989 The Regents of the University of California.
15** All rights reserved.
16**
17** Redistribution and use in source and binary forms are permitted
18** provided that the above copyright notice and this paragraph are
19** duplicated in all such forms and that any documentation,
20** advertising materials, and other materials related to such
21** distribution and use acknowledge that the software was developed
22** by the University of California, Berkeley. The name of the
23** University may not be used to endorse or promote products derived
24** from this software without specific prior written permission.
25** THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
26** IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
27** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
28*/
29
30#ifndef LIBC_SCCS
31#ifndef lint
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070032static const char sccsid[] = "@(#)strftime.c 5.4 (Berkeley) 3/14/89";
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080033#endif /* !defined lint */
34#endif /* !defined LIBC_SCCS */
35
36#include "tzfile.h"
37#include "fcntl.h"
38#include "locale.h"
39#include <ctype.h>
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070040#include <time64.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070042/* struct lc_time_T is now defined as strftime_locale
43 * in <time.h>
44 */
45#if 1
46#define lc_time_T strftime_locale
47#else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080048struct lc_time_T {
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070049 const char * mon[MONSPERYEAR];
50 const char * month[MONSPERYEAR];
51 const char * wday[DAYSPERWEEK];
52 const char * weekday[DAYSPERWEEK];
53 const char * X_fmt;
54 const char * x_fmt;
55 const char * c_fmt;
56 const char * am;
57 const char * pm;
58 const char * date_fmt;
59};
60#endif
61
62#define Locale (&C_time_locale)
63
64static const struct lc_time_T C_time_locale = {
65 {
66 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
67 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
68 }, {
69 "January", "February", "March", "April", "May", "June",
70 "July", "August", "September", "October", "November", "December"
71 }, {
72 "Sun", "Mon", "Tue", "Wed",
73 "Thu", "Fri", "Sat"
74 }, {
75 "Sunday", "Monday", "Tuesday", "Wednesday",
76 "Thursday", "Friday", "Saturday"
77 },
78
79 /* X_fmt */
80 "%H:%M:%S",
81
82 /*
83 ** x_fmt
84 ** C99 requires this format.
85 ** Using just numbers (as here) makes Quakers happier;
86 ** it's also compatible with SVR4.
87 */
88 "%m/%d/%y",
89
90 /*
91 ** c_fmt
92 ** C99 requires this format.
93 ** Previously this code used "%D %X", but we now conform to C99.
94 ** Note that
95 ** "%a %b %d %H:%M:%S %Y"
96 ** is used by Solaris 2.3.
97 */
98 "%a %b %e %T %Y",
99
100 /* am */
101 "AM",
102
103 /* pm */
104 "PM",
105
106 /* date_fmt */
107 "%a %b %e %H:%M:%S %Z %Y"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800108};
109
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700110static char * _add P((const char *, char *, const char *, int));
111static char * _conv P((int, const char *, char *, const char *));
112static char * _fmt P((const char *, const struct tm *, char *, const char *,
113 int *, const struct strftime_locale*));
114static char * _yconv P((int, int, int, int, char *, const char *, int));
115static char * getformat P((int, char *, char *, char *, char *));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800116
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700117extern char * tzname[];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800118
119#ifndef YEAR_2000_NAME
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700120#define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800121#endif /* !defined YEAR_2000_NAME */
122
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700123#define IN_NONE 0
124#define IN_SOME 1
125#define IN_THIS 2
126#define IN_ALL 3
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800127
128#define FORCE_LOWER_CASE 0x100
129
130size_t
131strftime(s, maxsize, format, t)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700132char * const s;
133const size_t maxsize;
134const char * const format;
135const struct tm * const t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800136{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700137 return strftime_tz(s, maxsize, format, t, Locale);
138}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800139
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700140size_t
141strftime_tz(s, maxsize, format, t, locale)
142char * const s;
143const size_t maxsize;
144const char * const format;
145const struct tm * const t;
146const struct strftime_locale *locale;
147{
148 char * p;
149 int warn;
150
151 tzset();
152 warn = IN_NONE;
153 p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn, locale);
154#if 0 /* ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
155 if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {
156 (void) fprintf(stderr, "\n");
157 if (format == NULL)
158 (void) fprintf(stderr, "NULL strftime format ");
159 else (void) fprintf(stderr, "strftime format \"%s\" ",
160 format);
161 (void) fprintf(stderr, "yields only two digits of years in ");
162 if (warn == IN_SOME)
163 (void) fprintf(stderr, "some locales");
164 else if (warn == IN_THIS)
165 (void) fprintf(stderr, "the current locale");
166 else (void) fprintf(stderr, "all locales");
167 (void) fprintf(stderr, "\n");
168 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800169#endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700170 if (p == s + maxsize)
171 return 0;
172 *p = '\0';
173 return p - s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800174}
175
176static char *getformat(int modifier, char *normal, char *underscore,
177 char *dash, char *zero) {
178 switch (modifier) {
179 case '_':
180 return underscore;
181
182 case '-':
183 return dash;
184
185 case '0':
186 return zero;
187 }
188
189 return normal;
190}
191
192static char *
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700193_fmt(format, t, pt, ptlim, warnp, locale)
194const char * format;
195const struct tm * const t;
196char * pt;
197const char * const ptlim;
198int * warnp;
199const struct strftime_locale* locale;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800200{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700201 for ( ; *format; ++format) {
202 if (*format == '%') {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800203 int modifier = 0;
204label:
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700205 switch (*++format) {
206 case '\0':
207 --format;
208 break;
209 case 'A':
210 pt = _add((t->tm_wday < 0 ||
211 t->tm_wday >= DAYSPERWEEK) ?
212 "?" : locale->weekday[t->tm_wday],
213 pt, ptlim, modifier);
214 continue;
215 case 'a':
216 pt = _add((t->tm_wday < 0 ||
217 t->tm_wday >= DAYSPERWEEK) ?
218 "?" : locale->wday[t->tm_wday],
219 pt, ptlim, modifier);
220 continue;
221 case 'B':
222 pt = _add((t->tm_mon < 0 ||
223 t->tm_mon >= MONSPERYEAR) ?
224 "?" : locale->month[t->tm_mon],
225 pt, ptlim, modifier);
226 continue;
227 case 'b':
228 case 'h':
229 pt = _add((t->tm_mon < 0 ||
230 t->tm_mon >= MONSPERYEAR) ?
231 "?" : locale->mon[t->tm_mon],
232 pt, ptlim, modifier);
233 continue;
234 case 'C':
235 /*
236 ** %C used to do a...
237 ** _fmt("%a %b %e %X %Y", t);
238 ** ...whereas now POSIX 1003.2 calls for
239 ** something completely different.
240 ** (ado, 1993-05-24)
241 */
242 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0,
243 pt, ptlim, modifier);
244 continue;
245 case 'c':
246 {
247 int warn2 = IN_SOME;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800248
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700249 pt = _fmt(locale->c_fmt, t, pt, ptlim, warnp, locale);
250 if (warn2 == IN_ALL)
251 warn2 = IN_THIS;
252 if (warn2 > *warnp)
253 *warnp = warn2;
254 }
255 continue;
256 case 'D':
257 pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp, locale);
258 continue;
259 case 'd':
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800260 pt = _conv(t->tm_mday,
261 getformat(modifier, "%02d",
262 "%2d", "%d", "%02d"),
263 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700264 continue;
265 case 'E':
266 case 'O':
267 /*
268 ** C99 locale modifiers.
269 ** The sequences
270 ** %Ec %EC %Ex %EX %Ey %EY
271 ** %Od %oe %OH %OI %Om %OM
272 ** %OS %Ou %OU %OV %Ow %OW %Oy
273 ** are supposed to provide alternate
274 ** representations.
275 */
276 goto label;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800277 case '_':
278 case '-':
279 case '0':
280 case '^':
281 case '#':
282 modifier = *format;
283 goto label;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700284 case 'e':
285 pt = _conv(t->tm_mday,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800286 getformat(modifier, "%2d",
287 "%2d", "%d", "%02d"),
288 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700289 continue;
290 case 'F':
291 pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp, locale);
292 continue;
293 case 'H':
294 pt = _conv(t->tm_hour,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800295 getformat(modifier, "%02d",
296 "%2d", "%d", "%02d"),
297 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700298 continue;
299 case 'I':
300 pt = _conv((t->tm_hour % 12) ?
301 (t->tm_hour % 12) : 12,
302 getformat(modifier, "%02d",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800303 "%2d", "%d", "%02d"),
304 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700305 continue;
306 case 'j':
307 pt = _conv(t->tm_yday + 1,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800308 getformat(modifier, "%03d", "%3d", "%d", "%03d"),
309 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700310 continue;
311 case 'k':
312 /*
313 ** This used to be...
314 ** _conv(t->tm_hour % 12 ?
315 ** t->tm_hour % 12 : 12, 2, ' ');
316 ** ...and has been changed to the below to
317 ** match SunOS 4.1.1 and Arnold Robbins'
318 ** strftime version 3.0. That is, "%k" and
319 ** "%l" have been swapped.
320 ** (ado, 1993-05-24)
321 */
322 pt = _conv(t->tm_hour,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800323 getformat(modifier, "%2d",
324 "%2d", "%d", "%02d"),
325 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700326 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800327#ifdef KITCHEN_SINK
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700328 case 'K':
329 /*
330 ** After all this time, still unclaimed!
331 */
332 pt = _add("kitchen sink", pt, ptlim, modifier);
333 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800334#endif /* defined KITCHEN_SINK */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700335 case 'l':
336 /*
337 ** This used to be...
338 ** _conv(t->tm_hour, 2, ' ');
339 ** ...and has been changed to the below to
340 ** match SunOS 4.1.1 and Arnold Robbin's
341 ** strftime version 3.0. That is, "%k" and
342 ** "%l" have been swapped.
343 ** (ado, 1993-05-24)
344 */
345 pt = _conv((t->tm_hour % 12) ?
346 (t->tm_hour % 12) : 12,
347 getformat(modifier, "%2d",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800348 "%2d", "%d", "%02d"),
349 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700350 continue;
351 case 'M':
352 pt = _conv(t->tm_min,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800353 getformat(modifier, "%02d",
354 "%2d", "%d", "%02d"),
355 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700356 continue;
357 case 'm':
358 pt = _conv(t->tm_mon + 1,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800359 getformat(modifier, "%02d",
360 "%2d", "%d", "%02d"),
361 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700362 continue;
363 case 'n':
364 pt = _add("\n", pt, ptlim, modifier);
365 continue;
366 case 'p':
367 pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
368 locale->pm :
369 locale->am,
370 pt, ptlim, modifier);
371 continue;
372 case 'P':
373 pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
374 locale->pm :
375 locale->am,
376 pt, ptlim, FORCE_LOWER_CASE);
377 continue;
378 case 'R':
379 pt = _fmt("%H:%M", t, pt, ptlim, warnp, locale);
380 continue;
381 case 'r':
382 pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp, locale);
383 continue;
384 case 'S':
385 pt = _conv(t->tm_sec,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800386 getformat(modifier, "%02d",
387 "%2d", "%d", "%02d"),
388 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700389 continue;
390 case 's':
391 {
392 struct tm tm;
393 char buf[INT_STRLEN_MAXIMUM(
394 time64_t) + 1];
395 time64_t mkt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800396
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700397 tm = *t;
398 mkt = mktime64(&tm);
399 if (TYPE_SIGNED(time64_t))
400 (void) sprintf(buf, "%lld",
401 (long long) mkt);
402 else (void) sprintf(buf, "%llu",
403 (unsigned long long) mkt);
404 pt = _add(buf, pt, ptlim, modifier);
405 }
406 continue;
407 case 'T':
408 pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp, locale);
409 continue;
410 case 't':
411 pt = _add("\t", pt, ptlim, modifier);
412 continue;
413 case 'U':
414 pt = _conv((t->tm_yday + DAYSPERWEEK -
415 t->tm_wday) / DAYSPERWEEK,
416 getformat(modifier, "%02d",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800417 "%2d", "%d", "%02d"),
418 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700419 continue;
420 case 'u':
421 /*
422 ** From Arnold Robbins' strftime version 3.0:
423 ** "ISO 8601: Weekday as a decimal number
424 ** [1 (Monday) - 7]"
425 ** (ado, 1993-05-24)
426 */
427 pt = _conv((t->tm_wday == 0) ?
428 DAYSPERWEEK : t->tm_wday, "%d", pt, ptlim);
429 continue;
430 case 'V': /* ISO 8601 week number */
431 case 'G': /* ISO 8601 year (four digits) */
432 case 'g': /* ISO 8601 year (two digits) */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800433/*
434** From Arnold Robbins' strftime version 3.0: "the week number of the
435** year (the first Monday as the first day of week 1) as a decimal number
436** (01-53)."
437** (ado, 1993-05-24)
438**
439** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
440** "Week 01 of a year is per definition the first week which has the
441** Thursday in this year, which is equivalent to the week which contains
442** the fourth day of January. In other words, the first week of a new year
443** is the week which has the majority of its days in the new year. Week 01
444** might also contain days from the previous year and the week before week
445** 01 of a year is the last week (52 or 53) of the previous year even if
446** it contains days from the new year. A week starts with Monday (day 1)
447** and ends with Sunday (day 7). For example, the first week of the year
448** 1997 lasts from 1996-12-30 to 1997-01-05..."
449** (ado, 1996-01-02)
450*/
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700451 {
452 int year;
453 int base;
454 int yday;
455 int wday;
456 int w;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800457
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700458 year = t->tm_year;
459 base = TM_YEAR_BASE;
460 yday = t->tm_yday;
461 wday = t->tm_wday;
462 for ( ; ; ) {
463 int len;
464 int bot;
465 int top;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800466
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700467 len = isleap_sum(year, base) ?
468 DAYSPERLYEAR :
469 DAYSPERNYEAR;
470 /*
471 ** What yday (-3 ... 3) does
472 ** the ISO year begin on?
473 */
474 bot = ((yday + 11 - wday) %
475 DAYSPERWEEK) - 3;
476 /*
477 ** What yday does the NEXT
478 ** ISO year begin on?
479 */
480 top = bot -
481 (len % DAYSPERWEEK);
482 if (top < -3)
483 top += DAYSPERWEEK;
484 top += len;
485 if (yday >= top) {
486 ++base;
487 w = 1;
488 break;
489 }
490 if (yday >= bot) {
491 w = 1 + ((yday - bot) /
492 DAYSPERWEEK);
493 break;
494 }
495 --base;
496 yday += isleap_sum(year, base) ?
497 DAYSPERLYEAR :
498 DAYSPERNYEAR;
499 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800500#ifdef XPG4_1994_04_09
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700501 if ((w == 52 &&
502 t->tm_mon == TM_JANUARY) ||
503 (w == 1 &&
504 t->tm_mon == TM_DECEMBER))
505 w = 53;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800506#endif /* defined XPG4_1994_04_09 */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700507 if (*format == 'V')
508 pt = _conv(w,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800509 getformat(modifier,
510 "%02d",
511 "%2d",
512 "%d",
513 "%02d"),
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700514 pt, ptlim);
515 else if (*format == 'g') {
516 *warnp = IN_ALL;
517 pt = _yconv(year, base, 0, 1,
518 pt, ptlim, modifier);
519 } else pt = _yconv(year, base, 1, 1,
520 pt, ptlim, modifier);
521 }
522 continue;
523 case 'v':
524 /*
525 ** From Arnold Robbins' strftime version 3.0:
526 ** "date as dd-bbb-YYYY"
527 ** (ado, 1993-05-24)
528 */
529 pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp, locale);
530 continue;
531 case 'W':
532 pt = _conv((t->tm_yday + DAYSPERWEEK -
533 (t->tm_wday ?
534 (t->tm_wday - 1) :
535 (DAYSPERWEEK - 1))) / DAYSPERWEEK,
536 getformat(modifier, "%02d",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800537 "%2d", "%d", "%02d"),
538 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700539 continue;
540 case 'w':
541 pt = _conv(t->tm_wday, "%d", pt, ptlim);
542 continue;
543 case 'X':
544 pt = _fmt(locale->X_fmt, t, pt, ptlim, warnp, locale);
545 continue;
546 case 'x':
547 {
548 int warn2 = IN_SOME;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800549
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700550 pt = _fmt(locale->x_fmt, t, pt, ptlim, &warn2, locale);
551 if (warn2 == IN_ALL)
552 warn2 = IN_THIS;
553 if (warn2 > *warnp)
554 *warnp = warn2;
555 }
556 continue;
557 case 'y':
558 *warnp = IN_ALL;
559 pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1,
560 pt, ptlim, modifier);
561 continue;
562 case 'Y':
563 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1,
564 pt, ptlim, modifier);
565 continue;
566 case 'Z':
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800567#ifdef TM_ZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700568 if (t->TM_ZONE != NULL)
569 pt = _add(t->TM_ZONE, pt, ptlim,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800570 modifier);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700571 else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800572#endif /* defined TM_ZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700573 if (t->tm_isdst >= 0)
574 pt = _add(tzname[t->tm_isdst != 0],
575 pt, ptlim, modifier);
576 /*
577 ** C99 says that %Z must be replaced by the
578 ** empty string if the time zone is not
579 ** determinable.
580 */
581 continue;
582 case 'z':
583 {
584 int diff;
585 char const * sign;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800586
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700587 if (t->tm_isdst < 0)
588 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800589#ifdef TM_GMTOFF
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700590 diff = t->TM_GMTOFF;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800591#else /* !defined TM_GMTOFF */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700592 /*
593 ** C99 says that the UTC offset must
594 ** be computed by looking only at
595 ** tm_isdst. This requirement is
596 ** incorrect, since it means the code
597 ** must rely on magic (in this case
598 ** altzone and timezone), and the
599 ** magic might not have the correct
600 ** offset. Doing things correctly is
601 ** tricky and requires disobeying C99;
602 ** see GNU C strftime for details.
603 ** For now, punt and conform to the
604 ** standard, even though it's incorrect.
605 **
606 ** C99 says that %z must be replaced by the
607 ** empty string if the time zone is not
608 ** determinable, so output nothing if the
609 ** appropriate variables are not available.
610 */
611 if (t->tm_isdst == 0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800612#ifdef USG_COMPAT
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700613 diff = -timezone;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800614#else /* !defined USG_COMPAT */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700615 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800616#endif /* !defined USG_COMPAT */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700617 else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800618#ifdef ALTZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700619 diff = -altzone;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800620#else /* !defined ALTZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700621 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800622#endif /* !defined ALTZONE */
623#endif /* !defined TM_GMTOFF */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700624 if (diff < 0) {
625 sign = "-";
626 diff = -diff;
627 } else sign = "+";
628 pt = _add(sign, pt, ptlim, modifier);
629 diff /= SECSPERMIN;
630 diff = (diff / MINSPERHOUR) * 100 +
631 (diff % MINSPERHOUR);
632 pt = _conv(diff,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800633 getformat(modifier, "%04d",
634 "%4d", "%d", "%04d"),
635 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700636 }
637 continue;
638 case '+':
639 pt = _fmt(locale->date_fmt, t, pt, ptlim,
640 warnp, locale);
641 continue;
642 case '%':
643 /*
644 ** X311J/88-090 (4.12.3.5): if conversion char is
645 ** undefined, behavior is undefined. Print out the
646 ** character itself as printf(3) also does.
647 */
648 default:
649 break;
650 }
651 }
652 if (pt == ptlim)
653 break;
654 *pt++ = *format;
655 }
656 return pt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800657}
658
659static char *
660_conv(n, format, pt, ptlim)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700661const int n;
662const char * const format;
663char * const pt;
664const char * const ptlim;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800665{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700666 char buf[INT_STRLEN_MAXIMUM(int) + 1];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800667
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700668 (void) snprintf(buf, sizeof(buf), format, n);
669 return _add(buf, pt, ptlim, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800670}
671
672static char *
673_add(str, pt, ptlim, modifier)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700674const char * str;
675char * pt;
676const char * const ptlim;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800677int modifier;
678{
679 int c;
680
681 switch (modifier) {
682 case FORCE_LOWER_CASE:
683 while (pt < ptlim && (*pt = tolower(*str++)) != '\0') {
684 ++pt;
685 }
686 break;
687
688 case '^':
689 while (pt < ptlim && (*pt = toupper(*str++)) != '\0') {
690 ++pt;
691 }
692 break;
693
694 case '#':
695 while (pt < ptlim && (c = *str++) != '\0') {
696 if (isupper(c)) {
697 c = tolower(c);
698 } else if (islower(c)) {
699 c = toupper(c);
700 }
701 *pt = c;
702 ++pt;
703 }
704
705 break;
706
707 default:
708 while (pt < ptlim && (*pt = *str++) != '\0') {
709 ++pt;
710 }
711 }
712
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700713 return pt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800714}
715
716/*
717** POSIX and the C Standard are unclear or inconsistent about
718** what %C and %y do if the year is negative or exceeds 9999.
719** Use the convention that %C concatenated with %y yields the
720** same output as %Y, and that %Y contains at least 4 bytes,
721** with more only if necessary.
722*/
723
724static char *
725_yconv(a, b, convert_top, convert_yy, pt, ptlim, modifier)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700726const int a;
727const int b;
728const int convert_top;
729const int convert_yy;
730char * pt;
731const char * const ptlim;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800732int modifier;
733{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700734 register int lead;
735 register int trail;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800736
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700737#define DIVISOR 100
738 trail = a % DIVISOR + b % DIVISOR;
739 lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
740 trail %= DIVISOR;
741 if (trail < 0 && lead > 0) {
742 trail += DIVISOR;
743 --lead;
744 } else if (lead < 0 && trail > 0) {
745 trail -= DIVISOR;
746 ++lead;
747 }
748 if (convert_top) {
749 if (lead == 0 && trail < 0)
750 pt = _add("-0", pt, ptlim, modifier);
751 else pt = _conv(lead, getformat(modifier, "%02d",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800752 "%2d", "%d", "%02d"),
753 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700754 }
755 if (convert_yy)
756 pt = _conv(((trail < 0) ? -trail : trail),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800757 getformat(modifier, "%02d", "%2d", "%d", "%02d"),
758 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700759 return pt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800760}