blob: 10dfb4b6c89019fc8bb419868685979ce8900a04 [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
Elliott Hughes905e6d52014-07-25 11:55:59 -070033/* LP32 had a 32-bit time_t, so we need to work around that here. */
Elliott Hughes52defb72014-05-05 17:14:02 -070034#if defined(__LP64__)
35#define time64_t time_t
36#define mktime64 mktime
37#else
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070038#include <time64.h>
Elliott Hughes52defb72014-05-05 17:14:02 -070039#endif
Elliott Hughes905e6d52014-07-25 11:55:59 -070040
Elliott Hughes5f564542014-06-19 13:54:10 -070041#include <ctype.h>
Elliott Hughes905e6d52014-07-25 11:55:59 -070042
Elliott Hughes39d903a2014-07-25 15:50:31 -070043#endif
Elliott Hughes905e6d52014-07-25 11:55:59 -070044
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080045struct lc_time_T {
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070046 const char * mon[MONSPERYEAR];
47 const char * month[MONSPERYEAR];
48 const char * wday[DAYSPERWEEK];
49 const char * weekday[DAYSPERWEEK];
50 const char * X_fmt;
51 const char * x_fmt;
52 const char * c_fmt;
53 const char * am;
54 const char * pm;
55 const char * date_fmt;
56};
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070057
58#define Locale (&C_time_locale)
59
60static const struct lc_time_T C_time_locale = {
61 {
62 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
63 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
64 }, {
65 "January", "February", "March", "April", "May", "June",
66 "July", "August", "September", "October", "November", "December"
67 }, {
68 "Sun", "Mon", "Tue", "Wed",
69 "Thu", "Fri", "Sat"
70 }, {
71 "Sunday", "Monday", "Tuesday", "Wednesday",
72 "Thursday", "Friday", "Saturday"
73 },
74
75 /* X_fmt */
76 "%H:%M:%S",
77
78 /*
79 ** x_fmt
80 ** C99 requires this format.
81 ** Using just numbers (as here) makes Quakers happier;
82 ** it's also compatible with SVR4.
83 */
84 "%m/%d/%y",
85
86 /*
87 ** c_fmt
88 ** C99 requires this format.
89 ** Previously this code used "%D %X", but we now conform to C99.
90 ** Note that
91 ** "%a %b %d %H:%M:%S %Y"
92 ** is used by Solaris 2.3.
93 */
94 "%a %b %e %T %Y",
95
96 /* am */
97 "AM",
98
99 /* pm */
100 "PM",
101
102 /* date_fmt */
103 "%a %b %e %H:%M:%S %Z %Y"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800104};
105
Elliott Hughesce4783c2013-07-12 17:31:11 -0700106static char * _add(const char *, char *, const char *, int);
107static char * _conv(int, const char *, char *, const char *);
108static char * _fmt(const char *, const struct tm *, char *, const char *,
Elliott Hughes39d903a2014-07-25 15:50:31 -0700109 int *);
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700110static char * _yconv(int, int, bool, bool, char *, const char *, int);
Elliott Hughesce4783c2013-07-12 17:31:11 -0700111static char * getformat(int, char *, char *, char *, char *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800112
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700113extern char * tzname[];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800114
115#ifndef YEAR_2000_NAME
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700116#define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800117#endif /* !defined YEAR_2000_NAME */
118
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700119#define IN_NONE 0
120#define IN_SOME 1
121#define IN_THIS 2
122#define IN_ALL 3
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800123
124#define FORCE_LOWER_CASE 0x100
125
126size_t
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700127strftime(char *s, size_t maxsize, const char *format, const struct tm *t)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700128{
129 char * p;
130 int warn;
131
132 tzset();
133 warn = IN_NONE;
Elliott Hughes39d903a2014-07-25 15:50:31 -0700134 p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn);
Elliott Hughes9a5a3e82014-05-05 20:28:28 -0700135#ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700136 if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700137 fprintf(stderr, "\n");
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700138 if (format == NULL)
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700139 fprintf(stderr, "NULL strftime format ");
140 else fprintf(stderr, "strftime format \"%s\" ",
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700141 format);
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700142 fprintf(stderr, "yields only two digits of years in ");
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700143 if (warn == IN_SOME)
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700144 fprintf(stderr, "some locales");
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700145 else if (warn == IN_THIS)
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700146 fprintf(stderr, "the current locale");
147 else fprintf(stderr, "all locales");
148 fprintf(stderr, "\n");
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700149 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800150#endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700151 if (p == s + maxsize)
152 return 0;
153 *p = '\0';
154 return p - s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800155}
156
157static char *getformat(int modifier, char *normal, char *underscore,
158 char *dash, char *zero) {
159 switch (modifier) {
160 case '_':
161 return underscore;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800162 case '-':
163 return dash;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800164 case '0':
165 return zero;
166 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800167 return normal;
168}
169
170static char *
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700171_fmt(const char *format, const struct tm *t, char *pt,
172 const char *ptlim, int *warnp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800173{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700174 for ( ; *format; ++format) {
175 if (*format == '%') {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800176 int modifier = 0;
177label:
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700178 switch (*++format) {
179 case '\0':
180 --format;
181 break;
182 case 'A':
183 pt = _add((t->tm_wday < 0 ||
184 t->tm_wday >= DAYSPERWEEK) ?
Elliott Hughes39d903a2014-07-25 15:50:31 -0700185 "?" : Locale->weekday[t->tm_wday],
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700186 pt, ptlim, modifier);
187 continue;
188 case 'a':
189 pt = _add((t->tm_wday < 0 ||
190 t->tm_wday >= DAYSPERWEEK) ?
Elliott Hughes39d903a2014-07-25 15:50:31 -0700191 "?" : Locale->wday[t->tm_wday],
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700192 pt, ptlim, modifier);
193 continue;
194 case 'B':
Elliott Hughes39d903a2014-07-25 15:50:31 -0700195 pt = _add((t->tm_mon < 0 ||
Eric Fischera48fa7f2009-05-15 13:33:20 -0700196 t->tm_mon >= MONSPERYEAR) ?
Elliott Hughes39d903a2014-07-25 15:50:31 -0700197 "?" : Locale->month[t->tm_mon],
Eric Fischera48fa7f2009-05-15 13:33:20 -0700198 pt, ptlim, modifier);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700199 continue;
200 case 'b':
201 case 'h':
202 pt = _add((t->tm_mon < 0 ||
203 t->tm_mon >= MONSPERYEAR) ?
Elliott Hughes39d903a2014-07-25 15:50:31 -0700204 "?" : Locale->mon[t->tm_mon],
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700205 pt, ptlim, modifier);
206 continue;
207 case 'C':
208 /*
209 ** %C used to do a...
210 ** _fmt("%a %b %e %X %Y", t);
211 ** ...whereas now POSIX 1003.2 calls for
212 ** something completely different.
213 ** (ado, 1993-05-24)
214 */
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700215 pt = _yconv(t->tm_year, TM_YEAR_BASE,
216 true, false, pt, ptlim, modifier);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700217 continue;
218 case 'c':
219 {
220 int warn2 = IN_SOME;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800221
Elliott Hughes39d903a2014-07-25 15:50:31 -0700222 pt = _fmt(Locale->c_fmt, t, pt, ptlim, &warn2);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700223 if (warn2 == IN_ALL)
224 warn2 = IN_THIS;
225 if (warn2 > *warnp)
226 *warnp = warn2;
227 }
228 continue;
229 case 'D':
Elliott Hughes39d903a2014-07-25 15:50:31 -0700230 pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700231 continue;
232 case 'd':
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700233 pt = _conv(t->tm_mday, getformat(modifier, "%02d", "%2d", "%d", "%02d"), pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700234 continue;
235 case 'E':
236 case 'O':
237 /*
238 ** C99 locale modifiers.
239 ** The sequences
240 ** %Ec %EC %Ex %EX %Ey %EY
241 ** %Od %oe %OH %OI %Om %OM
242 ** %OS %Ou %OU %OV %Ow %OW %Oy
243 ** are supposed to provide alternate
244 ** representations.
245 */
246 goto label;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800247 case '_':
248 case '-':
249 case '0':
250 case '^':
251 case '#':
252 modifier = *format;
253 goto label;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700254 case 'e':
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700255 pt = _conv(t->tm_mday, getformat(modifier, "%2d", "%2d", "%d", "%02d"), pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700256 continue;
257 case 'F':
Elliott Hughes39d903a2014-07-25 15:50:31 -0700258 pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700259 continue;
260 case 'H':
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700261 pt = _conv(t->tm_hour, getformat(modifier, "%02d", "%2d", "%d", "%02d"), pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700262 continue;
263 case 'I':
264 pt = _conv((t->tm_hour % 12) ?
265 (t->tm_hour % 12) : 12,
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700266 getformat(modifier, "%02d", "%2d", "%d", "%02d"), pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700267 continue;
268 case 'j':
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700269 pt = _conv(t->tm_yday + 1, getformat(modifier, "%03d", "%3d", "%d", "%03d"), pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700270 continue;
271 case 'k':
272 /*
273 ** This used to be...
274 ** _conv(t->tm_hour % 12 ?
275 ** t->tm_hour % 12 : 12, 2, ' ');
276 ** ...and has been changed to the below to
277 ** match SunOS 4.1.1 and Arnold Robbins'
278 ** strftime version 3.0. That is, "%k" and
279 ** "%l" have been swapped.
280 ** (ado, 1993-05-24)
281 */
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700282 pt = _conv(t->tm_hour, getformat(modifier, "%2d", "%2d", "%d", "%02d"), pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700283 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800284#ifdef KITCHEN_SINK
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700285 case 'K':
286 /*
287 ** After all this time, still unclaimed!
288 */
Elliott Hughes39d903a2014-07-25 15:50:31 -0700289 pt = _add("kitchen sink", pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700290 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800291#endif /* defined KITCHEN_SINK */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700292 case 'l':
293 /*
294 ** This used to be...
295 ** _conv(t->tm_hour, 2, ' ');
296 ** ...and has been changed to the below to
297 ** match SunOS 4.1.1 and Arnold Robbin's
298 ** strftime version 3.0. That is, "%k" and
299 ** "%l" have been swapped.
300 ** (ado, 1993-05-24)
301 */
302 pt = _conv((t->tm_hour % 12) ?
303 (t->tm_hour % 12) : 12,
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700304 getformat(modifier, "%2d", "%2d", "%d", "%02d"), pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700305 continue;
306 case 'M':
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700307 pt = _conv(t->tm_min, getformat(modifier, "%02d", "%2d", "%d", "%02d"), pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700308 continue;
309 case 'm':
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700310 pt = _conv(t->tm_mon + 1, getformat(modifier, "%02d", "%2d", "%d", "%02d"), pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700311 continue;
312 case 'n':
313 pt = _add("\n", pt, ptlim, modifier);
314 continue;
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700315 case 'P':
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700316 case 'p':
317 pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
Elliott Hughes39d903a2014-07-25 15:50:31 -0700318 Locale->pm :
319 Locale->am,
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700320 pt, ptlim, (*format == 'P') ? FORCE_LOWER_CASE : modifier);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700321 continue;
322 case 'R':
Elliott Hughes39d903a2014-07-25 15:50:31 -0700323 pt = _fmt("%H:%M", t, pt, ptlim, warnp);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700324 continue;
325 case 'r':
Elliott Hughes39d903a2014-07-25 15:50:31 -0700326 pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700327 continue;
328 case 'S':
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700329 pt = _conv(t->tm_sec, getformat(modifier, "%02d", "%2d", "%d", "%02d"), pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700330 continue;
331 case 's':
332 {
333 struct tm tm;
334 char buf[INT_STRLEN_MAXIMUM(
335 time64_t) + 1];
336 time64_t mkt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800337
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700338 tm = *t;
339 mkt = mktime64(&tm);
340 if (TYPE_SIGNED(time64_t))
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700341 snprintf(buf, sizeof(buf), "%"PRIdMAX,
342 (intmax_t) mkt);
343 else snprintf(buf, sizeof(buf), "%"PRIuMAX,
344 (uintmax_t) mkt);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700345 pt = _add(buf, pt, ptlim, modifier);
346 }
347 continue;
348 case 'T':
Elliott Hughes39d903a2014-07-25 15:50:31 -0700349 pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700350 continue;
351 case 't':
352 pt = _add("\t", pt, ptlim, modifier);
353 continue;
354 case 'U':
355 pt = _conv((t->tm_yday + DAYSPERWEEK -
356 t->tm_wday) / DAYSPERWEEK,
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700357 getformat(modifier, "%02d", "%2d", "%d", "%02d"), pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700358 continue;
359 case 'u':
360 /*
361 ** From Arnold Robbins' strftime version 3.0:
362 ** "ISO 8601: Weekday as a decimal number
363 ** [1 (Monday) - 7]"
364 ** (ado, 1993-05-24)
365 */
366 pt = _conv((t->tm_wday == 0) ?
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700367 DAYSPERWEEK : t->tm_wday,
368 "%d", pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700369 continue;
370 case 'V': /* ISO 8601 week number */
371 case 'G': /* ISO 8601 year (four digits) */
372 case 'g': /* ISO 8601 year (two digits) */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800373/*
374** From Arnold Robbins' strftime version 3.0: "the week number of the
375** year (the first Monday as the first day of week 1) as a decimal number
376** (01-53)."
377** (ado, 1993-05-24)
378**
Elliott Hughes39d903a2014-07-25 15:50:31 -0700379** From <http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html> by Markus Kuhn:
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800380** "Week 01 of a year is per definition the first week which has the
381** Thursday in this year, which is equivalent to the week which contains
382** the fourth day of January. In other words, the first week of a new year
383** is the week which has the majority of its days in the new year. Week 01
384** might also contain days from the previous year and the week before week
385** 01 of a year is the last week (52 or 53) of the previous year even if
386** it contains days from the new year. A week starts with Monday (day 1)
387** and ends with Sunday (day 7). For example, the first week of the year
388** 1997 lasts from 1996-12-30 to 1997-01-05..."
389** (ado, 1996-01-02)
390*/
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700391 {
392 int year;
393 int base;
394 int yday;
395 int wday;
396 int w;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800397
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700398 year = t->tm_year;
399 base = TM_YEAR_BASE;
400 yday = t->tm_yday;
401 wday = t->tm_wday;
402 for ( ; ; ) {
403 int len;
404 int bot;
405 int top;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800406
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700407 len = isleap_sum(year, base) ?
408 DAYSPERLYEAR :
409 DAYSPERNYEAR;
410 /*
411 ** What yday (-3 ... 3) does
412 ** the ISO year begin on?
413 */
414 bot = ((yday + 11 - wday) %
415 DAYSPERWEEK) - 3;
416 /*
417 ** What yday does the NEXT
418 ** ISO year begin on?
419 */
420 top = bot -
421 (len % DAYSPERWEEK);
422 if (top < -3)
423 top += DAYSPERWEEK;
424 top += len;
425 if (yday >= top) {
426 ++base;
427 w = 1;
428 break;
429 }
430 if (yday >= bot) {
431 w = 1 + ((yday - bot) /
432 DAYSPERWEEK);
433 break;
434 }
435 --base;
436 yday += isleap_sum(year, base) ?
437 DAYSPERLYEAR :
438 DAYSPERNYEAR;
439 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800440#ifdef XPG4_1994_04_09
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700441 if ((w == 52 &&
442 t->tm_mon == TM_JANUARY) ||
443 (w == 1 &&
444 t->tm_mon == TM_DECEMBER))
445 w = 53;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800446#endif /* defined XPG4_1994_04_09 */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700447 if (*format == 'V')
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700448 pt = _conv(w, getformat(modifier, "%02d", "%2d", "%d", "%02d"),
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700449 pt, ptlim);
450 else if (*format == 'g') {
451 *warnp = IN_ALL;
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700452 pt = _yconv(year, base,
453 false, true,
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700454 pt, ptlim, modifier);
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700455 } else pt = _yconv(year, base,
456 true, true,
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700457 pt, ptlim, modifier);
458 }
459 continue;
460 case 'v':
461 /*
462 ** From Arnold Robbins' strftime version 3.0:
463 ** "date as dd-bbb-YYYY"
464 ** (ado, 1993-05-24)
465 */
Elliott Hughes39d903a2014-07-25 15:50:31 -0700466 pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700467 continue;
468 case 'W':
469 pt = _conv((t->tm_yday + DAYSPERWEEK -
470 (t->tm_wday ?
471 (t->tm_wday - 1) :
472 (DAYSPERWEEK - 1))) / DAYSPERWEEK,
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700473 getformat(modifier, "%02d", "%2d", "%d", "%02d"), pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700474 continue;
475 case 'w':
476 pt = _conv(t->tm_wday, "%d", pt, ptlim);
477 continue;
478 case 'X':
Elliott Hughes39d903a2014-07-25 15:50:31 -0700479 pt = _fmt(Locale->X_fmt, t, pt, ptlim, warnp);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700480 continue;
481 case 'x':
482 {
483 int warn2 = IN_SOME;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800484
Elliott Hughes39d903a2014-07-25 15:50:31 -0700485 pt = _fmt(Locale->x_fmt, t, pt, ptlim, &warn2);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700486 if (warn2 == IN_ALL)
487 warn2 = IN_THIS;
488 if (warn2 > *warnp)
489 *warnp = warn2;
490 }
491 continue;
492 case 'y':
493 *warnp = IN_ALL;
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700494 pt = _yconv(t->tm_year, TM_YEAR_BASE,
495 false, true,
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700496 pt, ptlim, modifier);
497 continue;
498 case 'Y':
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700499 pt = _yconv(t->tm_year, TM_YEAR_BASE,
500 true, true,
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700501 pt, ptlim, modifier);
502 continue;
503 case 'Z':
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800504#ifdef TM_ZONE
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700505 pt = _add(t->TM_ZONE, pt, ptlim, modifier);
506#else
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700507 if (t->tm_isdst >= 0)
508 pt = _add(tzname[t->tm_isdst != 0],
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700509 pt, ptlim);
510#endif
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700511 /*
512 ** C99 says that %Z must be replaced by the
513 ** empty string if the time zone is not
514 ** determinable.
515 */
516 continue;
517 case 'z':
518 {
Elliott Hughese0d0b152013-09-27 00:04:30 -0700519 long diff;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700520 char const * sign;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800521
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700522 if (t->tm_isdst < 0)
523 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800524#ifdef TM_GMTOFF
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700525 diff = t->TM_GMTOFF;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800526#else /* !defined TM_GMTOFF */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700527 /*
Elliott Hughese0d0b152013-09-27 00:04:30 -0700528 ** C99 says that the UT offset must
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700529 ** be computed by looking only at
530 ** tm_isdst. This requirement is
531 ** incorrect, since it means the code
532 ** must rely on magic (in this case
533 ** altzone and timezone), and the
534 ** magic might not have the correct
535 ** offset. Doing things correctly is
536 ** tricky and requires disobeying C99;
537 ** see GNU C strftime for details.
538 ** For now, punt and conform to the
539 ** standard, even though it's incorrect.
540 **
541 ** C99 says that %z must be replaced by the
542 ** empty string if the time zone is not
543 ** determinable, so output nothing if the
544 ** appropriate variables are not available.
545 */
546 if (t->tm_isdst == 0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800547#ifdef USG_COMPAT
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700548 diff = -timezone;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800549#else /* !defined USG_COMPAT */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700550 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800551#endif /* !defined USG_COMPAT */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700552 else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800553#ifdef ALTZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700554 diff = -altzone;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800555#else /* !defined ALTZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700556 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800557#endif /* !defined ALTZONE */
558#endif /* !defined TM_GMTOFF */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700559 if (diff < 0) {
560 sign = "-";
561 diff = -diff;
562 } else sign = "+";
563 pt = _add(sign, pt, ptlim, modifier);
564 diff /= SECSPERMIN;
565 diff = (diff / MINSPERHOUR) * 100 +
566 (diff % MINSPERHOUR);
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700567 pt = _conv(diff, getformat(modifier, "%04d", "%4d", "%d", "%04d"), pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700568 }
569 continue;
570 case '+':
Elliott Hughes39d903a2014-07-25 15:50:31 -0700571 pt = _fmt(Locale->date_fmt, t, pt, ptlim,
572 warnp);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700573 continue;
574 case '%':
575 /*
576 ** X311J/88-090 (4.12.3.5): if conversion char is
577 ** undefined, behavior is undefined. Print out the
578 ** character itself as printf(3) also does.
579 */
580 default:
581 break;
582 }
583 }
584 if (pt == ptlim)
585 break;
586 *pt++ = *format;
587 }
588 return pt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800589}
590
591static char *
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700592_conv(int n, const char *format, char *pt, const char *ptlim)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800593{
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700594 char buf[INT_STRLEN_MAXIMUM(int) + 1];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800595
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700596 snprintf(buf, sizeof(buf), format, n);
597 return _add(buf, pt, ptlim, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800598}
599
600static char *
Elliott Hughes39d903a2014-07-25 15:50:31 -0700601_add(const char *str, char *pt, const char *const ptlim, int modifier)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800602{
603 int c;
604
605 switch (modifier) {
606 case FORCE_LOWER_CASE:
607 while (pt < ptlim && (*pt = tolower(*str++)) != '\0') {
608 ++pt;
609 }
610 break;
611
612 case '^':
613 while (pt < ptlim && (*pt = toupper(*str++)) != '\0') {
614 ++pt;
615 }
616 break;
617
618 case '#':
619 while (pt < ptlim && (c = *str++) != '\0') {
620 if (isupper(c)) {
621 c = tolower(c);
622 } else if (islower(c)) {
623 c = toupper(c);
624 }
625 *pt = c;
626 ++pt;
627 }
628
629 break;
630
631 default:
632 while (pt < ptlim && (*pt = *str++) != '\0') {
633 ++pt;
634 }
635 }
636
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700637 return pt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800638}
639
640/*
641** POSIX and the C Standard are unclear or inconsistent about
642** what %C and %y do if the year is negative or exceeds 9999.
643** Use the convention that %C concatenated with %y yields the
644** same output as %Y, and that %Y contains at least 4 bytes,
645** with more only if necessary.
646*/
647
648static char *
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700649_yconv(int a, int b, bool convert_top, bool convert_yy,
650 char *pt, const char *ptlim, int modifier)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800651{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700652 register int lead;
653 register int trail;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800654
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700655#define DIVISOR 100
656 trail = a % DIVISOR + b % DIVISOR;
657 lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
658 trail %= DIVISOR;
659 if (trail < 0 && lead > 0) {
660 trail += DIVISOR;
661 --lead;
662 } else if (lead < 0 && trail > 0) {
663 trail -= DIVISOR;
664 ++lead;
665 }
666 if (convert_top) {
667 if (lead == 0 && trail < 0)
668 pt = _add("-0", pt, ptlim, modifier);
Elliott Hughes39d903a2014-07-25 15:50:31 -0700669 else pt = _conv(lead, getformat(modifier, "%02d", "%2d", "%d", "%02d"), pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700670 }
671 if (convert_yy)
Elliott Hughes39d903a2014-07-25 15:50:31 -0700672 pt = _conv(((trail < 0) ? -trail : trail), getformat(modifier, "%02d", "%2d", "%d", "%02d"), pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700673 return pt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800674}