1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
/*
* seconds absolute_date ... - convert absolute_date to seconds since epoch
*/
#include <u.h>
#include <libc.h>
#include <ctype.h>
typedef ulong Time;
enum {
AM, PM, HR24,
/* token types */
Month = 1,
Year,
Day,
Timetok,
Tz,
Dtz,
Ignore,
Ampm,
Maxtok = 6, /* only this many chars are stored in datetktbl */
Maxdateflds = 25,
};
/*
* macros for squeezing values into low 7 bits of "value".
* all timezones we care about are divisible by 10, and the largest value
* (780) when divided is 78.
*/
#define TOVAL(tp, v) ((tp)->value = (v) / 10)
#define FROMVAL(tp) ((tp)->value * 10) /* uncompress */
/* keep this struct small since we have an array of them */
typedef struct {
char token[Maxtok];
char type;
schar value;
} Datetok;
int dtok_numparsed;
/* forwards */
Datetok *datetoktype(char *s, int *bigvalp);
static Datetok datetktbl[];
static unsigned szdatetktbl;
/* parse 1- or 2-digit number, advance *cpp past it */
static int
eatnum(char **cpp)
{
int c, x;
char *cp;
cp = *cpp;
c = *cp;
if (!isascii(c) || !isdigit(c))
return -1;
x = c - '0';
c = *++cp;
if (isascii(c) && isdigit(c)) {
x = 10*x + c - '0';
cp++;
}
*cpp = cp;
return x;
}
/* return -1 on failure */
int
parsetime(char *time, Tm *tm)
{
tm->hour = eatnum(&time);
if (tm->hour == -1 || *time++ != ':')
return -1; /* only hour; too short */
tm->min = eatnum(&time);
if (tm->min == -1)
return -1;
if (*time++ != ':') {
tm->sec = 0;
return 0; /* no seconds; okay */
}
tm->sec = eatnum(&time);
if (tm->sec == -1)
return -1;
/* this may be considered too strict. garbage at end of time? */
return *time == '\0' || isascii(*time) && isspace(*time)? 0: -1;
}
/*
* try to parse pre-split timestr in fields as an absolute date
*/
int
tryabsdate(char **fields, int nf, Tm *now, Tm *tm)
{
int i, mer = HR24, bigval = -1;
long flg = 0, ty;
Datetok *tp;
now = localtime(time(0)); /* default to local time (zone) */
tm->tzoff = now->tzoff;
strncpy(tm->zone, now->zone, sizeof tm->zone);
tm->mday = tm->mon = tm->year = -1; /* mandatory */
tm->hour = tm->min = tm->sec = 0;
dtok_numparsed = 0;
for (i = 0; i < nf; i++) {
if (fields[i][0] == '\0')
continue;
tp = datetoktype(fields[i], &bigval);
ty = (1L << tp->type) & ~(1L << Ignore);
if (flg & ty)
return -1; /* repeated type */
flg |= ty;
switch (tp->type) {
case Year:
tm->year = bigval;
if (tm->year < 1970 || tm->year > 2106)
return -1; /* can't represent in ulong */
/* convert 4-digit year to 1900 origin */
if (tm->year >= 1900)
tm->year -= 1900;
break;
case Day:
tm->mday = bigval;
break;
case Month:
tm->mon = tp->value - 1; /* convert to zero-origin */
break;
case Timetok:
if (parsetime(fields[i], tm) < 0)
return -1;
break;
case Dtz:
case Tz:
/* tm2sec mangles timezones, so we do our own handling */
tm->tzoff = FROMVAL(tp);
snprint(tm->zone, sizeof(tm->zone), "GMT");
break;
case Ignore:
break;
case Ampm:
mer = tp->value;
break;
default:
return -1; /* bad token type: CANTHAPPEN */
}
}
if (tm->year == -1 || tm->mon == -1 || tm->mday == -1)
return -1; /* missing component */
if (mer == PM)
tm->hour += 12;
return 0;
}
int
prsabsdate(char *timestr, Tm *now, Tm *tm)
{
int nf;
char *fields[Maxdateflds];
static char delims[] = "- \t\n/,";
nf = gettokens(timestr, fields, nelem(fields), delims+1);
if (nf > nelem(fields))
return -1;
if (tryabsdate(fields, nf, now, tm) < 0) {
char *p = timestr;
/*
* could be a DEC-date; glue it all back together, split it
* with dash as a delimiter and try again. Yes, this is a
* hack, but so are DEC-dates.
*/
while (--nf > 0) {
while (*p++ != '\0')
;
p[-1] = ' ';
}
nf = gettokens(timestr, fields, nelem(fields), delims);
if (nf > nelem(fields) || tryabsdate(fields, nf, now, tm) < 0)
return -1;
}
return 0;
}
int
validtm(Tm *tm)
{
if (tm->year < 0 || tm->mon < 0 || tm->mon > 11 ||
tm->mday < 1 || tm->hour < 0 || tm->hour >= 24 ||
tm->min < 0 || tm->min > 59 ||
tm->sec < 0 || tm->sec > 61) /* allow 2 leap seconds */
return 0;
return 1;
}
Time
seconds(char *timestr)
{
Tm date;
memset(&date, 0, sizeof date);
if (prsabsdate(timestr, localtime(time(0)), &date) < 0)
return -1;
return validtm(&date)? tm2sec(&date) - 60*date.tzoff: -1;
}
int
convert(char *timestr)
{
char *copy;
Time tstime;
copy = strdup(timestr);
if (copy == nil)
sysfatal("out of memory");
tstime = seconds(copy);
free(copy);
if (tstime == -1) {
fprint(2, "%s: `%s' not a valid date\n", argv0, timestr);
return 1;
}
print("%lud\n", tstime);
return 0;
}
static void
usage(void)
{
fprint(2, "usage: %s date-time ...\n", argv0);
exits("usage");
}
void
main(int argc, char **argv)
{
int i, sts;
sts = 0;
ARGBEGIN{
default:
usage();
}ARGEND
if (argc == 0)
usage();
for (i = 0; i < argc; i++)
sts |= convert(argv[i]);
exits(sts != 0? "bad": 0);
}
/*
* Binary search -- from Knuth (6.2.1) Algorithm B. Special case like this
* is WAY faster than the generic bsearch().
*/
Datetok *
datebsearch(char *key, Datetok *base, unsigned nel)
{
int cmp;
Datetok *last = base + nel - 1, *pos;
while (last >= base) {
pos = base + ((last - base) >> 1);
cmp = key[0] - pos->token[0];
if (cmp == 0) {
cmp = strncmp(key, pos->token, Maxtok);
if (cmp == 0)
return pos;
}
if (cmp < 0)
last = pos - 1;
else
base = pos + 1;
}
return 0;
}
Datetok *
datetoktype(char *s, int *bigvalp)
{
char *cp = s;
char c = *cp;
static Datetok t;
Datetok *tp = &t;
if (isascii(c) && isdigit(c)) {
int len = strlen(cp);
if (len > 3 && (cp[1] == ':' || cp[2] == ':'))
tp->type = Timetok;
else {
if (bigvalp != nil)
*bigvalp = atoi(cp); /* won't fit in tp->value */
if (len == 4)
tp->type = Year;
else if (++dtok_numparsed == 1)
tp->type = Day;
else
tp->type = Year;
}
} else if (c == '-' || c == '+') {
int val = atoi(cp + 1);
int hr = val / 100;
int min = val % 100;
val = hr*60 + min;
TOVAL(tp, c == '-'? -val: val);
tp->type = Tz;
} else {
char lowtoken[Maxtok+1];
char *ltp = lowtoken, *endltp = lowtoken+Maxtok;
/* copy to lowtoken to avoid modifying s */
while ((c = *cp++) != '\0' && ltp < endltp)
*ltp++ = (isascii(c) && isupper(c)? tolower(c): c);
*ltp = '\0';
tp = datebsearch(lowtoken, datetktbl, szdatetktbl);
if (tp == nil) {
tp = &t;
tp->type = Ignore;
}
}
return tp;
}
/*
* to keep this table reasonably small, we divide the lexval for Tz and Dtz
* entries by 10 and truncate the text field at MAXTOKLEN characters.
* the text field is not guaranteed to be NUL-terminated.
*/
static Datetok datetktbl[] = {
/* text token lexval */
"acsst", Dtz, 63, /* Cent. Australia */
"acst", Tz, 57, /* Cent. Australia */
"adt", Dtz, -18, /* Atlantic Daylight Time */
"aesst", Dtz, 66, /* E. Australia */
"aest", Tz, 60, /* Australia Eastern Std Time */
"ahst", Tz, 60, /* Alaska-Hawaii Std Time */
"am", Ampm, AM,
"apr", Month, 4,
"april", Month, 4,
"ast", Tz, -24, /* Atlantic Std Time (Canada) */
"at", Ignore, 0, /* "at" (throwaway) */
"aug", Month, 8,
"august", Month, 8,
"awsst", Dtz, 54, /* W. Australia */
"awst", Tz, 48, /* W. Australia */
"bst", Tz, 6, /* British Summer Time */
"bt", Tz, 18, /* Baghdad Time */
"cadt", Dtz, 63, /* Central Australian DST */
"cast", Tz, 57, /* Central Australian ST */
"cat", Tz, -60, /* Central Alaska Time */
"cct", Tz, 48, /* China Coast */
"cdt", Dtz, -30, /* Central Daylight Time */
"cet", Tz, 6, /* Central European Time */
"cetdst", Dtz, 12, /* Central European Dayl.Time */
"cst", Tz, -36, /* Central Standard Time */
"dec", Month, 12,
"decemb", Month, 12,
"dnt", Tz, 6, /* Dansk Normal Tid */
"dst", Ignore, 0,
"east", Tz, -60, /* East Australian Std Time */
"edt", Dtz, -24, /* Eastern Daylight Time */
"eet", Tz, 12, /* East. Europe, USSR Zone 1 */
"eetdst", Dtz, 18, /* Eastern Europe */
"est", Tz, -30, /* Eastern Standard Time */
"feb", Month, 2,
"februa", Month, 2,
"fri", Ignore, 5,
"friday", Ignore, 5,
"fst", Tz, 6, /* French Summer Time */
"fwt", Dtz, 12, /* French Winter Time */
"gmt", Tz, 0, /* Greenwish Mean Time */
"gst", Tz, 60, /* Guam Std Time, USSR Zone 9 */
"hdt", Dtz, -54, /* Hawaii/Alaska */
"hmt", Dtz, 18, /* Hellas ? ? */
"hst", Tz, -60, /* Hawaii Std Time */
"idle", Tz, 72, /* Intl. Date Line, East */
"idlw", Tz, -72, /* Intl. Date Line, West */
"ist", Tz, 12, /* Israel */
"it", Tz, 22, /* Iran Time */
"jan", Month, 1,
"januar", Month, 1,
"jst", Tz, 54, /* Japan Std Time,USSR Zone 8 */
"jt", Tz, 45, /* Java Time */
"jul", Month, 7,
"july", Month, 7,
"jun", Month, 6,
"june", Month, 6,
"kst", Tz, 54, /* Korea Standard Time */
"ligt", Tz, 60, /* From Melbourne, Australia */
"mar", Month, 3,
"march", Month, 3,
"may", Month, 5,
"mdt", Dtz, -36, /* Mountain Daylight Time */
"mest", Dtz, 12, /* Middle Europe Summer Time */
"met", Tz, 6, /* Middle Europe Time */
"metdst", Dtz, 12, /* Middle Europe Daylight Time*/
"mewt", Tz, 6, /* Middle Europe Winter Time */
"mez", Tz, 6, /* Middle Europe Zone */
"mon", Ignore, 1,
"monday", Ignore, 1,
"mst", Tz, -42, /* Mountain Standard Time */
"mt", Tz, 51, /* Moluccas Time */
"ndt", Dtz, -15, /* Nfld. Daylight Time */
"nft", Tz, -21, /* Newfoundland Standard Time */
"nor", Tz, 6, /* Norway Standard Time */
"nov", Month, 11,
"novemb", Month, 11,
"nst", Tz, -21, /* Nfld. Standard Time */
"nt", Tz, -66, /* Nome Time */
"nzdt", Dtz, 78, /* New Zealand Daylight Time */
"nzst", Tz, 72, /* New Zealand Standard Time */
"nzt", Tz, 72, /* New Zealand Time */
"oct", Month, 10,
"octobe", Month, 10,
"on", Ignore, 0, /* "on" (throwaway) */
"pdt", Dtz, -42, /* Pacific Daylight Time */
"pm", Ampm, PM,
"pst", Tz, -48, /* Pacific Standard Time */
"sadt", Dtz, 63, /* S. Australian Dayl. Time */
"sast", Tz, 57, /* South Australian Std Time */
"sat", Ignore, 6,
"saturd", Ignore, 6,
"sep", Month, 9,
"sept", Month, 9,
"septem", Month, 9,
"set", Tz, -6, /* Seychelles Time ?? */
"sst", Dtz, 12, /* Swedish Summer Time */
"sun", Ignore, 0,
"sunday", Ignore, 0,
"swt", Tz, 6, /* Swedish Winter Time */
"thu", Ignore, 4,
"thur", Ignore, 4,
"thurs", Ignore, 4,
"thursd", Ignore, 4,
"tue", Ignore, 2,
"tues", Ignore, 2,
"tuesda", Ignore, 2,
"ut", Tz, 0,
"utc", Tz, 0,
"wadt", Dtz, 48, /* West Australian DST */
"wast", Tz, 42, /* West Australian Std Time */
"wat", Tz, -6, /* West Africa Time */
"wdt", Dtz, 54, /* West Australian DST */
"wed", Ignore, 3,
"wednes", Ignore, 3,
"weds", Ignore, 3,
"wet", Tz, 0, /* Western Europe */
"wetdst", Dtz, 6, /* Western Europe */
"wst", Tz, 48, /* West Australian Std Time */
"ydt", Dtz, -48, /* Yukon Daylight Time */
"yst", Tz, -54, /* Yukon Standard Time */
"zp4", Tz, -24, /* GMT +4 hours. */
"zp5", Tz, -30, /* GMT +5 hours. */
"zp6", Tz, -36, /* GMT +6 hours. */
};
static unsigned szdatetktbl = nelem(datetktbl);
|