blob: 967629de61a556667a060eded3ce4f8c0f32fa83 [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>
Elliott Hughes52defb72014-05-05 17:14:02 -070040#if defined(__LP64__)
41#define time64_t time_t
42#define mktime64 mktime
43#else
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070044#include <time64.h>
Elliott Hughes52defb72014-05-05 17:14:02 -070045#endif
Elliott Hugheseb847bc2013-10-09 15:50:50 -070046#include "private/bionic_time.h" /* for strftime_tz */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080047
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070048/* struct lc_time_T is now defined as strftime_locale
49 * in <time.h>
50 */
51#if 1
52#define lc_time_T strftime_locale
53#else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080054struct lc_time_T {
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070055 const char * mon[MONSPERYEAR];
56 const char * month[MONSPERYEAR];
57 const char * wday[DAYSPERWEEK];
58 const char * weekday[DAYSPERWEEK];
59 const char * X_fmt;
60 const char * x_fmt;
61 const char * c_fmt;
62 const char * am;
63 const char * pm;
64 const char * date_fmt;
65};
66#endif
67
68#define Locale (&C_time_locale)
69
70static const struct lc_time_T C_time_locale = {
71 {
72 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
73 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
74 }, {
75 "January", "February", "March", "April", "May", "June",
76 "July", "August", "September", "October", "November", "December"
77 }, {
Eric Fischera48fa7f2009-05-15 13:33:20 -070078 "January", "February", "March", "April", "May", "June",
79 "July", "August", "September", "October", "November", "December"
80 }, {
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070081 "Sun", "Mon", "Tue", "Wed",
82 "Thu", "Fri", "Sat"
83 }, {
84 "Sunday", "Monday", "Tuesday", "Wednesday",
85 "Thursday", "Friday", "Saturday"
86 },
87
88 /* X_fmt */
89 "%H:%M:%S",
90
91 /*
92 ** x_fmt
93 ** C99 requires this format.
94 ** Using just numbers (as here) makes Quakers happier;
95 ** it's also compatible with SVR4.
96 */
97 "%m/%d/%y",
98
99 /*
100 ** c_fmt
101 ** C99 requires this format.
102 ** Previously this code used "%D %X", but we now conform to C99.
103 ** Note that
104 ** "%a %b %d %H:%M:%S %Y"
105 ** is used by Solaris 2.3.
106 */
107 "%a %b %e %T %Y",
108
109 /* am */
110 "AM",
111
112 /* pm */
113 "PM",
114
115 /* date_fmt */
116 "%a %b %e %H:%M:%S %Z %Y"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800117};
118
Elliott Hughesce4783c2013-07-12 17:31:11 -0700119static char * _add(const char *, char *, const char *, int);
120static char * _conv(int, const char *, char *, const char *);
121static char * _fmt(const char *, const struct tm *, char *, const char *,
122 int *, const struct strftime_locale*);
123static char * _yconv(int, int, int, int, char *, const char *, int);
124static char * getformat(int, char *, char *, char *, char *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800125
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700126extern char * tzname[];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800127
128#ifndef YEAR_2000_NAME
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700129#define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800130#endif /* !defined YEAR_2000_NAME */
131
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700132#define IN_NONE 0
133#define IN_SOME 1
134#define IN_THIS 2
135#define IN_ALL 3
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800136
137#define FORCE_LOWER_CASE 0x100
138
139size_t
140strftime(s, maxsize, format, t)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700141char * const s;
142const size_t maxsize;
143const char * const format;
144const struct tm * const t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800145{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700146 return strftime_tz(s, maxsize, format, t, Locale);
147}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800148
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700149size_t
150strftime_tz(s, maxsize, format, t, locale)
151char * const s;
152const size_t maxsize;
153const char * const format;
154const struct tm * const t;
155const struct strftime_locale *locale;
156{
157 char * p;
158 int warn;
159
160 tzset();
161 warn = IN_NONE;
162 p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn, locale);
Elliott Hughes9a5a3e82014-05-05 20:28:28 -0700163#ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700164 if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {
165 (void) fprintf(stderr, "\n");
166 if (format == NULL)
167 (void) fprintf(stderr, "NULL strftime format ");
168 else (void) fprintf(stderr, "strftime format \"%s\" ",
169 format);
170 (void) fprintf(stderr, "yields only two digits of years in ");
171 if (warn == IN_SOME)
172 (void) fprintf(stderr, "some locales");
173 else if (warn == IN_THIS)
174 (void) fprintf(stderr, "the current locale");
175 else (void) fprintf(stderr, "all locales");
176 (void) fprintf(stderr, "\n");
177 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800178#endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700179 if (p == s + maxsize)
180 return 0;
181 *p = '\0';
182 return p - s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800183}
184
185static char *getformat(int modifier, char *normal, char *underscore,
186 char *dash, char *zero) {
187 switch (modifier) {
188 case '_':
189 return underscore;
190
191 case '-':
192 return dash;
193
194 case '0':
195 return zero;
196 }
197
198 return normal;
199}
200
201static char *
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700202_fmt(format, t, pt, ptlim, warnp, locale)
203const char * format;
204const struct tm * const t;
205char * pt;
206const char * const ptlim;
207int * warnp;
208const struct strftime_locale* locale;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800209{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700210 for ( ; *format; ++format) {
211 if (*format == '%') {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800212 int modifier = 0;
213label:
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700214 switch (*++format) {
215 case '\0':
216 --format;
217 break;
218 case 'A':
219 pt = _add((t->tm_wday < 0 ||
220 t->tm_wday >= DAYSPERWEEK) ?
221 "?" : locale->weekday[t->tm_wday],
222 pt, ptlim, modifier);
223 continue;
224 case 'a':
225 pt = _add((t->tm_wday < 0 ||
226 t->tm_wday >= DAYSPERWEEK) ?
227 "?" : locale->wday[t->tm_wday],
228 pt, ptlim, modifier);
229 continue;
230 case 'B':
Eric Fischera48fa7f2009-05-15 13:33:20 -0700231 if (modifier == '-') {
232 pt = _add((t->tm_mon < 0 ||
233 t->tm_mon >= MONSPERYEAR) ?
Eric Fischerd5f72af2009-08-03 15:43:18 -0700234 "?" : locale->standalone_month[t->tm_mon],
Eric Fischera48fa7f2009-05-15 13:33:20 -0700235 pt, ptlim, modifier);
236 } else {
237 pt = _add((t->tm_mon < 0 ||
238 t->tm_mon >= MONSPERYEAR) ?
Eric Fischerd5f72af2009-08-03 15:43:18 -0700239 "?" : locale->month[t->tm_mon],
Eric Fischera48fa7f2009-05-15 13:33:20 -0700240 pt, ptlim, modifier);
241 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700242 continue;
243 case 'b':
244 case 'h':
245 pt = _add((t->tm_mon < 0 ||
246 t->tm_mon >= MONSPERYEAR) ?
247 "?" : locale->mon[t->tm_mon],
248 pt, ptlim, modifier);
249 continue;
250 case 'C':
251 /*
252 ** %C used to do a...
253 ** _fmt("%a %b %e %X %Y", t);
254 ** ...whereas now POSIX 1003.2 calls for
255 ** something completely different.
256 ** (ado, 1993-05-24)
257 */
258 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0,
259 pt, ptlim, modifier);
260 continue;
261 case 'c':
262 {
263 int warn2 = IN_SOME;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800264
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700265 pt = _fmt(locale->c_fmt, t, pt, ptlim, warnp, locale);
266 if (warn2 == IN_ALL)
267 warn2 = IN_THIS;
268 if (warn2 > *warnp)
269 *warnp = warn2;
270 }
271 continue;
272 case 'D':
273 pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp, locale);
274 continue;
275 case 'd':
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800276 pt = _conv(t->tm_mday,
277 getformat(modifier, "%02d",
278 "%2d", "%d", "%02d"),
279 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700280 continue;
281 case 'E':
282 case 'O':
283 /*
284 ** C99 locale modifiers.
285 ** The sequences
286 ** %Ec %EC %Ex %EX %Ey %EY
287 ** %Od %oe %OH %OI %Om %OM
288 ** %OS %Ou %OU %OV %Ow %OW %Oy
289 ** are supposed to provide alternate
290 ** representations.
291 */
292 goto label;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800293 case '_':
294 case '-':
295 case '0':
296 case '^':
297 case '#':
298 modifier = *format;
299 goto label;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700300 case 'e':
301 pt = _conv(t->tm_mday,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800302 getformat(modifier, "%2d",
303 "%2d", "%d", "%02d"),
304 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700305 continue;
306 case 'F':
307 pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp, locale);
308 continue;
309 case 'H':
310 pt = _conv(t->tm_hour,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800311 getformat(modifier, "%02d",
312 "%2d", "%d", "%02d"),
313 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700314 continue;
315 case 'I':
316 pt = _conv((t->tm_hour % 12) ?
317 (t->tm_hour % 12) : 12,
318 getformat(modifier, "%02d",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800319 "%2d", "%d", "%02d"),
320 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700321 continue;
322 case 'j':
323 pt = _conv(t->tm_yday + 1,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800324 getformat(modifier, "%03d", "%3d", "%d", "%03d"),
325 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700326 continue;
327 case 'k':
328 /*
329 ** This used to be...
330 ** _conv(t->tm_hour % 12 ?
331 ** t->tm_hour % 12 : 12, 2, ' ');
332 ** ...and has been changed to the below to
333 ** match SunOS 4.1.1 and Arnold Robbins'
334 ** strftime version 3.0. That is, "%k" and
335 ** "%l" have been swapped.
336 ** (ado, 1993-05-24)
337 */
338 pt = _conv(t->tm_hour,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800339 getformat(modifier, "%2d",
340 "%2d", "%d", "%02d"),
341 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700342 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800343#ifdef KITCHEN_SINK
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700344 case 'K':
345 /*
346 ** After all this time, still unclaimed!
347 */
348 pt = _add("kitchen sink", pt, ptlim, modifier);
349 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800350#endif /* defined KITCHEN_SINK */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700351 case 'l':
352 /*
353 ** This used to be...
354 ** _conv(t->tm_hour, 2, ' ');
355 ** ...and has been changed to the below to
356 ** match SunOS 4.1.1 and Arnold Robbin's
357 ** strftime version 3.0. That is, "%k" and
358 ** "%l" have been swapped.
359 ** (ado, 1993-05-24)
360 */
361 pt = _conv((t->tm_hour % 12) ?
362 (t->tm_hour % 12) : 12,
363 getformat(modifier, "%2d",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800364 "%2d", "%d", "%02d"),
365 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700366 continue;
367 case 'M':
368 pt = _conv(t->tm_min,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800369 getformat(modifier, "%02d",
370 "%2d", "%d", "%02d"),
371 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700372 continue;
373 case 'm':
374 pt = _conv(t->tm_mon + 1,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800375 getformat(modifier, "%02d",
376 "%2d", "%d", "%02d"),
377 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700378 continue;
379 case 'n':
380 pt = _add("\n", pt, ptlim, modifier);
381 continue;
382 case 'p':
383 pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
384 locale->pm :
385 locale->am,
386 pt, ptlim, modifier);
387 continue;
388 case 'P':
389 pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
390 locale->pm :
391 locale->am,
392 pt, ptlim, FORCE_LOWER_CASE);
393 continue;
394 case 'R':
395 pt = _fmt("%H:%M", t, pt, ptlim, warnp, locale);
396 continue;
397 case 'r':
398 pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp, locale);
399 continue;
400 case 'S':
401 pt = _conv(t->tm_sec,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800402 getformat(modifier, "%02d",
403 "%2d", "%d", "%02d"),
404 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700405 continue;
406 case 's':
407 {
408 struct tm tm;
409 char buf[INT_STRLEN_MAXIMUM(
410 time64_t) + 1];
411 time64_t mkt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800412
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700413 tm = *t;
414 mkt = mktime64(&tm);
415 if (TYPE_SIGNED(time64_t))
Jim Huange6cff932011-06-16 22:35:16 +0800416 (void) snprintf(buf, sizeof(buf), "%lld",
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700417 (long long) mkt);
Jim Huange6cff932011-06-16 22:35:16 +0800418 else (void) snprintf(buf, sizeof(buf), "%llu",
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700419 (unsigned long long) mkt);
420 pt = _add(buf, pt, ptlim, modifier);
421 }
422 continue;
423 case 'T':
424 pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp, locale);
425 continue;
426 case 't':
427 pt = _add("\t", pt, ptlim, modifier);
428 continue;
429 case 'U':
430 pt = _conv((t->tm_yday + DAYSPERWEEK -
431 t->tm_wday) / DAYSPERWEEK,
432 getformat(modifier, "%02d",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800433 "%2d", "%d", "%02d"),
434 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700435 continue;
436 case 'u':
437 /*
438 ** From Arnold Robbins' strftime version 3.0:
439 ** "ISO 8601: Weekday as a decimal number
440 ** [1 (Monday) - 7]"
441 ** (ado, 1993-05-24)
442 */
443 pt = _conv((t->tm_wday == 0) ?
444 DAYSPERWEEK : t->tm_wday, "%d", pt, ptlim);
445 continue;
446 case 'V': /* ISO 8601 week number */
447 case 'G': /* ISO 8601 year (four digits) */
448 case 'g': /* ISO 8601 year (two digits) */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800449/*
450** From Arnold Robbins' strftime version 3.0: "the week number of the
451** year (the first Monday as the first day of week 1) as a decimal number
452** (01-53)."
453** (ado, 1993-05-24)
454**
455** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
456** "Week 01 of a year is per definition the first week which has the
457** Thursday in this year, which is equivalent to the week which contains
458** the fourth day of January. In other words, the first week of a new year
459** is the week which has the majority of its days in the new year. Week 01
460** might also contain days from the previous year and the week before week
461** 01 of a year is the last week (52 or 53) of the previous year even if
462** it contains days from the new year. A week starts with Monday (day 1)
463** and ends with Sunday (day 7). For example, the first week of the year
464** 1997 lasts from 1996-12-30 to 1997-01-05..."
465** (ado, 1996-01-02)
466*/
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700467 {
468 int year;
469 int base;
470 int yday;
471 int wday;
472 int w;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800473
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700474 year = t->tm_year;
475 base = TM_YEAR_BASE;
476 yday = t->tm_yday;
477 wday = t->tm_wday;
478 for ( ; ; ) {
479 int len;
480 int bot;
481 int top;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800482
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700483 len = isleap_sum(year, base) ?
484 DAYSPERLYEAR :
485 DAYSPERNYEAR;
486 /*
487 ** What yday (-3 ... 3) does
488 ** the ISO year begin on?
489 */
490 bot = ((yday + 11 - wday) %
491 DAYSPERWEEK) - 3;
492 /*
493 ** What yday does the NEXT
494 ** ISO year begin on?
495 */
496 top = bot -
497 (len % DAYSPERWEEK);
498 if (top < -3)
499 top += DAYSPERWEEK;
500 top += len;
501 if (yday >= top) {
502 ++base;
503 w = 1;
504 break;
505 }
506 if (yday >= bot) {
507 w = 1 + ((yday - bot) /
508 DAYSPERWEEK);
509 break;
510 }
511 --base;
512 yday += isleap_sum(year, base) ?
513 DAYSPERLYEAR :
514 DAYSPERNYEAR;
515 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800516#ifdef XPG4_1994_04_09
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700517 if ((w == 52 &&
518 t->tm_mon == TM_JANUARY) ||
519 (w == 1 &&
520 t->tm_mon == TM_DECEMBER))
521 w = 53;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800522#endif /* defined XPG4_1994_04_09 */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700523 if (*format == 'V')
524 pt = _conv(w,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800525 getformat(modifier,
526 "%02d",
527 "%2d",
528 "%d",
529 "%02d"),
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700530 pt, ptlim);
531 else if (*format == 'g') {
532 *warnp = IN_ALL;
533 pt = _yconv(year, base, 0, 1,
534 pt, ptlim, modifier);
535 } else pt = _yconv(year, base, 1, 1,
536 pt, ptlim, modifier);
537 }
538 continue;
539 case 'v':
540 /*
541 ** From Arnold Robbins' strftime version 3.0:
542 ** "date as dd-bbb-YYYY"
543 ** (ado, 1993-05-24)
544 */
545 pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp, locale);
546 continue;
547 case 'W':
548 pt = _conv((t->tm_yday + DAYSPERWEEK -
549 (t->tm_wday ?
550 (t->tm_wday - 1) :
551 (DAYSPERWEEK - 1))) / DAYSPERWEEK,
552 getformat(modifier, "%02d",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800553 "%2d", "%d", "%02d"),
554 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700555 continue;
556 case 'w':
557 pt = _conv(t->tm_wday, "%d", pt, ptlim);
558 continue;
559 case 'X':
560 pt = _fmt(locale->X_fmt, t, pt, ptlim, warnp, locale);
561 continue;
562 case 'x':
563 {
564 int warn2 = IN_SOME;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800565
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700566 pt = _fmt(locale->x_fmt, t, pt, ptlim, &warn2, locale);
567 if (warn2 == IN_ALL)
568 warn2 = IN_THIS;
569 if (warn2 > *warnp)
570 *warnp = warn2;
571 }
572 continue;
573 case 'y':
574 *warnp = IN_ALL;
575 pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1,
576 pt, ptlim, modifier);
577 continue;
578 case 'Y':
579 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1,
580 pt, ptlim, modifier);
581 continue;
582 case 'Z':
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800583#ifdef TM_ZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700584 if (t->TM_ZONE != NULL)
585 pt = _add(t->TM_ZONE, pt, ptlim,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800586 modifier);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700587 else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800588#endif /* defined TM_ZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700589 if (t->tm_isdst >= 0)
590 pt = _add(tzname[t->tm_isdst != 0],
591 pt, ptlim, modifier);
592 /*
593 ** C99 says that %Z must be replaced by the
594 ** empty string if the time zone is not
595 ** determinable.
596 */
597 continue;
598 case 'z':
599 {
Elliott Hughese0d0b152013-09-27 00:04:30 -0700600 long diff;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700601 char const * sign;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800602
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700603 if (t->tm_isdst < 0)
604 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800605#ifdef TM_GMTOFF
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700606 diff = t->TM_GMTOFF;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800607#else /* !defined TM_GMTOFF */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700608 /*
Elliott Hughese0d0b152013-09-27 00:04:30 -0700609 ** C99 says that the UT offset must
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700610 ** be computed by looking only at
611 ** tm_isdst. This requirement is
612 ** incorrect, since it means the code
613 ** must rely on magic (in this case
614 ** altzone and timezone), and the
615 ** magic might not have the correct
616 ** offset. Doing things correctly is
617 ** tricky and requires disobeying C99;
618 ** see GNU C strftime for details.
619 ** For now, punt and conform to the
620 ** standard, even though it's incorrect.
621 **
622 ** C99 says that %z must be replaced by the
623 ** empty string if the time zone is not
624 ** determinable, so output nothing if the
625 ** appropriate variables are not available.
626 */
627 if (t->tm_isdst == 0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800628#ifdef USG_COMPAT
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700629 diff = -timezone;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800630#else /* !defined USG_COMPAT */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700631 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800632#endif /* !defined USG_COMPAT */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700633 else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800634#ifdef ALTZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700635 diff = -altzone;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800636#else /* !defined ALTZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700637 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800638#endif /* !defined ALTZONE */
639#endif /* !defined TM_GMTOFF */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700640 if (diff < 0) {
641 sign = "-";
642 diff = -diff;
643 } else sign = "+";
644 pt = _add(sign, pt, ptlim, modifier);
645 diff /= SECSPERMIN;
646 diff = (diff / MINSPERHOUR) * 100 +
647 (diff % MINSPERHOUR);
648 pt = _conv(diff,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800649 getformat(modifier, "%04d",
650 "%4d", "%d", "%04d"),
651 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700652 }
653 continue;
654 case '+':
655 pt = _fmt(locale->date_fmt, t, pt, ptlim,
656 warnp, locale);
657 continue;
658 case '%':
659 /*
660 ** X311J/88-090 (4.12.3.5): if conversion char is
661 ** undefined, behavior is undefined. Print out the
662 ** character itself as printf(3) also does.
663 */
664 default:
665 break;
666 }
667 }
668 if (pt == ptlim)
669 break;
670 *pt++ = *format;
671 }
672 return pt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800673}
674
675static char *
676_conv(n, format, pt, ptlim)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700677const int n;
678const char * const format;
679char * const pt;
680const char * const ptlim;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800681{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700682 char buf[INT_STRLEN_MAXIMUM(int) + 1];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800683
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700684 (void) snprintf(buf, sizeof(buf), format, n);
685 return _add(buf, pt, ptlim, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800686}
687
688static char *
689_add(str, pt, ptlim, modifier)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700690const char * str;
691char * pt;
692const char * const ptlim;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800693int modifier;
694{
695 int c;
696
697 switch (modifier) {
698 case FORCE_LOWER_CASE:
699 while (pt < ptlim && (*pt = tolower(*str++)) != '\0') {
700 ++pt;
701 }
702 break;
703
704 case '^':
705 while (pt < ptlim && (*pt = toupper(*str++)) != '\0') {
706 ++pt;
707 }
708 break;
709
710 case '#':
711 while (pt < ptlim && (c = *str++) != '\0') {
712 if (isupper(c)) {
713 c = tolower(c);
714 } else if (islower(c)) {
715 c = toupper(c);
716 }
717 *pt = c;
718 ++pt;
719 }
720
721 break;
722
723 default:
724 while (pt < ptlim && (*pt = *str++) != '\0') {
725 ++pt;
726 }
727 }
728
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700729 return pt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800730}
731
732/*
733** POSIX and the C Standard are unclear or inconsistent about
734** what %C and %y do if the year is negative or exceeds 9999.
735** Use the convention that %C concatenated with %y yields the
736** same output as %Y, and that %Y contains at least 4 bytes,
737** with more only if necessary.
738*/
739
740static char *
741_yconv(a, b, convert_top, convert_yy, pt, ptlim, modifier)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700742const int a;
743const int b;
744const int convert_top;
745const int convert_yy;
746char * pt;
747const char * const ptlim;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800748int modifier;
749{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700750 register int lead;
751 register int trail;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800752
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700753#define DIVISOR 100
754 trail = a % DIVISOR + b % DIVISOR;
755 lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
756 trail %= DIVISOR;
757 if (trail < 0 && lead > 0) {
758 trail += DIVISOR;
759 --lead;
760 } else if (lead < 0 && trail > 0) {
761 trail -= DIVISOR;
762 ++lead;
763 }
764 if (convert_top) {
765 if (lead == 0 && trail < 0)
766 pt = _add("-0", pt, ptlim, modifier);
767 else pt = _conv(lead, getformat(modifier, "%02d",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800768 "%2d", "%d", "%02d"),
769 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700770 }
771 if (convert_yy)
772 pt = _conv(((trail < 0) ? -trail : trail),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800773 getformat(modifier, "%02d", "%2d", "%d", "%02d"),
774 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700775 return pt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800776}