[32] | 1 | /* $NetBSD: strptime.c,v 1.62 2017/08/24 01:01:09 ginsbach Exp $ */
|
---|
| 2 | /* http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/lib/libc/time/strptime.c?only_with_tag=HEAD
|
---|
| 3 | * NetBSD implementation strptime().
|
---|
| 4 | * Format description: https://netbsd.gw.com/cgi-bin/man-cgi?strptime+3+NetBSD-current
|
---|
| 5 | */
|
---|
| 6 | /*-
|
---|
| 7 | * Copyright (c) 1997, 1998, 2005, 2008 The NetBSD Foundation, Inc.
|
---|
| 8 | * All rights reserved.
|
---|
| 9 | *
|
---|
| 10 | * This code was contributed to The NetBSD Foundation by Klaus Klein.
|
---|
| 11 | * Heavily optimised by David Laight
|
---|
| 12 | *
|
---|
| 13 | * Redistribution and use in source and binary forms, with or without
|
---|
| 14 | * modification, are permitted provided that the following conditions
|
---|
| 15 | * are met:
|
---|
| 16 | * 1. Redistributions of source code must retain the above copyright
|
---|
| 17 | * notice, this list of conditions and the following disclaimer.
|
---|
| 18 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
| 19 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 20 | * documentation and/or other materials provided with the distribution.
|
---|
| 21 | *
|
---|
| 22 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
---|
| 23 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
---|
| 24 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
| 25 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
---|
| 26 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
---|
| 27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
---|
| 28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
---|
| 29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
---|
| 30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
---|
| 31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
---|
| 32 | * POSSIBILITY OF SUCH DAMAGE.
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | //#include <sys/cdefs.h>
|
---|
| 36 | //__RCSID("$NetBSD: strptime.c,v 1.62 2017/08/24 01:01:09 ginsbach Exp $");
|
---|
| 37 |
|
---|
[377] | 38 | #if defined(__MINGW32__) || defined(_MSC_VER || defined(__BORLANDC__) || defined(__WATCOMC__) || defined(__NeXT__) || defined(__bsdi__)
|
---|
[34] | 39 |
|
---|
[315] | 40 | #include <stdlib.h>
|
---|
[32] | 41 | #include <ctype.h>
|
---|
| 42 | #include <string.h>
|
---|
| 43 | #include <time.h>
|
---|
| 44 | #include <stdint.h>
|
---|
[243] | 45 |
|
---|
| 46 | #ifdef __WATCOMC__
|
---|
[315] | 47 | #ifndef __NETWARE__
|
---|
[241] | 48 | #include <strings.h>
|
---|
[243] | 49 | #endif
|
---|
[315] | 50 | #endif
|
---|
[32] | 51 |
|
---|
| 52 | static const unsigned char *conv_num(const unsigned char *, int *, unsigned int, unsigned int);
|
---|
| 53 | static const unsigned char *find_string(const unsigned char *, int *, const char * const *, const char * const *, int);
|
---|
| 54 |
|
---|
| 55 | /*
|
---|
| 56 | * We do not implement alternate representations. However, we always
|
---|
| 57 | * check whether a given modifier is allowed for a certain conversion.
|
---|
| 58 | */
|
---|
| 59 | #define ALT_E 0x01
|
---|
| 60 | #define ALT_O 0x02
|
---|
| 61 | #define LEGAL_ALT(x) { if (alt_format & ~(x)) return NULL; }
|
---|
| 62 |
|
---|
| 63 | #define TM_YEAR_BASE 1900
|
---|
| 64 |
|
---|
| 65 | #define TM_SUNDAY 0
|
---|
| 66 | #define TM_MONDAY 1
|
---|
| 67 | #define TM_TUESDAY 2
|
---|
| 68 | #define TM_WEDNESDAY 3
|
---|
| 69 | #define TM_THURSDAY 4
|
---|
| 70 | #define TM_FRIDAY 5
|
---|
| 71 | #define TM_SATURDAY 6
|
---|
| 72 |
|
---|
| 73 | #define S_YEAR (1 << 0)
|
---|
| 74 | #define S_MON (1 << 1)
|
---|
| 75 | #define S_YDAY (1 << 2)
|
---|
| 76 | #define S_MDAY (1 << 3)
|
---|
| 77 | #define S_WDAY (1 << 4)
|
---|
| 78 | #define S_HOUR (1 << 5)
|
---|
| 79 |
|
---|
| 80 | #define HAVE_MDAY(s) (s & S_MDAY)
|
---|
| 81 | #define HAVE_MON(s) (s & S_MON)
|
---|
| 82 | #define HAVE_WDAY(s) (s & S_WDAY)
|
---|
| 83 | #define HAVE_YDAY(s) (s & S_YDAY)
|
---|
| 84 | #define HAVE_YEAR(s) (s & S_YEAR)
|
---|
| 85 | #define HAVE_HOUR(s) (s & S_HOUR)
|
---|
| 86 |
|
---|
| 87 | #define SECSPERMIN 60
|
---|
| 88 | #define MINSPERHOUR 60
|
---|
| 89 | #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
|
---|
| 90 | #define HOURSPERDAY 24
|
---|
| 91 |
|
---|
| 92 | #define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y"
|
---|
| 93 | #define HERE_D_FMT "%y/%m/%d"
|
---|
| 94 | #define HERE_T_FMT_AMPM "%I:%M:%S %p"
|
---|
| 95 | #define HERE_T_FMT "%H:%M:%S"
|
---|
| 96 |
|
---|
| 97 | #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
|
---|
| 98 |
|
---|
| 99 | /*
|
---|
| 100 | ** Since everything in isleap is modulo 400 (or a factor of 400), we know that
|
---|
| 101 | ** isleap(y) == isleap(y % 400)
|
---|
| 102 | ** and so
|
---|
| 103 | ** isleap(a + b) == isleap((a + b) % 400)
|
---|
| 104 | ** or
|
---|
| 105 | ** isleap(a + b) == isleap(a % 400 + b % 400)
|
---|
| 106 | ** This is true even if % means modulo rather than Fortran remainder
|
---|
| 107 | ** (which is allowed by C89 but not by C99 or later).
|
---|
| 108 | ** We use this to avoid addition overflow problems.
|
---|
| 109 | */
|
---|
| 110 |
|
---|
| 111 | #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
|
---|
| 112 |
|
---|
[215] | 113 | #if defined(_MSC_VER) || defined(__BORLANDC__)
|
---|
[32] | 114 | #define tzname _tzname
|
---|
| 115 | #define strncasecmp _strnicmp
|
---|
| 116 | #endif
|
---|
| 117 |
|
---|
[241] | 118 | #ifdef __WATCOMC__
|
---|
| 119 | #define _tzset tzset
|
---|
| 120 | #endif
|
---|
| 121 |
|
---|
[315] | 122 | #if defined(__BORLANDC__) || defined(__NETWARE__)
|
---|
[215] | 123 | char* cm_strdup(const char* str);
|
---|
| 124 |
|
---|
[315] | 125 | #ifdef __NETWARE__
|
---|
| 126 | #define strncasecmp _strnicmp
|
---|
| 127 | #endif
|
---|
| 128 |
|
---|
[215] | 129 | int _strnicmp(const char* _a, const char* _b, int len){
|
---|
| 130 | char* a = cm_strdup(_a);
|
---|
| 131 | char* b = cm_strdup(_b);
|
---|
| 132 | int i;
|
---|
[315] | 133 | int r;
|
---|
[215] | 134 | for(i = 0; a[i] != 0; i++){
|
---|
| 135 | a[i] = tolower(a[i]);
|
---|
| 136 | }
|
---|
| 137 | for(i = 0; b[i] != 0; i++){
|
---|
| 138 | b[i] = tolower(b[i]);
|
---|
| 139 | }
|
---|
| 140 | r = strncmp(a, b, len);
|
---|
| 141 | free(a);
|
---|
| 142 | free(b);
|
---|
| 143 | return r;
|
---|
| 144 | }
|
---|
| 145 | #endif
|
---|
| 146 |
|
---|
[32] | 147 | #ifdef TM_ZONE
|
---|
| 148 | static char* utc = "UTC";
|
---|
| 149 | #endif
|
---|
| 150 | /* RFC-822/RFC-2822 */
|
---|
| 151 | static const char* const nast[] = {
|
---|
| 152 | "EST", "CST", "MST", "PST", "\0\0\0"
|
---|
| 153 | };
|
---|
| 154 | static const char* const nadt[] = {
|
---|
| 155 | "EDT", "CDT", "MDT", "PDT", "\0\0\0"
|
---|
| 156 | };
|
---|
| 157 | static const char* weekday_name[] =
|
---|
| 158 | {
|
---|
| 159 | "Sunday", "Monday", "Tuesday", "Wednesday",
|
---|
| 160 | "Thursday", "Friday", "Saturday"
|
---|
| 161 | };
|
---|
| 162 | static const char* ab_weekday_name[] =
|
---|
| 163 | {
|
---|
| 164 | "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
|
---|
| 165 | };
|
---|
| 166 | static const char* month_name[] =
|
---|
| 167 | {
|
---|
| 168 | "January", "February", "March", "April", "May", "June",
|
---|
| 169 | "July", "August", "September", "October", "November", "December"
|
---|
| 170 | };
|
---|
| 171 | static const char* ab_month_name[] =
|
---|
| 172 | {
|
---|
| 173 | "Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
---|
| 174 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
---|
| 175 | };
|
---|
| 176 | static const char* am_pm[] = {"AM", "PM"};
|
---|
| 177 |
|
---|
| 178 |
|
---|
| 179 | /*
|
---|
| 180 | * Table to determine the ordinal date for the start of a month.
|
---|
| 181 | * Ref: http://en.wikipedia.org/wiki/ISO_week_date
|
---|
| 182 | */
|
---|
| 183 | static const int start_of_month[2][13] = {
|
---|
| 184 | /* non-leap year */
|
---|
| 185 | { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
|
---|
| 186 | /* leap year */
|
---|
| 187 | { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
|
---|
| 188 | };
|
---|
| 189 |
|
---|
| 190 | /*
|
---|
| 191 | * Calculate the week day of the first day of a year. Valid for
|
---|
| 192 | * the Gregorian calendar, which began Sept 14, 1752 in the UK
|
---|
| 193 | * and its colonies. Ref:
|
---|
| 194 | * http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week
|
---|
| 195 | */
|
---|
| 196 |
|
---|
| 197 | static int
|
---|
| 198 | first_wday_of(int yr)
|
---|
| 199 | {
|
---|
| 200 | return ((2 * (3 - (yr / 100) % 4)) + (yr % 100) + ((yr % 100) / 4) +
|
---|
| 201 | (isleap(yr) ? 6 : 0) + 1) % 7;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | #define delim(p) ((p) == '\0' || isspace((unsigned char)(p)))
|
---|
| 205 |
|
---|
| 206 | static int
|
---|
| 207 | fromzone(const unsigned char **bp, struct tm *tm, int mandatory)
|
---|
| 208 | {
|
---|
| 209 | // timezone_t tz;
|
---|
| 210 | char buf[512], *p;
|
---|
| 211 | const unsigned char *rp;
|
---|
| 212 |
|
---|
| 213 | for (p = buf, rp = *bp; !delim(*rp) && p < &buf[sizeof(buf) - 1]; rp++)
|
---|
| 214 | *p++ = *rp;
|
---|
| 215 | *p = '\0';
|
---|
| 216 |
|
---|
| 217 | if (mandatory)
|
---|
| 218 | *bp = rp;
|
---|
| 219 | if (!isalnum((unsigned char)*buf))
|
---|
| 220 | return 0;
|
---|
| 221 | // tz = tzalloc(buf);
|
---|
| 222 | // if (tz == NULL)
|
---|
| 223 | // return 0;
|
---|
| 224 |
|
---|
| 225 | *bp = rp;
|
---|
| 226 | tm->tm_isdst = 0; /* XXX */
|
---|
| 227 | #ifdef TM_GMTOFF
|
---|
| 228 | tm->TM_GMTOFF = tzgetgmtoff(tz, tm->tm_isdst);
|
---|
| 229 | #endif
|
---|
| 230 | #ifdef TM_ZONE
|
---|
| 231 | // Can't use tzgetname() here because we are going to free()
|
---|
| 232 | tm->TM_ZONE = NULL; /* XXX */
|
---|
| 233 | #endif
|
---|
| 234 | // tzfree(tz);
|
---|
| 235 | return 1;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | char* strptime(const char *buf, const char *fmt, struct tm *tm)
|
---|
| 239 | {
|
---|
| 240 | unsigned char c;
|
---|
| 241 | const unsigned char *bp, *ep, *zname;
|
---|
| 242 | int alt_format, i, split_year = 0, neg = 0, state = 0,
|
---|
| 243 | day_offset = -1, week_offset = 0, offs, mandatory;
|
---|
| 244 | const char *new_fmt;
|
---|
| 245 |
|
---|
| 246 | bp = (const unsigned char *)buf;
|
---|
| 247 |
|
---|
| 248 | while (bp != NULL && (c = *fmt++) != '\0') {
|
---|
| 249 | /* Clear `alternate' modifier prior to new conversion. */
|
---|
| 250 | alt_format = 0;
|
---|
| 251 | i = 0;
|
---|
| 252 |
|
---|
| 253 | /* Eat up white-space. */
|
---|
| 254 | if (isspace(c)) {
|
---|
| 255 | while (isspace(*bp))
|
---|
| 256 | bp++;
|
---|
| 257 | continue;
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | if (c != '%')
|
---|
| 261 | goto literal;
|
---|
| 262 |
|
---|
| 263 |
|
---|
| 264 | again: switch (c = *fmt++) {
|
---|
| 265 | case '%': /* "%%" is converted to "%". */
|
---|
| 266 | literal:
|
---|
| 267 | if (c != *bp++)
|
---|
| 268 | return NULL;
|
---|
| 269 | LEGAL_ALT(0);
|
---|
| 270 | continue;
|
---|
| 271 |
|
---|
| 272 | /*
|
---|
| 273 | * "Alternative" modifiers. Just set the appropriate flag
|
---|
| 274 | * and start over again.
|
---|
| 275 | */
|
---|
| 276 | case 'E': /* "%E?" alternative conversion modifier. */
|
---|
| 277 | LEGAL_ALT(0);
|
---|
| 278 | alt_format |= ALT_E;
|
---|
| 279 | goto again;
|
---|
| 280 |
|
---|
| 281 | case 'O': /* "%O?" alternative conversion modifier. */
|
---|
| 282 | LEGAL_ALT(0);
|
---|
| 283 | alt_format |= ALT_O;
|
---|
| 284 | goto again;
|
---|
| 285 |
|
---|
| 286 | /*
|
---|
| 287 | * "Complex" conversion rules, implemented through recursion.
|
---|
| 288 | */
|
---|
| 289 | case 'c': /* Date and time, using the locale's format. */
|
---|
| 290 | // new_fmt = _TIME_LOCALE(loc)->d_t_fmt;
|
---|
| 291 | new_fmt = HERE_D_T_FMT;
|
---|
| 292 | state |= S_WDAY | S_MON | S_MDAY | S_YEAR;
|
---|
| 293 | goto recurse;
|
---|
| 294 |
|
---|
| 295 | case 'F': /* The date as "%Y-%m-%d". */
|
---|
| 296 | new_fmt = "%Y-%m-%d";
|
---|
| 297 | LEGAL_ALT(0);
|
---|
| 298 | state |= S_MON | S_MDAY | S_YEAR;
|
---|
| 299 | goto recurse;
|
---|
| 300 |
|
---|
| 301 | case 'R': /* The time as "%H:%M". */
|
---|
| 302 | new_fmt = "%H:%M";
|
---|
| 303 | LEGAL_ALT(0);
|
---|
| 304 | goto recurse;
|
---|
| 305 |
|
---|
| 306 | case 'r': /* The time in 12-hour clock representation. */
|
---|
| 307 | // new_fmt = _TIME_LOCALE(loc)->t_fmt_ampm;
|
---|
| 308 | new_fmt = HERE_T_FMT_AMPM;
|
---|
| 309 | LEGAL_ALT(0);
|
---|
| 310 | goto recurse;
|
---|
| 311 |
|
---|
| 312 | case 'X': /* The time, using the locale's format. */
|
---|
| 313 | /* fall through */
|
---|
| 314 |
|
---|
| 315 | case 'T': /* The time as "%H:%M:%S". */
|
---|
| 316 | new_fmt = HERE_T_FMT;
|
---|
| 317 | LEGAL_ALT(0);
|
---|
| 318 |
|
---|
| 319 | recurse:
|
---|
| 320 | bp = (const unsigned char *)strptime((const char *)bp,
|
---|
| 321 | new_fmt, tm);
|
---|
| 322 | LEGAL_ALT(ALT_E);
|
---|
| 323 | continue;
|
---|
| 324 |
|
---|
| 325 | case 'x': /* The date, using the locale's format. */
|
---|
| 326 | /* fall throug */
|
---|
| 327 |
|
---|
| 328 | case 'D': /* The date as "%y/%m/%d". */
|
---|
| 329 | {
|
---|
[212] | 330 | int year;
|
---|
[32] | 331 | new_fmt = HERE_D_FMT;
|
---|
| 332 | LEGAL_ALT(0);
|
---|
| 333 | state |= S_MON | S_MDAY | S_YEAR;
|
---|
[212] | 334 | year = split_year ? tm->tm_year : 0;
|
---|
[32] | 335 |
|
---|
| 336 | bp = (const unsigned char *)strptime((const char *)bp,
|
---|
| 337 | new_fmt, tm);
|
---|
| 338 | LEGAL_ALT(ALT_E);
|
---|
| 339 | tm->tm_year += year;
|
---|
| 340 | if (split_year && tm->tm_year % (2000 - TM_YEAR_BASE) <= 68)
|
---|
| 341 | tm->tm_year -= 2000 - TM_YEAR_BASE;
|
---|
| 342 | split_year = 1;
|
---|
| 343 | continue;
|
---|
| 344 | }
|
---|
| 345 | /*
|
---|
| 346 | * "Elementary" conversion rules.
|
---|
| 347 | */
|
---|
| 348 | case 'A': /* The day of week, using the locale's form. */
|
---|
| 349 | case 'a':
|
---|
| 350 | bp = find_string(bp, &tm->tm_wday, weekday_name, ab_weekday_name, 7);
|
---|
| 351 | LEGAL_ALT(0);
|
---|
| 352 | state |= S_WDAY;
|
---|
| 353 | continue;
|
---|
| 354 |
|
---|
| 355 | case 'B': /* The month, using the locale's form. */
|
---|
| 356 | case 'b':
|
---|
| 357 | case 'h':
|
---|
| 358 | bp = find_string(bp, &tm->tm_mon, month_name, ab_month_name, 12);
|
---|
| 359 | LEGAL_ALT(0);
|
---|
| 360 | state |= S_MON;
|
---|
| 361 | continue;
|
---|
| 362 |
|
---|
| 363 | case 'C': /* The century number. */
|
---|
| 364 | i = 20;
|
---|
| 365 | bp = conv_num(bp, &i, 0, 99);
|
---|
| 366 |
|
---|
| 367 | i = i * 100 - TM_YEAR_BASE;
|
---|
| 368 | if (split_year)
|
---|
| 369 | i += tm->tm_year % 100;
|
---|
| 370 | split_year = 1;
|
---|
| 371 | tm->tm_year = i;
|
---|
| 372 | LEGAL_ALT(ALT_E);
|
---|
| 373 | state |= S_YEAR;
|
---|
| 374 | continue;
|
---|
| 375 |
|
---|
| 376 | case 'd': /* The day of month. */
|
---|
| 377 | case 'e':
|
---|
| 378 | bp = conv_num(bp, &tm->tm_mday, 1, 31);
|
---|
| 379 | LEGAL_ALT(ALT_O);
|
---|
| 380 | state |= S_MDAY;
|
---|
| 381 | continue;
|
---|
| 382 |
|
---|
| 383 | case 'k': /* The hour (24-hour clock representation). */
|
---|
| 384 | LEGAL_ALT(0);
|
---|
| 385 | /* FALLTHROUGH */
|
---|
| 386 | case 'H':
|
---|
| 387 | bp = conv_num(bp, &tm->tm_hour, 0, 23);
|
---|
| 388 | LEGAL_ALT(ALT_O);
|
---|
| 389 | state |= S_HOUR;
|
---|
| 390 | continue;
|
---|
| 391 |
|
---|
| 392 | case 'l': /* The hour (12-hour clock representation). */
|
---|
| 393 | LEGAL_ALT(0);
|
---|
| 394 | /* FALLTHROUGH */
|
---|
| 395 | case 'I':
|
---|
| 396 | bp = conv_num(bp, &tm->tm_hour, 1, 12);
|
---|
| 397 | if (tm->tm_hour == 12)
|
---|
| 398 | tm->tm_hour = 0;
|
---|
| 399 | LEGAL_ALT(ALT_O);
|
---|
| 400 | state |= S_HOUR;
|
---|
| 401 | continue;
|
---|
| 402 |
|
---|
| 403 | case 'j': /* The day of year. */
|
---|
| 404 | i = 1;
|
---|
| 405 | bp = conv_num(bp, &i, 1, 366);
|
---|
| 406 | tm->tm_yday = i - 1;
|
---|
| 407 | LEGAL_ALT(0);
|
---|
| 408 | state |= S_YDAY;
|
---|
| 409 | continue;
|
---|
| 410 |
|
---|
| 411 | case 'M': /* The minute. */
|
---|
| 412 | bp = conv_num(bp, &tm->tm_min, 0, 59);
|
---|
| 413 | LEGAL_ALT(ALT_O);
|
---|
| 414 | continue;
|
---|
| 415 |
|
---|
| 416 | case 'm': /* The month. */
|
---|
| 417 | i = 1;
|
---|
| 418 | bp = conv_num(bp, &i, 1, 12);
|
---|
| 419 | tm->tm_mon = i - 1;
|
---|
| 420 | LEGAL_ALT(ALT_O);
|
---|
| 421 | state |= S_MON;
|
---|
| 422 | continue;
|
---|
| 423 |
|
---|
| 424 | case 'p': /* The locale's equivalent of AM/PM. */
|
---|
| 425 | bp = find_string(bp, &i, am_pm, NULL, 2);
|
---|
| 426 | if (HAVE_HOUR(state) && tm->tm_hour > 11)
|
---|
| 427 | return NULL;
|
---|
| 428 | tm->tm_hour += i * 12;
|
---|
| 429 | LEGAL_ALT(0);
|
---|
| 430 | continue;
|
---|
| 431 |
|
---|
| 432 | case 'S': /* The seconds. */
|
---|
| 433 | bp = conv_num(bp, &tm->tm_sec, 0, 61);
|
---|
| 434 | LEGAL_ALT(ALT_O);
|
---|
| 435 | continue;
|
---|
| 436 |
|
---|
| 437 | #ifndef TIME_MAX
|
---|
[342] | 438 | #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__NETWARE__) || defined(__NeXT__)
|
---|
[212] | 439 | #define TIME_MAX INT32_MAX
|
---|
| 440 | #else
|
---|
[32] | 441 | #define TIME_MAX INT64_MAX
|
---|
| 442 | #endif
|
---|
[212] | 443 | #endif
|
---|
[32] | 444 | case 's': /* seconds since the epoch */
|
---|
| 445 | {
|
---|
| 446 | time_t sse = 0;
|
---|
[215] | 447 | #if defined(_MSC_VER) || defined(__BORLANDC__)
|
---|
[212] | 448 | uint32_t rulim = TIME_MAX;
|
---|
| 449 | #else
|
---|
[32] | 450 | uint64_t rulim = TIME_MAX;
|
---|
[212] | 451 | #endif
|
---|
[32] | 452 |
|
---|
| 453 | if (*bp < '0' || *bp > '9') {
|
---|
| 454 | bp = NULL;
|
---|
| 455 | continue;
|
---|
| 456 | }
|
---|
| 457 |
|
---|
| 458 | do {
|
---|
| 459 | sse *= 10;
|
---|
| 460 | sse += *bp++ - '0';
|
---|
| 461 | rulim /= 10;
|
---|
| 462 | } while ((sse * 10 <= TIME_MAX) &&
|
---|
| 463 | rulim && *bp >= '0' && *bp <= '9');
|
---|
[215] | 464 | #if defined(_MSC_VER) || defined(__BORLANDC__)
|
---|
[212] | 465 | if (sse < 0 || (uint32_t)sse > TIME_MAX) {
|
---|
| 466 | #else
|
---|
[32] | 467 | if (sse < 0 || (uint64_t)sse > TIME_MAX) {
|
---|
[212] | 468 | #endif
|
---|
[32] | 469 | bp = NULL;
|
---|
| 470 | continue;
|
---|
| 471 | }
|
---|
[359] | 472 | #if defined(_WIN32) || defined(__OS2__) || defined(__NeXT__) || defined(__DOS__)
|
---|
[343] | 473 | #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__) || defined(__NeXT__)
|
---|
[212] | 474 | if (1)
|
---|
[242] | 475 | #else
|
---|
[32] | 476 | if (localtime_s(tm, &sse) == 0)
|
---|
[212] | 477 | #endif
|
---|
[32] | 478 | #else
|
---|
| 479 | if (localtime_r(&sse, tm))
|
---|
| 480 | #endif
|
---|
| 481 | state |= S_YDAY | S_WDAY | S_MON | S_MDAY | S_YEAR;
|
---|
| 482 | else
|
---|
| 483 | bp = NULL;
|
---|
| 484 | }
|
---|
| 485 | continue;
|
---|
| 486 |
|
---|
| 487 | case 'U': /* The week of year, beginning on sunday. */
|
---|
| 488 | case 'W': /* The week of year, beginning on monday. */
|
---|
| 489 | /*
|
---|
| 490 | * This is bogus, as we can not assume any valid
|
---|
| 491 | * information present in the tm structure at this
|
---|
| 492 | * point to calculate a real value, so save the
|
---|
| 493 | * week for now in case it can be used later.
|
---|
| 494 | */
|
---|
| 495 | bp = conv_num(bp, &i, 0, 53);
|
---|
| 496 | LEGAL_ALT(ALT_O);
|
---|
| 497 | if (c == 'U')
|
---|
| 498 | day_offset = TM_SUNDAY;
|
---|
| 499 | else
|
---|
| 500 | day_offset = TM_MONDAY;
|
---|
| 501 | week_offset = i;
|
---|
| 502 | continue;
|
---|
| 503 |
|
---|
| 504 | case 'w': /* The day of week, beginning on sunday. */
|
---|
| 505 | bp = conv_num(bp, &tm->tm_wday, 0, 6);
|
---|
| 506 | LEGAL_ALT(ALT_O);
|
---|
| 507 | state |= S_WDAY;
|
---|
| 508 | continue;
|
---|
| 509 |
|
---|
| 510 | case 'u': /* The day of week, monday = 1. */
|
---|
| 511 | bp = conv_num(bp, &i, 1, 7);
|
---|
| 512 | tm->tm_wday = i % 7;
|
---|
| 513 | LEGAL_ALT(ALT_O);
|
---|
| 514 | state |= S_WDAY;
|
---|
| 515 | continue;
|
---|
| 516 |
|
---|
| 517 | case 'g': /* The year corresponding to the ISO week
|
---|
| 518 | * number but without the century.
|
---|
| 519 | */
|
---|
| 520 | bp = conv_num(bp, &i, 0, 99);
|
---|
| 521 | continue;
|
---|
| 522 |
|
---|
| 523 | case 'G': /* The year corresponding to the ISO week
|
---|
| 524 | * number with century.
|
---|
| 525 | */
|
---|
| 526 | do
|
---|
| 527 | bp++;
|
---|
| 528 | while (isdigit(*bp));
|
---|
| 529 | continue;
|
---|
| 530 |
|
---|
| 531 | case 'V': /* The ISO 8601:1988 week number as decimal */
|
---|
| 532 | bp = conv_num(bp, &i, 0, 53);
|
---|
| 533 | continue;
|
---|
| 534 |
|
---|
| 535 | case 'Y': /* The year. */
|
---|
| 536 | i = TM_YEAR_BASE; /* just for data sanity... */
|
---|
| 537 | bp = conv_num(bp, &i, 0, 9999);
|
---|
| 538 | tm->tm_year = i - TM_YEAR_BASE;
|
---|
| 539 | LEGAL_ALT(ALT_E);
|
---|
| 540 | state |= S_YEAR;
|
---|
| 541 | continue;
|
---|
| 542 |
|
---|
| 543 | case 'y': /* The year within 100 years of the epoch. */
|
---|
| 544 | /* LEGAL_ALT(ALT_E | ALT_O); */
|
---|
| 545 | bp = conv_num(bp, &i, 0, 99);
|
---|
| 546 |
|
---|
| 547 | if (split_year)
|
---|
| 548 | /* preserve century */
|
---|
| 549 | i += (tm->tm_year / 100) * 100;
|
---|
| 550 | else {
|
---|
| 551 | split_year = 1;
|
---|
| 552 | if (i <= 68)
|
---|
| 553 | i = i + 2000 - TM_YEAR_BASE;
|
---|
| 554 | }
|
---|
| 555 | tm->tm_year = i;
|
---|
| 556 | state |= S_YEAR;
|
---|
| 557 | continue;
|
---|
| 558 |
|
---|
| 559 | case 'Z': // time zone name
|
---|
| 560 | case 'z': //
|
---|
| 561 | #ifdef _WIN32
|
---|
| 562 | _tzset();
|
---|
| 563 | #else
|
---|
| 564 | tzset();
|
---|
| 565 | #endif
|
---|
| 566 | mandatory = c == 'z';
|
---|
| 567 | /*
|
---|
| 568 | * We recognize all ISO 8601 formats:
|
---|
| 569 | * Z = Zulu time/UTC
|
---|
| 570 | * [+-]hhmm
|
---|
| 571 | * [+-]hh:mm
|
---|
| 572 | * [+-]hh
|
---|
| 573 | * We recognize all RFC-822/RFC-2822 formats:
|
---|
| 574 | * UT|GMT
|
---|
| 575 | * North American : UTC offsets
|
---|
| 576 | * E[DS]T = Eastern : -4 | -5
|
---|
| 577 | * C[DS]T = Central : -5 | -6
|
---|
| 578 | * M[DS]T = Mountain: -6 | -7
|
---|
| 579 | * P[DS]T = Pacific : -7 | -8
|
---|
| 580 | * Nautical/Military
|
---|
| 581 | * [A-IL-M] = -1 ... -9 (J not used)
|
---|
| 582 | * [N-Y] = +1 ... +12
|
---|
| 583 | * Note: J maybe used to denote non-nautical
|
---|
| 584 | * local time
|
---|
| 585 | */
|
---|
| 586 | if (mandatory)
|
---|
| 587 | while (isspace(*bp))
|
---|
| 588 | bp++;
|
---|
| 589 |
|
---|
| 590 | zname = bp;
|
---|
| 591 | switch (*bp++) {
|
---|
| 592 | case 'G':
|
---|
| 593 | if (*bp++ != 'M')
|
---|
| 594 | goto namedzone;
|
---|
| 595 | /*FALLTHROUGH*/
|
---|
| 596 | case 'U':
|
---|
| 597 | if (*bp++ != 'T')
|
---|
| 598 | goto namedzone;
|
---|
| 599 | else if (!delim(*bp) && *bp++ != 'C')
|
---|
| 600 | goto namedzone;
|
---|
| 601 | /*FALLTHROUGH*/
|
---|
| 602 | case 'Z':
|
---|
| 603 | if (!delim(*bp))
|
---|
| 604 | goto namedzone;
|
---|
| 605 | tm->tm_isdst = 0;
|
---|
| 606 | #ifdef TM_GMTOFF
|
---|
| 607 | tm->TM_GMTOFF = 0;
|
---|
| 608 | #endif
|
---|
| 609 | #ifdef TM_ZONE
|
---|
| 610 | tm->TM_ZONE = utc;
|
---|
| 611 | #endif
|
---|
| 612 | continue;
|
---|
| 613 | case '+':
|
---|
| 614 | neg = 0;
|
---|
| 615 | break;
|
---|
| 616 | case '-':
|
---|
| 617 | neg = 1;
|
---|
| 618 | break;
|
---|
| 619 | default:
|
---|
| 620 | namedzone:
|
---|
| 621 | bp = zname;
|
---|
| 622 |
|
---|
| 623 | /* Nautical / Military style */
|
---|
| 624 | if (delim(bp[1]) &&
|
---|
| 625 | ((*bp >= 'A' && *bp <= 'I') ||
|
---|
| 626 | (*bp >= 'L' && *bp <= 'Y'))) {
|
---|
| 627 | #ifdef TM_GMTOFF
|
---|
| 628 | /* Argh! No 'J'! */
|
---|
| 629 | if (*bp >= 'A' && *bp <= 'I')
|
---|
| 630 | tm->TM_GMTOFF =
|
---|
| 631 | (int)*bp - ('A' - 1);
|
---|
| 632 | else if (*bp >= 'L' && *bp <= 'M')
|
---|
| 633 | tm->TM_GMTOFF = (int)*bp - 'A';
|
---|
| 634 | else if (*bp >= 'N' && *bp <= 'Y')
|
---|
| 635 | tm->TM_GMTOFF = 'M' - (int)*bp;
|
---|
| 636 | tm->TM_GMTOFF *= SECSPERHOUR;
|
---|
| 637 | #endif
|
---|
| 638 | #ifdef TM_ZONE
|
---|
| 639 | tm->TM_ZONE = NULL; /* XXX */
|
---|
| 640 | #endif
|
---|
| 641 | bp++;
|
---|
| 642 | continue;
|
---|
| 643 | }
|
---|
| 644 | /* 'J' is local time */
|
---|
| 645 | if (delim(bp[1]) && *bp == 'J') {
|
---|
| 646 | #ifdef TM_GMTOFF
|
---|
| 647 | tm->TM_GMTOFF = -timezone;
|
---|
| 648 | #endif
|
---|
| 649 | #ifdef TM_ZONE
|
---|
| 650 | tm->TM_ZONE = NULL; /* XXX */
|
---|
| 651 | #endif
|
---|
| 652 | bp++;
|
---|
| 653 | continue;
|
---|
| 654 | }
|
---|
| 655 |
|
---|
| 656 | /*
|
---|
| 657 | * From our 3 letter hard-coded table
|
---|
| 658 | * XXX: Can be removed, handled by tzload()
|
---|
| 659 | */
|
---|
| 660 | if (delim(bp[0]) || delim(bp[1]) ||
|
---|
| 661 | delim(bp[2]) || !delim(bp[3]))
|
---|
| 662 | goto loadzone;
|
---|
| 663 | ep = find_string(bp, &i, nast, NULL, 4);
|
---|
| 664 | if (ep != NULL) {
|
---|
| 665 | #ifdef TM_GMTOFF
|
---|
| 666 | tm->TM_GMTOFF = (-5 - i) * SECSPERHOUR;
|
---|
| 667 | #endif
|
---|
| 668 | #ifdef TM_ZONE
|
---|
| 669 | tm->TM_ZONE = __UNCONST(nast[i]);
|
---|
| 670 | #endif
|
---|
| 671 | bp = ep;
|
---|
| 672 | continue;
|
---|
| 673 | }
|
---|
| 674 | ep = find_string(bp, &i, nadt, NULL, 4);
|
---|
| 675 | if (ep != NULL) {
|
---|
| 676 | tm->tm_isdst = 1;
|
---|
| 677 | #ifdef TM_GMTOFF
|
---|
| 678 | tm->TM_GMTOFF = (-4 - i) * SECSPERHOUR;
|
---|
| 679 | #endif
|
---|
| 680 | #ifdef TM_ZONE
|
---|
| 681 | tm->TM_ZONE = __UNCONST(nadt[i]);
|
---|
| 682 | #endif
|
---|
| 683 | bp = ep;
|
---|
| 684 | continue;
|
---|
| 685 | }
|
---|
| 686 | /*
|
---|
| 687 | * Our current timezone
|
---|
| 688 | */
|
---|
| 689 | ep = find_string(bp, &i,
|
---|
[44] | 690 | (const char * const *)NULL,
|
---|
[32] | 691 | NULL, 2);
|
---|
| 692 | if (ep != NULL) {
|
---|
| 693 | tm->tm_isdst = i;
|
---|
| 694 | #ifdef TM_GMTOFF
|
---|
| 695 | tm->TM_GMTOFF = -timezone;
|
---|
| 696 | #endif
|
---|
| 697 | #ifdef TM_ZONE
|
---|
| 698 | tm->TM_ZONE = tzname[i];
|
---|
| 699 | #endif
|
---|
| 700 | bp = ep;
|
---|
| 701 | continue;
|
---|
| 702 | }
|
---|
| 703 | loadzone:
|
---|
| 704 | /*
|
---|
| 705 | * The hard way, load the zone!
|
---|
| 706 | */
|
---|
| 707 | if (fromzone(&bp, tm, mandatory))
|
---|
| 708 | continue;
|
---|
| 709 | goto out;
|
---|
| 710 | }
|
---|
| 711 | offs = 0;
|
---|
| 712 | for (i = 0; i < 4; ) {
|
---|
| 713 | if (isdigit(*bp)) {
|
---|
| 714 | offs = offs * 10 + (*bp++ - '0');
|
---|
| 715 | i++;
|
---|
| 716 | continue;
|
---|
| 717 | }
|
---|
| 718 | if (i == 2 && *bp == ':') {
|
---|
| 719 | bp++;
|
---|
| 720 | continue;
|
---|
| 721 | }
|
---|
| 722 | break;
|
---|
| 723 | }
|
---|
| 724 | if (isdigit(*bp))
|
---|
| 725 | goto out;
|
---|
| 726 | switch (i) {
|
---|
| 727 | case 2:
|
---|
| 728 | offs *= SECSPERHOUR;
|
---|
| 729 | break;
|
---|
| 730 | case 4:
|
---|
| 731 | i = offs % 100;
|
---|
| 732 | offs /= 100;
|
---|
| 733 | if (i >= SECSPERMIN)
|
---|
| 734 | goto out;
|
---|
| 735 | /* Convert minutes into decimal */
|
---|
| 736 | offs = offs * SECSPERHOUR + i * SECSPERMIN;
|
---|
| 737 | break;
|
---|
| 738 | default:
|
---|
| 739 | out:
|
---|
| 740 | if (mandatory)
|
---|
| 741 | return NULL;
|
---|
| 742 | bp = zname;
|
---|
| 743 | continue;
|
---|
| 744 | }
|
---|
| 745 | /* ISO 8601 & RFC 3339 limit to 23:59 max */
|
---|
| 746 | if (offs >= (HOURSPERDAY * SECSPERHOUR))
|
---|
| 747 | goto out;
|
---|
| 748 | if (neg)
|
---|
| 749 | offs = -offs;
|
---|
| 750 | tm->tm_isdst = 0; /* XXX */
|
---|
| 751 | #ifdef TM_GMTOFF
|
---|
| 752 | tm->TM_GMTOFF = offs;
|
---|
| 753 | #endif
|
---|
| 754 | #ifdef TM_ZONE
|
---|
| 755 | tm->TM_ZONE = NULL; /* XXX */
|
---|
| 756 | #endif
|
---|
| 757 | continue;
|
---|
| 758 |
|
---|
| 759 | /*
|
---|
| 760 | * Miscellaneous conversions.
|
---|
| 761 | */
|
---|
| 762 | case 'n': /* Any kind of white-space. */
|
---|
| 763 | case 't':
|
---|
| 764 | while (isspace(*bp))
|
---|
| 765 | bp++;
|
---|
| 766 | LEGAL_ALT(0);
|
---|
| 767 | continue;
|
---|
| 768 |
|
---|
| 769 |
|
---|
| 770 | default: /* Unknown/unsupported conversion. */
|
---|
| 771 | return NULL;
|
---|
| 772 | }
|
---|
| 773 | }
|
---|
| 774 |
|
---|
| 775 | if (!HAVE_YDAY(state) && HAVE_YEAR(state)) {
|
---|
| 776 | if (HAVE_MON(state) && HAVE_MDAY(state)) {
|
---|
| 777 | /* calculate day of year (ordinal date) */
|
---|
| 778 | tm->tm_yday = start_of_month[isleap_sum(tm->tm_year,
|
---|
| 779 | TM_YEAR_BASE)][tm->tm_mon] + (tm->tm_mday - 1);
|
---|
| 780 | state |= S_YDAY;
|
---|
| 781 | } else if (day_offset != -1) {
|
---|
| 782 | /*
|
---|
| 783 | * Set the date to the first Sunday (or Monday)
|
---|
| 784 | * of the specified week of the year.
|
---|
| 785 | */
|
---|
| 786 | if (!HAVE_WDAY(state)) {
|
---|
| 787 | tm->tm_wday = day_offset;
|
---|
| 788 | state |= S_WDAY;
|
---|
| 789 | }
|
---|
| 790 | tm->tm_yday = (7 -
|
---|
| 791 | first_wday_of(tm->tm_year + TM_YEAR_BASE) +
|
---|
| 792 | day_offset) % 7 + (week_offset - 1) * 7 +
|
---|
| 793 | tm->tm_wday - day_offset;
|
---|
| 794 | state |= S_YDAY;
|
---|
| 795 | }
|
---|
| 796 | }
|
---|
| 797 |
|
---|
| 798 | if (HAVE_YDAY(state) && HAVE_YEAR(state)) {
|
---|
| 799 | int isleap;
|
---|
| 800 |
|
---|
| 801 | if (!HAVE_MON(state)) {
|
---|
| 802 | /* calculate month of day of year */
|
---|
| 803 | i = 0;
|
---|
| 804 | isleap = isleap_sum(tm->tm_year, TM_YEAR_BASE);
|
---|
| 805 | while (tm->tm_yday >= start_of_month[isleap][i])
|
---|
| 806 | i++;
|
---|
| 807 | if (i > 12) {
|
---|
| 808 | i = 1;
|
---|
| 809 | tm->tm_yday -= start_of_month[isleap][12];
|
---|
| 810 | tm->tm_year++;
|
---|
| 811 | }
|
---|
| 812 | tm->tm_mon = i - 1;
|
---|
| 813 | state |= S_MON;
|
---|
| 814 | }
|
---|
| 815 |
|
---|
| 816 | if (!HAVE_MDAY(state)) {
|
---|
| 817 | /* calculate day of month */
|
---|
| 818 | isleap = isleap_sum(tm->tm_year, TM_YEAR_BASE);
|
---|
| 819 | tm->tm_mday = tm->tm_yday -
|
---|
| 820 | start_of_month[isleap][tm->tm_mon] + 1;
|
---|
| 821 | state |= S_MDAY;
|
---|
| 822 | }
|
---|
| 823 |
|
---|
| 824 | if (!HAVE_WDAY(state)) {
|
---|
| 825 | /* calculate day of week */
|
---|
| 826 | i = 0;
|
---|
| 827 | week_offset = first_wday_of(tm->tm_year);
|
---|
| 828 | while (i++ <= tm->tm_yday) {
|
---|
| 829 | if (week_offset++ >= 6)
|
---|
| 830 | week_offset = 0;
|
---|
| 831 | }
|
---|
| 832 | tm->tm_wday = week_offset;
|
---|
| 833 | state |= S_WDAY;
|
---|
| 834 | }
|
---|
| 835 | }
|
---|
| 836 |
|
---|
| 837 | return (char*)bp;
|
---|
| 838 | }
|
---|
| 839 |
|
---|
| 840 |
|
---|
| 841 | static const unsigned char *
|
---|
| 842 | conv_num(const unsigned char *buf, int *dest, unsigned int llim, unsigned int ulim)
|
---|
| 843 | {
|
---|
| 844 | unsigned int result = 0;
|
---|
| 845 | unsigned char ch;
|
---|
| 846 |
|
---|
| 847 | /* The limit also determines the number of valid digits. */
|
---|
| 848 | unsigned int rulim = ulim;
|
---|
| 849 |
|
---|
| 850 | ch = *buf;
|
---|
| 851 | if (ch < '0' || ch > '9')
|
---|
| 852 | return NULL;
|
---|
| 853 |
|
---|
| 854 | do {
|
---|
| 855 | result *= 10;
|
---|
| 856 | result += ch - '0';
|
---|
| 857 | rulim /= 10;
|
---|
| 858 | ch = *++buf;
|
---|
| 859 | } while ((result <= ulim) && rulim && ch >= '0' && ch <= '9');
|
---|
| 860 |
|
---|
| 861 | if (result < llim || result > ulim)
|
---|
| 862 | return NULL;
|
---|
| 863 |
|
---|
| 864 | *dest = result;
|
---|
| 865 | return buf;
|
---|
| 866 | }
|
---|
| 867 |
|
---|
| 868 | static const unsigned char *
|
---|
| 869 | find_string(const unsigned char *bp, int *tgt, const char * const *n1,
|
---|
| 870 | const char * const *n2, int c)
|
---|
| 871 | {
|
---|
| 872 | int i;
|
---|
| 873 | size_t len;
|
---|
| 874 |
|
---|
| 875 | /* check full name - then abbreviated ones */
|
---|
| 876 | for (; n1 != NULL; n1 = n2, n2 = NULL) {
|
---|
| 877 | for (i = 0; i < c; i++, n1++) {
|
---|
| 878 | len = strlen(*n1);
|
---|
| 879 | if (strncasecmp(*n1, (const char *)bp, len) == 0) {
|
---|
| 880 | *tgt = i;
|
---|
| 881 | return bp + len;
|
---|
| 882 | }
|
---|
| 883 | }
|
---|
| 884 | }
|
---|
| 885 |
|
---|
| 886 | /* Nothing matched */
|
---|
| 887 | return NULL;
|
---|
| 888 | }
|
---|
[34] | 889 |
|
---|
| 890 | #endif
|
---|