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