1 /*
2 * Copyright (c) 2002-2018 Balabit
3 * Copyright (c) 1998-2018 Balázs Scheidler
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * As an additional exemption you are allowed to compile & link against the
20 * OpenSSL libraries as published by the OpenSSL project. See the file
21 * COPYING for details.
22 *
23 */
24 /* $NetBSD: strptime.c,v 1.49 2015/10/09 17:21:45 christos Exp $ */
25
26 /*-
27 * Copyright (c) 1997, 1998, 2005, 2008 The NetBSD Foundation, Inc.
28 * All rights reserved.
29 *
30 * This code was contributed to The NetBSD Foundation by Klaus Klein.
31 * Heavily optimised by David Laight
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 *
42 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
43 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
44 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
45 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
46 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
47 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
48 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
49 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
50 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
52 * POSSIBILITY OF SUCH DAMAGE.
53 */
54
55 /*
56 * MIT: musl http://git.musl-libc.org/cgit/musl/tree/src/time/strftime.c?id=6ad514e4e278f0c3b18eb2db1d45638c9af1c07f#n19
57 * The license below only applies for the ISO8601 week calculation in this file.
58 *
59 */
60 /*
61 *
62 * Copyright © 2005-2019 Rich Felker, et al.
63 *
64 * Permission is hereby granted, free of charge, to any person obtaining
65 * a copy of this software and associated documentation files (the
66 * "Software"), to deal in the Software without restriction, including
67 * without limitation the rights to use, copy, modify, merge, publish,
68 * distribute, sublicense, and/or sell copies of the Software, and to
69 * permit persons to whom the Software is furnished to do so, subject to
70 * the following conditions:
71 *
72 * The above copyright notice and this permission notice shall be
73 * included in all copies or substantial portions of the Software.
74 *
75 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
76 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
77 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
78 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
79 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
80 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
81 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
82 */
83
84 #include <ctype.h>
85 #include <stdint.h>
86 #include <string.h>
87
88 #include "timeutils/wallclocktime.h"
89 #include "timeutils/unixtime.h"
90 #include "timeutils/cache.h"
91 #include "timeutils/misc.h"
92
93 void
wall_clock_time_unset(WallClockTime * self)94 wall_clock_time_unset(WallClockTime *self)
95 {
96 WallClockTime val = WALL_CLOCK_TIME_INIT;
97 *self = val;
98 }
99
100 #define __UNCONST(a) ((void *)(unsigned long)(const void *)(a))
101
102 #define TM_YEAR_BASE 1900
103 #define TM_SUNDAY 0
104 #define TM_MONDAY 1
105 #define TM_TUESDAY 2
106 #define TM_WEDNESDAY 3
107 #define TM_THURSDAY 4
108 #define TM_FRIDAY 5
109 #define TM_SATURDAY 6
110
111 #define TM_JANUARY 0
112 #define TM_FEBRUARY 1
113 #define TM_MARCH 2
114 #define TM_APRIL 3
115 #define TM_MAY 4
116 #define TM_JUNE 5
117 #define TM_JULY 6
118 #define TM_AUGUST 7
119 #define TM_SEPTEMBER 8
120 #define TM_OCTOBER 9
121 #define TM_NOVEMBER 10
122 #define TM_DECEMBER 11
123
124 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
125 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
126
127 static gboolean
_is_jan1_3_days_past_monday(WallClockTime * wct)128 _is_jan1_3_days_past_monday(WallClockTime *wct)
129 {
130 return ((wct->wct_wday + 371 - wct->wct_yday - 2) % 7 <= 2);
131 }
132
133 static gint
_get_dec31_day_of_week(WallClockTime * wct)134 _get_dec31_day_of_week(WallClockTime *wct)
135 {
136 return (wct->wct_wday + 7 - wct->wct_yday - 1) % 7;
137 }
138
139 static gint
_get_jan1_day_of_week(WallClockTime * wct)140 _get_jan1_day_of_week(WallClockTime *wct)
141 {
142 return (wct->wct_wday + 371 - wct->wct_yday) % 7;
143 }
144
145 guint32
wall_clock_time_iso_week_number(WallClockTime * wct)146 wall_clock_time_iso_week_number(WallClockTime *wct)
147 {
148 gint week_number = (wct->wct_yday - (wct->wct_wday - 1 + 7) % 7 + 7 ) / 7;
149
150 if (_is_jan1_3_days_past_monday(wct))
151 week_number++;
152
153 if (week_number == 0)
154 {
155 gint last_dec31 = _get_dec31_day_of_week(wct);
156 if (last_dec31 == 4 || (last_dec31 == 5 && isleap(wct->wct_year-1)))
157 return 53;
158 return 52;
159 }
160
161 if (week_number == 53)
162 {
163 int jan1 = _get_jan1_day_of_week(wct);
164 if (jan1 != 4 && (jan1 != 3 || !isleap(wct->wct_year)))
165 return 1;
166 }
167
168 return week_number;
169 }
170
171 typedef struct
172 {
173 const char *abday[7];
174 const char *day[7];
175 const char *abmon[12];
176 const char *mon[12];
177 const char *am_pm[2];
178 const char *d_t_fmt;
179 const char *d_fmt;
180 const char *t_fmt;
181 const char *t_fmt_ampm;
182 } _TimeLocale;
183
184 static const _TimeLocale _DefaultTimeLocale =
185 {
186 /* abbreviated day */
187 {
188 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
189 },
190 /* days */
191 {
192 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
193 "Friday", "Saturday"
194 },
195 /* abbreviated month */
196 {
197 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
198 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
199 },
200 /* month */
201 {
202 "January", "February", "March", "April", "May", "June", "July",
203 "August", "September", "October", "November", "December"
204 },
205 /* ampm */
206 {
207 "AM", "PM"
208 },
209 "%a %b %e %H:%M:%S %Y",
210 "%m/%d/%y",
211 "%H:%M:%S",
212 "%I:%M:%S %p"
213 };
214
215 #define _TIME_LOCALE(loc) \
216 (&_DefaultTimeLocale)
217
218 static const unsigned char *conv_num(const unsigned char *, int *, unsigned int, unsigned int);
219 static const unsigned char *find_string(const unsigned char *, int *, const char *const *,
220 const char *const *, int);
221 static int first_wday_of(int yr);
222
223
224 /*
225 * We do not implement alternate representations. However, we always
226 * check whether a given modifier is allowed for a certain conversion.
227 */
228 #define ALT_E 0x01
229 #define ALT_O 0x02
230 #define LEGAL_ALT(x) { if (alt_format & ~(x)) return NULL; }
231
232 #define S_YEAR (1 << 0)
233 #define S_MON (1 << 1)
234 #define S_YDAY (1 << 2)
235 #define S_MDAY (1 << 3)
236 #define S_WDAY (1 << 4)
237 #define S_HOUR (1 << 5)
238 #define S_USEC (1 << 6)
239
240 #define HAVE_MDAY(s) (s & S_MDAY)
241 #define HAVE_MON(s) (s & S_MON)
242 #define HAVE_WDAY(s) (s & S_WDAY)
243 #define HAVE_YDAY(s) (s & S_YDAY)
244 #define HAVE_YEAR(s) (s & S_YEAR)
245 #define HAVE_HOUR(s) (s & S_HOUR)
246 #define HAVE_USEC(s) (s & S_USEC)
247
248 static char utc[] = { "UTC" };
249 /* RFC-822/RFC-2822 */
250 static const char *const nast[5] =
251 {
252 "EST", "CST", "MST", "PST", "\0\0\0"
253 };
254 static const char *const nadt[5] =
255 {
256 "EDT", "CDT", "MDT", "PDT", "\0\0\0"
257 };
258
259 /*
260 * Table to determine the ordinal date for the start of a month.
261 * Ref: http://en.wikipedia.org/wiki/ISO_week_date
262 */
263 static const int start_of_month[2][13] =
264 {
265 /* non-leap year */
266 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
267 /* leap year */
268 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
269 };
270
271 gchar *
wall_clock_time_strptime(WallClockTime * wct,const gchar * format,const gchar * input)272 wall_clock_time_strptime(WallClockTime *wct, const gchar *format, const gchar *input)
273 {
274 unsigned char c;
275 const unsigned char *bp, *ep, *zname;
276 int alt_format, i, split_year = 0, neg = 0, state = 0,
277 day_offset = -1, week_offset = 0, offs, mandatory;
278 const char *new_fmt;
279 const char *const *system_tznames;
280
281 bp = (const unsigned char *)input;
282
283 while (bp != NULL && (c = *format++) != '\0')
284 {
285 /* Clear `alternate' modifier prior to new conversion. */
286 alt_format = 0;
287 i = 0;
288
289 /* Eat up white-space. */
290 if (isspace(c))
291 {
292 while (isspace(*bp))
293 bp++;
294 continue;
295 }
296
297 if (c != '%')
298 goto literal;
299
300
301 again:
302 switch (c = *format++)
303 {
304 case '%': /* "%%" is converted to "%". */
305 literal:
306 if (c != *bp++)
307 return NULL;
308 LEGAL_ALT(0);
309 continue;
310
311 /*
312 * "Alternative" modifiers. Just set the appropriate flag
313 * and start over again.
314 */
315 case 'E': /* "%E?" alternative conversion modifier. */
316 LEGAL_ALT(0);
317 alt_format |= ALT_E;
318 goto again;
319
320 case 'O': /* "%O?" alternative conversion modifier. */
321 LEGAL_ALT(0);
322 alt_format |= ALT_O;
323 goto again;
324
325 /*
326 * "Complex" conversion rules, implemented through recursion.
327 */
328 case 'c': /* Date and time, using the locale's format. */
329 new_fmt = _TIME_LOCALE(loc)->d_t_fmt;
330 state |= S_WDAY | S_MON | S_MDAY | S_YEAR;
331 goto recurse;
332
333 case 'D': /* The date as "%m/%d/%y". */
334 new_fmt = "%m/%d/%y";
335 LEGAL_ALT(0);
336 state |= S_MON | S_MDAY | S_YEAR;
337 goto recurse;
338
339 case 'F': /* The date as "%Y-%m-%d". */
340 new_fmt = "%Y-%m-%d";
341 LEGAL_ALT(0);
342 state |= S_MON | S_MDAY | S_YEAR;
343 goto recurse;
344
345 case 'R': /* The time as "%H:%M". */
346 new_fmt = "%H:%M";
347 LEGAL_ALT(0);
348 goto recurse;
349
350 case 'r': /* The time in 12-hour clock representation. */
351 new_fmt = _TIME_LOCALE(loc)->t_fmt_ampm;
352 LEGAL_ALT(0);
353 goto recurse;
354
355 case 'T': /* The time as "%H:%M:%S". */
356 new_fmt = "%H:%M:%S";
357 LEGAL_ALT(0);
358 goto recurse;
359
360 case 'X': /* The time, using the locale's format. */
361 new_fmt = _TIME_LOCALE(loc)->t_fmt;
362 goto recurse;
363
364 case 'x': /* The date, using the locale's format. */
365 new_fmt = _TIME_LOCALE(loc)->d_fmt;
366 state |= S_MON | S_MDAY | S_YEAR;
367 recurse:
368 bp = (const unsigned char *)wall_clock_time_strptime(wct, new_fmt, (const char *)bp);
369 LEGAL_ALT(ALT_E);
370 continue;
371
372 /*
373 * "Elementary" conversion rules.
374 */
375 case 'A': /* The day of week, using the locale's form. */
376 case 'a':
377 bp = find_string(bp, &wct->tm.tm_wday,
378 _TIME_LOCALE(loc)->day, _TIME_LOCALE(loc)->abday, 7);
379 LEGAL_ALT(0);
380 state |= S_WDAY;
381 continue;
382
383 case 'B': /* The month, using the locale's form. */
384 case 'b':
385 case 'h':
386 bp = find_string(bp, &wct->tm.tm_mon,
387 _TIME_LOCALE(loc)->mon, _TIME_LOCALE(loc)->abmon,
388 12);
389 LEGAL_ALT(0);
390 state |= S_MON;
391 continue;
392
393 case 'C': /* The century number. */
394 i = 20;
395 bp = conv_num(bp, &i, 0, 99);
396
397 i = i * 100 - TM_YEAR_BASE;
398 if (split_year)
399 i += wct->tm.tm_year % 100;
400 split_year = 1;
401 wct->tm.tm_year = i;
402 LEGAL_ALT(ALT_E);
403 state |= S_YEAR;
404 continue;
405
406 case 'd': /* The day of month. */
407 case 'e':
408 bp = conv_num(bp, &wct->tm.tm_mday, 1, 31);
409 LEGAL_ALT(ALT_O);
410 state |= S_MDAY;
411 continue;
412
413 case 'f':
414 {
415 const unsigned char *end = conv_num(bp, &wct->wct_usec, 0, 999999);
416 if (!end)
417 return NULL;
418 int digits = end - bp;
419
420 /*
421 * We have read up to 6 digits, but if the message has
422 * sub-microsecond precision, eat-up the digits we cannot handle.
423 */
424 while (isdigit(*end))
425 {
426 end++;
427 }
428
429 /*
430 * If we read less than 6 digits, we need to adjust the value:
431 * "012" was parsed as 12 but is 12000 us.
432 */
433 while (digits++ < 6)
434 {
435 wct->wct_usec *= 10;
436 }
437
438 bp = end;
439 LEGAL_ALT(0);
440 state |= S_USEC;
441 continue;
442 }
443
444 case 'k': /* The hour (24-hour clock representation). */
445 LEGAL_ALT(0);
446 /* FALLTHROUGH */
447 case 'H':
448 bp = conv_num(bp, &wct->tm.tm_hour, 0, 23);
449 LEGAL_ALT(ALT_O);
450 state |= S_HOUR;
451 continue;
452
453 case 'l': /* The hour (12-hour clock representation). */
454 LEGAL_ALT(0);
455 /* FALLTHROUGH */
456 case 'I':
457 bp = conv_num(bp, &wct->tm.tm_hour, 1, 12);
458 if (wct->tm.tm_hour == 12)
459 wct->tm.tm_hour = 0;
460 LEGAL_ALT(ALT_O);
461 state |= S_HOUR;
462 continue;
463
464 case 'j': /* The day of year. */
465 i = 1;
466 bp = conv_num(bp, &i, 1, 366);
467 wct->tm.tm_yday = i - 1;
468 LEGAL_ALT(0);
469 state |= S_YDAY;
470 continue;
471
472 case 'M': /* The minute. */
473 bp = conv_num(bp, &wct->tm.tm_min, 0, 59);
474 LEGAL_ALT(ALT_O);
475 continue;
476
477 case 'm': /* The month. */
478 i = 1;
479 bp = conv_num(bp, &i, 1, 12);
480 wct->tm.tm_mon = i - 1;
481 LEGAL_ALT(ALT_O);
482 state |= S_MON;
483 continue;
484
485 case 'p': /* The locale's equivalent of AM/PM. */
486 bp = find_string(bp, &i, _TIME_LOCALE(loc)->am_pm,
487 NULL, 2);
488 if (HAVE_HOUR(state) && wct->tm.tm_hour > 11)
489 return NULL;
490 wct->tm.tm_hour += i * 12;
491 LEGAL_ALT(0);
492 continue;
493
494 case 'S': /* The seconds. */
495 bp = conv_num(bp, &wct->tm.tm_sec, 0, 61);
496 LEGAL_ALT(ALT_O);
497 continue;
498
499 #ifndef TIME_MAX
500 #define TIME_MAX INT64_MAX
501 #endif
502 case 's': /* seconds since the epoch */
503 {
504 time_t sse = 0;
505 uint64_t rulim = TIME_MAX;
506
507 if (*bp < '0' || *bp > '9')
508 {
509 bp = NULL;
510 continue;
511 }
512
513 do
514 {
515 sse *= 10;
516 sse += *bp++ - '0';
517 rulim /= 10;
518 }
519 while ((sse * 10 <= TIME_MAX) &&
520 rulim && *bp >= '0' && *bp <= '9');
521
522 if (sse < 0 || (uint64_t)sse > TIME_MAX)
523 {
524 bp = NULL;
525 continue;
526 }
527
528 cached_localtime(&sse, &wct->tm);
529 state |= S_YDAY | S_WDAY |
530 S_MON | S_MDAY | S_YEAR;
531 }
532 continue;
533
534 case 'U': /* The week of year, beginning on sunday. */
535 case 'W': /* The week of year, beginning on monday. */
536 /*
537 * XXX This is bogus, as we can not assume any valid
538 * information present in the tm structure at this
539 * point to calculate a real value, so just check the
540 * range for now.
541 */
542 bp = conv_num(bp, &i, 0, 53);
543 LEGAL_ALT(ALT_O);
544 if (c == 'U')
545 day_offset = TM_SUNDAY;
546 else
547 day_offset = TM_MONDAY;
548 week_offset = i;
549 continue;
550
551 case 'w': /* The day of week, beginning on sunday. */
552 bp = conv_num(bp, &wct->tm.tm_wday, 0, 6);
553 LEGAL_ALT(ALT_O);
554 state |= S_WDAY;
555 continue;
556
557 case 'u': /* The day of week, monday = 1. */
558 bp = conv_num(bp, &i, 1, 7);
559 wct->tm.tm_wday = i % 7;
560 LEGAL_ALT(ALT_O);
561 state |= S_WDAY;
562 continue;
563
564 case 'g': /* The year corresponding to the ISO week
565 * number but without the century.
566 */
567 bp = conv_num(bp, &i, 0, 99);
568 continue;
569
570 case 'G': /* The year corresponding to the ISO week
571 * number with century.
572 */
573 do
574 bp++;
575 while (isdigit(*bp));
576 continue;
577
578 case 'V': /* The ISO 8601:1988 week number as decimal */
579 bp = conv_num(bp, &i, 0, 53);
580 continue;
581
582 case 'Y': /* The year. */
583 i = TM_YEAR_BASE; /* just for data sanity... */
584 bp = conv_num(bp, &i, 0, 9999);
585 wct->tm.tm_year = i - TM_YEAR_BASE;
586 LEGAL_ALT(ALT_E);
587 state |= S_YEAR;
588 continue;
589
590 case 'y': /* The year within 100 years of the epoch. */
591 /* LEGAL_ALT(ALT_E | ALT_O); */
592 bp = conv_num(bp, &i, 0, 99);
593
594 if (split_year)
595 /* preserve century */
596 i += (wct->tm.tm_year / 100) * 100;
597 else
598 {
599 split_year = 1;
600 if (i <= 68)
601 i = i + 2000 - TM_YEAR_BASE;
602 else
603 i = i + 1900 - TM_YEAR_BASE;
604 }
605 wct->tm.tm_year = i;
606 state |= S_YEAR;
607 continue;
608
609 case 'Z':
610 case 'z':
611 mandatory = c == 'z';
612 /*
613 * We recognize all ISO 8601 formats:
614 * Z = Zulu time/UTC
615 * [+-]hhmm
616 * [+-]hh:mm
617 * [+-]hh
618 * We recognize all RFC-822/RFC-2822 formats:
619 * UT|GMT
620 * North American : UTC offsets
621 * E[DS]T = Eastern : -4 | -5
622 * C[DS]T = Central : -5 | -6
623 * M[DS]T = Mountain: -6 | -7
624 * P[DS]T = Pacific : -7 | -8
625 * Military
626 * [A-IL-M] = -1 ... -9 (J not used)
627 * [N-Y] = +1 ... +12
628 */
629 if (mandatory)
630 while (isspace(*bp))
631 bp++;
632
633 zname = bp;
634 switch (*bp++)
635 {
636 case 'G':
637 if (*bp++ != 'M')
638 return NULL;
639 /*FALLTHROUGH*/
640 case 'U':
641 if (*bp++ != 'T')
642 return NULL;
643 /*FALLTHROUGH*/
644 case 'Z':
645 wct->tm.tm_isdst = 0;
646 wct->wct_gmtoff = 0;
647 wct->wct_zone = utc;
648 continue;
649 case '+':
650 neg = 0;
651 break;
652 case '-':
653 neg = 1;
654 break;
655 default:
656 --bp;
657 ep = find_string(bp, &i, nast, NULL, 4);
658 if (ep != NULL)
659 {
660 wct->wct_gmtoff = (-5 - i) * 3600;
661 wct->wct_zone = __UNCONST(nast[i]);
662 bp = ep;
663 continue;
664 }
665 ep = find_string(bp, &i, nadt, NULL, 4);
666 if (ep != NULL)
667 {
668 wct->tm.tm_isdst = 1;
669 wct->wct_gmtoff = (-4 - i) * 3600;
670 wct->wct_zone = __UNCONST(nadt[i]);
671 bp = ep;
672 continue;
673 }
674 system_tznames = cached_get_system_tznames();
675 ep = find_string(bp, &i, system_tznames, NULL, 2);
676 if (ep != NULL)
677 {
678 wct->tm.tm_isdst = i;
679 wct->wct_gmtoff = -cached_get_system_tzofs() + wct->tm.tm_isdst*3600;
680 wct->wct_zone = system_tznames[i];
681 bp = ep;
682 continue;
683 }
684
685
686 if ((*bp >= 'A' && *bp <= 'I') ||
687 (*bp >= 'L' && *bp <= 'Y'))
688 {
689 /* Argh! No 'J'! */
690 if (*bp >= 'A' && *bp <= 'I')
691 wct->wct_gmtoff =
692 (('A' - 1) - (int)*bp) * 3600;
693 else if (*bp >= 'L' && *bp <= 'M')
694 wct->wct_gmtoff = ('A' - (int)*bp) * 3600;
695 else if (*bp >= 'N' && *bp <= 'Y')
696 wct->wct_gmtoff = ((int)*bp - 'M') * 3600;
697 wct->wct_zone = utc; /* XXX */
698 bp++;
699 continue;
700 }
701 if (mandatory)
702 return NULL;
703
704 bp = zname;
705 continue;
706 }
707 offs = 0;
708 for (i = 0; i < 4; )
709 {
710 if (isdigit(*bp))
711 {
712 offs = offs * 10 + (*bp++ - '0');
713 i++;
714 continue;
715 }
716 if (i == 2 && *bp == ':')
717 {
718 bp++;
719 continue;
720 }
721 break;
722 }
723 switch (i)
724 {
725 case 2:
726 offs *= 3600;
727 break;
728 case 4:
729 i = offs % 100;
730 offs /= 100;
731 if (i >= 60)
732 goto out;
733 /* Convert minutes into decimal */
734 offs = offs * 3600 + i * 60;
735 break;
736 default:
737 out:
738 if (mandatory)
739 return NULL;
740 bp = zname;
741 continue;
742 }
743 if (neg)
744 offs = -offs;
745 wct->tm.tm_isdst = 0; /* XXX */
746 wct->wct_gmtoff = offs;
747 wct->wct_zone = utc; /* XXX */
748 continue;
749
750 /*
751 * Miscellaneous conversions.
752 */
753 case 'n': /* Any kind of white-space. */
754 case 't':
755 while (isspace(*bp))
756 bp++;
757 LEGAL_ALT(0);
758 continue;
759
760
761 default: /* Unknown/unsupported conversion. */
762 return NULL;
763 }
764 }
765
766 if (!HAVE_YDAY(state) && HAVE_YEAR(state))
767 {
768 if (HAVE_MON(state) && HAVE_MDAY(state))
769 {
770 /* calculate day of year (ordinal date) */
771 wct->tm.tm_yday = start_of_month[isleap_sum(wct->tm.tm_year,
772 TM_YEAR_BASE)][wct->tm.tm_mon] + (wct->tm.tm_mday - 1);
773 state |= S_YDAY;
774 }
775 else if (day_offset != -1)
776 {
777 /*
778 * Set the date to the first Sunday (or Monday)
779 * of the specified week of the year.
780 */
781 if (!HAVE_WDAY(state))
782 {
783 wct->tm.tm_wday = day_offset;
784 state |= S_WDAY;
785 }
786 wct->tm.tm_yday = (7 -
787 first_wday_of(wct->tm.tm_year + TM_YEAR_BASE) +
788 day_offset) % 7 + (week_offset - 1) * 7 +
789 wct->tm.tm_wday - day_offset;
790 state |= S_YDAY;
791 }
792 }
793
794 if (HAVE_YDAY(state) && HAVE_YEAR(state))
795 {
796 int isleap;
797
798 if (!HAVE_MON(state))
799 {
800 /* calculate month of day of year */
801 i = 0;
802 isleap = isleap_sum(wct->tm.tm_year, TM_YEAR_BASE);
803 while (wct->tm.tm_yday >= start_of_month[isleap][i])
804 i++;
805 if (i > 12)
806 {
807 i = 1;
808 wct->tm.tm_yday -= start_of_month[isleap][12];
809 wct->tm.tm_year++;
810 }
811 wct->tm.tm_mon = i - 1;
812 state |= S_MON;
813 }
814
815 if (!HAVE_MDAY(state))
816 {
817 /* calculate day of month */
818 isleap = isleap_sum(wct->tm.tm_year, TM_YEAR_BASE);
819 wct->tm.tm_mday = wct->tm.tm_yday -
820 start_of_month[isleap][wct->tm.tm_mon] + 1;
821 state |= S_MDAY;
822 }
823
824 if (!HAVE_WDAY(state))
825 {
826 /* calculate day of week */
827 i = 0;
828 week_offset = first_wday_of(wct->tm.tm_year);
829 while (i++ <= wct->tm.tm_yday)
830 {
831 if (week_offset++ >= 6)
832 week_offset = 0;
833 }
834 wct->tm.tm_wday = week_offset;
835 state |= S_WDAY;
836 }
837 }
838
839 if (!HAVE_USEC(state))
840 {
841 wct->wct_usec = 0;
842 }
843
844 return __UNCONST(bp);
845 }
846
847 /* Determine (guess) the year for the month.
848 *
849 * It can be used for BSD logs, where year is missing.
850 */
851 static gint
determine_year_for_month(gint month,const struct tm * now)852 determine_year_for_month(gint month, const struct tm *now)
853 {
854 if (month == 11 && now->tm_mon == 0)
855 return now->tm_year - 1;
856 else if (month == 0 && now->tm_mon == 11)
857 return now->tm_year + 1;
858 else
859 return now->tm_year;
860 }
861
862 void
wall_clock_time_guess_missing_year(WallClockTime * self)863 wall_clock_time_guess_missing_year(WallClockTime *self)
864 {
865 if (self->wct_year == -1)
866 {
867 time_t now;
868 struct tm tm;
869
870 now = cached_g_current_time_sec();
871 cached_localtime(&now, &tm);
872 self->wct_year = determine_year_for_month(self->wct_mon, &tm);
873 }
874 }
875
876 void
wall_clock_time_guess_missing_fields(WallClockTime * self)877 wall_clock_time_guess_missing_fields(WallClockTime *self)
878 {
879 /*
880 * The missing cases for date can be divided into three types:
881 * 1) missing all fileds -> use current date
882 * 2) only miss year -> guess year based on current year and month (current
883 * year, last year or next year)
884 * 3) the rest of the cases don't make much sense, so zero initialization of
885 * the missing field makes sense. And the year is initializeed to the current
886 * one.
887 */
888 time_t now;
889 struct tm tm;
890
891 now = cached_g_current_time_sec();
892 cached_localtime(&now, &tm);
893
894 if (self->wct_year == -1 && self->wct_mon == -1 && self->wct_mday == -1)
895 {
896 self->wct_year = tm.tm_year;
897 self->wct_mon = tm.tm_mon;
898 self->wct_mday = tm.tm_mday;
899 }
900 else if (self->wct_year == -1 && self->wct_mon != -1 && self->wct_mday != -1)
901 {
902 self->wct_year = determine_year_for_month(self->wct_mon, &tm);
903 }
904 else
905 {
906 if (self->wct_year == -1)
907 self->wct_year = tm.tm_year;
908 if (self->wct_mon == -1)
909 self->wct_mon = 0;
910 if (self->wct_mday == -1)
911 self->wct_mday = 1; // day of the month - [1, 31]
912 }
913
914 if (self->wct_hour == -1)
915 self->wct_hour = 0;
916 if (self->wct_min == -1)
917 self->wct_min = 0;
918 if (self->wct_sec == -1)
919 self->wct_sec = 0;
920 }
921
922
923 /*
924 * Calculate the week day of the first day of a year. Valid for
925 * the Gregorian calendar, which began Sept 14, 1752 in the UK
926 * and its colonies. Ref:
927 * http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week
928 */
929
930 static int
first_wday_of(int yr)931 first_wday_of(int yr)
932 {
933 return ((2 * (3 - (yr / 100) % 4)) + (yr % 100) + ((yr % 100) / 4) +
934 (isleap(yr) ? 6 : 0) + 1) % 7;
935 }
936
937
938 static const unsigned char *
conv_num(const unsigned char * input,int * dest,unsigned int llim,unsigned int ulim)939 conv_num(const unsigned char *input, int *dest, unsigned int llim, unsigned int ulim)
940 {
941 unsigned int result = 0;
942 unsigned char ch;
943
944 /* The limit also determines the number of valid digits. */
945 unsigned int rulim = ulim;
946
947 ch = *input;
948 if (ch < '0' || ch > '9')
949 return NULL;
950
951 do
952 {
953 result *= 10;
954 result += ch - '0';
955 rulim /= 10;
956 ch = *++input;
957 }
958 while ((result * 10 <= ulim) && rulim && ch >= '0' && ch <= '9');
959
960 if (result < llim || result > ulim)
961 return NULL;
962
963 *dest = result;
964 return input;
965 }
966
967 static const unsigned char *
find_string(const unsigned char * bp,int * tgt,const char * const * n1,const char * const * n2,int c)968 find_string(const unsigned char *bp, int *tgt, const char *const *n1,
969 const char *const *n2, int c)
970 {
971 int i;
972 size_t len;
973
974 /* check full name - then abbreviated ones */
975 for (; n1 != NULL; n1 = n2, n2 = NULL)
976 {
977 for (i = 0; i < c; i++, n1++)
978 {
979 len = strlen(*n1);
980 if (strncasecmp(*n1, (const char *)bp, len) == 0)
981 {
982 *tgt = i;
983 return bp + len;
984 }
985 }
986 }
987
988 /* Nothing matched */
989 return NULL;
990 }
991