mirrored from git://git.sv.gnu.org/gnulib.git
-
Notifications
You must be signed in to change notification settings - Fork 106
/
parse-datetime.y
2438 lines (2158 loc) · 73.7 KB
/
parse-datetime.y
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%{
/* Parse a string into an internal timestamp.
Copyright (C) 1999-2000, 2002-2023 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Originally written by Steven M. Bellovin <smb@research.att.com> while
at the University of North Carolina at Chapel Hill. Later tweaked by
a couple of people on Usenet. Completely overhauled by Rich $alz
<rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990.
Modified by Assaf Gordon <assafgordon@gmail.com> in 2016 to add
debug output.
Modified by Paul Eggert <eggert@twinsun.com> in 1999 to do the
right thing about local DST. Also modified by Paul Eggert
<eggert@cs.ucla.edu> in 2004 to support nanosecond-resolution
timestamps, in 2004 to support TZ strings in dates, and in 2017 and 2020 to
check for integer overflow and to support longer-than-'long'
'time_t' and 'tv_nsec'. */
#include <config.h>
#include "parse-datetime.h"
#include "idx.h"
#include "intprops.h"
#include "timespec.h"
#include "strftime.h"
/* There's no need to extend the stack, so there's no need to involve
alloca. */
#define YYSTACK_USE_ALLOCA 0
/* Tell Bison how much stack space is needed. 20 should be plenty for
this grammar, which is not right recursive. Beware setting it too
high, since that might cause problems on machines whose
implementations have lame stack-overflow checking. */
#define YYMAXDEPTH 20
#define YYINITDEPTH YYMAXDEPTH
#include <inttypes.h>
#include <c-ctype.h>
#include <stdarg.h>
#include <stdckdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gettext.h"
#define _(str) gettext (str)
/* Bison's skeleton tests _STDLIB_H, while some stdlib.h headers
use _STDLIB_H_ as witness. Map the latter to the one bison uses. */
/* FIXME: this is temporary. Remove when we have a mechanism to ensure
that the version we're using is fixed, too. */
#ifdef _STDLIB_H_
# undef _STDLIB_H
# define _STDLIB_H 1
#endif
/* Shift A right by B bits portably, by dividing A by 2**B and
truncating towards minus infinity. A and B should be free of side
effects, and B should be in the range 0 <= B <= INT_BITS - 2, where
INT_BITS is the number of useful bits in an int. GNU code can
assume that INT_BITS is at least 32.
ISO C99 says that A >> B is implementation-defined if A < 0. Some
implementations (e.g., UNICOS 9.0 on a Cray Y-MP EL) don't shift
right in the usual way when A < 0, so SHR falls back on division if
ordinary A >> B doesn't seem to be the usual signed shift. */
#define SHR(a, b) \
(-1 >> 1 == -1 \
? (a) >> (b) \
: (a) / (1 << (b)) - ((a) % (1 << (b)) < 0))
#define HOUR(x) (60 * 60 * (x))
#define STREQ(a, b) (strcmp (a, b) == 0)
/* Verify that time_t is an integer as POSIX requires, and that every
time_t value fits in intmax_t. Please file a bug report if these
assumptions are false on your platform. */
static_assert (TYPE_IS_INTEGER (time_t));
static_assert (!TYPE_SIGNED (time_t) || INTMAX_MIN <= TYPE_MINIMUM (time_t));
static_assert (TYPE_MAXIMUM (time_t) <= INTMAX_MAX);
/* True if N is out of range for time_t. */
static bool
time_overflow (intmax_t n)
{
return ! ((TYPE_SIGNED (time_t) ? TYPE_MINIMUM (time_t) <= n : 0 <= n)
&& n <= TYPE_MAXIMUM (time_t));
}
/* Convert a possibly-signed character to an unsigned character. This is
a bit safer than casting to unsigned char, since it catches some type
errors that the cast doesn't. */
static unsigned char to_uchar (char ch) { return ch; }
static void _GL_ATTRIBUTE_FORMAT ((__printf__, 1, 2))
dbg_printf (char const *msg, ...)
{
va_list args;
/* TODO: use gnulib's 'program_name' instead? */
fputs ("date: ", stderr);
va_start (args, msg);
vfprintf (stderr, msg, args);
va_end (args);
}
/* An integer value, and the number of digits in its textual
representation. */
typedef struct
{
bool negative;
intmax_t value;
idx_t digits;
} textint;
/* An entry in the lexical lookup table. */
typedef struct
{
char const *name;
int type;
int value;
} table;
/* Meridian: am, pm, or 24-hour style. */
enum { MERam, MERpm, MER24 };
/* A reasonable upper bound for the buffer used in debug output. */
enum { DBGBUFSIZE = 100 };
enum { BILLION = 1000000000, LOG10_BILLION = 9 };
/* Relative times. */
typedef struct
{
/* Relative year, month, day, hour, minutes, seconds, and nanoseconds. */
intmax_t year;
intmax_t month;
intmax_t day;
intmax_t hour;
intmax_t minutes;
intmax_t seconds;
int ns;
} relative_time;
#if HAVE_COMPOUND_LITERALS
# define RELATIVE_TIME_0 ((relative_time) { 0, 0, 0, 0, 0, 0, 0 })
#else
static relative_time const RELATIVE_TIME_0;
#endif
/* Information passed to and from the parser. */
typedef struct
{
/* The input string remaining to be parsed. */
const char *input;
/* N, if this is the Nth Tuesday. */
intmax_t day_ordinal;
/* Day of week; Sunday is 0. */
int day_number;
/* tm_isdst flag for the local zone. */
int local_isdst;
/* Time zone, in seconds east of UT. */
int time_zone;
/* Style used for time. */
int meridian;
/* Gregorian year, month, day, hour, minutes, seconds, and nanoseconds. */
textint year;
intmax_t month;
intmax_t day;
intmax_t hour;
intmax_t minutes;
struct timespec seconds; /* includes nanoseconds */
/* Relative year, month, day, hour, minutes, seconds, and nanoseconds. */
relative_time rel;
/* Presence or counts of nonterminals of various flavors parsed so far. */
bool timespec_seen;
bool rels_seen;
idx_t dates_seen;
idx_t days_seen;
idx_t J_zones_seen;
idx_t local_zones_seen;
idx_t dsts_seen;
idx_t times_seen;
idx_t zones_seen;
bool year_seen;
#ifdef GNULIB_PARSE_DATETIME2
/* Print debugging output to stderr. */
bool parse_datetime_debug;
#endif
/* Which of the 'seen' parts have been printed when debugging. */
bool debug_dates_seen;
bool debug_days_seen;
bool debug_local_zones_seen;
bool debug_times_seen;
bool debug_zones_seen;
bool debug_year_seen;
/* The user specified explicit ordinal day value. */
bool debug_ordinal_day_seen;
/* Table of local time zone abbreviations, terminated by a null entry. */
table local_time_zone_table[3];
} parser_control;
static bool
debugging (parser_control const *pc)
{
#ifdef GNULIB_PARSE_DATETIME2
return pc->parse_datetime_debug;
#else
return false;
#endif
}
union YYSTYPE;
static int yylex (union YYSTYPE *, parser_control *);
static int yyerror (parser_control const *, char const *);
static bool time_zone_hhmm (parser_control *, textint, intmax_t);
/* Extract into *PC any date and time info from a string of digits
of the form e.g., YYYYMMDD, YYMMDD, HHMM, HH (and sometimes YYY,
YYYY, ...). */
static void
digits_to_date_time (parser_control *pc, textint text_int)
{
if (pc->dates_seen && ! pc->year.digits
&& ! pc->rels_seen && (pc->times_seen || 2 < text_int.digits))
{
pc->year_seen = true;
pc->year = text_int;
}
else
{
if (4 < text_int.digits)
{
pc->dates_seen++;
pc->day = text_int.value % 100;
pc->month = (text_int.value / 100) % 100;
pc->year.value = text_int.value / 10000;
pc->year.digits = text_int.digits - 4;
}
else
{
pc->times_seen++;
if (text_int.digits <= 2)
{
pc->hour = text_int.value;
pc->minutes = 0;
}
else
{
pc->hour = text_int.value / 100;
pc->minutes = text_int.value % 100;
}
pc->seconds = (struct timespec) {0};
pc->meridian = MER24;
}
}
}
/* Increment PC->rel by FACTOR * REL (FACTOR is 1 or -1). Return true
if successful, false if an overflow occurred. */
static bool
apply_relative_time (parser_control *pc, relative_time rel, int factor)
{
if (factor < 0
? (ckd_sub (&pc->rel.ns, pc->rel.ns, rel.ns)
| ckd_sub (&pc->rel.seconds, pc->rel.seconds, rel.seconds)
| ckd_sub (&pc->rel.minutes, pc->rel.minutes, rel.minutes)
| ckd_sub (&pc->rel.hour, pc->rel.hour, rel.hour)
| ckd_sub (&pc->rel.day, pc->rel.day, rel.day)
| ckd_sub (&pc->rel.month, pc->rel.month, rel.month)
| ckd_sub (&pc->rel.year, pc->rel.year, rel.year))
: (ckd_add (&pc->rel.ns, pc->rel.ns, rel.ns)
| ckd_add (&pc->rel.seconds, pc->rel.seconds, rel.seconds)
| ckd_add (&pc->rel.minutes, pc->rel.minutes, rel.minutes)
| ckd_add (&pc->rel.hour, pc->rel.hour, rel.hour)
| ckd_add (&pc->rel.day, pc->rel.day, rel.day)
| ckd_add (&pc->rel.month, pc->rel.month, rel.month)
| ckd_add (&pc->rel.year, pc->rel.year, rel.year)))
return false;
pc->rels_seen = true;
return true;
}
/* Set PC-> hour, minutes, seconds and nanoseconds members from arguments. */
static void
set_hhmmss (parser_control *pc, intmax_t hour, intmax_t minutes,
time_t sec, int nsec)
{
pc->hour = hour;
pc->minutes = minutes;
pc->seconds = (struct timespec) { .tv_sec = sec, .tv_nsec = nsec };
}
/* Return a textual representation of the day ordinal/number values
in the parser_control struct (e.g., "last wed", "this tues", "thu"). */
static const char *
str_days (parser_control *pc, char *buffer, int n)
{
/* TODO: use relative_time_table for reverse lookup. */
static char const ordinal_values[][11] = {
"last",
"this",
"next/first",
"(SECOND)", /* SECOND is commented out in relative_time_table. */
"third",
"fourth",
"fifth",
"sixth",
"seventh",
"eight",
"ninth",
"tenth",
"eleventh",
"twelfth"
};
static char const days_values[][4] = {
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat"
};
int len;
/* Don't add an ordinal prefix if the user didn't specify it
(e.g., "this wed" vs "wed"). */
if (pc->debug_ordinal_day_seen)
{
/* Use word description if possible (e.g., -1 = last, 3 = third). */
len = (-1 <= pc->day_ordinal && pc->day_ordinal <= 12
? snprintf (buffer, n, "%s", ordinal_values[pc->day_ordinal + 1])
: snprintf (buffer, n, "%"PRIdMAX, pc->day_ordinal));
}
else
{
buffer[0] = '\0';
len = 0;
}
/* Add the day name */
if (0 <= pc->day_number && pc->day_number <= 6 && 0 <= len && len < n)
snprintf (buffer + len, n - len, &" %s"[len == 0],
days_values[pc->day_number]);
else
{
/* invalid day_number value - should never happen */
}
return buffer;
}
/* Convert a time zone to its string representation. */
enum { TIME_ZONE_BUFSIZE = INT_STRLEN_BOUND (intmax_t) + sizeof ":MM:SS" } ;
static char const *
time_zone_str (int time_zone, char time_zone_buf[TIME_ZONE_BUFSIZE])
{
char *p = time_zone_buf;
char sign = time_zone < 0 ? '-' : '+';
int hour = abs (time_zone / (60 * 60));
p += sprintf (time_zone_buf, "%c%02d", sign, hour);
int offset_from_hour = abs (time_zone % (60 * 60));
if (offset_from_hour != 0)
{
int mm = offset_from_hour / 60;
int ss = offset_from_hour % 60;
*p++ = ':';
*p++ = '0' + mm / 10;
*p++ = '0' + mm % 10;
if (ss)
{
*p++ = ':';
*p++ = '0' + ss / 10;
*p++ = '0' + ss % 10;
}
*p = '\0';
}
return time_zone_buf;
}
/* debugging: print the current time in the parser_control structure.
The parser will increment "*_seen" members for those which were parsed.
This function will print only newly seen parts. */
static void
debug_print_current_time (char const *item, parser_control *pc)
{
bool space = false;
if (!debugging (pc))
return;
/* no newline, more items printed below */
dbg_printf (_("parsed %s part: "), item);
if (pc->dates_seen && !pc->debug_dates_seen)
{
/*TODO: use pc->year.negative? */
fprintf (stderr, "(Y-M-D) %04"PRIdMAX"-%02"PRIdMAX"-%02"PRIdMAX,
pc->year.value, pc->month, pc->day);
pc->debug_dates_seen = true;
space = true;
}
if (pc->year_seen != pc->debug_year_seen)
{
if (space)
fputc (' ', stderr);
fprintf (stderr, _("year: %04"PRIdMAX), pc->year.value);
pc->debug_year_seen = pc->year_seen;
space = true;
}
if (pc->times_seen && !pc->debug_times_seen)
{
intmax_t sec = pc->seconds.tv_sec;
fprintf (stderr, &" %02"PRIdMAX":%02"PRIdMAX":%02"PRIdMAX[!space],
pc->hour, pc->minutes, sec);
if (pc->seconds.tv_nsec != 0)
{
int nsec = pc->seconds.tv_nsec;
fprintf (stderr, ".%09d", nsec);
}
if (pc->meridian == MERpm)
fputs ("pm", stderr);
pc->debug_times_seen = true;
space = true;
}
if (pc->days_seen && !pc->debug_days_seen)
{
if (space)
fputc (' ', stderr);
char tmp[DBGBUFSIZE];
fprintf (stderr, _("%s (day ordinal=%"PRIdMAX" number=%d)"),
str_days (pc, tmp, sizeof tmp),
pc->day_ordinal, pc->day_number);
pc->debug_days_seen = true;
space = true;
}
/* local zone strings only change the DST settings,
not the timezone value. If seen, inform about the DST. */
if (pc->local_zones_seen && !pc->debug_local_zones_seen)
{
fprintf (stderr, &" isdst=%d%s"[!space],
pc->local_isdst, pc->dsts_seen ? " DST" : "");
pc->debug_local_zones_seen = true;
space = true;
}
if (pc->zones_seen && !pc->debug_zones_seen)
{
char time_zone_buf[TIME_ZONE_BUFSIZE];
fprintf (stderr, &" UTC%s"[!space],
time_zone_str (pc->time_zone, time_zone_buf));
pc->debug_zones_seen = true;
space = true;
}
if (pc->timespec_seen)
{
intmax_t sec = pc->seconds.tv_sec;
if (space)
fputc (' ', stderr);
fprintf (stderr, _("number of seconds: %"PRIdMAX), sec);
}
fputc ('\n', stderr);
}
/* Debugging: print the current relative values. */
static bool
print_rel_part (bool space, intmax_t val, char const *name)
{
if (val == 0)
return space;
fprintf (stderr, &" %+"PRIdMAX" %s"[!space], val, name);
return true;
}
static void
debug_print_relative_time (char const *item, parser_control const *pc)
{
bool space = false;
if (!debugging (pc))
return;
/* no newline, more items printed below */
dbg_printf (_("parsed %s part: "), item);
if (pc->rel.year == 0 && pc->rel.month == 0 && pc->rel.day == 0
&& pc->rel.hour == 0 && pc->rel.minutes == 0 && pc->rel.seconds == 0
&& pc->rel.ns == 0)
{
/* Special case: relative time of this/today/now */
fputs (_("today/this/now\n"), stderr);
return;
}
space = print_rel_part (space, pc->rel.year, "year(s)");
space = print_rel_part (space, pc->rel.month, "month(s)");
space = print_rel_part (space, pc->rel.day, "day(s)");
space = print_rel_part (space, pc->rel.hour, "hour(s)");
space = print_rel_part (space, pc->rel.minutes, "minutes");
space = print_rel_part (space, pc->rel.seconds, "seconds");
print_rel_part (space, pc->rel.ns, "nanoseconds");
fputc ('\n', stderr);
}
%}
/* We want a reentrant parser, even if the TZ manipulation and the calls to
localtime and gmtime are not reentrant. */
%define api.pure
%parse-param { parser_control *pc }
%lex-param { parser_control *pc }
/* This grammar has 31 shift/reduce conflicts. */
%expect 31
%union
{
intmax_t intval;
textint textintval;
struct timespec timespec;
relative_time rel;
}
%token <intval> tAGO
%token tDST
%token tYEAR_UNIT tMONTH_UNIT tHOUR_UNIT tMINUTE_UNIT tSEC_UNIT
%token <intval> tDAY_UNIT tDAY_SHIFT
%token <intval> tDAY tDAYZONE tLOCAL_ZONE tMERIDIAN
%token <intval> tMONTH tORDINAL tZONE
%token <textintval> tSNUMBER tUNUMBER
%token <timespec> tSDECIMAL_NUMBER tUDECIMAL_NUMBER
%type <intval> o_colon_minutes
%type <timespec> seconds signed_seconds unsigned_seconds
%type <rel> relunit relunit_snumber dayshift
%%
spec:
timespec
| items
;
timespec:
'@' seconds
{
pc->seconds = $2;
pc->timespec_seen = true;
debug_print_current_time (_("number of seconds"), pc);
}
;
items:
/* empty */
| items item
;
item:
datetime
{
pc->times_seen++; pc->dates_seen++;
debug_print_current_time (_("datetime"), pc);
}
| time
{
pc->times_seen++;
debug_print_current_time (_("time"), pc);
}
| local_zone
{
pc->local_zones_seen++;
debug_print_current_time (_("local_zone"), pc);
}
| 'J'
{
pc->J_zones_seen++;
debug_print_current_time ("J", pc);
}
| zone
{
pc->zones_seen++;
debug_print_current_time (_("zone"), pc);
}
| date
{
pc->dates_seen++;
debug_print_current_time (_("date"), pc);
}
| day
{
pc->days_seen++;
debug_print_current_time (_("day"), pc);
}
| rel
{
debug_print_relative_time (_("relative"), pc);
}
| number
{
debug_print_current_time (_("number"), pc);
}
| hybrid
{
debug_print_relative_time (_("hybrid"), pc);
}
;
datetime:
iso_8601_datetime
;
iso_8601_datetime:
iso_8601_date 'T' iso_8601_time
;
time:
tUNUMBER tMERIDIAN
{
set_hhmmss (pc, $1.value, 0, 0, 0);
pc->meridian = $2;
}
| tUNUMBER ':' tUNUMBER tMERIDIAN
{
set_hhmmss (pc, $1.value, $3.value, 0, 0);
pc->meridian = $4;
}
| tUNUMBER ':' tUNUMBER ':' unsigned_seconds tMERIDIAN
{
set_hhmmss (pc, $1.value, $3.value, $5.tv_sec, $5.tv_nsec);
pc->meridian = $6;
}
| iso_8601_time
;
iso_8601_time:
tUNUMBER zone_offset
{
set_hhmmss (pc, $1.value, 0, 0, 0);
pc->meridian = MER24;
}
| tUNUMBER ':' tUNUMBER o_zone_offset
{
set_hhmmss (pc, $1.value, $3.value, 0, 0);
pc->meridian = MER24;
}
| tUNUMBER ':' tUNUMBER ':' unsigned_seconds o_zone_offset
{
set_hhmmss (pc, $1.value, $3.value, $5.tv_sec, $5.tv_nsec);
pc->meridian = MER24;
}
;
o_zone_offset:
/* empty */
| zone_offset
;
zone_offset:
tSNUMBER o_colon_minutes
{
pc->zones_seen++;
if (! time_zone_hhmm (pc, $1, $2)) YYABORT;
}
;
/* Local zone strings affect only the DST setting, and take effect
only if the current TZ setting is relevant.
Example 1:
'EEST' is parsed as tLOCAL_ZONE, as it relates to the effective TZ:
TZ='Europe/Helsinki' date -d '2016-06-30 EEST'
Example 2:
'EEST' is parsed as tDAYZONE:
TZ='Asia/Tokyo' date -d '2016-06-30 EEST'
This is implemented by probing the next three calendar quarters
of the effective timezone and looking for DST changes -
if found, the timezone name (EEST) is inserted into
the lexical lookup table with type tLOCAL_ZONE.
(Search for 'quarter' comment in 'parse_datetime2'.)
*/
local_zone:
tLOCAL_ZONE
{ pc->local_isdst = $1; }
| tLOCAL_ZONE tDST
{
pc->local_isdst = 1;
pc->dsts_seen++;
}
;
/* Note 'T' is a special case, as it is used as the separator in ISO
8601 date and time of day representation. */
zone:
tZONE
{ pc->time_zone = $1; }
| 'T'
{ pc->time_zone = -HOUR (7); }
| tZONE relunit_snumber
{ pc->time_zone = $1;
if (! apply_relative_time (pc, $2, 1)) YYABORT;
debug_print_relative_time (_("relative"), pc);
}
| 'T' relunit_snumber
{ pc->time_zone = -HOUR (7);
if (! apply_relative_time (pc, $2, 1)) YYABORT;
debug_print_relative_time (_("relative"), pc);
}
| tZONE tSNUMBER o_colon_minutes
{ if (! time_zone_hhmm (pc, $2, $3)) YYABORT;
if (ckd_add (&pc->time_zone, pc->time_zone, $1)) YYABORT; }
| tDAYZONE
{ pc->time_zone = $1 + 60 * 60; }
| tZONE tDST
{ pc->time_zone = $1 + 60 * 60; }
;
day:
tDAY
{
pc->day_ordinal = 0;
pc->day_number = $1;
}
| tDAY ','
{
pc->day_ordinal = 0;
pc->day_number = $1;
}
| tORDINAL tDAY
{
pc->day_ordinal = $1;
pc->day_number = $2;
pc->debug_ordinal_day_seen = true;
}
| tUNUMBER tDAY
{
pc->day_ordinal = $1.value;
pc->day_number = $2;
pc->debug_ordinal_day_seen = true;
}
;
date:
tUNUMBER '/' tUNUMBER
{
pc->month = $1.value;
pc->day = $3.value;
}
| tUNUMBER '/' tUNUMBER '/' tUNUMBER
{
/* Interpret as YYYY/MM/DD if the first value has 4 or more digits,
otherwise as MM/DD/YY.
The goal in recognizing YYYY/MM/DD is solely to support legacy
machine-generated dates like those in an RCS log listing. If
you want portability, use the ISO 8601 format. */
if (4 <= $1.digits)
{
if (debugging (pc))
{
intmax_t digits = $1.digits;
dbg_printf (_("warning: value %"PRIdMAX" has %"PRIdMAX" digits. "
"Assuming YYYY/MM/DD\n"),
$1.value, digits);
}
pc->year = $1;
pc->month = $3.value;
pc->day = $5.value;
}
else
{
if (debugging (pc))
dbg_printf (_("warning: value %"PRIdMAX" has less than 4 digits. "
"Assuming MM/DD/YY[YY]\n"),
$1.value);
pc->month = $1.value;
pc->day = $3.value;
pc->year = $5;
}
}
| tUNUMBER tMONTH tSNUMBER
{
/* E.g., 17-JUN-1992. */
pc->day = $1.value;
pc->month = $2;
if (ckd_sub (&pc->year.value, 0, $3.value)) YYABORT;
pc->year.digits = $3.digits;
}
| tMONTH tSNUMBER tSNUMBER
{
/* E.g., JUN-17-1992. */
pc->month = $1;
if (ckd_sub (&pc->day, 0, $2.value)) YYABORT;
if (ckd_sub (&pc->year.value, 0, $3.value)) YYABORT;
pc->year.digits = $3.digits;
}
| tMONTH tUNUMBER
{
pc->month = $1;
pc->day = $2.value;
}
| tMONTH tUNUMBER ',' tUNUMBER
{
pc->month = $1;
pc->day = $2.value;
pc->year = $4;
}
| tUNUMBER tMONTH
{
pc->day = $1.value;
pc->month = $2;
}
| tUNUMBER tMONTH tUNUMBER
{
pc->day = $1.value;
pc->month = $2;
pc->year = $3;
}
| iso_8601_date
;
iso_8601_date:
tUNUMBER tSNUMBER tSNUMBER
{
/* ISO 8601 format. YYYY-MM-DD. */
pc->year = $1;
if (ckd_sub (&pc->month, 0, $2.value)) YYABORT;
if (ckd_sub (&pc->day, 0, $3.value)) YYABORT;
}
;
rel:
relunit tAGO
{ if (! apply_relative_time (pc, $1, $2)) YYABORT; }
| relunit
{ if (! apply_relative_time (pc, $1, 1)) YYABORT; }
| dayshift
{ if (! apply_relative_time (pc, $1, 1)) YYABORT; }
;
relunit:
tORDINAL tYEAR_UNIT
{ $$ = RELATIVE_TIME_0; $$.year = $1; }
| tUNUMBER tYEAR_UNIT
{ $$ = RELATIVE_TIME_0; $$.year = $1.value; }
| tYEAR_UNIT
{ $$ = RELATIVE_TIME_0; $$.year = 1; }
| tORDINAL tMONTH_UNIT
{ $$ = RELATIVE_TIME_0; $$.month = $1; }
| tUNUMBER tMONTH_UNIT
{ $$ = RELATIVE_TIME_0; $$.month = $1.value; }
| tMONTH_UNIT
{ $$ = RELATIVE_TIME_0; $$.month = 1; }
| tORDINAL tDAY_UNIT
{ $$ = RELATIVE_TIME_0;
if (ckd_mul (&$$.day, $1, $2)) YYABORT; }
| tUNUMBER tDAY_UNIT
{ $$ = RELATIVE_TIME_0;
if (ckd_mul (&$$.day, $1.value, $2)) YYABORT; }
| tDAY_UNIT
{ $$ = RELATIVE_TIME_0; $$.day = $1; }
| tORDINAL tHOUR_UNIT
{ $$ = RELATIVE_TIME_0; $$.hour = $1; }
| tUNUMBER tHOUR_UNIT
{ $$ = RELATIVE_TIME_0; $$.hour = $1.value; }
| tHOUR_UNIT
{ $$ = RELATIVE_TIME_0; $$.hour = 1; }
| tORDINAL tMINUTE_UNIT
{ $$ = RELATIVE_TIME_0; $$.minutes = $1; }
| tUNUMBER tMINUTE_UNIT
{ $$ = RELATIVE_TIME_0; $$.minutes = $1.value; }
| tMINUTE_UNIT
{ $$ = RELATIVE_TIME_0; $$.minutes = 1; }
| tORDINAL tSEC_UNIT
{ $$ = RELATIVE_TIME_0; $$.seconds = $1; }
| tUNUMBER tSEC_UNIT
{ $$ = RELATIVE_TIME_0; $$.seconds = $1.value; }
| tSDECIMAL_NUMBER tSEC_UNIT
{ $$ = RELATIVE_TIME_0; $$.seconds = $1.tv_sec; $$.ns = $1.tv_nsec; }
| tUDECIMAL_NUMBER tSEC_UNIT
{ $$ = RELATIVE_TIME_0; $$.seconds = $1.tv_sec; $$.ns = $1.tv_nsec; }
| tSEC_UNIT
{ $$ = RELATIVE_TIME_0; $$.seconds = 1; }
| relunit_snumber
;
relunit_snumber:
tSNUMBER tYEAR_UNIT
{ $$ = RELATIVE_TIME_0; $$.year = $1.value; }
| tSNUMBER tMONTH_UNIT
{ $$ = RELATIVE_TIME_0; $$.month = $1.value; }
| tSNUMBER tDAY_UNIT
{ $$ = RELATIVE_TIME_0;
if (ckd_mul (&$$.day, $1.value, $2)) YYABORT; }
| tSNUMBER tHOUR_UNIT
{ $$ = RELATIVE_TIME_0; $$.hour = $1.value; }
| tSNUMBER tMINUTE_UNIT
{ $$ = RELATIVE_TIME_0; $$.minutes = $1.value; }
| tSNUMBER tSEC_UNIT
{ $$ = RELATIVE_TIME_0; $$.seconds = $1.value; }
;
dayshift:
tDAY_SHIFT
{ $$ = RELATIVE_TIME_0; $$.day = $1; }
;
seconds: signed_seconds | unsigned_seconds;
signed_seconds:
tSDECIMAL_NUMBER
| tSNUMBER
{ if (time_overflow ($1.value)) YYABORT;
$$ = (struct timespec) { .tv_sec = $1.value }; }
;
unsigned_seconds:
tUDECIMAL_NUMBER
| tUNUMBER
{ if (time_overflow ($1.value)) YYABORT;
$$ = (struct timespec) { .tv_sec = $1.value }; }
;
number:
tUNUMBER
{ digits_to_date_time (pc, $1); }
;
hybrid:
tUNUMBER relunit_snumber
{
/* Hybrid all-digit and relative offset, so that we accept e.g.,
"YYYYMMDD +N days" as well as "YYYYMMDD N days". */
digits_to_date_time (pc, $1);
if (! apply_relative_time (pc, $2, 1)) YYABORT;
}
;
o_colon_minutes:
/* empty */
{ $$ = -1; }
| ':' tUNUMBER
{ $$ = $2.value; }
;
%%