The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* |
Elliott Hughes | 5f56454 | 2014-06-19 13:54:10 -0700 | [diff] [blame] | 2 | ** Based on the UCB version with the copyright notice and sccsid |
| 3 | ** appearing below. |
| 4 | ** |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 5 | ** This is ANSIish only when "multibyte character == plain character". |
| 6 | */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 7 | |
| 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 Hughes | 5f56454 | 2014-06-19 13:54:10 -0700 | [diff] [blame] | 22 | ** THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 23 | ** IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 24 | ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 25 | */ |
| 26 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 27 | #include "tzfile.h" |
| 28 | #include "fcntl.h" |
| 29 | #include "locale.h" |
Elliott Hughes | 5f56454 | 2014-06-19 13:54:10 -0700 | [diff] [blame] | 30 | |
| 31 | #if __ANDROID__ |
Elliott Hughes | 905e6d5 | 2014-07-25 11:55:59 -0700 | [diff] [blame] | 32 | |
Elliott Hughes | 905e6d5 | 2014-07-25 11:55:59 -0700 | [diff] [blame] | 33 | /* LP32 had a 32-bit time_t, so we need to work around that here. */ |
Elliott Hughes | 52defb7 | 2014-05-05 17:14:02 -0700 | [diff] [blame] | 34 | #if defined(__LP64__) |
| 35 | #define time64_t time_t |
| 36 | #define mktime64 mktime |
| 37 | #else |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 38 | #include <time64.h> |
Elliott Hughes | 52defb7 | 2014-05-05 17:14:02 -0700 | [diff] [blame] | 39 | #endif |
Elliott Hughes | 905e6d5 | 2014-07-25 11:55:59 -0700 | [diff] [blame] | 40 | |
Elliott Hughes | 5f56454 | 2014-06-19 13:54:10 -0700 | [diff] [blame] | 41 | #include <ctype.h> |
Elliott Hughes | 905e6d5 | 2014-07-25 11:55:59 -0700 | [diff] [blame] | 42 | |
Elliott Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 43 | #endif |
Elliott Hughes | 905e6d5 | 2014-07-25 11:55:59 -0700 | [diff] [blame] | 44 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 45 | struct lc_time_T { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 46 | 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 Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 57 | |
Elliott Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 58 | #ifdef LOCALE_HOME |
Elliott Hughes | 5f56454 | 2014-06-19 13:54:10 -0700 | [diff] [blame] | 59 | #include "sys/stat.h" |
| 60 | static struct lc_time_T localebuf; |
| 61 | static struct lc_time_T * _loc(void); |
| 62 | #define Locale _loc() |
| 63 | #endif /* defined LOCALE_HOME */ |
| 64 | #ifndef LOCALE_HOME |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 65 | #define Locale (&C_time_locale) |
Elliott Hughes | 5f56454 | 2014-06-19 13:54:10 -0700 | [diff] [blame] | 66 | #endif /* !defined LOCALE_HOME */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 67 | |
| 68 | static 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 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 *, |
Elliott Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 117 | int *); |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 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 |
Elliott Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 135 | strftime(char * const s, const size_t maxsize, const char *const format, |
| 136 | const struct tm *const t) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 137 | { |
| 138 | char * p; |
| 139 | int warn; |
| 140 | |
| 141 | tzset(); |
Elliott Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 142 | #ifdef LOCALE_HOME |
| 143 | localebuf.mon[0] = 0; |
| 144 | #endif /* defined LOCALE_HOME */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 145 | warn = IN_NONE; |
Elliott Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 146 | p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn); |
Elliott Hughes | 9a5a3e8 | 2014-05-05 20:28:28 -0700 | [diff] [blame] | 147 | #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] | 148 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 162 | #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] | 163 | if (p == s + maxsize) |
| 164 | return 0; |
| 165 | *p = '\0'; |
| 166 | return p - s; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static 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 | |
| 185 | static char * |
Elliott Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 186 | _fmt(const char *format, const struct tm *const t, char * pt, |
| 187 | const char *const ptlim, int *warnp) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 188 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 189 | for ( ; *format; ++format) { |
| 190 | if (*format == '%') { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 191 | int modifier = 0; |
| 192 | label: |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 193 | 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 Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 200 | "?" : Locale->weekday[t->tm_wday], |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 201 | pt, ptlim, modifier); |
| 202 | continue; |
| 203 | case 'a': |
| 204 | pt = _add((t->tm_wday < 0 || |
| 205 | t->tm_wday >= DAYSPERWEEK) ? |
Elliott Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 206 | "?" : Locale->wday[t->tm_wday], |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 207 | pt, ptlim, modifier); |
| 208 | continue; |
| 209 | case 'B': |
Elliott Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 210 | pt = _add((t->tm_mon < 0 || |
Eric Fischer | a48fa7f | 2009-05-15 13:33:20 -0700 | [diff] [blame] | 211 | t->tm_mon >= MONSPERYEAR) ? |
Elliott Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 212 | "?" : Locale->month[t->tm_mon], |
Eric Fischer | a48fa7f | 2009-05-15 13:33:20 -0700 | [diff] [blame] | 213 | pt, ptlim, modifier); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 214 | continue; |
| 215 | case 'b': |
| 216 | case 'h': |
| 217 | pt = _add((t->tm_mon < 0 || |
| 218 | t->tm_mon >= MONSPERYEAR) ? |
Elliott Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 219 | "?" : Locale->mon[t->tm_mon], |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 220 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 236 | |
Elliott Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 237 | pt = _fmt(Locale->c_fmt, t, pt, ptlim, &warn2); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 238 | if (warn2 == IN_ALL) |
| 239 | warn2 = IN_THIS; |
| 240 | if (warn2 > *warnp) |
| 241 | *warnp = warn2; |
| 242 | } |
| 243 | continue; |
| 244 | case 'D': |
Elliott Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 245 | pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 246 | continue; |
| 247 | case 'd': |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 248 | pt = _conv(t->tm_mday, |
| 249 | getformat(modifier, "%02d", |
| 250 | "%2d", "%d", "%02d"), |
| 251 | pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 252 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 265 | case '_': |
| 266 | case '-': |
| 267 | case '0': |
| 268 | case '^': |
| 269 | case '#': |
| 270 | modifier = *format; |
| 271 | goto label; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 272 | case 'e': |
| 273 | pt = _conv(t->tm_mday, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 274 | getformat(modifier, "%2d", |
| 275 | "%2d", "%d", "%02d"), |
| 276 | pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 277 | continue; |
| 278 | case 'F': |
Elliott Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 279 | pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 280 | continue; |
| 281 | case 'H': |
| 282 | pt = _conv(t->tm_hour, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 283 | getformat(modifier, "%02d", |
| 284 | "%2d", "%d", "%02d"), |
| 285 | pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 286 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 291 | "%2d", "%d", "%02d"), |
| 292 | pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 293 | continue; |
| 294 | case 'j': |
| 295 | pt = _conv(t->tm_yday + 1, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 296 | getformat(modifier, "%03d", "%3d", "%d", "%03d"), |
| 297 | pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 298 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 311 | getformat(modifier, "%2d", |
| 312 | "%2d", "%d", "%02d"), |
| 313 | pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 314 | continue; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 315 | #ifdef KITCHEN_SINK |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 316 | case 'K': |
| 317 | /* |
| 318 | ** After all this time, still unclaimed! |
| 319 | */ |
Elliott Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 320 | pt = _add("kitchen sink", pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 321 | continue; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 322 | #endif /* defined KITCHEN_SINK */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 323 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 336 | "%2d", "%d", "%02d"), |
| 337 | pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 338 | continue; |
| 339 | case 'M': |
| 340 | pt = _conv(t->tm_min, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 341 | getformat(modifier, "%02d", |
| 342 | "%2d", "%d", "%02d"), |
| 343 | pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 344 | continue; |
| 345 | case 'm': |
| 346 | pt = _conv(t->tm_mon + 1, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 347 | getformat(modifier, "%02d", |
| 348 | "%2d", "%d", "%02d"), |
| 349 | pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 350 | 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 Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 356 | Locale->pm : |
| 357 | Locale->am, |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 358 | pt, ptlim, modifier); |
| 359 | continue; |
| 360 | case 'P': |
| 361 | pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ? |
Elliott Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 362 | Locale->pm : |
| 363 | Locale->am, |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 364 | pt, ptlim, FORCE_LOWER_CASE); |
| 365 | continue; |
| 366 | case 'R': |
Elliott Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 367 | pt = _fmt("%H:%M", t, pt, ptlim, warnp); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 368 | continue; |
| 369 | case 'r': |
Elliott Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 370 | pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 371 | continue; |
| 372 | case 'S': |
| 373 | pt = _conv(t->tm_sec, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 374 | getformat(modifier, "%02d", |
| 375 | "%2d", "%d", "%02d"), |
| 376 | pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 377 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 384 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 385 | tm = *t; |
| 386 | mkt = mktime64(&tm); |
| 387 | if (TYPE_SIGNED(time64_t)) |
Jim Huang | e6cff93 | 2011-06-16 22:35:16 +0800 | [diff] [blame] | 388 | (void) snprintf(buf, sizeof(buf), "%lld", |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 389 | (long long) mkt); |
Jim Huang | e6cff93 | 2011-06-16 22:35:16 +0800 | [diff] [blame] | 390 | else (void) snprintf(buf, sizeof(buf), "%llu", |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 391 | (unsigned long long) mkt); |
| 392 | pt = _add(buf, pt, ptlim, modifier); |
| 393 | } |
| 394 | continue; |
| 395 | case 'T': |
Elliott Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 396 | pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 397 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 405 | "%2d", "%d", "%02d"), |
| 406 | pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 407 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 421 | /* |
| 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 Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 427 | ** From <http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html> by Markus Kuhn: |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 428 | ** "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 Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 439 | { |
| 440 | int year; |
| 441 | int base; |
| 442 | int yday; |
| 443 | int wday; |
| 444 | int w; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 445 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 446 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 454 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 455 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 488 | #ifdef XPG4_1994_04_09 |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 489 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 494 | #endif /* defined XPG4_1994_04_09 */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 495 | if (*format == 'V') |
| 496 | pt = _conv(w, |
Elliott Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 497 | getformat(modifier, "%02d", "%2d", "%d", "%02d"), |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 498 | 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 Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 513 | pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 514 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 521 | "%2d", "%d", "%02d"), |
| 522 | pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 523 | continue; |
| 524 | case 'w': |
| 525 | pt = _conv(t->tm_wday, "%d", pt, ptlim); |
| 526 | continue; |
| 527 | case 'X': |
Elliott Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 528 | pt = _fmt(Locale->X_fmt, t, pt, ptlim, warnp); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 529 | continue; |
| 530 | case 'x': |
| 531 | { |
| 532 | int warn2 = IN_SOME; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 533 | |
Elliott Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 534 | pt = _fmt(Locale->x_fmt, t, pt, ptlim, &warn2); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 535 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 551 | #ifdef TM_ZONE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 552 | if (t->TM_ZONE != NULL) |
| 553 | pt = _add(t->TM_ZONE, pt, ptlim, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 554 | modifier); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 555 | else |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 556 | #endif /* defined TM_ZONE */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 557 | 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 Hughes | e0d0b15 | 2013-09-27 00:04:30 -0700 | [diff] [blame] | 568 | long diff; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 569 | char const * sign; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 570 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 571 | if (t->tm_isdst < 0) |
| 572 | continue; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 573 | #ifdef TM_GMTOFF |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 574 | diff = t->TM_GMTOFF; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 575 | #else /* !defined TM_GMTOFF */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 576 | /* |
Elliott Hughes | e0d0b15 | 2013-09-27 00:04:30 -0700 | [diff] [blame] | 577 | ** C99 says that the UT offset must |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 578 | ** 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 596 | #ifdef USG_COMPAT |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 597 | diff = -timezone; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 598 | #else /* !defined USG_COMPAT */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 599 | continue; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 600 | #endif /* !defined USG_COMPAT */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 601 | else |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 602 | #ifdef ALTZONE |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 603 | diff = -altzone; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 604 | #else /* !defined ALTZONE */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 605 | continue; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 606 | #endif /* !defined ALTZONE */ |
| 607 | #endif /* !defined TM_GMTOFF */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 608 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 617 | getformat(modifier, "%04d", |
| 618 | "%4d", "%d", "%04d"), |
| 619 | pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 620 | } |
| 621 | continue; |
| 622 | case '+': |
Elliott Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 623 | pt = _fmt(Locale->date_fmt, t, pt, ptlim, |
| 624 | warnp); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 625 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | static char * |
Elliott Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 644 | _conv(const int n, const char *const format, char *const pt, |
| 645 | const char *const ptlim) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 646 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 647 | char buf[INT_STRLEN_MAXIMUM(int) + 1]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 648 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 649 | (void) snprintf(buf, sizeof(buf), format, n); |
| 650 | return _add(buf, pt, ptlim, 0); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | static char * |
Elliott Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 654 | _add(const char *str, char *pt, const char *const ptlim, int modifier) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 655 | { |
| 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 Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 690 | return pt; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 691 | } |
| 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 | |
| 701 | static char * |
Elliott Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 702 | _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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 704 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 705 | register int lead; |
| 706 | register int trail; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 707 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 708 | #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 Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 722 | else pt = _conv(lead, getformat(modifier, "%02d", "%2d", "%d", "%02d"), pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 723 | } |
| 724 | if (convert_yy) |
Elliott Hughes | 39d903a | 2014-07-25 15:50:31 -0700 | [diff] [blame] | 725 | pt = _conv(((trail < 0) ? -trail : trail), getformat(modifier, "%02d", "%2d", "%d", "%02d"), pt, ptlim); |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 726 | return pt; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 727 | } |