2
* This code is in the public domain and has no copyright.
4
* This is a plain C recursive-descent translation of an old
5
* public-domain YACC grammar that has been used for parsing dates in
6
* very many open-source projects.
8
* Since the original authors were generous enough to donate their
9
* work to the public domain, I feel compelled to match their
12
* Tim Kientzle, February 2009.
16
* Header comment from original getdate.y:
20
** Originally written by Steven M. Bellovin <smb@research.att.com> while
21
** at the University of North Carolina at Chapel Hill. Later tweaked by
22
** a couple of people on Usenet. Completely overhauled by Rich $alz
23
** <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990;
25
** This grammar has 10 shift/reduce conflicts.
27
** This code is in the public domain and has no copyright.
31
#include <sys/cdefs.h>
32
__FBSDID("$FreeBSD$");
41
/* This file defines a single public function. */
42
time_t get_date(time_t now, char *);
44
/* Basic time units. */
47
#define HOUR (60L * MINUTE)
48
#define DAY (24L * HOUR)
50
/* Daylight-savings mode: on, off, or not yet known. */
51
enum DSTMODE { DSTon, DSToff, DSTmaybe };
52
/* Meridian: am or pm. */
54
/* Token types returned by nexttoken() */
55
enum { tAGO = 260, tDAY, tDAYZONE, tAMPM, tMONTH, tMONTH_UNIT, tSEC_UNIT,
56
tUNUMBER, tZONE, tDST };
57
struct token { int token; time_t value; };
63
struct token *tokenp; /* Pointer to next token. */
64
/* HaveXxxx counts how many of this kind of phrase we've seen;
65
* it's a fatal error to have more than one time, zone, day,
70
int HaveWeekDay; /* Day of week */
71
int HaveTime; /* Hour/minute/second */
72
int HaveZone; /* timezone and/or DST info */
73
int HaveRel; /* time offset; we can have more than one */
74
/* Absolute time values. */
75
time_t Timezone; /* Seconds offset from GMT */
84
/* Day of week accounting, e.g., "3rd Tuesday" */
85
time_t DayOrdinal; /* "3" in "3rd Tuesday" */
86
time_t DayNumber; /* "Tuesday" in "3rd Tuesday" */
87
/* Relative time values: hour/day/week offsets are measured in
88
* seconds, month/year are counted in months. */
94
* A series of functions that recognize certain common time phrases.
95
* Each function returns 1 if it managed to make sense of some of the
96
* tokens, zero otherwise.
100
* hour:minute or hour:minute:second with optional AM, PM, or numeric
104
timephrase(struct gdstate *gds)
106
if (gds->tokenp[0].token == tUNUMBER
107
&& gds->tokenp[1].token == ':'
108
&& gds->tokenp[2].token == tUNUMBER
109
&& gds->tokenp[3].token == ':'
110
&& gds->tokenp[4].token == tUNUMBER) {
111
/* "12:14:18" or "22:08:07" */
113
gds->Hour = gds->tokenp[0].value;
114
gds->Minutes = gds->tokenp[2].value;
115
gds->Seconds = gds->tokenp[4].value;
118
else if (gds->tokenp[0].token == tUNUMBER
119
&& gds->tokenp[1].token == ':'
120
&& gds->tokenp[2].token == tUNUMBER) {
121
/* "12:14" or "22:08" */
123
gds->Hour = gds->tokenp[0].value;
124
gds->Minutes = gds->tokenp[2].value;
128
else if (gds->tokenp[0].token == tUNUMBER
129
&& gds->tokenp[1].token == tAMPM) {
130
/* "7" is a time if it's followed by "am" or "pm" */
132
gds->Hour = gds->tokenp[0].value;
133
gds->Minutes = gds->Seconds = 0;
134
/* We'll handle the AM/PM below. */
137
/* We can't handle this. */
141
if (gds->tokenp[0].token == tAMPM) {
142
/* "7:12pm", "12:20:13am" */
145
if (gds->tokenp[0].value == tPM)
149
if (gds->tokenp[0].token == '+'
150
&& gds->tokenp[1].token == tUNUMBER) {
153
gds->DSTmode = DSToff;
154
gds->Timezone = - ((gds->tokenp[1].value / 100) * HOUR
155
+ (gds->tokenp[1].value % 100) * MINUTE);
158
if (gds->tokenp[0].token == '-'
159
&& gds->tokenp[1].token == tUNUMBER) {
160
/* "19:14:12-0530" */
162
gds->DSTmode = DSToff;
163
gds->Timezone = + ((gds->tokenp[1].value / 100) * HOUR
164
+ (gds->tokenp[1].value % 100) * MINUTE);
171
* Timezone name, possibly including DST.
174
zonephrase(struct gdstate *gds)
176
if (gds->tokenp[0].token == tZONE
177
&& gds->tokenp[1].token == tDST) {
179
gds->Timezone = gds->tokenp[0].value;
180
gds->DSTmode = DSTon;
185
if (gds->tokenp[0].token == tZONE) {
187
gds->Timezone = gds->tokenp[0].value;
188
gds->DSTmode = DSToff;
193
if (gds->tokenp[0].token == tDAYZONE) {
195
gds->Timezone = gds->tokenp[0].value;
196
gds->DSTmode = DSTon;
204
* Year/month/day in various combinations.
207
datephrase(struct gdstate *gds)
209
if (gds->tokenp[0].token == tUNUMBER
210
&& gds->tokenp[1].token == '/'
211
&& gds->tokenp[2].token == tUNUMBER
212
&& gds->tokenp[3].token == '/'
213
&& gds->tokenp[4].token == tUNUMBER) {
217
if (gds->tokenp[0].value >= 13) {
218
/* First number is big: 2004/01/29, 99/02/17 */
219
gds->Year = gds->tokenp[0].value;
220
gds->Month = gds->tokenp[2].value;
221
gds->Day = gds->tokenp[4].value;
222
} else if ((gds->tokenp[4].value >= 13)
223
|| (gds->tokenp[2].value >= 13)) {
224
/* Last number is big: 01/07/98 */
225
/* Middle number is big: 01/29/04 */
226
gds->Month = gds->tokenp[0].value;
227
gds->Day = gds->tokenp[2].value;
228
gds->Year = gds->tokenp[4].value;
230
/* No significant clues: 02/03/04 */
231
gds->Month = gds->tokenp[0].value;
232
gds->Day = gds->tokenp[2].value;
233
gds->Year = gds->tokenp[4].value;
239
if (gds->tokenp[0].token == tUNUMBER
240
&& gds->tokenp[1].token == '/'
241
&& gds->tokenp[2].token == tUNUMBER) {
245
gds->Month = gds->tokenp[0].value;
246
gds->Day = gds->tokenp[2].value;
251
if (gds->tokenp[0].token == tUNUMBER
252
&& gds->tokenp[1].token == '-'
253
&& gds->tokenp[2].token == tUNUMBER
254
&& gds->tokenp[3].token == '-'
255
&& gds->tokenp[4].token == tUNUMBER) {
256
/* ISO 8601 format. yyyy-mm-dd. */
260
gds->Year = gds->tokenp[0].value;
261
gds->Month = gds->tokenp[2].value;
262
gds->Day = gds->tokenp[4].value;
267
if (gds->tokenp[0].token == tUNUMBER
268
&& gds->tokenp[1].token == '-'
269
&& gds->tokenp[2].token == tMONTH
270
&& gds->tokenp[3].token == '-'
271
&& gds->tokenp[4].token == tUNUMBER) {
275
if (gds->tokenp[0].value > 31) {
276
/* e.g. 1992-Jun-17 */
277
gds->Year = gds->tokenp[0].value;
278
gds->Month = gds->tokenp[2].value;
279
gds->Day = gds->tokenp[4].value;
281
/* e.g. 17-JUN-1992. */
282
gds->Day = gds->tokenp[0].value;
283
gds->Month = gds->tokenp[2].value;
284
gds->Year = gds->tokenp[4].value;
290
if (gds->tokenp[0].token == tMONTH
291
&& gds->tokenp[1].token == tUNUMBER
292
&& gds->tokenp[2].token == ','
293
&& gds->tokenp[3].token == tUNUMBER) {
294
/* "June 17, 2001" */
298
gds->Month = gds->tokenp[0].value;
299
gds->Day = gds->tokenp[1].value;
300
gds->Year = gds->tokenp[3].value;
305
if (gds->tokenp[0].token == tMONTH
306
&& gds->tokenp[1].token == tUNUMBER) {
310
gds->Month = gds->tokenp[0].value;
311
gds->Day = gds->tokenp[1].value;
316
if (gds->tokenp[0].token == tUNUMBER
317
&& gds->tokenp[1].token == tMONTH
318
&& gds->tokenp[2].token == tUNUMBER) {
323
gds->Day = gds->tokenp[0].value;
324
gds->Month = gds->tokenp[1].value;
325
gds->Year = gds->tokenp[2].value;
330
if (gds->tokenp[0].token == tUNUMBER
331
&& gds->tokenp[1].token == tMONTH) {
335
gds->Day = gds->tokenp[0].value;
336
gds->Month = gds->tokenp[1].value;
345
* Relative time phrase: "tomorrow", "yesterday", "+1 hour", etc.
348
relunitphrase(struct gdstate *gds)
350
if (gds->tokenp[0].token == '-'
351
&& gds->tokenp[1].token == tUNUMBER
352
&& gds->tokenp[2].token == tSEC_UNIT) {
355
gds->RelSeconds -= gds->tokenp[1].value * gds->tokenp[2].value;
359
if (gds->tokenp[0].token == '+'
360
&& gds->tokenp[1].token == tUNUMBER
361
&& gds->tokenp[2].token == tSEC_UNIT) {
364
gds->RelSeconds += gds->tokenp[1].value * gds->tokenp[2].value;
368
if (gds->tokenp[0].token == tUNUMBER
369
&& gds->tokenp[1].token == tSEC_UNIT) {
372
gds->RelSeconds += gds->tokenp[1].value * gds->tokenp[2].value;
376
if (gds->tokenp[0].token == '-'
377
&& gds->tokenp[1].token == tUNUMBER
378
&& gds->tokenp[2].token == tMONTH_UNIT) {
381
gds->RelMonth -= gds->tokenp[1].value * gds->tokenp[2].value;
385
if (gds->tokenp[0].token == '+'
386
&& gds->tokenp[1].token == tUNUMBER
387
&& gds->tokenp[2].token == tMONTH_UNIT) {
390
gds->RelMonth += gds->tokenp[1].value * gds->tokenp[2].value;
394
if (gds->tokenp[0].token == tUNUMBER
395
&& gds->tokenp[1].token == tMONTH_UNIT) {
398
gds->RelMonth += gds->tokenp[0].value * gds->tokenp[1].value;
402
if (gds->tokenp[0].token == tSEC_UNIT) {
403
/* "now", "tomorrow" */
405
gds->RelSeconds += gds->tokenp[0].value;
409
if (gds->tokenp[0].token == tMONTH_UNIT) {
412
gds->RelMonth += gds->tokenp[0].value;
420
* Day of the week specification.
423
dayphrase(struct gdstate *gds)
425
if (gds->tokenp[0].token == tDAY) {
426
/* "tues", "wednesday," */
429
gds->DayNumber = gds->tokenp[0].value;
431
if (gds->tokenp[0].token == ',')
435
if (gds->tokenp[0].token == tUNUMBER
436
&& gds->tokenp[1].token == tDAY) {
437
/* "second tues" "3 wed" */
439
gds->DayOrdinal = gds->tokenp[0].value;
440
gds->DayNumber = gds->tokenp[1].value;
448
* Try to match a phrase using one of the above functions.
449
* This layer also deals with a couple of generic issues.
452
phrase(struct gdstate *gds)
462
if (relunitphrase(gds)) {
463
if (gds->tokenp[0].token == tAGO) {
464
gds->RelSeconds = -gds->RelSeconds;
465
gds->RelMonth = -gds->RelMonth;
471
/* Bare numbers sometimes have meaning. */
472
if (gds->tokenp[0].token == tUNUMBER) {
473
if (gds->HaveTime && !gds->HaveYear && !gds->HaveRel) {
475
gds->Year = gds->tokenp[0].value;
480
if(gds->tokenp[0].value > 10000) {
485
gds->Day= (gds->tokenp[0].value)%100;
486
gds->Month= (gds->tokenp[0].value/100)%100;
487
gds->Year = gds->tokenp[0].value/10000;
492
if (gds->tokenp[0].value < 24) {
494
gds->Hour = gds->tokenp[0].value;
501
if ((gds->tokenp[0].value / 100 < 24)
502
&& (gds->tokenp[0].value % 100 < 60)) {
503
/* "513" is same as "5:13" */
504
gds->Hour = gds->tokenp[0].value / 100;
505
gds->Minutes = gds->tokenp[0].value % 100;
516
* A dictionary of time words.
518
static struct LEXICON {
523
} const TimeWords[] = {
525
{ 0, "am", tAMPM, tAM },
526
{ 0, "pm", tAMPM, tPM },
529
{ 3, "january", tMONTH, 1 },
530
{ 3, "february", tMONTH, 2 },
531
{ 3, "march", tMONTH, 3 },
532
{ 3, "april", tMONTH, 4 },
533
{ 3, "may", tMONTH, 5 },
534
{ 3, "june", tMONTH, 6 },
535
{ 3, "july", tMONTH, 7 },
536
{ 3, "august", tMONTH, 8 },
537
{ 3, "september", tMONTH, 9 },
538
{ 3, "october", tMONTH, 10 },
539
{ 3, "november", tMONTH, 11 },
540
{ 3, "december", tMONTH, 12 },
542
/* Days of the week. */
543
{ 2, "sunday", tDAY, 0 },
544
{ 3, "monday", tDAY, 1 },
545
{ 2, "tuesday", tDAY, 2 },
546
{ 3, "wednesday", tDAY, 3 },
547
{ 2, "thursday", tDAY, 4 },
548
{ 2, "friday", tDAY, 5 },
549
{ 2, "saturday", tDAY, 6 },
551
/* Timezones: Offsets are in seconds. */
552
{ 0, "gmt", tZONE, 0*HOUR }, /* Greenwich Mean */
553
{ 0, "ut", tZONE, 0*HOUR }, /* Universal (Coordinated) */
554
{ 0, "utc", tZONE, 0*HOUR },
555
{ 0, "wet", tZONE, 0*HOUR }, /* Western European */
556
{ 0, "bst", tDAYZONE, 0*HOUR }, /* British Summer */
557
{ 0, "wat", tZONE, 1*HOUR }, /* West Africa */
558
{ 0, "at", tZONE, 2*HOUR }, /* Azores */
559
/* { 0, "bst", tZONE, 3*HOUR }, */ /* Brazil Standard: Conflict */
560
/* { 0, "gst", tZONE, 3*HOUR }, */ /* Greenland Standard: Conflict*/
561
{ 0, "nft", tZONE, 3*HOUR+30*MINUTE }, /* Newfoundland */
562
{ 0, "nst", tZONE, 3*HOUR+30*MINUTE }, /* Newfoundland Standard */
563
{ 0, "ndt", tDAYZONE, 3*HOUR+30*MINUTE }, /* Newfoundland Daylight */
564
{ 0, "ast", tZONE, 4*HOUR }, /* Atlantic Standard */
565
{ 0, "adt", tDAYZONE, 4*HOUR }, /* Atlantic Daylight */
566
{ 0, "est", tZONE, 5*HOUR }, /* Eastern Standard */
567
{ 0, "edt", tDAYZONE, 5*HOUR }, /* Eastern Daylight */
568
{ 0, "cst", tZONE, 6*HOUR }, /* Central Standard */
569
{ 0, "cdt", tDAYZONE, 6*HOUR }, /* Central Daylight */
570
{ 0, "mst", tZONE, 7*HOUR }, /* Mountain Standard */
571
{ 0, "mdt", tDAYZONE, 7*HOUR }, /* Mountain Daylight */
572
{ 0, "pst", tZONE, 8*HOUR }, /* Pacific Standard */
573
{ 0, "pdt", tDAYZONE, 8*HOUR }, /* Pacific Daylight */
574
{ 0, "yst", tZONE, 9*HOUR }, /* Yukon Standard */
575
{ 0, "ydt", tDAYZONE, 9*HOUR }, /* Yukon Daylight */
576
{ 0, "hst", tZONE, 10*HOUR }, /* Hawaii Standard */
577
{ 0, "hdt", tDAYZONE, 10*HOUR }, /* Hawaii Daylight */
578
{ 0, "cat", tZONE, 10*HOUR }, /* Central Alaska */
579
{ 0, "ahst", tZONE, 10*HOUR }, /* Alaska-Hawaii Standard */
580
{ 0, "nt", tZONE, 11*HOUR }, /* Nome */
581
{ 0, "idlw", tZONE, 12*HOUR }, /* Intl Date Line West */
582
{ 0, "cet", tZONE, -1*HOUR }, /* Central European */
583
{ 0, "met", tZONE, -1*HOUR }, /* Middle European */
584
{ 0, "mewt", tZONE, -1*HOUR }, /* Middle European Winter */
585
{ 0, "mest", tDAYZONE, -1*HOUR }, /* Middle European Summer */
586
{ 0, "swt", tZONE, -1*HOUR }, /* Swedish Winter */
587
{ 0, "sst", tDAYZONE, -1*HOUR }, /* Swedish Summer */
588
{ 0, "fwt", tZONE, -1*HOUR }, /* French Winter */
589
{ 0, "fst", tDAYZONE, -1*HOUR }, /* French Summer */
590
{ 0, "eet", tZONE, -2*HOUR }, /* Eastern Eur, USSR Zone 1 */
591
{ 0, "bt", tZONE, -3*HOUR }, /* Baghdad, USSR Zone 2 */
592
{ 0, "it", tZONE, -3*HOUR-30*MINUTE },/* Iran */
593
{ 0, "zp4", tZONE, -4*HOUR }, /* USSR Zone 3 */
594
{ 0, "zp5", tZONE, -5*HOUR }, /* USSR Zone 4 */
595
{ 0, "ist", tZONE, -5*HOUR-30*MINUTE },/* Indian Standard */
596
{ 0, "zp6", tZONE, -6*HOUR }, /* USSR Zone 5 */
597
/* { 0, "nst", tZONE, -6.5*HOUR }, */ /* North Sumatra: Conflict */
598
/* { 0, "sst", tZONE, -7*HOUR }, */ /* So Sumatra, USSR 6: Conflict */
599
{ 0, "wast", tZONE, -7*HOUR }, /* West Australian Standard */
600
{ 0, "wadt", tDAYZONE, -7*HOUR }, /* West Australian Daylight */
601
{ 0, "jt", tZONE, -7*HOUR-30*MINUTE },/* Java (3pm in Cronusland!)*/
602
{ 0, "cct", tZONE, -8*HOUR }, /* China Coast, USSR Zone 7 */
603
{ 0, "jst", tZONE, -9*HOUR }, /* Japan Std, USSR Zone 8 */
604
{ 0, "cast", tZONE, -9*HOUR-30*MINUTE },/* Ctrl Australian Std */
605
{ 0, "cadt", tDAYZONE, -9*HOUR-30*MINUTE },/* Ctrl Australian Daylt */
606
{ 0, "east", tZONE, -10*HOUR }, /* Eastern Australian Std */
607
{ 0, "eadt", tDAYZONE, -10*HOUR }, /* Eastern Australian Daylt */
608
{ 0, "gst", tZONE, -10*HOUR }, /* Guam Std, USSR Zone 9 */
609
{ 0, "nzt", tZONE, -12*HOUR }, /* New Zealand */
610
{ 0, "nzst", tZONE, -12*HOUR }, /* New Zealand Standard */
611
{ 0, "nzdt", tDAYZONE, -12*HOUR }, /* New Zealand Daylight */
612
{ 0, "idle", tZONE, -12*HOUR }, /* Intl Date Line East */
614
{ 0, "dst", tDST, 0 },
617
{ 4, "years", tMONTH_UNIT, 12 },
618
{ 5, "months", tMONTH_UNIT, 1 },
619
{ 9, "fortnights", tSEC_UNIT, 14 * DAY },
620
{ 4, "weeks", tSEC_UNIT, 7 * DAY },
621
{ 3, "days", tSEC_UNIT, DAY },
622
{ 4, "hours", tSEC_UNIT, HOUR },
623
{ 3, "minutes", tSEC_UNIT, MINUTE },
624
{ 3, "seconds", tSEC_UNIT, 1 },
626
/* Relative-time words. */
627
{ 0, "tomorrow", tSEC_UNIT, DAY },
628
{ 0, "yesterday", tSEC_UNIT, -DAY },
629
{ 0, "today", tSEC_UNIT, 0 },
630
{ 0, "now", tSEC_UNIT, 0 },
631
{ 0, "last", tUNUMBER, -1 },
632
{ 0, "this", tSEC_UNIT, 0 },
633
{ 0, "next", tUNUMBER, 2 },
634
{ 0, "first", tUNUMBER, 1 },
635
{ 0, "1st", tUNUMBER, 1 },
636
/* { 0, "second", tUNUMBER, 2 }, */
637
{ 0, "2nd", tUNUMBER, 2 },
638
{ 0, "third", tUNUMBER, 3 },
639
{ 0, "3rd", tUNUMBER, 3 },
640
{ 0, "fourth", tUNUMBER, 4 },
641
{ 0, "4th", tUNUMBER, 4 },
642
{ 0, "fifth", tUNUMBER, 5 },
643
{ 0, "5th", tUNUMBER, 5 },
644
{ 0, "sixth", tUNUMBER, 6 },
645
{ 0, "seventh", tUNUMBER, 7 },
646
{ 0, "eighth", tUNUMBER, 8 },
647
{ 0, "ninth", tUNUMBER, 9 },
648
{ 0, "tenth", tUNUMBER, 10 },
649
{ 0, "eleventh", tUNUMBER, 11 },
650
{ 0, "twelfth", tUNUMBER, 12 },
651
{ 0, "ago", tAGO, 1 },
653
/* Military timezones. */
654
{ 0, "a", tZONE, 1*HOUR },
655
{ 0, "b", tZONE, 2*HOUR },
656
{ 0, "c", tZONE, 3*HOUR },
657
{ 0, "d", tZONE, 4*HOUR },
658
{ 0, "e", tZONE, 5*HOUR },
659
{ 0, "f", tZONE, 6*HOUR },
660
{ 0, "g", tZONE, 7*HOUR },
661
{ 0, "h", tZONE, 8*HOUR },
662
{ 0, "i", tZONE, 9*HOUR },
663
{ 0, "k", tZONE, 10*HOUR },
664
{ 0, "l", tZONE, 11*HOUR },
665
{ 0, "m", tZONE, 12*HOUR },
666
{ 0, "n", tZONE, -1*HOUR },
667
{ 0, "o", tZONE, -2*HOUR },
668
{ 0, "p", tZONE, -3*HOUR },
669
{ 0, "q", tZONE, -4*HOUR },
670
{ 0, "r", tZONE, -5*HOUR },
671
{ 0, "s", tZONE, -6*HOUR },
672
{ 0, "t", tZONE, -7*HOUR },
673
{ 0, "u", tZONE, -8*HOUR },
674
{ 0, "v", tZONE, -9*HOUR },
675
{ 0, "w", tZONE, -10*HOUR },
676
{ 0, "x", tZONE, -11*HOUR },
677
{ 0, "y", tZONE, -12*HOUR },
678
{ 0, "z", tZONE, 0*HOUR },
686
* = A number from 0 to 99, which means a year from 1970 to 2069, or
687
* = The actual year (>=100).
690
Convert(time_t Month, time_t Day, time_t Year,
691
time_t Hours, time_t Minutes, time_t Seconds,
692
time_t Timezone, enum DSTMODE DSTmode)
694
static int DaysInMonth[12] = {
695
31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
704
DaysInMonth[1] = Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)
706
/* Checking for 2038 bogusly assumes that time_t is 32 bits. But
707
I'm too lazy to try to check for time_t overflow in another way. */
708
if (Year < EPOCH || Year > 2038
709
|| Month < 1 || Month > 12
710
/* Lint fluff: "conversion from long may lose accuracy" */
711
|| Day < 1 || Day > DaysInMonth[(int)--Month]
712
|| Hours < 0 || Hours > 23
713
|| Minutes < 0 || Minutes > 59
714
|| Seconds < 0 || Seconds > 59)
718
for (i = 0; i < Month; i++)
719
Julian += DaysInMonth[i];
720
for (i = EPOCH; i < Year; i++)
721
Julian += 365 + (i % 4 == 0);
724
Julian += Hours * HOUR + Minutes * MINUTE + Seconds;
726
|| (DSTmode == DSTmaybe && localtime(&Julian)->tm_isdst))
733
DSTcorrect(time_t Start, time_t Future)
738
StartDay = (localtime(&Start)->tm_hour + 1) % 24;
739
FutureDay = (localtime(&Future)->tm_hour + 1) % 24;
740
return (Future - Start) + (StartDay - FutureDay) * HOUR;
745
RelativeDate(time_t Start, time_t zone, int dstmode,
746
time_t DayOrdinal, time_t DayNumber)
754
now += DAY * ((DayNumber - tm->tm_wday + 7) % 7);
755
now += 7 * DAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1);
756
if (dstmode == DSTmaybe)
757
return DSTcorrect(Start, now);
763
RelativeMonth(time_t Start, time_t Timezone, time_t RelMonth)
771
tm = localtime(&Start);
772
Month = 12 * (tm->tm_year + 1900) + tm->tm_mon + RelMonth;
774
Month = Month % 12 + 1;
775
return DSTcorrect(Start,
776
Convert(Month, (time_t)tm->tm_mday, Year,
777
(time_t)tm->tm_hour, (time_t)tm->tm_min, (time_t)tm->tm_sec,
778
Timezone, DSTmaybe));
785
nexttoken(char **in, time_t *value)
791
while (isspace((unsigned char)**in))
794
/* Skip parenthesized comments. */
809
/* Try the next token in the word table first. */
810
/* This allows us to match "2nd", for example. */
813
const struct LEXICON *tp;
816
/* Force to lowercase and strip '.' characters. */
818
&& (isalnum((unsigned char)*src) || *src == '.')
819
&& i < sizeof(buff)-1) {
821
if (isupper((unsigned char)*src))
822
buff[i++] = tolower((unsigned char)*src);
831
* Find the first match. If the word can be
832
* abbreviated, make sure we match at least
833
* the minimum abbreviation.
835
for (tp = TimeWords; tp->name; tp++) {
836
size_t abbrev = tp->abbrev;
838
abbrev = strlen(tp->name);
839
if (strlen(buff) >= abbrev
840
&& strncmp(tp->name, buff, strlen(buff))
842
/* Skip over token. */
844
/* Return the match. */
852
* Not in the word table, maybe it's a number. Note:
853
* Because '-' and '+' have other special meanings, I
854
* don't deal with signed numbers here.
856
if (isdigit((unsigned char)(c = **in))) {
857
for (*value = 0; isdigit((unsigned char)(c = *(*in)++)); )
858
*value = 10 * *value + c - '0';
867
#define TM_YEAR_ORIGIN 1900
869
/* Yield A - B, measured in seconds. */
871
difftm (struct tm *a, struct tm *b)
873
int ay = a->tm_year + (TM_YEAR_ORIGIN - 1);
874
int by = b->tm_year + (TM_YEAR_ORIGIN - 1);
876
/* difference in day of year */
877
a->tm_yday - b->tm_yday
878
/* + intervening leap days */
879
+ ((ay >> 2) - (by >> 2))
881
+ ((ay/100 >> 2) - (by/100 >> 2))
882
/* + difference in years * 365 */
883
+ (long)(ay-by) * 365
885
return (days * DAY + (a->tm_hour - b->tm_hour) * HOUR
886
+ (a->tm_min - b->tm_min) * MINUTE
887
+ (a->tm_sec - b->tm_sec));
892
* The public function.
894
* TODO: tokens[] array should be dynamically sized.
897
get_date(time_t now, char *p)
899
struct token tokens[256];
901
struct token *lasttoken;
903
struct tm local, *tm;
904
struct tm gmt, *gmt_ptr;
909
/* Clear out the parsed token array. */
910
memset(tokens, 0, sizeof(tokens));
911
/* Initialize the parser state. */
912
memset(&_gds, 0, sizeof(_gds));
915
/* Look up the current time. */
916
memset(&local, 0, sizeof(local));
917
tm = localtime (&now);
922
/* Look up UTC if we can and use that to determine the current
923
* timezone offset. */
924
memset(&gmt, 0, sizeof(gmt));
925
gmt_ptr = gmtime (&now);
926
if (gmt_ptr != NULL) {
927
/* Copy, in case localtime and gmtime use the same buffer. */
931
tzone = difftm (&gmt, &local);
933
/* This system doesn't understand timezones; fake it. */
938
/* Tokenize the input string. */
940
while ((lasttoken->token = nexttoken(&p, &lasttoken->value)) != 0) {
942
if (lasttoken > tokens + 255)
945
gds->tokenp = tokens;
947
/* Match phrases until we run out of input tokens. */
948
while (gds->tokenp < lasttoken) {
953
/* Use current local timezone if none was specified. */
954
if (!gds->HaveZone) {
955
gds->Timezone = tzone;
956
gds->DSTmode = DSTmaybe;
959
/* If a timezone was specified, use that for generating the default
960
* time components instead of the local timezone. */
961
if (gds->HaveZone && gmt_ptr != NULL) {
962
now -= gds->Timezone;
963
gmt_ptr = gmtime (&now);
966
now += gds->Timezone;
970
gds->Year = local.tm_year + 1900;
972
gds->Month = local.tm_mon + 1;
974
gds->Day = local.tm_mday;
975
/* Note: No default for hour/min/sec; a specifier that just
976
* gives date always refers to 00:00 on that date. */
978
/* If we saw more than one time, timezone, weekday, year, month,
979
* or day, then give up. */
980
if (gds->HaveTime > 1 || gds->HaveZone > 1 || gds->HaveWeekDay > 1
981
|| gds->HaveYear > 1 || gds->HaveMonth > 1 || gds->HaveDay > 1)
984
/* Compute an absolute time based on whatever absolute information
986
if (gds->HaveYear || gds->HaveMonth || gds->HaveDay
987
|| gds->HaveTime || gds->HaveWeekDay) {
988
Start = Convert(gds->Month, gds->Day, gds->Year,
989
gds->Hour, gds->Minutes, gds->Seconds,
990
gds->Timezone, gds->DSTmode);
996
Start -= local.tm_hour * HOUR + local.tm_min * MINUTE
1000
/* Add the relative offset. */
1001
Start += gds->RelSeconds;
1002
Start += RelativeMonth(Start, gds->Timezone, gds->RelMonth);
1004
/* Adjust for day-of-week offsets. */
1005
if (gds->HaveWeekDay
1006
&& !(gds->HaveYear || gds->HaveMonth || gds->HaveDay)) {
1007
tod = RelativeDate(Start, gds->Timezone,
1008
gds->DSTmode, gds->DayOrdinal, gds->DayNumber);
1012
/* -1 is an error indicator, so return 0 instead of -1 if
1013
* that's the actual time. */
1014
return Start == -1 ? 0 : Start;
1022
main(int argc, char **argv)
1026
while (*++argv != NULL) {
1027
(void)printf("Input: %s\n", *argv);
1028
d = get_date(*argv);
1030
(void)printf("Bad format - couldn't convert.\n");
1032
(void)printf("Output: %s\n", ctime(&d));
1037
#endif /* defined(TEST) */