blob: c4ff19819c486011e076b7a7e90b4cd2658448aa [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
Elliott Hughes5f564542014-06-19 13:54:10 -07002** Based on the UCB version with the copyright notice and sccsid
3** appearing below.
4**
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08005** This is ANSIish only when "multibyte character == plain character".
6*/
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08007
8#include "private.h"
9
10/*
11** Copyright (c) 1989 The Regents of the University of California.
12** All rights reserved.
13**
14** Redistribution and use in source and binary forms are permitted
15** provided that the above copyright notice and this paragraph are
16** duplicated in all such forms and that any documentation,
17** advertising materials, and other materials related to such
18** distribution and use acknowledge that the software was developed
19** by the University of California, Berkeley. The name of the
20** University may not be used to endorse or promote products derived
21** from this software without specific prior written permission.
Elliott Hughes5f564542014-06-19 13:54:10 -070022** THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080023** IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
24** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
25*/
26
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080027#include "tzfile.h"
28#include "fcntl.h"
29#include "locale.h"
Elliott Hughes5f564542014-06-19 13:54:10 -070030
31#if __ANDROID__
Elliott Hughes905e6d52014-07-25 11:55:59 -070032
33/*
34 * This has an extra standalone_month array field compared to upstream.
35 * We only need to keep that if we leave the strftime_tz symbol exposed.
36 * Even then, this structure was never in an NDK header file.
37 */
38struct lc_time_T {
39 const char * mon[12];
40 const char * month[12];
41 const char * standalone_month[12];
42 const char * wday[7];
43 const char * weekday[7];
44 const char * X_fmt;
45 const char * x_fmt;
46 const char * c_fmt;
47 const char * am;
48 const char * pm;
49 const char * date_fmt;
50};
51
52/* LP32 had a 32-bit time_t, so we need to work around that here. */
Elliott Hughes52defb72014-05-05 17:14:02 -070053#if defined(__LP64__)
54#define time64_t time_t
55#define mktime64 mktime
56#else
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070057#include <time64.h>
Elliott Hughes52defb72014-05-05 17:14:02 -070058#endif
Elliott Hughes905e6d52014-07-25 11:55:59 -070059
Elliott Hughes5f564542014-06-19 13:54:10 -070060#include <ctype.h>
Elliott Hughes905e6d52014-07-25 11:55:59 -070061
62size_t strftime_tz(char*, size_t, const char*, const struct tm*, const struct lc_time_T*);
63
Elliott Hughes5f564542014-06-19 13:54:10 -070064#else // not __ANDROID__
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080065struct lc_time_T {
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070066 const char * mon[MONSPERYEAR];
67 const char * month[MONSPERYEAR];
68 const char * wday[DAYSPERWEEK];
69 const char * weekday[DAYSPERWEEK];
70 const char * X_fmt;
71 const char * x_fmt;
72 const char * c_fmt;
73 const char * am;
74 const char * pm;
75 const char * date_fmt;
76};
77#endif
78
Elliott Hughes5f564542014-06-19 13:54:10 -070079#if LOCALE_HOME
80#include "sys/stat.h"
81static struct lc_time_T localebuf;
82static struct lc_time_T * _loc(void);
83#define Locale _loc()
84#endif /* defined LOCALE_HOME */
85#ifndef LOCALE_HOME
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070086#define Locale (&C_time_locale)
Elliott Hughes5f564542014-06-19 13:54:10 -070087#endif /* !defined LOCALE_HOME */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070088
89static const struct lc_time_T C_time_locale = {
90 {
91 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
92 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
93 }, {
94 "January", "February", "March", "April", "May", "June",
95 "July", "August", "September", "October", "November", "December"
96 }, {
Eric Fischera48fa7f2009-05-15 13:33:20 -070097 "January", "February", "March", "April", "May", "June",
98 "July", "August", "September", "October", "November", "December"
99 }, {
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700100 "Sun", "Mon", "Tue", "Wed",
101 "Thu", "Fri", "Sat"
102 }, {
103 "Sunday", "Monday", "Tuesday", "Wednesday",
104 "Thursday", "Friday", "Saturday"
105 },
106
107 /* X_fmt */
108 "%H:%M:%S",
109
110 /*
111 ** x_fmt
112 ** C99 requires this format.
113 ** Using just numbers (as here) makes Quakers happier;
114 ** it's also compatible with SVR4.
115 */
116 "%m/%d/%y",
117
118 /*
119 ** c_fmt
120 ** C99 requires this format.
121 ** Previously this code used "%D %X", but we now conform to C99.
122 ** Note that
123 ** "%a %b %d %H:%M:%S %Y"
124 ** is used by Solaris 2.3.
125 */
126 "%a %b %e %T %Y",
127
128 /* am */
129 "AM",
130
131 /* pm */
132 "PM",
133
134 /* date_fmt */
135 "%a %b %e %H:%M:%S %Z %Y"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800136};
137
Elliott Hughesce4783c2013-07-12 17:31:11 -0700138static char * _add(const char *, char *, const char *, int);
139static char * _conv(int, const char *, char *, const char *);
140static char * _fmt(const char *, const struct tm *, char *, const char *,
Elliott Hughes905e6d52014-07-25 11:55:59 -0700141 int *, const struct lc_time_T*);
Elliott Hughesce4783c2013-07-12 17:31:11 -0700142static char * _yconv(int, int, int, int, char *, const char *, int);
143static char * getformat(int, char *, char *, char *, char *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800144
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700145extern char * tzname[];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800146
147#ifndef YEAR_2000_NAME
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700148#define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800149#endif /* !defined YEAR_2000_NAME */
150
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700151#define IN_NONE 0
152#define IN_SOME 1
153#define IN_THIS 2
154#define IN_ALL 3
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800155
156#define FORCE_LOWER_CASE 0x100
157
158size_t
159strftime(s, maxsize, format, t)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700160char * const s;
161const size_t maxsize;
162const char * const format;
163const struct tm * const t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800164{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700165 return strftime_tz(s, maxsize, format, t, Locale);
166}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800167
Elliott Hughes905e6d52014-07-25 11:55:59 -0700168__LIBC64_HIDDEN__ size_t strftime_tz(s, maxsize, format, t, locale)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700169char * const s;
170const size_t maxsize;
171const char * const format;
172const struct tm * const t;
Elliott Hughes905e6d52014-07-25 11:55:59 -0700173const struct lc_time_T *locale;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700174{
175 char * p;
176 int warn;
177
178 tzset();
179 warn = IN_NONE;
180 p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn, locale);
Elliott Hughes9a5a3e82014-05-05 20:28:28 -0700181#ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700182 if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {
183 (void) fprintf(stderr, "\n");
184 if (format == NULL)
185 (void) fprintf(stderr, "NULL strftime format ");
186 else (void) fprintf(stderr, "strftime format \"%s\" ",
187 format);
188 (void) fprintf(stderr, "yields only two digits of years in ");
189 if (warn == IN_SOME)
190 (void) fprintf(stderr, "some locales");
191 else if (warn == IN_THIS)
192 (void) fprintf(stderr, "the current locale");
193 else (void) fprintf(stderr, "all locales");
194 (void) fprintf(stderr, "\n");
195 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800196#endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700197 if (p == s + maxsize)
198 return 0;
199 *p = '\0';
200 return p - s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800201}
202
203static char *getformat(int modifier, char *normal, char *underscore,
204 char *dash, char *zero) {
205 switch (modifier) {
206 case '_':
207 return underscore;
208
209 case '-':
210 return dash;
211
212 case '0':
213 return zero;
214 }
215
216 return normal;
217}
218
219static char *
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700220_fmt(format, t, pt, ptlim, warnp, locale)
221const char * format;
222const struct tm * const t;
223char * pt;
224const char * const ptlim;
225int * warnp;
Elliott Hughes905e6d52014-07-25 11:55:59 -0700226const struct lc_time_T* locale;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800227{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700228 for ( ; *format; ++format) {
229 if (*format == '%') {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800230 int modifier = 0;
231label:
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700232 switch (*++format) {
233 case '\0':
234 --format;
235 break;
236 case 'A':
237 pt = _add((t->tm_wday < 0 ||
238 t->tm_wday >= DAYSPERWEEK) ?
239 "?" : locale->weekday[t->tm_wday],
240 pt, ptlim, modifier);
241 continue;
242 case 'a':
243 pt = _add((t->tm_wday < 0 ||
244 t->tm_wday >= DAYSPERWEEK) ?
245 "?" : locale->wday[t->tm_wday],
246 pt, ptlim, modifier);
247 continue;
248 case 'B':
Eric Fischera48fa7f2009-05-15 13:33:20 -0700249 if (modifier == '-') {
250 pt = _add((t->tm_mon < 0 ||
251 t->tm_mon >= MONSPERYEAR) ?
Eric Fischerd5f72af2009-08-03 15:43:18 -0700252 "?" : locale->standalone_month[t->tm_mon],
Eric Fischera48fa7f2009-05-15 13:33:20 -0700253 pt, ptlim, modifier);
254 } else {
255 pt = _add((t->tm_mon < 0 ||
256 t->tm_mon >= MONSPERYEAR) ?
Eric Fischerd5f72af2009-08-03 15:43:18 -0700257 "?" : locale->month[t->tm_mon],
Eric Fischera48fa7f2009-05-15 13:33:20 -0700258 pt, ptlim, modifier);
259 }
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700260 continue;
261 case 'b':
262 case 'h':
263 pt = _add((t->tm_mon < 0 ||
264 t->tm_mon >= MONSPERYEAR) ?
265 "?" : locale->mon[t->tm_mon],
266 pt, ptlim, modifier);
267 continue;
268 case 'C':
269 /*
270 ** %C used to do a...
271 ** _fmt("%a %b %e %X %Y", t);
272 ** ...whereas now POSIX 1003.2 calls for
273 ** something completely different.
274 ** (ado, 1993-05-24)
275 */
276 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0,
277 pt, ptlim, modifier);
278 continue;
279 case 'c':
280 {
281 int warn2 = IN_SOME;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800282
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700283 pt = _fmt(locale->c_fmt, t, pt, ptlim, warnp, locale);
284 if (warn2 == IN_ALL)
285 warn2 = IN_THIS;
286 if (warn2 > *warnp)
287 *warnp = warn2;
288 }
289 continue;
290 case 'D':
291 pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp, locale);
292 continue;
293 case 'd':
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800294 pt = _conv(t->tm_mday,
295 getformat(modifier, "%02d",
296 "%2d", "%d", "%02d"),
297 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700298 continue;
299 case 'E':
300 case 'O':
301 /*
302 ** C99 locale modifiers.
303 ** The sequences
304 ** %Ec %EC %Ex %EX %Ey %EY
305 ** %Od %oe %OH %OI %Om %OM
306 ** %OS %Ou %OU %OV %Ow %OW %Oy
307 ** are supposed to provide alternate
308 ** representations.
309 */
310 goto label;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800311 case '_':
312 case '-':
313 case '0':
314 case '^':
315 case '#':
316 modifier = *format;
317 goto label;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700318 case 'e':
319 pt = _conv(t->tm_mday,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800320 getformat(modifier, "%2d",
321 "%2d", "%d", "%02d"),
322 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700323 continue;
324 case 'F':
325 pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp, locale);
326 continue;
327 case 'H':
328 pt = _conv(t->tm_hour,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800329 getformat(modifier, "%02d",
330 "%2d", "%d", "%02d"),
331 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700332 continue;
333 case 'I':
334 pt = _conv((t->tm_hour % 12) ?
335 (t->tm_hour % 12) : 12,
336 getformat(modifier, "%02d",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800337 "%2d", "%d", "%02d"),
338 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700339 continue;
340 case 'j':
341 pt = _conv(t->tm_yday + 1,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800342 getformat(modifier, "%03d", "%3d", "%d", "%03d"),
343 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700344 continue;
345 case 'k':
346 /*
347 ** This used to be...
348 ** _conv(t->tm_hour % 12 ?
349 ** t->tm_hour % 12 : 12, 2, ' ');
350 ** ...and has been changed to the below to
351 ** match SunOS 4.1.1 and Arnold Robbins'
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,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800357 getformat(modifier, "%2d",
358 "%2d", "%d", "%02d"),
359 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700360 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800361#ifdef KITCHEN_SINK
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700362 case 'K':
363 /*
364 ** After all this time, still unclaimed!
365 */
366 pt = _add("kitchen sink", pt, ptlim, modifier);
367 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800368#endif /* defined KITCHEN_SINK */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700369 case 'l':
370 /*
371 ** This used to be...
372 ** _conv(t->tm_hour, 2, ' ');
373 ** ...and has been changed to the below to
374 ** match SunOS 4.1.1 and Arnold Robbin's
375 ** strftime version 3.0. That is, "%k" and
376 ** "%l" have been swapped.
377 ** (ado, 1993-05-24)
378 */
379 pt = _conv((t->tm_hour % 12) ?
380 (t->tm_hour % 12) : 12,
381 getformat(modifier, "%2d",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800382 "%2d", "%d", "%02d"),
383 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700384 continue;
385 case 'M':
386 pt = _conv(t->tm_min,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800387 getformat(modifier, "%02d",
388 "%2d", "%d", "%02d"),
389 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700390 continue;
391 case 'm':
392 pt = _conv(t->tm_mon + 1,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800393 getformat(modifier, "%02d",
394 "%2d", "%d", "%02d"),
395 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700396 continue;
397 case 'n':
398 pt = _add("\n", pt, ptlim, modifier);
399 continue;
400 case 'p':
401 pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
402 locale->pm :
403 locale->am,
404 pt, ptlim, modifier);
405 continue;
406 case 'P':
407 pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
408 locale->pm :
409 locale->am,
410 pt, ptlim, FORCE_LOWER_CASE);
411 continue;
412 case 'R':
413 pt = _fmt("%H:%M", t, pt, ptlim, warnp, locale);
414 continue;
415 case 'r':
416 pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp, locale);
417 continue;
418 case 'S':
419 pt = _conv(t->tm_sec,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800420 getformat(modifier, "%02d",
421 "%2d", "%d", "%02d"),
422 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700423 continue;
424 case 's':
425 {
426 struct tm tm;
427 char buf[INT_STRLEN_MAXIMUM(
428 time64_t) + 1];
429 time64_t mkt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800430
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700431 tm = *t;
432 mkt = mktime64(&tm);
433 if (TYPE_SIGNED(time64_t))
Jim Huange6cff932011-06-16 22:35:16 +0800434 (void) snprintf(buf, sizeof(buf), "%lld",
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700435 (long long) mkt);
Jim Huange6cff932011-06-16 22:35:16 +0800436 else (void) snprintf(buf, sizeof(buf), "%llu",
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700437 (unsigned long long) mkt);
438 pt = _add(buf, pt, ptlim, modifier);
439 }
440 continue;
441 case 'T':
442 pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp, locale);
443 continue;
444 case 't':
445 pt = _add("\t", pt, ptlim, modifier);
446 continue;
447 case 'U':
448 pt = _conv((t->tm_yday + DAYSPERWEEK -
449 t->tm_wday) / DAYSPERWEEK,
450 getformat(modifier, "%02d",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800451 "%2d", "%d", "%02d"),
452 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700453 continue;
454 case 'u':
455 /*
456 ** From Arnold Robbins' strftime version 3.0:
457 ** "ISO 8601: Weekday as a decimal number
458 ** [1 (Monday) - 7]"
459 ** (ado, 1993-05-24)
460 */
461 pt = _conv((t->tm_wday == 0) ?
462 DAYSPERWEEK : t->tm_wday, "%d", pt, ptlim);
463 continue;
464 case 'V': /* ISO 8601 week number */
465 case 'G': /* ISO 8601 year (four digits) */
466 case 'g': /* ISO 8601 year (two digits) */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800467/*
468** From Arnold Robbins' strftime version 3.0: "the week number of the
469** year (the first Monday as the first day of week 1) as a decimal number
470** (01-53)."
471** (ado, 1993-05-24)
472**
473** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
474** "Week 01 of a year is per definition the first week which has the
475** Thursday in this year, which is equivalent to the week which contains
476** the fourth day of January. In other words, the first week of a new year
477** is the week which has the majority of its days in the new year. Week 01
478** might also contain days from the previous year and the week before week
479** 01 of a year is the last week (52 or 53) of the previous year even if
480** it contains days from the new year. A week starts with Monday (day 1)
481** and ends with Sunday (day 7). For example, the first week of the year
482** 1997 lasts from 1996-12-30 to 1997-01-05..."
483** (ado, 1996-01-02)
484*/
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700485 {
486 int year;
487 int base;
488 int yday;
489 int wday;
490 int w;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800491
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700492 year = t->tm_year;
493 base = TM_YEAR_BASE;
494 yday = t->tm_yday;
495 wday = t->tm_wday;
496 for ( ; ; ) {
497 int len;
498 int bot;
499 int top;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800500
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700501 len = isleap_sum(year, base) ?
502 DAYSPERLYEAR :
503 DAYSPERNYEAR;
504 /*
505 ** What yday (-3 ... 3) does
506 ** the ISO year begin on?
507 */
508 bot = ((yday + 11 - wday) %
509 DAYSPERWEEK) - 3;
510 /*
511 ** What yday does the NEXT
512 ** ISO year begin on?
513 */
514 top = bot -
515 (len % DAYSPERWEEK);
516 if (top < -3)
517 top += DAYSPERWEEK;
518 top += len;
519 if (yday >= top) {
520 ++base;
521 w = 1;
522 break;
523 }
524 if (yday >= bot) {
525 w = 1 + ((yday - bot) /
526 DAYSPERWEEK);
527 break;
528 }
529 --base;
530 yday += isleap_sum(year, base) ?
531 DAYSPERLYEAR :
532 DAYSPERNYEAR;
533 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800534#ifdef XPG4_1994_04_09
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700535 if ((w == 52 &&
536 t->tm_mon == TM_JANUARY) ||
537 (w == 1 &&
538 t->tm_mon == TM_DECEMBER))
539 w = 53;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800540#endif /* defined XPG4_1994_04_09 */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700541 if (*format == 'V')
542 pt = _conv(w,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800543 getformat(modifier,
544 "%02d",
545 "%2d",
546 "%d",
547 "%02d"),
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700548 pt, ptlim);
549 else if (*format == 'g') {
550 *warnp = IN_ALL;
551 pt = _yconv(year, base, 0, 1,
552 pt, ptlim, modifier);
553 } else pt = _yconv(year, base, 1, 1,
554 pt, ptlim, modifier);
555 }
556 continue;
557 case 'v':
558 /*
559 ** From Arnold Robbins' strftime version 3.0:
560 ** "date as dd-bbb-YYYY"
561 ** (ado, 1993-05-24)
562 */
563 pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp, locale);
564 continue;
565 case 'W':
566 pt = _conv((t->tm_yday + DAYSPERWEEK -
567 (t->tm_wday ?
568 (t->tm_wday - 1) :
569 (DAYSPERWEEK - 1))) / DAYSPERWEEK,
570 getformat(modifier, "%02d",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800571 "%2d", "%d", "%02d"),
572 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700573 continue;
574 case 'w':
575 pt = _conv(t->tm_wday, "%d", pt, ptlim);
576 continue;
577 case 'X':
578 pt = _fmt(locale->X_fmt, t, pt, ptlim, warnp, locale);
579 continue;
580 case 'x':
581 {
582 int warn2 = IN_SOME;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800583
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700584 pt = _fmt(locale->x_fmt, t, pt, ptlim, &warn2, locale);
585 if (warn2 == IN_ALL)
586 warn2 = IN_THIS;
587 if (warn2 > *warnp)
588 *warnp = warn2;
589 }
590 continue;
591 case 'y':
592 *warnp = IN_ALL;
593 pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1,
594 pt, ptlim, modifier);
595 continue;
596 case 'Y':
597 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1,
598 pt, ptlim, modifier);
599 continue;
600 case 'Z':
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800601#ifdef TM_ZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700602 if (t->TM_ZONE != NULL)
603 pt = _add(t->TM_ZONE, pt, ptlim,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800604 modifier);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700605 else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800606#endif /* defined TM_ZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700607 if (t->tm_isdst >= 0)
608 pt = _add(tzname[t->tm_isdst != 0],
609 pt, ptlim, modifier);
610 /*
611 ** C99 says that %Z must be replaced by the
612 ** empty string if the time zone is not
613 ** determinable.
614 */
615 continue;
616 case 'z':
617 {
Elliott Hughese0d0b152013-09-27 00:04:30 -0700618 long diff;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700619 char const * sign;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800620
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700621 if (t->tm_isdst < 0)
622 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800623#ifdef TM_GMTOFF
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700624 diff = t->TM_GMTOFF;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800625#else /* !defined TM_GMTOFF */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700626 /*
Elliott Hughese0d0b152013-09-27 00:04:30 -0700627 ** C99 says that the UT offset must
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700628 ** be computed by looking only at
629 ** tm_isdst. This requirement is
630 ** incorrect, since it means the code
631 ** must rely on magic (in this case
632 ** altzone and timezone), and the
633 ** magic might not have the correct
634 ** offset. Doing things correctly is
635 ** tricky and requires disobeying C99;
636 ** see GNU C strftime for details.
637 ** For now, punt and conform to the
638 ** standard, even though it's incorrect.
639 **
640 ** C99 says that %z must be replaced by the
641 ** empty string if the time zone is not
642 ** determinable, so output nothing if the
643 ** appropriate variables are not available.
644 */
645 if (t->tm_isdst == 0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800646#ifdef USG_COMPAT
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700647 diff = -timezone;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800648#else /* !defined USG_COMPAT */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700649 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800650#endif /* !defined USG_COMPAT */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700651 else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800652#ifdef ALTZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700653 diff = -altzone;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800654#else /* !defined ALTZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700655 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800656#endif /* !defined ALTZONE */
657#endif /* !defined TM_GMTOFF */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700658 if (diff < 0) {
659 sign = "-";
660 diff = -diff;
661 } else sign = "+";
662 pt = _add(sign, pt, ptlim, modifier);
663 diff /= SECSPERMIN;
664 diff = (diff / MINSPERHOUR) * 100 +
665 (diff % MINSPERHOUR);
666 pt = _conv(diff,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800667 getformat(modifier, "%04d",
668 "%4d", "%d", "%04d"),
669 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700670 }
671 continue;
672 case '+':
673 pt = _fmt(locale->date_fmt, t, pt, ptlim,
674 warnp, locale);
675 continue;
676 case '%':
677 /*
678 ** X311J/88-090 (4.12.3.5): if conversion char is
679 ** undefined, behavior is undefined. Print out the
680 ** character itself as printf(3) also does.
681 */
682 default:
683 break;
684 }
685 }
686 if (pt == ptlim)
687 break;
688 *pt++ = *format;
689 }
690 return pt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800691}
692
693static char *
694_conv(n, format, pt, ptlim)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700695const int n;
696const char * const format;
697char * const pt;
698const char * const ptlim;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800699{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700700 char buf[INT_STRLEN_MAXIMUM(int) + 1];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800701
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700702 (void) snprintf(buf, sizeof(buf), format, n);
703 return _add(buf, pt, ptlim, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800704}
705
706static char *
707_add(str, pt, ptlim, modifier)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700708const char * str;
709char * pt;
710const char * const ptlim;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800711int modifier;
712{
713 int c;
714
715 switch (modifier) {
716 case FORCE_LOWER_CASE:
717 while (pt < ptlim && (*pt = tolower(*str++)) != '\0') {
718 ++pt;
719 }
720 break;
721
722 case '^':
723 while (pt < ptlim && (*pt = toupper(*str++)) != '\0') {
724 ++pt;
725 }
726 break;
727
728 case '#':
729 while (pt < ptlim && (c = *str++) != '\0') {
730 if (isupper(c)) {
731 c = tolower(c);
732 } else if (islower(c)) {
733 c = toupper(c);
734 }
735 *pt = c;
736 ++pt;
737 }
738
739 break;
740
741 default:
742 while (pt < ptlim && (*pt = *str++) != '\0') {
743 ++pt;
744 }
745 }
746
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700747 return pt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800748}
749
750/*
751** POSIX and the C Standard are unclear or inconsistent about
752** what %C and %y do if the year is negative or exceeds 9999.
753** Use the convention that %C concatenated with %y yields the
754** same output as %Y, and that %Y contains at least 4 bytes,
755** with more only if necessary.
756*/
757
758static char *
759_yconv(a, b, convert_top, convert_yy, pt, ptlim, modifier)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700760const int a;
761const int b;
762const int convert_top;
763const int convert_yy;
764char * pt;
765const char * const ptlim;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800766int modifier;
767{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700768 register int lead;
769 register int trail;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800770
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700771#define DIVISOR 100
772 trail = a % DIVISOR + b % DIVISOR;
773 lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
774 trail %= DIVISOR;
775 if (trail < 0 && lead > 0) {
776 trail += DIVISOR;
777 --lead;
778 } else if (lead < 0 && trail > 0) {
779 trail -= DIVISOR;
780 ++lead;
781 }
782 if (convert_top) {
783 if (lead == 0 && trail < 0)
784 pt = _add("-0", pt, ptlim, modifier);
785 else pt = _conv(lead, getformat(modifier, "%02d",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800786 "%2d", "%d", "%02d"),
787 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700788 }
789 if (convert_yy)
790 pt = _conv(((trail < 0) ? -trail : trail),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800791 getformat(modifier, "%02d", "%2d", "%d", "%02d"),
792 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700793 return pt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800794}