The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | #ifndef lint |
| 2 | #ifndef NOID |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 3 | static char elsieid[] = "@(#)strftime.c 8.1"; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 4 | /* |
| 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 Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 32 | static const char sccsid[] = "@(#)strftime.c 5.4 (Berkeley) 3/14/89"; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 33 | #endif /* !defined lint */ |
| 34 | #endif /* !defined LIBC_SCCS */ |
| 35 | |
| 36 | #include "tzfile.h" |
| 37 | #include "fcntl.h" |
| 38 | #include "locale.h" |
| 39 | #include <ctype.h> |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 40 | #include <time64.h> |
David 'Digit' Turner | 208898e | 2012-01-13 14:24:08 +0100 | [diff] [blame] | 41 | #include <bionic_time.h> /* for strftime_tz */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 42 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 43 | /* struct lc_time_T is now defined as strftime_locale |
| 44 | * in <time.h> |
| 45 | */ |
| 46 | #if 1 |
| 47 | #define lc_time_T strftime_locale |
| 48 | #else |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 49 | struct lc_time_T { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 50 | const char * mon[MONSPERYEAR]; |
| 51 | const char * month[MONSPERYEAR]; |
| 52 | const char * wday[DAYSPERWEEK]; |
| 53 | const char * weekday[DAYSPERWEEK]; |
| 54 | const char * X_fmt; |
| 55 | const char * x_fmt; |
| 56 | const char * c_fmt; |
| 57 | const char * am; |
| 58 | const char * pm; |
| 59 | const char * date_fmt; |
| 60 | }; |
| 61 | #endif |
| 62 | |
| 63 | #define Locale (&C_time_locale) |
| 64 | |
| 65 | static const struct lc_time_T C_time_locale = { |
| 66 | { |
| 67 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 68 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
| 69 | }, { |
| 70 | "January", "February", "March", "April", "May", "June", |
| 71 | "July", "August", "September", "October", "November", "December" |
| 72 | }, { |
Eric Fischer | a48fa7f | 2009-05-15 13:33:20 -0700 | [diff] [blame] | 73 | "January", "February", "March", "April", "May", "June", |
| 74 | "July", "August", "September", "October", "November", "December" |
| 75 | }, { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 112 | }; |
| 113 | |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame^] | 114 | static char * _add(const char *, char *, const char *, int); |
| 115 | static char * _conv(int, const char *, char *, const char *); |
| 116 | static char * _fmt(const char *, const struct tm *, char *, const char *, |
| 117 | int *, const struct strftime_locale*); |
| 118 | static char * _yconv(int, int, int, int, char *, const char *, int); |
| 119 | static char * getformat(int, char *, char *, char *, char *); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 120 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 121 | extern char * tzname[]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 122 | |
| 123 | #ifndef YEAR_2000_NAME |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 124 | #define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 125 | #endif /* !defined YEAR_2000_NAME */ |
| 126 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 127 | #define IN_NONE 0 |
| 128 | #define IN_SOME 1 |
| 129 | #define IN_THIS 2 |
| 130 | #define IN_ALL 3 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 131 | |
| 132 | #define FORCE_LOWER_CASE 0x100 |
| 133 | |
| 134 | size_t |
| 135 | strftime(s, maxsize, format, t) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 136 | char * const s; |
| 137 | const size_t maxsize; |
| 138 | const char * const format; |
| 139 | const struct tm * const t; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 140 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 141 | return strftime_tz(s, maxsize, format, t, Locale); |
| 142 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 143 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 144 | size_t |
| 145 | strftime_tz(s, maxsize, format, t, locale) |
| 146 | char * const s; |
| 147 | const size_t maxsize; |
| 148 | const char * const format; |
| 149 | const struct tm * const t; |
| 150 | const struct strftime_locale *locale; |
| 151 | { |
| 152 | char * p; |
| 153 | int warn; |
| 154 | |
| 155 | tzset(); |
| 156 | warn = IN_NONE; |
| 157 | p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn, locale); |
| 158 | #if 0 /* ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */ |
| 159 | if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) { |
| 160 | (void) fprintf(stderr, "\n"); |
| 161 | if (format == NULL) |
| 162 | (void) fprintf(stderr, "NULL strftime format "); |
| 163 | else (void) fprintf(stderr, "strftime format \"%s\" ", |
| 164 | format); |
| 165 | (void) fprintf(stderr, "yields only two digits of years in "); |
| 166 | if (warn == IN_SOME) |
| 167 | (void) fprintf(stderr, "some locales"); |
| 168 | else if (warn == IN_THIS) |
| 169 | (void) fprintf(stderr, "the current locale"); |
| 170 | else (void) fprintf(stderr, "all locales"); |
| 171 | (void) fprintf(stderr, "\n"); |
| 172 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 173 | #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 174 | if (p == s + maxsize) |
| 175 | return 0; |
| 176 | *p = '\0'; |
| 177 | return p - s; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static char *getformat(int modifier, char *normal, char *underscore, |
| 181 | char *dash, char *zero) { |
| 182 | switch (modifier) { |
| 183 | case '_': |
| 184 | return underscore; |
| 185 | |
| 186 | case '-': |
| 187 | return dash; |
| 188 | |
| 189 | case '0': |
| 190 | return zero; |
| 191 | } |
| 192 | |
| 193 | return normal; |
| 194 | } |
| 195 | |
| 196 | static char * |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 197 | _fmt(format, t, pt, ptlim, warnp, locale) |
| 198 | const char * format; |
| 199 | const struct tm * const t; |
| 200 | char * pt; |
| 201 | const char * const ptlim; |
| 202 | int * warnp; |
| 203 | const struct strftime_locale* locale; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 204 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 205 | for ( ; *format; ++format) { |
| 206 | if (*format == '%') { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 207 | int modifier = 0; |
| 208 | label: |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 209 | switch (*++format) { |
| 210 | case '\0': |
| 211 | --format; |
| 212 | break; |
| 213 | case 'A': |
| 214 | pt = _add((t->tm_wday < 0 || |
| 215 | t->tm_wday >= DAYSPERWEEK) ? |
| 216 | "?" : locale->weekday[t->tm_wday], |
| 217 | pt, ptlim, modifier); |
| 218 | continue; |
| 219 | case 'a': |
| 220 | pt = _add((t->tm_wday < 0 || |
| 221 | t->tm_wday >= DAYSPERWEEK) ? |
| 222 | "?" : locale->wday[t->tm_wday], |
| 223 | pt, ptlim, modifier); |
| 224 | continue; |
| 225 | case 'B': |
Eric Fischer | a48fa7f | 2009-05-15 13:33:20 -0700 | [diff] [blame] | 226 | if (modifier == '-') { |
| 227 | pt = _add((t->tm_mon < 0 || |
| 228 | t->tm_mon >= MONSPERYEAR) ? |
Eric Fischer | d5f72af | 2009-08-03 15:43:18 -0700 | [diff] [blame] | 229 | "?" : locale->standalone_month[t->tm_mon], |
Eric Fischer | a48fa7f | 2009-05-15 13:33:20 -0700 | [diff] [blame] | 230 | pt, ptlim, modifier); |
| 231 | } else { |
| 232 | pt = _add((t->tm_mon < 0 || |
| 233 | t->tm_mon >= MONSPERYEAR) ? |
Eric Fischer | d5f72af | 2009-08-03 15:43:18 -0700 | [diff] [blame] | 234 | "?" : locale->month[t->tm_mon], |
Eric Fischer | a48fa7f | 2009-05-15 13:33:20 -0700 | [diff] [blame] | 235 | pt, ptlim, modifier); |
| 236 | } |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 237 | continue; |
| 238 | case 'b': |
| 239 | case 'h': |
| 240 | pt = _add((t->tm_mon < 0 || |
| 241 | t->tm_mon >= MONSPERYEAR) ? |
| 242 | "?" : locale->mon[t->tm_mon], |
| 243 | pt, ptlim, modifier); |
| 244 | continue; |
| 245 | case 'C': |
| 246 | /* |
| 247 | ** %C used to do a... |
| 248 | ** _fmt("%a %b %e %X %Y", t); |
| 249 | ** ...whereas now POSIX 1003.2 calls for |
| 250 | ** something completely different. |
| 251 | ** (ado, 1993-05-24) |
| 252 | */ |
| 253 | pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0, |
| 254 | pt, ptlim, modifier); |
| 255 | continue; |
| 256 | case 'c': |
| 257 | { |
| 258 | int warn2 = IN_SOME; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 259 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 260 | pt = _fmt(locale->c_fmt, t, pt, ptlim, warnp, locale); |
| 261 | if (warn2 == IN_ALL) |
| 262 | warn2 = IN_THIS; |
| 263 | if (warn2 > *warnp) |
| 264 | *warnp = warn2; |
| 265 | } |
| 266 | continue; |
| 267 | case 'D': |
| 268 | pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp, locale); |
| 269 | continue; |
| 270 | case 'd': |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 271 | pt = _conv(t->tm_mday, |
| 272 | getformat(modifier, "%02d", |
| 273 | "%2d", "%d", "%02d"), |
| 274 | pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 275 | continue; |
| 276 | case 'E': |
| 277 | case 'O': |
| 278 | /* |
| 279 | ** C99 locale modifiers. |
| 280 | ** The sequences |
| 281 | ** %Ec %EC %Ex %EX %Ey %EY |
| 282 | ** %Od %oe %OH %OI %Om %OM |
| 283 | ** %OS %Ou %OU %OV %Ow %OW %Oy |
| 284 | ** are supposed to provide alternate |
| 285 | ** representations. |
| 286 | */ |
| 287 | goto label; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 288 | case '_': |
| 289 | case '-': |
| 290 | case '0': |
| 291 | case '^': |
| 292 | case '#': |
| 293 | modifier = *format; |
| 294 | goto label; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 295 | case 'e': |
| 296 | pt = _conv(t->tm_mday, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 297 | getformat(modifier, "%2d", |
| 298 | "%2d", "%d", "%02d"), |
| 299 | pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 300 | continue; |
| 301 | case 'F': |
| 302 | pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp, locale); |
| 303 | continue; |
| 304 | case 'H': |
| 305 | pt = _conv(t->tm_hour, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 306 | getformat(modifier, "%02d", |
| 307 | "%2d", "%d", "%02d"), |
| 308 | pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 309 | continue; |
| 310 | case 'I': |
| 311 | pt = _conv((t->tm_hour % 12) ? |
| 312 | (t->tm_hour % 12) : 12, |
| 313 | getformat(modifier, "%02d", |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 314 | "%2d", "%d", "%02d"), |
| 315 | pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 316 | continue; |
| 317 | case 'j': |
| 318 | pt = _conv(t->tm_yday + 1, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 319 | getformat(modifier, "%03d", "%3d", "%d", "%03d"), |
| 320 | pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 321 | continue; |
| 322 | case 'k': |
| 323 | /* |
| 324 | ** This used to be... |
| 325 | ** _conv(t->tm_hour % 12 ? |
| 326 | ** t->tm_hour % 12 : 12, 2, ' '); |
| 327 | ** ...and has been changed to the below to |
| 328 | ** match SunOS 4.1.1 and Arnold Robbins' |
| 329 | ** strftime version 3.0. That is, "%k" and |
| 330 | ** "%l" have been swapped. |
| 331 | ** (ado, 1993-05-24) |
| 332 | */ |
| 333 | pt = _conv(t->tm_hour, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 334 | getformat(modifier, "%2d", |
| 335 | "%2d", "%d", "%02d"), |
| 336 | pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 337 | continue; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 338 | #ifdef KITCHEN_SINK |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 339 | case 'K': |
| 340 | /* |
| 341 | ** After all this time, still unclaimed! |
| 342 | */ |
| 343 | pt = _add("kitchen sink", pt, ptlim, modifier); |
| 344 | continue; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 345 | #endif /* defined KITCHEN_SINK */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 346 | case 'l': |
| 347 | /* |
| 348 | ** This used to be... |
| 349 | ** _conv(t->tm_hour, 2, ' '); |
| 350 | ** ...and has been changed to the below to |
| 351 | ** match SunOS 4.1.1 and Arnold Robbin's |
| 352 | ** strftime version 3.0. That is, "%k" and |
| 353 | ** "%l" have been swapped. |
| 354 | ** (ado, 1993-05-24) |
| 355 | */ |
| 356 | pt = _conv((t->tm_hour % 12) ? |
| 357 | (t->tm_hour % 12) : 12, |
| 358 | getformat(modifier, "%2d", |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 359 | "%2d", "%d", "%02d"), |
| 360 | pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 361 | continue; |
| 362 | case 'M': |
| 363 | pt = _conv(t->tm_min, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 364 | getformat(modifier, "%02d", |
| 365 | "%2d", "%d", "%02d"), |
| 366 | pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 367 | continue; |
| 368 | case 'm': |
| 369 | pt = _conv(t->tm_mon + 1, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 370 | getformat(modifier, "%02d", |
| 371 | "%2d", "%d", "%02d"), |
| 372 | pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 373 | continue; |
| 374 | case 'n': |
| 375 | pt = _add("\n", pt, ptlim, modifier); |
| 376 | continue; |
| 377 | case 'p': |
| 378 | pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ? |
| 379 | locale->pm : |
| 380 | locale->am, |
| 381 | pt, ptlim, modifier); |
| 382 | continue; |
| 383 | case 'P': |
| 384 | pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ? |
| 385 | locale->pm : |
| 386 | locale->am, |
| 387 | pt, ptlim, FORCE_LOWER_CASE); |
| 388 | continue; |
| 389 | case 'R': |
| 390 | pt = _fmt("%H:%M", t, pt, ptlim, warnp, locale); |
| 391 | continue; |
| 392 | case 'r': |
| 393 | pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp, locale); |
| 394 | continue; |
| 395 | case 'S': |
| 396 | pt = _conv(t->tm_sec, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 397 | getformat(modifier, "%02d", |
| 398 | "%2d", "%d", "%02d"), |
| 399 | pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 400 | continue; |
| 401 | case 's': |
| 402 | { |
| 403 | struct tm tm; |
| 404 | char buf[INT_STRLEN_MAXIMUM( |
| 405 | time64_t) + 1]; |
| 406 | time64_t mkt; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 407 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 408 | tm = *t; |
| 409 | mkt = mktime64(&tm); |
| 410 | if (TYPE_SIGNED(time64_t)) |
Jim Huang | e6cff93 | 2011-06-16 22:35:16 +0800 | [diff] [blame] | 411 | (void) snprintf(buf, sizeof(buf), "%lld", |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 412 | (long long) mkt); |
Jim Huang | e6cff93 | 2011-06-16 22:35:16 +0800 | [diff] [blame] | 413 | else (void) snprintf(buf, sizeof(buf), "%llu", |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 414 | (unsigned long long) mkt); |
| 415 | pt = _add(buf, pt, ptlim, modifier); |
| 416 | } |
| 417 | continue; |
| 418 | case 'T': |
| 419 | pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp, locale); |
| 420 | continue; |
| 421 | case 't': |
| 422 | pt = _add("\t", pt, ptlim, modifier); |
| 423 | continue; |
| 424 | case 'U': |
| 425 | pt = _conv((t->tm_yday + DAYSPERWEEK - |
| 426 | t->tm_wday) / DAYSPERWEEK, |
| 427 | getformat(modifier, "%02d", |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 428 | "%2d", "%d", "%02d"), |
| 429 | pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 430 | continue; |
| 431 | case 'u': |
| 432 | /* |
| 433 | ** From Arnold Robbins' strftime version 3.0: |
| 434 | ** "ISO 8601: Weekday as a decimal number |
| 435 | ** [1 (Monday) - 7]" |
| 436 | ** (ado, 1993-05-24) |
| 437 | */ |
| 438 | pt = _conv((t->tm_wday == 0) ? |
| 439 | DAYSPERWEEK : t->tm_wday, "%d", pt, ptlim); |
| 440 | continue; |
| 441 | case 'V': /* ISO 8601 week number */ |
| 442 | case 'G': /* ISO 8601 year (four digits) */ |
| 443 | case 'g': /* ISO 8601 year (two digits) */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 444 | /* |
| 445 | ** From Arnold Robbins' strftime version 3.0: "the week number of the |
| 446 | ** year (the first Monday as the first day of week 1) as a decimal number |
| 447 | ** (01-53)." |
| 448 | ** (ado, 1993-05-24) |
| 449 | ** |
| 450 | ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn: |
| 451 | ** "Week 01 of a year is per definition the first week which has the |
| 452 | ** Thursday in this year, which is equivalent to the week which contains |
| 453 | ** the fourth day of January. In other words, the first week of a new year |
| 454 | ** is the week which has the majority of its days in the new year. Week 01 |
| 455 | ** might also contain days from the previous year and the week before week |
| 456 | ** 01 of a year is the last week (52 or 53) of the previous year even if |
| 457 | ** it contains days from the new year. A week starts with Monday (day 1) |
| 458 | ** and ends with Sunday (day 7). For example, the first week of the year |
| 459 | ** 1997 lasts from 1996-12-30 to 1997-01-05..." |
| 460 | ** (ado, 1996-01-02) |
| 461 | */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 462 | { |
| 463 | int year; |
| 464 | int base; |
| 465 | int yday; |
| 466 | int wday; |
| 467 | int w; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 468 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 469 | year = t->tm_year; |
| 470 | base = TM_YEAR_BASE; |
| 471 | yday = t->tm_yday; |
| 472 | wday = t->tm_wday; |
| 473 | for ( ; ; ) { |
| 474 | int len; |
| 475 | int bot; |
| 476 | int top; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 477 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 478 | len = isleap_sum(year, base) ? |
| 479 | DAYSPERLYEAR : |
| 480 | DAYSPERNYEAR; |
| 481 | /* |
| 482 | ** What yday (-3 ... 3) does |
| 483 | ** the ISO year begin on? |
| 484 | */ |
| 485 | bot = ((yday + 11 - wday) % |
| 486 | DAYSPERWEEK) - 3; |
| 487 | /* |
| 488 | ** What yday does the NEXT |
| 489 | ** ISO year begin on? |
| 490 | */ |
| 491 | top = bot - |
| 492 | (len % DAYSPERWEEK); |
| 493 | if (top < -3) |
| 494 | top += DAYSPERWEEK; |
| 495 | top += len; |
| 496 | if (yday >= top) { |
| 497 | ++base; |
| 498 | w = 1; |
| 499 | break; |
| 500 | } |
| 501 | if (yday >= bot) { |
| 502 | w = 1 + ((yday - bot) / |
| 503 | DAYSPERWEEK); |
| 504 | break; |
| 505 | } |
| 506 | --base; |
| 507 | yday += isleap_sum(year, base) ? |
| 508 | DAYSPERLYEAR : |
| 509 | DAYSPERNYEAR; |
| 510 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 511 | #ifdef XPG4_1994_04_09 |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 512 | if ((w == 52 && |
| 513 | t->tm_mon == TM_JANUARY) || |
| 514 | (w == 1 && |
| 515 | t->tm_mon == TM_DECEMBER)) |
| 516 | w = 53; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 517 | #endif /* defined XPG4_1994_04_09 */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 518 | if (*format == 'V') |
| 519 | pt = _conv(w, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 520 | getformat(modifier, |
| 521 | "%02d", |
| 522 | "%2d", |
| 523 | "%d", |
| 524 | "%02d"), |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 525 | pt, ptlim); |
| 526 | else if (*format == 'g') { |
| 527 | *warnp = IN_ALL; |
| 528 | pt = _yconv(year, base, 0, 1, |
| 529 | pt, ptlim, modifier); |
| 530 | } else pt = _yconv(year, base, 1, 1, |
| 531 | pt, ptlim, modifier); |
| 532 | } |
| 533 | continue; |
| 534 | case 'v': |
| 535 | /* |
| 536 | ** From Arnold Robbins' strftime version 3.0: |
| 537 | ** "date as dd-bbb-YYYY" |
| 538 | ** (ado, 1993-05-24) |
| 539 | */ |
| 540 | pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp, locale); |
| 541 | continue; |
| 542 | case 'W': |
| 543 | pt = _conv((t->tm_yday + DAYSPERWEEK - |
| 544 | (t->tm_wday ? |
| 545 | (t->tm_wday - 1) : |
| 546 | (DAYSPERWEEK - 1))) / DAYSPERWEEK, |
| 547 | getformat(modifier, "%02d", |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 548 | "%2d", "%d", "%02d"), |
| 549 | pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 550 | continue; |
| 551 | case 'w': |
| 552 | pt = _conv(t->tm_wday, "%d", pt, ptlim); |
| 553 | continue; |
| 554 | case 'X': |
| 555 | pt = _fmt(locale->X_fmt, t, pt, ptlim, warnp, locale); |
| 556 | continue; |
| 557 | case 'x': |
| 558 | { |
| 559 | int warn2 = IN_SOME; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 560 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 561 | pt = _fmt(locale->x_fmt, t, pt, ptlim, &warn2, locale); |
| 562 | if (warn2 == IN_ALL) |
| 563 | warn2 = IN_THIS; |
| 564 | if (warn2 > *warnp) |
| 565 | *warnp = warn2; |
| 566 | } |
| 567 | continue; |
| 568 | case 'y': |
| 569 | *warnp = IN_ALL; |
| 570 | pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1, |
| 571 | pt, ptlim, modifier); |
| 572 | continue; |
| 573 | case 'Y': |
| 574 | pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1, |
| 575 | pt, ptlim, modifier); |
| 576 | continue; |
| 577 | case 'Z': |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 578 | #ifdef TM_ZONE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 579 | if (t->TM_ZONE != NULL) |
| 580 | pt = _add(t->TM_ZONE, pt, ptlim, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 581 | modifier); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 582 | else |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 583 | #endif /* defined TM_ZONE */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 584 | if (t->tm_isdst >= 0) |
| 585 | pt = _add(tzname[t->tm_isdst != 0], |
| 586 | pt, ptlim, modifier); |
| 587 | /* |
| 588 | ** C99 says that %Z must be replaced by the |
| 589 | ** empty string if the time zone is not |
| 590 | ** determinable. |
| 591 | */ |
| 592 | continue; |
| 593 | case 'z': |
| 594 | { |
| 595 | int diff; |
| 596 | char const * sign; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 597 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 598 | if (t->tm_isdst < 0) |
| 599 | continue; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 600 | #ifdef TM_GMTOFF |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 601 | diff = t->TM_GMTOFF; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 602 | #else /* !defined TM_GMTOFF */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 603 | /* |
| 604 | ** C99 says that the UTC offset must |
| 605 | ** be computed by looking only at |
| 606 | ** tm_isdst. This requirement is |
| 607 | ** incorrect, since it means the code |
| 608 | ** must rely on magic (in this case |
| 609 | ** altzone and timezone), and the |
| 610 | ** magic might not have the correct |
| 611 | ** offset. Doing things correctly is |
| 612 | ** tricky and requires disobeying C99; |
| 613 | ** see GNU C strftime for details. |
| 614 | ** For now, punt and conform to the |
| 615 | ** standard, even though it's incorrect. |
| 616 | ** |
| 617 | ** C99 says that %z must be replaced by the |
| 618 | ** empty string if the time zone is not |
| 619 | ** determinable, so output nothing if the |
| 620 | ** appropriate variables are not available. |
| 621 | */ |
| 622 | if (t->tm_isdst == 0) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 623 | #ifdef USG_COMPAT |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 624 | diff = -timezone; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 625 | #else /* !defined USG_COMPAT */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 626 | continue; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 627 | #endif /* !defined USG_COMPAT */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 628 | else |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 629 | #ifdef ALTZONE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 630 | diff = -altzone; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 631 | #else /* !defined ALTZONE */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 632 | continue; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 633 | #endif /* !defined ALTZONE */ |
| 634 | #endif /* !defined TM_GMTOFF */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 635 | if (diff < 0) { |
| 636 | sign = "-"; |
| 637 | diff = -diff; |
| 638 | } else sign = "+"; |
| 639 | pt = _add(sign, pt, ptlim, modifier); |
| 640 | diff /= SECSPERMIN; |
| 641 | diff = (diff / MINSPERHOUR) * 100 + |
| 642 | (diff % MINSPERHOUR); |
| 643 | pt = _conv(diff, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 644 | getformat(modifier, "%04d", |
| 645 | "%4d", "%d", "%04d"), |
| 646 | pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 647 | } |
| 648 | continue; |
| 649 | case '+': |
| 650 | pt = _fmt(locale->date_fmt, t, pt, ptlim, |
| 651 | warnp, locale); |
| 652 | continue; |
| 653 | case '%': |
| 654 | /* |
| 655 | ** X311J/88-090 (4.12.3.5): if conversion char is |
| 656 | ** undefined, behavior is undefined. Print out the |
| 657 | ** character itself as printf(3) also does. |
| 658 | */ |
| 659 | default: |
| 660 | break; |
| 661 | } |
| 662 | } |
| 663 | if (pt == ptlim) |
| 664 | break; |
| 665 | *pt++ = *format; |
| 666 | } |
| 667 | return pt; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | static char * |
| 671 | _conv(n, format, pt, ptlim) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 672 | const int n; |
| 673 | const char * const format; |
| 674 | char * const pt; |
| 675 | const char * const ptlim; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 676 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 677 | char buf[INT_STRLEN_MAXIMUM(int) + 1]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 678 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 679 | (void) snprintf(buf, sizeof(buf), format, n); |
| 680 | return _add(buf, pt, ptlim, 0); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | static char * |
| 684 | _add(str, pt, ptlim, modifier) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 685 | const char * str; |
| 686 | char * pt; |
| 687 | const char * const ptlim; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 688 | int modifier; |
| 689 | { |
| 690 | int c; |
| 691 | |
| 692 | switch (modifier) { |
| 693 | case FORCE_LOWER_CASE: |
| 694 | while (pt < ptlim && (*pt = tolower(*str++)) != '\0') { |
| 695 | ++pt; |
| 696 | } |
| 697 | break; |
| 698 | |
| 699 | case '^': |
| 700 | while (pt < ptlim && (*pt = toupper(*str++)) != '\0') { |
| 701 | ++pt; |
| 702 | } |
| 703 | break; |
| 704 | |
| 705 | case '#': |
| 706 | while (pt < ptlim && (c = *str++) != '\0') { |
| 707 | if (isupper(c)) { |
| 708 | c = tolower(c); |
| 709 | } else if (islower(c)) { |
| 710 | c = toupper(c); |
| 711 | } |
| 712 | *pt = c; |
| 713 | ++pt; |
| 714 | } |
| 715 | |
| 716 | break; |
| 717 | |
| 718 | default: |
| 719 | while (pt < ptlim && (*pt = *str++) != '\0') { |
| 720 | ++pt; |
| 721 | } |
| 722 | } |
| 723 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 724 | return pt; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | /* |
| 728 | ** POSIX and the C Standard are unclear or inconsistent about |
| 729 | ** what %C and %y do if the year is negative or exceeds 9999. |
| 730 | ** Use the convention that %C concatenated with %y yields the |
| 731 | ** same output as %Y, and that %Y contains at least 4 bytes, |
| 732 | ** with more only if necessary. |
| 733 | */ |
| 734 | |
| 735 | static char * |
| 736 | _yconv(a, b, convert_top, convert_yy, pt, ptlim, modifier) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 737 | const int a; |
| 738 | const int b; |
| 739 | const int convert_top; |
| 740 | const int convert_yy; |
| 741 | char * pt; |
| 742 | const char * const ptlim; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 743 | int modifier; |
| 744 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 745 | register int lead; |
| 746 | register int trail; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 747 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 748 | #define DIVISOR 100 |
| 749 | trail = a % DIVISOR + b % DIVISOR; |
| 750 | lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR; |
| 751 | trail %= DIVISOR; |
| 752 | if (trail < 0 && lead > 0) { |
| 753 | trail += DIVISOR; |
| 754 | --lead; |
| 755 | } else if (lead < 0 && trail > 0) { |
| 756 | trail -= DIVISOR; |
| 757 | ++lead; |
| 758 | } |
| 759 | if (convert_top) { |
| 760 | if (lead == 0 && trail < 0) |
| 761 | pt = _add("-0", pt, ptlim, modifier); |
| 762 | else pt = _conv(lead, getformat(modifier, "%02d", |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 763 | "%2d", "%d", "%02d"), |
| 764 | pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 765 | } |
| 766 | if (convert_yy) |
| 767 | pt = _conv(((trail < 0) ? -trail : trail), |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 768 | getformat(modifier, "%02d", "%2d", "%d", "%02d"), |
| 769 | pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 770 | return pt; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 771 | } |