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