blob: 1b223e5fbdf9985b3e9bd72b991773fef421bbc5 [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>
David 'Digit' Turner208898e2012-01-13 14:24:08 +010041#include <bionic_time.h> /* for strftime_tz */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080042
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070043/* struct lc_time_T is now defined as strftime_locale
44 * in <time.h>
45 */
46#if 1
47#define lc_time_T strftime_locale
48#else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080049struct lc_time_T {
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070050 const char * mon[MONSPERYEAR];
51 const char * month[MONSPERYEAR];
52 const char * wday[DAYSPERWEEK];
53 const char * weekday[DAYSPERWEEK];
54 const char * X_fmt;
55 const char * x_fmt;
56 const char * c_fmt;
57 const char * am;
58 const char * pm;
59 const char * date_fmt;
60};
61#endif
62
63#define Locale (&C_time_locale)
64
65static const struct lc_time_T C_time_locale = {
66 {
67 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
68 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
69 }, {
70 "January", "February", "March", "April", "May", "June",
71 "July", "August", "September", "October", "November", "December"
72 }, {
Eric Fischera48fa7f2009-05-15 13:33:20 -070073 "January", "February", "March", "April", "May", "June",
74 "July", "August", "September", "October", "November", "December"
75 }, {
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070076 "Sun", "Mon", "Tue", "Wed",
77 "Thu", "Fri", "Sat"
78 }, {
79 "Sunday", "Monday", "Tuesday", "Wednesday",
80 "Thursday", "Friday", "Saturday"
81 },
82
83 /* X_fmt */
84 "%H:%M:%S",
85
86 /*
87 ** x_fmt
88 ** C99 requires this format.
89 ** Using just numbers (as here) makes Quakers happier;
90 ** it's also compatible with SVR4.
91 */
92 "%m/%d/%y",
93
94 /*
95 ** c_fmt
96 ** C99 requires this format.
97 ** Previously this code used "%D %X", but we now conform to C99.
98 ** Note that
99 ** "%a %b %d %H:%M:%S %Y"
100 ** is used by Solaris 2.3.
101 */
102 "%a %b %e %T %Y",
103
104 /* am */
105 "AM",
106
107 /* pm */
108 "PM",
109
110 /* date_fmt */
111 "%a %b %e %H:%M:%S %Z %Y"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800112};
113
Elliott Hughesce4783c2013-07-12 17:31:11 -0700114static char * _add(const char *, char *, const char *, int);
115static char * _conv(int, const char *, char *, const char *);
116static char * _fmt(const char *, const struct tm *, char *, const char *,
117 int *, const struct strftime_locale*);
118static char * _yconv(int, int, int, int, char *, const char *, int);
119static char * getformat(int, char *, char *, char *, char *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800120
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700121extern char * tzname[];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800122
123#ifndef YEAR_2000_NAME
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700124#define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800125#endif /* !defined YEAR_2000_NAME */
126
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700127#define IN_NONE 0
128#define IN_SOME 1
129#define IN_THIS 2
130#define IN_ALL 3
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800131
132#define FORCE_LOWER_CASE 0x100
133
134size_t
135strftime(s, maxsize, format, t)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700136char * const s;
137const size_t maxsize;
138const char * const format;
139const struct tm * const t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800140{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700141 return strftime_tz(s, maxsize, format, t, Locale);
142}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800143
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700144size_t
145strftime_tz(s, maxsize, format, t, locale)
146char * const s;
147const size_t maxsize;
148const char * const format;
149const struct tm * const t;
150const struct strftime_locale *locale;
151{
152 char * p;
153 int warn;
154
155 tzset();
156 warn = IN_NONE;
157 p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn, locale);
158#if 0 /* ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
159 if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {
160 (void) fprintf(stderr, "\n");
161 if (format == NULL)
162 (void) fprintf(stderr, "NULL strftime format ");
163 else (void) fprintf(stderr, "strftime format \"%s\" ",
164 format);
165 (void) fprintf(stderr, "yields only two digits of years in ");
166 if (warn == IN_SOME)
167 (void) fprintf(stderr, "some locales");
168 else if (warn == IN_THIS)
169 (void) fprintf(stderr, "the current locale");
170 else (void) fprintf(stderr, "all locales");
171 (void) fprintf(stderr, "\n");
172 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800173#endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700174 if (p == s + maxsize)
175 return 0;
176 *p = '\0';
177 return p - s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800178}
179
180static char *getformat(int modifier, char *normal, char *underscore,
181 char *dash, char *zero) {
182 switch (modifier) {
183 case '_':
184 return underscore;
185
186 case '-':
187 return dash;
188
189 case '0':
190 return zero;
191 }
192
193 return normal;
194}
195
196static char *
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700197_fmt(format, t, pt, ptlim, warnp, locale)
198const char * format;
199const struct tm * const t;
200char * pt;
201const char * const ptlim;
202int * warnp;
203const struct strftime_locale* locale;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800204{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700205 for ( ; *format; ++format) {
206 if (*format == '%') {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800207 int modifier = 0;
208label:
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700209 switch (*++format) {
210 case '\0':
211 --format;
212 break;
213 case 'A':
214 pt = _add((t->tm_wday < 0 ||
215 t->tm_wday >= DAYSPERWEEK) ?
216 "?" : locale->weekday[t->tm_wday],
217 pt, ptlim, modifier);
218 continue;
219 case 'a':
220 pt = _add((t->tm_wday < 0 ||
221 t->tm_wday >= DAYSPERWEEK) ?
222 "?" : locale->wday[t->tm_wday],
223 pt, ptlim, modifier);
224 continue;
225 case 'B':
Eric Fischera48fa7f2009-05-15 13:33:20 -0700226 if (modifier == '-') {
227 pt = _add((t->tm_mon < 0 ||
228 t->tm_mon >= MONSPERYEAR) ?
Eric Fischerd5f72af2009-08-03 15:43:18 -0700229 "?" : locale->standalone_month[t->tm_mon],
Eric Fischera48fa7f2009-05-15 13:33:20 -0700230 pt, ptlim, modifier);
231 } else {
232 pt = _add((t->tm_mon < 0 ||
233 t->tm_mon >= MONSPERYEAR) ?
Eric Fischerd5f72af2009-08-03 15:43:18 -0700234 "?" : locale->month[t->tm_mon],
Eric Fischera48fa7f2009-05-15 13:33:20 -0700235 pt, ptlim, modifier);
236 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700237 continue;
238 case 'b':
239 case 'h':
240 pt = _add((t->tm_mon < 0 ||
241 t->tm_mon >= MONSPERYEAR) ?
242 "?" : locale->mon[t->tm_mon],
243 pt, ptlim, modifier);
244 continue;
245 case 'C':
246 /*
247 ** %C used to do a...
248 ** _fmt("%a %b %e %X %Y", t);
249 ** ...whereas now POSIX 1003.2 calls for
250 ** something completely different.
251 ** (ado, 1993-05-24)
252 */
253 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0,
254 pt, ptlim, modifier);
255 continue;
256 case 'c':
257 {
258 int warn2 = IN_SOME;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800259
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700260 pt = _fmt(locale->c_fmt, t, pt, ptlim, warnp, locale);
261 if (warn2 == IN_ALL)
262 warn2 = IN_THIS;
263 if (warn2 > *warnp)
264 *warnp = warn2;
265 }
266 continue;
267 case 'D':
268 pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp, locale);
269 continue;
270 case 'd':
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800271 pt = _conv(t->tm_mday,
272 getformat(modifier, "%02d",
273 "%2d", "%d", "%02d"),
274 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700275 continue;
276 case 'E':
277 case 'O':
278 /*
279 ** C99 locale modifiers.
280 ** The sequences
281 ** %Ec %EC %Ex %EX %Ey %EY
282 ** %Od %oe %OH %OI %Om %OM
283 ** %OS %Ou %OU %OV %Ow %OW %Oy
284 ** are supposed to provide alternate
285 ** representations.
286 */
287 goto label;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800288 case '_':
289 case '-':
290 case '0':
291 case '^':
292 case '#':
293 modifier = *format;
294 goto label;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700295 case 'e':
296 pt = _conv(t->tm_mday,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800297 getformat(modifier, "%2d",
298 "%2d", "%d", "%02d"),
299 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700300 continue;
301 case 'F':
302 pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp, locale);
303 continue;
304 case 'H':
305 pt = _conv(t->tm_hour,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800306 getformat(modifier, "%02d",
307 "%2d", "%d", "%02d"),
308 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700309 continue;
310 case 'I':
311 pt = _conv((t->tm_hour % 12) ?
312 (t->tm_hour % 12) : 12,
313 getformat(modifier, "%02d",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800314 "%2d", "%d", "%02d"),
315 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700316 continue;
317 case 'j':
318 pt = _conv(t->tm_yday + 1,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800319 getformat(modifier, "%03d", "%3d", "%d", "%03d"),
320 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700321 continue;
322 case 'k':
323 /*
324 ** This used to be...
325 ** _conv(t->tm_hour % 12 ?
326 ** t->tm_hour % 12 : 12, 2, ' ');
327 ** ...and has been changed to the below to
328 ** match SunOS 4.1.1 and Arnold Robbins'
329 ** strftime version 3.0. That is, "%k" and
330 ** "%l" have been swapped.
331 ** (ado, 1993-05-24)
332 */
333 pt = _conv(t->tm_hour,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800334 getformat(modifier, "%2d",
335 "%2d", "%d", "%02d"),
336 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700337 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800338#ifdef KITCHEN_SINK
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700339 case 'K':
340 /*
341 ** After all this time, still unclaimed!
342 */
343 pt = _add("kitchen sink", pt, ptlim, modifier);
344 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800345#endif /* defined KITCHEN_SINK */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700346 case 'l':
347 /*
348 ** This used to be...
349 ** _conv(t->tm_hour, 2, ' ');
350 ** ...and has been changed to the below to
351 ** match SunOS 4.1.1 and Arnold Robbin's
352 ** strftime version 3.0. That is, "%k" and
353 ** "%l" have been swapped.
354 ** (ado, 1993-05-24)
355 */
356 pt = _conv((t->tm_hour % 12) ?
357 (t->tm_hour % 12) : 12,
358 getformat(modifier, "%2d",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800359 "%2d", "%d", "%02d"),
360 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700361 continue;
362 case 'M':
363 pt = _conv(t->tm_min,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800364 getformat(modifier, "%02d",
365 "%2d", "%d", "%02d"),
366 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700367 continue;
368 case 'm':
369 pt = _conv(t->tm_mon + 1,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800370 getformat(modifier, "%02d",
371 "%2d", "%d", "%02d"),
372 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700373 continue;
374 case 'n':
375 pt = _add("\n", pt, ptlim, modifier);
376 continue;
377 case 'p':
378 pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
379 locale->pm :
380 locale->am,
381 pt, ptlim, modifier);
382 continue;
383 case 'P':
384 pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
385 locale->pm :
386 locale->am,
387 pt, ptlim, FORCE_LOWER_CASE);
388 continue;
389 case 'R':
390 pt = _fmt("%H:%M", t, pt, ptlim, warnp, locale);
391 continue;
392 case 'r':
393 pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp, locale);
394 continue;
395 case 'S':
396 pt = _conv(t->tm_sec,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800397 getformat(modifier, "%02d",
398 "%2d", "%d", "%02d"),
399 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700400 continue;
401 case 's':
402 {
403 struct tm tm;
404 char buf[INT_STRLEN_MAXIMUM(
405 time64_t) + 1];
406 time64_t mkt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800407
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700408 tm = *t;
409 mkt = mktime64(&tm);
410 if (TYPE_SIGNED(time64_t))
Jim Huange6cff932011-06-16 22:35:16 +0800411 (void) snprintf(buf, sizeof(buf), "%lld",
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700412 (long long) mkt);
Jim Huange6cff932011-06-16 22:35:16 +0800413 else (void) snprintf(buf, sizeof(buf), "%llu",
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700414 (unsigned long long) mkt);
415 pt = _add(buf, pt, ptlim, modifier);
416 }
417 continue;
418 case 'T':
419 pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp, locale);
420 continue;
421 case 't':
422 pt = _add("\t", pt, ptlim, modifier);
423 continue;
424 case 'U':
425 pt = _conv((t->tm_yday + DAYSPERWEEK -
426 t->tm_wday) / DAYSPERWEEK,
427 getformat(modifier, "%02d",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800428 "%2d", "%d", "%02d"),
429 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700430 continue;
431 case 'u':
432 /*
433 ** From Arnold Robbins' strftime version 3.0:
434 ** "ISO 8601: Weekday as a decimal number
435 ** [1 (Monday) - 7]"
436 ** (ado, 1993-05-24)
437 */
438 pt = _conv((t->tm_wday == 0) ?
439 DAYSPERWEEK : t->tm_wday, "%d", pt, ptlim);
440 continue;
441 case 'V': /* ISO 8601 week number */
442 case 'G': /* ISO 8601 year (four digits) */
443 case 'g': /* ISO 8601 year (two digits) */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800444/*
445** From Arnold Robbins' strftime version 3.0: "the week number of the
446** year (the first Monday as the first day of week 1) as a decimal number
447** (01-53)."
448** (ado, 1993-05-24)
449**
450** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
451** "Week 01 of a year is per definition the first week which has the
452** Thursday in this year, which is equivalent to the week which contains
453** the fourth day of January. In other words, the first week of a new year
454** is the week which has the majority of its days in the new year. Week 01
455** might also contain days from the previous year and the week before week
456** 01 of a year is the last week (52 or 53) of the previous year even if
457** it contains days from the new year. A week starts with Monday (day 1)
458** and ends with Sunday (day 7). For example, the first week of the year
459** 1997 lasts from 1996-12-30 to 1997-01-05..."
460** (ado, 1996-01-02)
461*/
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700462 {
463 int year;
464 int base;
465 int yday;
466 int wday;
467 int w;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800468
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700469 year = t->tm_year;
470 base = TM_YEAR_BASE;
471 yday = t->tm_yday;
472 wday = t->tm_wday;
473 for ( ; ; ) {
474 int len;
475 int bot;
476 int top;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800477
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700478 len = isleap_sum(year, base) ?
479 DAYSPERLYEAR :
480 DAYSPERNYEAR;
481 /*
482 ** What yday (-3 ... 3) does
483 ** the ISO year begin on?
484 */
485 bot = ((yday + 11 - wday) %
486 DAYSPERWEEK) - 3;
487 /*
488 ** What yday does the NEXT
489 ** ISO year begin on?
490 */
491 top = bot -
492 (len % DAYSPERWEEK);
493 if (top < -3)
494 top += DAYSPERWEEK;
495 top += len;
496 if (yday >= top) {
497 ++base;
498 w = 1;
499 break;
500 }
501 if (yday >= bot) {
502 w = 1 + ((yday - bot) /
503 DAYSPERWEEK);
504 break;
505 }
506 --base;
507 yday += isleap_sum(year, base) ?
508 DAYSPERLYEAR :
509 DAYSPERNYEAR;
510 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800511#ifdef XPG4_1994_04_09
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700512 if ((w == 52 &&
513 t->tm_mon == TM_JANUARY) ||
514 (w == 1 &&
515 t->tm_mon == TM_DECEMBER))
516 w = 53;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800517#endif /* defined XPG4_1994_04_09 */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700518 if (*format == 'V')
519 pt = _conv(w,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800520 getformat(modifier,
521 "%02d",
522 "%2d",
523 "%d",
524 "%02d"),
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700525 pt, ptlim);
526 else if (*format == 'g') {
527 *warnp = IN_ALL;
528 pt = _yconv(year, base, 0, 1,
529 pt, ptlim, modifier);
530 } else pt = _yconv(year, base, 1, 1,
531 pt, ptlim, modifier);
532 }
533 continue;
534 case 'v':
535 /*
536 ** From Arnold Robbins' strftime version 3.0:
537 ** "date as dd-bbb-YYYY"
538 ** (ado, 1993-05-24)
539 */
540 pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp, locale);
541 continue;
542 case 'W':
543 pt = _conv((t->tm_yday + DAYSPERWEEK -
544 (t->tm_wday ?
545 (t->tm_wday - 1) :
546 (DAYSPERWEEK - 1))) / DAYSPERWEEK,
547 getformat(modifier, "%02d",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800548 "%2d", "%d", "%02d"),
549 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700550 continue;
551 case 'w':
552 pt = _conv(t->tm_wday, "%d", pt, ptlim);
553 continue;
554 case 'X':
555 pt = _fmt(locale->X_fmt, t, pt, ptlim, warnp, locale);
556 continue;
557 case 'x':
558 {
559 int warn2 = IN_SOME;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800560
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700561 pt = _fmt(locale->x_fmt, t, pt, ptlim, &warn2, locale);
562 if (warn2 == IN_ALL)
563 warn2 = IN_THIS;
564 if (warn2 > *warnp)
565 *warnp = warn2;
566 }
567 continue;
568 case 'y':
569 *warnp = IN_ALL;
570 pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1,
571 pt, ptlim, modifier);
572 continue;
573 case 'Y':
574 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1,
575 pt, ptlim, modifier);
576 continue;
577 case 'Z':
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800578#ifdef TM_ZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700579 if (t->TM_ZONE != NULL)
580 pt = _add(t->TM_ZONE, pt, ptlim,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800581 modifier);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700582 else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800583#endif /* defined TM_ZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700584 if (t->tm_isdst >= 0)
585 pt = _add(tzname[t->tm_isdst != 0],
586 pt, ptlim, modifier);
587 /*
588 ** C99 says that %Z must be replaced by the
589 ** empty string if the time zone is not
590 ** determinable.
591 */
592 continue;
593 case 'z':
594 {
Elliott Hughese0d0b152013-09-27 00:04:30 -0700595 long diff;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700596 char const * sign;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800597
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700598 if (t->tm_isdst < 0)
599 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800600#ifdef TM_GMTOFF
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700601 diff = t->TM_GMTOFF;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800602#else /* !defined TM_GMTOFF */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700603 /*
Elliott Hughese0d0b152013-09-27 00:04:30 -0700604 ** C99 says that the UT offset must
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700605 ** be computed by looking only at
606 ** tm_isdst. This requirement is
607 ** incorrect, since it means the code
608 ** must rely on magic (in this case
609 ** altzone and timezone), and the
610 ** magic might not have the correct
611 ** offset. Doing things correctly is
612 ** tricky and requires disobeying C99;
613 ** see GNU C strftime for details.
614 ** For now, punt and conform to the
615 ** standard, even though it's incorrect.
616 **
617 ** C99 says that %z must be replaced by the
618 ** empty string if the time zone is not
619 ** determinable, so output nothing if the
620 ** appropriate variables are not available.
621 */
622 if (t->tm_isdst == 0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800623#ifdef USG_COMPAT
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700624 diff = -timezone;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800625#else /* !defined USG_COMPAT */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700626 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800627#endif /* !defined USG_COMPAT */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700628 else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800629#ifdef ALTZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700630 diff = -altzone;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800631#else /* !defined ALTZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700632 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800633#endif /* !defined ALTZONE */
634#endif /* !defined TM_GMTOFF */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700635 if (diff < 0) {
636 sign = "-";
637 diff = -diff;
638 } else sign = "+";
639 pt = _add(sign, pt, ptlim, modifier);
640 diff /= SECSPERMIN;
641 diff = (diff / MINSPERHOUR) * 100 +
642 (diff % MINSPERHOUR);
643 pt = _conv(diff,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800644 getformat(modifier, "%04d",
645 "%4d", "%d", "%04d"),
646 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700647 }
648 continue;
649 case '+':
650 pt = _fmt(locale->date_fmt, t, pt, ptlim,
651 warnp, locale);
652 continue;
653 case '%':
654 /*
655 ** X311J/88-090 (4.12.3.5): if conversion char is
656 ** undefined, behavior is undefined. Print out the
657 ** character itself as printf(3) also does.
658 */
659 default:
660 break;
661 }
662 }
663 if (pt == ptlim)
664 break;
665 *pt++ = *format;
666 }
667 return pt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800668}
669
670static char *
671_conv(n, format, pt, ptlim)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700672const int n;
673const char * const format;
674char * const pt;
675const char * const ptlim;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800676{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700677 char buf[INT_STRLEN_MAXIMUM(int) + 1];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800678
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700679 (void) snprintf(buf, sizeof(buf), format, n);
680 return _add(buf, pt, ptlim, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800681}
682
683static char *
684_add(str, pt, ptlim, modifier)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700685const char * str;
686char * pt;
687const char * const ptlim;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800688int modifier;
689{
690 int c;
691
692 switch (modifier) {
693 case FORCE_LOWER_CASE:
694 while (pt < ptlim && (*pt = tolower(*str++)) != '\0') {
695 ++pt;
696 }
697 break;
698
699 case '^':
700 while (pt < ptlim && (*pt = toupper(*str++)) != '\0') {
701 ++pt;
702 }
703 break;
704
705 case '#':
706 while (pt < ptlim && (c = *str++) != '\0') {
707 if (isupper(c)) {
708 c = tolower(c);
709 } else if (islower(c)) {
710 c = toupper(c);
711 }
712 *pt = c;
713 ++pt;
714 }
715
716 break;
717
718 default:
719 while (pt < ptlim && (*pt = *str++) != '\0') {
720 ++pt;
721 }
722 }
723
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700724 return pt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800725}
726
727/*
728** POSIX and the C Standard are unclear or inconsistent about
729** what %C and %y do if the year is negative or exceeds 9999.
730** Use the convention that %C concatenated with %y yields the
731** same output as %Y, and that %Y contains at least 4 bytes,
732** with more only if necessary.
733*/
734
735static char *
736_yconv(a, b, convert_top, convert_yy, pt, ptlim, modifier)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700737const int a;
738const int b;
739const int convert_top;
740const int convert_yy;
741char * pt;
742const char * const ptlim;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800743int modifier;
744{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700745 register int lead;
746 register int trail;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800747
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700748#define DIVISOR 100
749 trail = a % DIVISOR + b % DIVISOR;
750 lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
751 trail %= DIVISOR;
752 if (trail < 0 && lead > 0) {
753 trail += DIVISOR;
754 --lead;
755 } else if (lead < 0 && trail > 0) {
756 trail -= DIVISOR;
757 ++lead;
758 }
759 if (convert_top) {
760 if (lead == 0 && trail < 0)
761 pt = _add("-0", pt, ptlim, modifier);
762 else pt = _conv(lead, getformat(modifier, "%02d",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800763 "%2d", "%d", "%02d"),
764 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700765 }
766 if (convert_yy)
767 pt = _conv(((trail < 0) ? -trail : trail),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800768 getformat(modifier, "%02d", "%2d", "%d", "%02d"),
769 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700770 return pt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800771}