blob: 4328b4c53928a94a7ed5bd579847d1a92d91f502 [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
Elliott Hughes39d903a2014-07-25 15:50:31 -070058#ifdef LOCALE_HOME
Elliott Hughes5f564542014-06-19 13:54:10 -070059#include "sys/stat.h"
60static struct lc_time_T localebuf;
61static struct lc_time_T * _loc(void);
62#define Locale _loc()
63#endif /* defined LOCALE_HOME */
64#ifndef LOCALE_HOME
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070065#define Locale (&C_time_locale)
Elliott Hughes5f564542014-06-19 13:54:10 -070066#endif /* !defined LOCALE_HOME */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -070067
68static const struct lc_time_T C_time_locale = {
69 {
70 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
71 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
72 }, {
73 "January", "February", "March", "April", "May", "June",
74 "July", "August", "September", "October", "November", "December"
75 }, {
76 "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 *,
Elliott Hughes39d903a2014-07-25 15:50:31 -0700117 int *);
Elliott Hughesce4783c2013-07-12 17:31:11 -0700118static 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
Elliott Hughes39d903a2014-07-25 15:50:31 -0700135strftime(char * const s, const size_t maxsize, const char *const format,
136 const struct tm *const t)
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700137{
138 char * p;
139 int warn;
140
141 tzset();
Elliott Hughes39d903a2014-07-25 15:50:31 -0700142#ifdef LOCALE_HOME
143 localebuf.mon[0] = 0;
144#endif /* defined LOCALE_HOME */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700145 warn = IN_NONE;
Elliott Hughes39d903a2014-07-25 15:50:31 -0700146 p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn);
Elliott Hughes9a5a3e82014-05-05 20:28:28 -0700147#ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700148 if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {
149 (void) fprintf(stderr, "\n");
150 if (format == NULL)
151 (void) fprintf(stderr, "NULL strftime format ");
152 else (void) fprintf(stderr, "strftime format \"%s\" ",
153 format);
154 (void) fprintf(stderr, "yields only two digits of years in ");
155 if (warn == IN_SOME)
156 (void) fprintf(stderr, "some locales");
157 else if (warn == IN_THIS)
158 (void) fprintf(stderr, "the current locale");
159 else (void) fprintf(stderr, "all locales");
160 (void) fprintf(stderr, "\n");
161 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800162#endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700163 if (p == s + maxsize)
164 return 0;
165 *p = '\0';
166 return p - s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800167}
168
169static char *getformat(int modifier, char *normal, char *underscore,
170 char *dash, char *zero) {
171 switch (modifier) {
172 case '_':
173 return underscore;
174
175 case '-':
176 return dash;
177
178 case '0':
179 return zero;
180 }
181
182 return normal;
183}
184
185static char *
Elliott Hughes39d903a2014-07-25 15:50:31 -0700186_fmt(const char *format, const struct tm *const t, char * pt,
187 const char *const ptlim, int *warnp)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800188{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700189 for ( ; *format; ++format) {
190 if (*format == '%') {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800191 int modifier = 0;
192label:
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700193 switch (*++format) {
194 case '\0':
195 --format;
196 break;
197 case 'A':
198 pt = _add((t->tm_wday < 0 ||
199 t->tm_wday >= DAYSPERWEEK) ?
Elliott Hughes39d903a2014-07-25 15:50:31 -0700200 "?" : Locale->weekday[t->tm_wday],
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700201 pt, ptlim, modifier);
202 continue;
203 case 'a':
204 pt = _add((t->tm_wday < 0 ||
205 t->tm_wday >= DAYSPERWEEK) ?
Elliott Hughes39d903a2014-07-25 15:50:31 -0700206 "?" : Locale->wday[t->tm_wday],
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700207 pt, ptlim, modifier);
208 continue;
209 case 'B':
Elliott Hughes39d903a2014-07-25 15:50:31 -0700210 pt = _add((t->tm_mon < 0 ||
Eric Fischera48fa7f2009-05-15 13:33:20 -0700211 t->tm_mon >= MONSPERYEAR) ?
Elliott Hughes39d903a2014-07-25 15:50:31 -0700212 "?" : Locale->month[t->tm_mon],
Eric Fischera48fa7f2009-05-15 13:33:20 -0700213 pt, ptlim, modifier);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700214 continue;
215 case 'b':
216 case 'h':
217 pt = _add((t->tm_mon < 0 ||
218 t->tm_mon >= MONSPERYEAR) ?
Elliott Hughes39d903a2014-07-25 15:50:31 -0700219 "?" : Locale->mon[t->tm_mon],
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700220 pt, ptlim, modifier);
221 continue;
222 case 'C':
223 /*
224 ** %C used to do a...
225 ** _fmt("%a %b %e %X %Y", t);
226 ** ...whereas now POSIX 1003.2 calls for
227 ** something completely different.
228 ** (ado, 1993-05-24)
229 */
230 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0,
231 pt, ptlim, modifier);
232 continue;
233 case 'c':
234 {
235 int warn2 = IN_SOME;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800236
Elliott Hughes39d903a2014-07-25 15:50:31 -0700237 pt = _fmt(Locale->c_fmt, t, pt, ptlim, &warn2);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700238 if (warn2 == IN_ALL)
239 warn2 = IN_THIS;
240 if (warn2 > *warnp)
241 *warnp = warn2;
242 }
243 continue;
244 case 'D':
Elliott Hughes39d903a2014-07-25 15:50:31 -0700245 pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700246 continue;
247 case 'd':
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800248 pt = _conv(t->tm_mday,
249 getformat(modifier, "%02d",
250 "%2d", "%d", "%02d"),
251 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700252 continue;
253 case 'E':
254 case 'O':
255 /*
256 ** C99 locale modifiers.
257 ** The sequences
258 ** %Ec %EC %Ex %EX %Ey %EY
259 ** %Od %oe %OH %OI %Om %OM
260 ** %OS %Ou %OU %OV %Ow %OW %Oy
261 ** are supposed to provide alternate
262 ** representations.
263 */
264 goto label;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800265 case '_':
266 case '-':
267 case '0':
268 case '^':
269 case '#':
270 modifier = *format;
271 goto label;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700272 case 'e':
273 pt = _conv(t->tm_mday,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800274 getformat(modifier, "%2d",
275 "%2d", "%d", "%02d"),
276 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700277 continue;
278 case 'F':
Elliott Hughes39d903a2014-07-25 15:50:31 -0700279 pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700280 continue;
281 case 'H':
282 pt = _conv(t->tm_hour,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800283 getformat(modifier, "%02d",
284 "%2d", "%d", "%02d"),
285 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700286 continue;
287 case 'I':
288 pt = _conv((t->tm_hour % 12) ?
289 (t->tm_hour % 12) : 12,
290 getformat(modifier, "%02d",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800291 "%2d", "%d", "%02d"),
292 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700293 continue;
294 case 'j':
295 pt = _conv(t->tm_yday + 1,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800296 getformat(modifier, "%03d", "%3d", "%d", "%03d"),
297 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700298 continue;
299 case 'k':
300 /*
301 ** This used to be...
302 ** _conv(t->tm_hour % 12 ?
303 ** t->tm_hour % 12 : 12, 2, ' ');
304 ** ...and has been changed to the below to
305 ** match SunOS 4.1.1 and Arnold Robbins'
306 ** strftime version 3.0. That is, "%k" and
307 ** "%l" have been swapped.
308 ** (ado, 1993-05-24)
309 */
310 pt = _conv(t->tm_hour,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800311 getformat(modifier, "%2d",
312 "%2d", "%d", "%02d"),
313 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700314 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800315#ifdef KITCHEN_SINK
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700316 case 'K':
317 /*
318 ** After all this time, still unclaimed!
319 */
Elliott Hughes39d903a2014-07-25 15:50:31 -0700320 pt = _add("kitchen sink", pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700321 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800322#endif /* defined KITCHEN_SINK */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700323 case 'l':
324 /*
325 ** This used to be...
326 ** _conv(t->tm_hour, 2, ' ');
327 ** ...and has been changed to the below to
328 ** match SunOS 4.1.1 and Arnold Robbin's
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 % 12) ?
334 (t->tm_hour % 12) : 12,
335 getformat(modifier, "%2d",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800336 "%2d", "%d", "%02d"),
337 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700338 continue;
339 case 'M':
340 pt = _conv(t->tm_min,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800341 getformat(modifier, "%02d",
342 "%2d", "%d", "%02d"),
343 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700344 continue;
345 case 'm':
346 pt = _conv(t->tm_mon + 1,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800347 getformat(modifier, "%02d",
348 "%2d", "%d", "%02d"),
349 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700350 continue;
351 case 'n':
352 pt = _add("\n", pt, ptlim, modifier);
353 continue;
354 case 'p':
355 pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
Elliott Hughes39d903a2014-07-25 15:50:31 -0700356 Locale->pm :
357 Locale->am,
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700358 pt, ptlim, modifier);
359 continue;
360 case 'P':
361 pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
Elliott Hughes39d903a2014-07-25 15:50:31 -0700362 Locale->pm :
363 Locale->am,
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700364 pt, ptlim, FORCE_LOWER_CASE);
365 continue;
366 case 'R':
Elliott Hughes39d903a2014-07-25 15:50:31 -0700367 pt = _fmt("%H:%M", t, pt, ptlim, warnp);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700368 continue;
369 case 'r':
Elliott Hughes39d903a2014-07-25 15:50:31 -0700370 pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700371 continue;
372 case 'S':
373 pt = _conv(t->tm_sec,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800374 getformat(modifier, "%02d",
375 "%2d", "%d", "%02d"),
376 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700377 continue;
378 case 's':
379 {
380 struct tm tm;
381 char buf[INT_STRLEN_MAXIMUM(
382 time64_t) + 1];
383 time64_t mkt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800384
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700385 tm = *t;
386 mkt = mktime64(&tm);
387 if (TYPE_SIGNED(time64_t))
Jim Huange6cff932011-06-16 22:35:16 +0800388 (void) snprintf(buf, sizeof(buf), "%lld",
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700389 (long long) mkt);
Jim Huange6cff932011-06-16 22:35:16 +0800390 else (void) snprintf(buf, sizeof(buf), "%llu",
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700391 (unsigned long long) mkt);
392 pt = _add(buf, pt, ptlim, modifier);
393 }
394 continue;
395 case 'T':
Elliott Hughes39d903a2014-07-25 15:50:31 -0700396 pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700397 continue;
398 case 't':
399 pt = _add("\t", pt, ptlim, modifier);
400 continue;
401 case 'U':
402 pt = _conv((t->tm_yday + DAYSPERWEEK -
403 t->tm_wday) / DAYSPERWEEK,
404 getformat(modifier, "%02d",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800405 "%2d", "%d", "%02d"),
406 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700407 continue;
408 case 'u':
409 /*
410 ** From Arnold Robbins' strftime version 3.0:
411 ** "ISO 8601: Weekday as a decimal number
412 ** [1 (Monday) - 7]"
413 ** (ado, 1993-05-24)
414 */
415 pt = _conv((t->tm_wday == 0) ?
416 DAYSPERWEEK : t->tm_wday, "%d", pt, ptlim);
417 continue;
418 case 'V': /* ISO 8601 week number */
419 case 'G': /* ISO 8601 year (four digits) */
420 case 'g': /* ISO 8601 year (two digits) */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800421/*
422** From Arnold Robbins' strftime version 3.0: "the week number of the
423** year (the first Monday as the first day of week 1) as a decimal number
424** (01-53)."
425** (ado, 1993-05-24)
426**
Elliott Hughes39d903a2014-07-25 15:50:31 -0700427** From <http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html> by Markus Kuhn:
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800428** "Week 01 of a year is per definition the first week which has the
429** Thursday in this year, which is equivalent to the week which contains
430** the fourth day of January. In other words, the first week of a new year
431** is the week which has the majority of its days in the new year. Week 01
432** might also contain days from the previous year and the week before week
433** 01 of a year is the last week (52 or 53) of the previous year even if
434** it contains days from the new year. A week starts with Monday (day 1)
435** and ends with Sunday (day 7). For example, the first week of the year
436** 1997 lasts from 1996-12-30 to 1997-01-05..."
437** (ado, 1996-01-02)
438*/
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700439 {
440 int year;
441 int base;
442 int yday;
443 int wday;
444 int w;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800445
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700446 year = t->tm_year;
447 base = TM_YEAR_BASE;
448 yday = t->tm_yday;
449 wday = t->tm_wday;
450 for ( ; ; ) {
451 int len;
452 int bot;
453 int top;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800454
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700455 len = isleap_sum(year, base) ?
456 DAYSPERLYEAR :
457 DAYSPERNYEAR;
458 /*
459 ** What yday (-3 ... 3) does
460 ** the ISO year begin on?
461 */
462 bot = ((yday + 11 - wday) %
463 DAYSPERWEEK) - 3;
464 /*
465 ** What yday does the NEXT
466 ** ISO year begin on?
467 */
468 top = bot -
469 (len % DAYSPERWEEK);
470 if (top < -3)
471 top += DAYSPERWEEK;
472 top += len;
473 if (yday >= top) {
474 ++base;
475 w = 1;
476 break;
477 }
478 if (yday >= bot) {
479 w = 1 + ((yday - bot) /
480 DAYSPERWEEK);
481 break;
482 }
483 --base;
484 yday += isleap_sum(year, base) ?
485 DAYSPERLYEAR :
486 DAYSPERNYEAR;
487 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800488#ifdef XPG4_1994_04_09
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700489 if ((w == 52 &&
490 t->tm_mon == TM_JANUARY) ||
491 (w == 1 &&
492 t->tm_mon == TM_DECEMBER))
493 w = 53;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800494#endif /* defined XPG4_1994_04_09 */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700495 if (*format == 'V')
496 pt = _conv(w,
Elliott Hughes39d903a2014-07-25 15:50:31 -0700497 getformat(modifier, "%02d", "%2d", "%d", "%02d"),
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700498 pt, ptlim);
499 else if (*format == 'g') {
500 *warnp = IN_ALL;
501 pt = _yconv(year, base, 0, 1,
502 pt, ptlim, modifier);
503 } else pt = _yconv(year, base, 1, 1,
504 pt, ptlim, modifier);
505 }
506 continue;
507 case 'v':
508 /*
509 ** From Arnold Robbins' strftime version 3.0:
510 ** "date as dd-bbb-YYYY"
511 ** (ado, 1993-05-24)
512 */
Elliott Hughes39d903a2014-07-25 15:50:31 -0700513 pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700514 continue;
515 case 'W':
516 pt = _conv((t->tm_yday + DAYSPERWEEK -
517 (t->tm_wday ?
518 (t->tm_wday - 1) :
519 (DAYSPERWEEK - 1))) / DAYSPERWEEK,
520 getformat(modifier, "%02d",
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800521 "%2d", "%d", "%02d"),
522 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700523 continue;
524 case 'w':
525 pt = _conv(t->tm_wday, "%d", pt, ptlim);
526 continue;
527 case 'X':
Elliott Hughes39d903a2014-07-25 15:50:31 -0700528 pt = _fmt(Locale->X_fmt, t, pt, ptlim, warnp);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700529 continue;
530 case 'x':
531 {
532 int warn2 = IN_SOME;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800533
Elliott Hughes39d903a2014-07-25 15:50:31 -0700534 pt = _fmt(Locale->x_fmt, t, pt, ptlim, &warn2);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700535 if (warn2 == IN_ALL)
536 warn2 = IN_THIS;
537 if (warn2 > *warnp)
538 *warnp = warn2;
539 }
540 continue;
541 case 'y':
542 *warnp = IN_ALL;
543 pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1,
544 pt, ptlim, modifier);
545 continue;
546 case 'Y':
547 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1,
548 pt, ptlim, modifier);
549 continue;
550 case 'Z':
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800551#ifdef TM_ZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700552 if (t->TM_ZONE != NULL)
553 pt = _add(t->TM_ZONE, pt, ptlim,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800554 modifier);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700555 else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800556#endif /* defined TM_ZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700557 if (t->tm_isdst >= 0)
558 pt = _add(tzname[t->tm_isdst != 0],
559 pt, ptlim, modifier);
560 /*
561 ** C99 says that %Z must be replaced by the
562 ** empty string if the time zone is not
563 ** determinable.
564 */
565 continue;
566 case 'z':
567 {
Elliott Hughese0d0b152013-09-27 00:04:30 -0700568 long diff;
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700569 char const * sign;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800570
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700571 if (t->tm_isdst < 0)
572 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800573#ifdef TM_GMTOFF
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700574 diff = t->TM_GMTOFF;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800575#else /* !defined TM_GMTOFF */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700576 /*
Elliott Hughese0d0b152013-09-27 00:04:30 -0700577 ** C99 says that the UT offset must
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700578 ** be computed by looking only at
579 ** tm_isdst. This requirement is
580 ** incorrect, since it means the code
581 ** must rely on magic (in this case
582 ** altzone and timezone), and the
583 ** magic might not have the correct
584 ** offset. Doing things correctly is
585 ** tricky and requires disobeying C99;
586 ** see GNU C strftime for details.
587 ** For now, punt and conform to the
588 ** standard, even though it's incorrect.
589 **
590 ** C99 says that %z must be replaced by the
591 ** empty string if the time zone is not
592 ** determinable, so output nothing if the
593 ** appropriate variables are not available.
594 */
595 if (t->tm_isdst == 0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800596#ifdef USG_COMPAT
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700597 diff = -timezone;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800598#else /* !defined USG_COMPAT */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700599 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800600#endif /* !defined USG_COMPAT */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700601 else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800602#ifdef ALTZONE
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700603 diff = -altzone;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800604#else /* !defined ALTZONE */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700605 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800606#endif /* !defined ALTZONE */
607#endif /* !defined TM_GMTOFF */
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700608 if (diff < 0) {
609 sign = "-";
610 diff = -diff;
611 } else sign = "+";
612 pt = _add(sign, pt, ptlim, modifier);
613 diff /= SECSPERMIN;
614 diff = (diff / MINSPERHOUR) * 100 +
615 (diff % MINSPERHOUR);
616 pt = _conv(diff,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800617 getformat(modifier, "%04d",
618 "%4d", "%d", "%04d"),
619 pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700620 }
621 continue;
622 case '+':
Elliott Hughes39d903a2014-07-25 15:50:31 -0700623 pt = _fmt(Locale->date_fmt, t, pt, ptlim,
624 warnp);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700625 continue;
626 case '%':
627 /*
628 ** X311J/88-090 (4.12.3.5): if conversion char is
629 ** undefined, behavior is undefined. Print out the
630 ** character itself as printf(3) also does.
631 */
632 default:
633 break;
634 }
635 }
636 if (pt == ptlim)
637 break;
638 *pt++ = *format;
639 }
640 return pt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800641}
642
643static char *
Elliott Hughes39d903a2014-07-25 15:50:31 -0700644_conv(const int n, const char *const format, char *const pt,
645 const char *const ptlim)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800646{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700647 char buf[INT_STRLEN_MAXIMUM(int) + 1];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800648
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700649 (void) snprintf(buf, sizeof(buf), format, n);
650 return _add(buf, pt, ptlim, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800651}
652
653static char *
Elliott Hughes39d903a2014-07-25 15:50:31 -0700654_add(const char *str, char *pt, const char *const ptlim, int modifier)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800655{
656 int c;
657
658 switch (modifier) {
659 case FORCE_LOWER_CASE:
660 while (pt < ptlim && (*pt = tolower(*str++)) != '\0') {
661 ++pt;
662 }
663 break;
664
665 case '^':
666 while (pt < ptlim && (*pt = toupper(*str++)) != '\0') {
667 ++pt;
668 }
669 break;
670
671 case '#':
672 while (pt < ptlim && (c = *str++) != '\0') {
673 if (isupper(c)) {
674 c = tolower(c);
675 } else if (islower(c)) {
676 c = toupper(c);
677 }
678 *pt = c;
679 ++pt;
680 }
681
682 break;
683
684 default:
685 while (pt < ptlim && (*pt = *str++) != '\0') {
686 ++pt;
687 }
688 }
689
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700690 return pt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800691}
692
693/*
694** POSIX and the C Standard are unclear or inconsistent about
695** what %C and %y do if the year is negative or exceeds 9999.
696** Use the convention that %C concatenated with %y yields the
697** same output as %Y, and that %Y contains at least 4 bytes,
698** with more only if necessary.
699*/
700
701static char *
Elliott Hughes39d903a2014-07-25 15:50:31 -0700702_yconv(const int a, const int b, const int convert_top, const int convert_yy,
703 char *pt, const char *const ptlim, int modifier)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800704{
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700705 register int lead;
706 register int trail;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800707
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700708#define DIVISOR 100
709 trail = a % DIVISOR + b % DIVISOR;
710 lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
711 trail %= DIVISOR;
712 if (trail < 0 && lead > 0) {
713 trail += DIVISOR;
714 --lead;
715 } else if (lead < 0 && trail > 0) {
716 trail -= DIVISOR;
717 ++lead;
718 }
719 if (convert_top) {
720 if (lead == 0 && trail < 0)
721 pt = _add("-0", pt, ptlim, modifier);
Elliott Hughes39d903a2014-07-25 15:50:31 -0700722 else pt = _conv(lead, getformat(modifier, "%02d", "%2d", "%d", "%02d"), pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700723 }
724 if (convert_yy)
Elliott Hughes39d903a2014-07-25 15:50:31 -0700725 pt = _conv(((trail < 0) ? -trail : trail), getformat(modifier, "%02d", "%2d", "%d", "%02d"), pt, ptlim);
The Android Open Source Projectedbe7fc2009-03-18 22:20:24 -0700726 return pt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800727}