1
/* Parse a string, yielding a struct partime that describes it. */
3
/* Copyright (C) 1993, 1994, 1995, 1997, 2002, 2003, 2006 Paul Eggert
4
Distributed under license by the Free Software Foundation, Inc.
6
This file is part of RCS.
8
RCS is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 2, or (at your option)
13
RCS is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
18
You should have received a copy of the GNU General Public License
19
along with RCS; see the file COPYING.
20
If not, write to the Free Software Foundation,
21
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23
Report problems and direct all questions to:
25
rcs-bugs@cs.purdue.edu
42
# define CTYPE_DOMAIN(c) 1
44
# define CTYPE_DOMAIN(c) ((unsigned) (c) <= 0177)
46
#define ISALNUM(c) (CTYPE_DOMAIN (c) && isalnum (c))
47
#define ISALPHA(c) (CTYPE_DOMAIN (c) && isalpha (c))
48
#define ISSPACE(c) (CTYPE_DOMAIN (c) && isspace (c))
49
#define ISUPPER(c) (CTYPE_DOMAIN (c) && isupper (c))
50
#define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
53
/* Lookup tables for names of months, weekdays, time zones. */
55
#define NAME_LENGTH_MAXIMUM 4
59
char name[NAME_LENGTH_MAXIMUM];
64
static char const *parse_pattern_letter (char const *, int, struct partime *);
67
static struct name_val const month_names[] =
84
static struct name_val const weekday_names[] =
96
#define RELATIVE_CONS(member, multiplier) \
97
(offsetof (struct tm, member) + (multiplier) * sizeof (struct tm))
98
#define RELATIVE_OFFSET(c) ((c) % sizeof (struct tm))
99
#define RELATIVE_MULTIPLIER(c) ((c) / sizeof (struct tm))
100
static struct name_val const relative_units[] =
102
{"year", RELATIVE_CONS (tm_year, 1) },
103
{"mont", RELATIVE_CONS (tm_mon , 1) },
104
{"fort", RELATIVE_CONS (tm_mday, 14) },
105
{"week", RELATIVE_CONS (tm_mday, 7) },
106
{"day" , RELATIVE_CONS (tm_mday, 1) },
107
{"hour", RELATIVE_CONS (tm_hour, 1) },
108
{"min" , RELATIVE_CONS (tm_min , 1) },
109
{"sec" , RELATIVE_CONS (tm_sec , 1) },
113
static struct name_val const ago[] =
119
static struct name_val const dst_names[] =
125
#define hr60nonnegative(t) ((t)/100 * 60 + (t)%100)
126
#define hr60(t) ((t) < 0 ? - hr60nonnegative (-(t)) : hr60nonnegative (t))
127
#define zs(t, s) {s, hr60 (t)}
128
#define zd(t, s, d) zs (t, s), zs ((t) + 100, d)
130
static struct name_val const zone_names[] =
132
zs (-1000, "hst"), /* Hawaii */
133
zd (-1000, "hast", "hadt"), /* Hawaii-Aleutian */
134
zd (- 900, "akst", "akdt"), /* Alaska */
135
zd (- 800, "pst" , "pdt" ), /* Pacific */
136
zd (- 700, "mst" , "mdt" ), /* Mountain */
137
zd (- 600, "cst" , "cdt" ), /* Central */
138
zd (- 500, "est" , "edt" ), /* Eastern */
139
zd (- 400, "ast" , "adt" ), /* Atlantic */
140
zd (- 330, "nst" , "ndt" ), /* Newfoundland */
141
zs ( 000, "utc" ), /* Coordinated Universal */
142
zs ( 000, "uct" ), /* " */
143
zs ( 000, "cut" ), /* " */
144
zs ( 000, "ut"), /* Universal */
145
zs ( 000, "z"), /* Zulu (required by ISO 8601) */
146
zd ( 000, "gmt" , "bst" ), /* Greenwich Mean, British Summer */
147
zd ( 000, "wet" , "west"), /* Western European */
148
zd ( 100, "cet" , "cest"), /* Central European */
149
zd ( 100, "met" , "mest"), /* Middle European (bug in old tz versions) */
150
zd ( 100, "mez" , "mesz"), /* Mittel-Europaeische Zeit */
151
zd ( 200, "eet" , "eest"), /* Eastern European */
152
zs ( 530, "ist" ), /* India */
153
zd ( 900, "jst" , "jdt" ), /* Japan */
154
zd ( 900, "kst" , "kdt" ), /* Korea */
155
zd ( 1200, "nzst", "nzdt"), /* New Zealand */
158
/* The following names are duplicates or are not well attested.
159
It's not worth keeping a complete list, since alphabetic time zone names
160
are deprecated and there are lots more where these came from. */
161
zs (-1100, "sst" ), /* Samoan */
162
zd (- 900, "yst" , "ydt" ), /* Yukon - name is no longer used */
163
zd (- 500, "ast" , "adt" ), /* Acre */
164
zd (- 400, "wst" , "wdt" ), /* Western Brazil */
165
zd (- 400, "cst" , "cdt" ), /* Chile */
166
zd (- 200, "fst" , "fdt" ), /* Fernando de Noronha */
167
zs ( 000, "wat" ), /* West African */
168
zs ( 100, "cat" ), /* Central African */
169
zs ( 200, "sat" ), /* South African */
170
zd ( 200, "ist" , "idt" ), /* Israel */
171
zs ( 300, "eat" ), /* East African */
172
zd ( 300, "msk" , "msd" ), /* Moscow */
173
zd ( 330, "ist" , "idt" ), /* Iran */
174
zs ( 800, "hkt" ), /* Hong Kong */
175
zs ( 800, "sgt" ), /* Singapore */
176
zd ( 800, "cst" , "cdt" ), /* China */
177
zd ( 800, "wst" , "wst" ), /* Western Australia */
178
zd ( 930, "cst" , "cst" ), /* Central Australia */
179
zs ( 1000, "gst" ), /* Guam */
180
zd ( 1000, "est" , "est" ), /* Eastern Australia */
185
/* Look for a prefix of S in TABLE, returning val for first matching entry. */
187
lookup (char const *s, struct name_val const table[])
190
char buf[NAME_LENGTH_MAXIMUM];
192
for (j = 0; j < NAME_LENGTH_MAXIMUM; j++)
194
unsigned char c = *s;
200
buf[j] = ISUPPER (c) ? tolower (c) : c;
207
if (j == NAME_LENGTH_MAXIMUM || ! table[0].name[j])
209
else if (buf[j] != table[0].name[j])
214
/* Set *T to ``undefined'' values. */
216
undefine (struct partime *t)
218
t->tm.tm_sec = t->tm.tm_min = t->tm.tm_hour = t->tm.tm_mday = t->tm.tm_mon
219
= t->tm.tm_year = t->tm.tm_wday = t->tm.tm_yday
220
= t->wday_ordinal = t->ymodulus = t->yweek
222
t->tmr.tm_sec = t->tmr.tm_min = t->tmr.tm_hour =
223
t->tmr.tm_mday = t->tmr.tm_mon = t->tmr.tm_year = 0;
224
t->zone = TM_UNDEFINED_ZONE;
227
/* Patterns to look for in a time string.
228
Order is important: we look for the first matching pattern
229
whose values do not contradict values that we already know about.
230
See `parse_pattern_letter' below for the meaning of the pattern codes. */
231
static char const time_patterns[] =
233
/* Traditional patterns come first,
234
to prevent an ISO 8601 format from misinterpreting their prefixes. */
236
/* RFC 822, extended */
237
'E', '_', 'N', '_', 'y', '$', 0,
241
'4', '_', 'M', '_', 'D', '_', 'h', '_', 'm', '_', 's', '$', 0,
242
'R', '_', 'M', '_', 'D', '_', 'h', '_', 'm', '_', 's', '$', 0,
244
'N', '_', 'E', '_', 'y', ';', 0,
245
'N', '_', 'E', ';', 0,
247
't', ':', 'm', ':', 's', '_', 'A', 0,
248
't', ':', 'm', '_', 'A', 0,
251
/* traditional get_date */
253
'Y', '/', 'n', '/', 'E', ';', 0,
254
'n', '/', 'E', '/', 'y', ';', 0,
255
'n', '/', 'E', ';', 0,
258
/* ISO 8601:1988 formats, generalized a bit. */
259
'y', '-', 'M', '-', 'D', '$', 0,
260
'4', 'M', 'D', '$', 0,
261
'Y', '-', 'M', '$', 0,
262
'R', 'M', 'D', '$', 0,
263
'-', 'R', '=', 'M', '$', 0,
265
'-', '-', 'M', '=', 'D', '$', 0,
266
'M', '=', 'D', 'T', 0,
267
'-', '-', 'M', '$', 0,
268
'-', '-', '-', 'D', '$', 0,
270
'Y', '-', 'd', '$', 0,
272
'R', '=', 'd', '$', 0,
275
'y', '-', 'W', '-', 'X', 0,
278
'-', 'r', '-', 'W', '-', 'X', 0,
279
'r', '-', 'W', '-', 'X', 'T', 0,
280
'-', 'r', 'W', 'X', 0,
281
'r', 'W', 'X', 'T', 0,
282
'-', 'W', '=', 'X', 0,
283
'W', '=', 'X', 'T', 0,
285
'-', 'w', '-', 'X', 0,
286
'w', '-', 'X', 'T', 0,
287
'-', '-', '-', 'X', '$', 0,
291
'h', ':', 'm', ':', 's', '$', 0,
292
'h', 'm', 's', '$', 0,
293
'h', ':', 'L', '$', 0,
296
'-', 'm', ':', 's', '$', 0,
297
'-', 'm', 's', '$', 0,
299
'-', '-', 's', '$', 0,
306
/* Parse an initial prefix of STR according to *PATTERNS, setting *T.
307
Return the first character after the prefix, or 0 if it couldn't be parsed.
308
*PATTERNS is a character array containing one pattern string after another;
309
it is terminated by an empty string.
310
If success, set *PATTERNS to the next pattern to try.
311
Set *PATTERNS to 0 if we know there are no more patterns to try;
312
if *PATTERNS is initially 0, give up immediately. */
314
parse_prefix (char const *str, char const **patterns, struct partime *t)
316
char const *pat = *patterns;
322
/* Remove initial noise. */
323
while (! ISALNUM (c = *str) && c != '-' && c != '+')
335
/* Try a pattern until one succeeds. */
349
while ((s = parse_pattern_letter (s, c, t)) != 0);
358
/* Parse an initial prefix of S of length DIGITS; it must be a number.
359
Store the parsed number into *RES.
360
Return the first character after the prefix, or 0 if it wasn't parsed. */
362
parse_fixed (char const *s, int digits, int *res)
365
char const *lim = s + digits;
368
unsigned d = *s++ - '0';
377
/* Parse a possibly empty initial prefix of S.
378
Store the parsed number into *RES.
379
Return the first character after the prefix. */
381
parse_varying (char const *s, int *res)
386
unsigned d = *s - '0';
396
/* Parse an initial prefix of S of length DIGITS;
397
it must be a number in the range LO through HI.
398
Store the parsed number into *RES.
399
Return the first character after the prefix, or 0 if it wasn't parsed. */
401
parse_ranged (char const *s, int digits, int lo, int hi, int *res)
403
s = parse_fixed (s, digits, res);
404
return s && lo <= *res && *res <= hi ? s : 0;
407
/* Parse an initial prefix of S of length DIGITS;
408
it must be a number in the range LO through HI
409
and it may be followed by a fraction to be computed using RESOLUTION.
410
Store the parsed number into *RES; store the fraction times RESOLUTION,
411
rounded to the nearest integer, into *FRES.
412
Return the first character after the prefix, or 0 if it wasn't parsed. */
414
parse_decimal (char const *s, int digits, int lo, int hi, int resolution,
417
s = parse_fixed (s, digits, res);
418
if (s && lo <= *res && *res <= hi)
421
if ((s[0] == ',' || s[0] == '.') && ISDIGIT (s[1]))
423
char const *s1 = ++s;
424
int num10 = 0, denom10 = 10, product;
425
while (ISDIGIT (*++s))
427
int d = denom10 * 10;
428
if (d / 10 != denom10)
429
return 0; /* overflow */
432
s = parse_fixed (s1, (int) (s - s1), &num10);
433
product = num10 * resolution;
434
f = (product + (denom10 >> 1)) / denom10;
435
f -= f & (product % denom10 == denom10 >> 1); /* round to even */
436
if (f < 0 || product/resolution != num10)
437
return 0; /* overflow */
445
/* Parse an initial prefix of S; it must denote a time zone.
446
Set *ZONE to the number of seconds east of GMT,
447
or to TM_LOCAL_ZONE if it is the local time zone.
448
Return the first character after the prefix, or 0 if it wasn't parsed. */
457
int minutes_east_of_UTC;
461
/* The formats are LT, n, n DST, nDST, no, o
462
where n is a time zone name
463
and o is a time zone offset of the form [-+]hh[:mm[:ss]]. */
472
minutes_east_of_UTC = lookup (s, zone_names);
473
if (minutes_east_of_UTC == -1)
476
/* Don't bother to check rest of spelling,
477
but look for an embedded "DST". */
479
while (ISALPHA ((unsigned char) *s))
481
if ((*s == 'D' || *s == 'd') && lookup (s, dst_names))
487
/* Don't modify LT. */
488
if (minutes_east_of_UTC == 1)
490
*zone = TM_LOCAL_ZONE;
494
z = minutes_east_of_UTC * 60L;
497
/* Look for trailing "DST" or " DST". */
498
while (ISSPACE ((unsigned char) *s))
500
if (lookup (s, dst_names))
502
while (ISALPHA ((unsigned char) *s))
534
if (! (s = parse_ranged (s, 2, 0, 23, &hh)))
541
if (! (s = parse_ranged (s, 2, 0, 59, &mm)))
543
if (*s == ':' && s[-3] == ':' && ISDIGIT (s[1])
544
&& ! (s = parse_ranged (s + 1, 2, 0, 59, &ss)))
549
offset = (hh * 60 + mm) * 60L + ss;
550
*zone = z + (sign == '-' ? -offset : offset);
551
/* ?? Are fractions allowed here? If so, they're not implemented. */
555
/* Parse an initial prefix of S, matching the pattern whose code is C.
557
Return the first character after the prefix, or 0 if it wasn't parsed. */
559
parse_pattern_letter (char const *s, int c, struct partime *t)
565
case '$': /* The next character must be a non-digit. */
573
/* These characters stand for themselves. */
578
case '4': /* 4-digit year */
579
s = parse_fixed (s, 4, &t->tm.tm_year);
582
case ';': /* The next character must be a non-digit, and cannot be ':'. */
583
if (ISDIGIT (*s) || *s == ':')
587
case '=': /* optional '-' */
591
case 'A': /* AM or PM */
592
/* This matches the regular expression [AaPp]\.?([Mm]\.?)?.
593
It must not be followed by a letter or digit;
594
otherwise it would match prefixes of strings like "PST". */
599
if (t->tm.tm_hour == 12)
605
if (t->tm.tm_hour != 12)
622
if (ISALNUM ((unsigned char) *s))
626
case 'D': /* day of month [01-31] */
627
s = parse_ranged (s, 2, 1, 31, &t->tm.tm_mday);
630
case 'd': /* day of year [001-366] */
631
s = parse_ranged (s, 3, 1, 366, &t->tm.tm_yday);
635
case 'E': /* traditional day of month [1-9, 01-31] */
636
s = parse_ranged (s, (ISDIGIT (s[0]) && ISDIGIT (s[1])) + 1, 1, 31,
640
case 'h': /* hour [00-23] */
641
s = parse_ranged (s, 2, 0, 23, &t->tm.tm_hour);
644
case 'H': /* hour [00-23 followed by optional fraction] */
647
s = parse_decimal (s, 2, 0, 23, 60 * 60, &t->tm.tm_hour, &frac);
648
t->tm.tm_min = frac / 60;
649
t->tm.tm_sec = frac % 60;
653
case 'i': /* ordinal day number, e.g. "3rd" */
654
s = parse_varying (s, &t->wday_ordinal);
657
while (ISALPHA ((unsigned char) *s))
661
case 'L': /* minute [00-59 followed by optional fraction] */
662
s = parse_decimal (s, 2, 0, 59, 60, &t->tm.tm_min, &t->tm.tm_sec);
665
case 'm': /* minute [00-59] */
666
s = parse_ranged (s, 2, 0, 59, &t->tm.tm_min);
669
case 'M': /* month [01-12] */
670
s = parse_ranged (s, 2, 1, 12, &t->tm.tm_mon);
674
case 'n': /* traditional month [1-9, 01-12] */
675
s = parse_ranged (s, (ISDIGIT (s[0]) && ISDIGIT (s[1])) + 1, 1, 12,
680
case 'N': /* month name [e.g. "Jan"] */
681
if (! TM_DEFINED (t->tm.tm_mon = lookup (s, month_names)))
683
/* Don't bother to check rest of spelling. */
684
while (ISALPHA ((unsigned char) *s))
688
case 'r': /* year % 10 (remainder in origin-0 decade) [0-9] */
689
s = parse_fixed (s, 1, &t->tm.tm_year);
694
case 'R': /* year % 100 (remainder in origin-0 century) [00-99] */
695
s = parse_fixed (s, 2, &t->tm.tm_year);
699
case 's': /* second [00-60 followed by optional fraction] */
702
s = parse_decimal (s, 2, 0, 60, 1, &t->tm.tm_sec, &frac);
706
case 'T': /* 'T' or 't' */
717
case 't': /* traditional hour [1-9 or 01-12] */
718
s = parse_ranged (s, (ISDIGIT (s[0]) && ISDIGIT (s[1])) + 1, 1, 12,
722
case 'u': /* relative unit */
729
case '-': negative = 1;
734
s = parse_varying (s, &n);
741
while (! ISALNUM ((unsigned char) *s) && *s)
743
i = lookup (s, relative_units);
746
* (int *) ((char *) &t->tmr + RELATIVE_OFFSET (i))
747
+= n * RELATIVE_MULTIPLIER (i);
748
while (ISALPHA ((unsigned char) *s))
750
while (! ISALNUM ((unsigned char) *s) && *s)
752
if (TM_DEFINED (lookup (s, ago)))
754
t->tmr.tm_sec = - t->tmr.tm_sec;
755
t->tmr.tm_min = - t->tmr.tm_min;
756
t->tmr.tm_hour = - t->tmr.tm_hour;
757
t->tmr.tm_mday = - t->tmr.tm_mday;
758
t->tmr.tm_mon = - t->tmr.tm_mon;
759
t->tmr.tm_year = - t->tmr.tm_year;
760
while (ISALPHA ((unsigned char) *s))
766
case 'w': /* 'W' or 'w' only (stands for current week) */
777
case 'W': /* 'W' or 'w', followed by a week of year [00-53] */
786
s = parse_ranged (s, 2, 0, 53, &t->yweek);
789
case 'X': /* weekday (1=Mon ... 7=Sun) [1-7] */
790
s = parse_ranged (s, 1, 1, 7, &t->tm.tm_wday);
794
case 'x': /* weekday name [e.g. "Sun"] */
795
if (! TM_DEFINED (t->tm.tm_wday = lookup (s, weekday_names)))
797
/* Don't bother to check rest of spelling. */
798
while (ISALPHA ((unsigned char) *s))
802
case 'y': /* either R or Y */
803
if (ISDIGIT (s[0]) && ISDIGIT (s[1]) && ! ISDIGIT (s[2]))
806
case 'Y': /* year in full [4 or more digits] */
807
s = parse_varying (s, &t->tm.tm_year);
812
case 'Z': /* time zone */
813
s = parzone (s, &t->zone);
816
case '_': /* possibly empty sequence of non-alphanumerics */
817
while (! ISALNUM ((unsigned char) *s) && *s)
821
default: /* bad pattern */
828
/* If there is no conflict, merge into *T the additional information in *U
829
and return 0. Otherwise do nothing and return -1. */
831
merge_partime (struct partime *t, struct partime const *u)
833
# define conflict(a,b) ((a) != (b) && TM_DEFINED (a) && TM_DEFINED (b))
834
if (conflict (t->tm.tm_sec, u->tm.tm_sec)
835
|| conflict (t->tm.tm_min, u->tm.tm_min)
836
|| conflict (t->tm.tm_hour, u->tm.tm_hour)
837
|| conflict (t->tm.tm_mday, u->tm.tm_mday)
838
|| conflict (t->tm.tm_mon, u->tm.tm_mon)
839
|| conflict (t->tm.tm_year, u->tm.tm_year)
840
|| conflict (t->tm.tm_wday, u->tm.tm_wday)
841
|| conflict (t->tm.tm_yday, u->tm.tm_yday)
842
|| conflict (t->ymodulus, u->ymodulus)
843
|| conflict (t->yweek, u->yweek)
844
|| (t->zone != u->zone
845
&& t->zone != TM_UNDEFINED_ZONE
846
&& u->zone != TM_UNDEFINED_ZONE))
849
# define merge_(a,b) if (TM_DEFINED (b)) (a) = (b);
850
merge_ (t->tm.tm_sec, u->tm.tm_sec)
851
merge_ (t->tm.tm_min, u->tm.tm_min)
852
merge_ (t->tm.tm_hour, u->tm.tm_hour)
853
merge_ (t->tm.tm_mday, u->tm.tm_mday)
854
merge_ (t->tm.tm_mon, u->tm.tm_mon)
855
merge_ (t->tm.tm_year, u->tm.tm_year)
856
merge_ (t->tm.tm_wday, u->tm.tm_wday)
857
merge_ (t->tm.tm_yday, u->tm.tm_yday)
858
merge_ (t->ymodulus, u->ymodulus)
859
merge_ (t->yweek, u->yweek)
861
t->tmr.tm_sec += u->tmr.tm_sec;
862
t->tmr.tm_min += u->tmr.tm_min;
863
t->tmr.tm_hour += u->tmr.tm_hour;
864
t->tmr.tm_mday += u->tmr.tm_mday;
865
t->tmr.tm_mon += u->tmr.tm_mon;
866
t->tmr.tm_year += u->tmr.tm_year;
867
if (u->zone != TM_UNDEFINED_ZONE)
872
/* Parse a date/time prefix of S, putting the parsed result into *T.
873
Return the first character after the prefix.
874
The prefix may contain no useful information;
875
in that case, *T will contain only undefined values. */
887
char const *patterns = time_patterns;
892
if (! (s1 = parse_prefix (s, &patterns, &p)))
895
while (merge_partime (t, &p) != 0);