forked from derickr/timelib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_date.re
2531 lines (2268 loc) · 73.3 KB
/
parse_date.re
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
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2019 Derick Rethans
* Copyright (c) 2018 MongoDB, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "timelib.h"
#include "timelib_private.h"
#include <ctype.h>
#include <math.h>
#include <assert.h>
#if defined(_MSC_VER)
# define strtoll(s, f, b) _atoi64(s)
#elif !defined(HAVE_STRTOLL)
# if defined(HAVE_ATOLL)
# define strtoll(s, f, b) atoll(s)
# else
# define strtoll(s, f, b) strtol(s, f, b)
# endif
#endif
#define EOI 257
#define TIME 258
#define DATE 259
#define TIMELIB_XMLRPC_SOAP 260
#define TIMELIB_TIME12 261
#define TIMELIB_TIME24 262
#define TIMELIB_GNU_NOCOLON 263
#define TIMELIB_GNU_NOCOLON_TZ 264
#define TIMELIB_ISO_NOCOLON 265
#define TIMELIB_AMERICAN 266
#define TIMELIB_ISO_DATE 267
#define TIMELIB_DATE_FULL 268
#define TIMELIB_DATE_TEXT 269
#define TIMELIB_DATE_NOCOLON 270
#define TIMELIB_PG_YEARDAY 271
#define TIMELIB_PG_TEXT 272
#define TIMELIB_PG_REVERSE 273
#define TIMELIB_CLF 274
#define TIMELIB_DATE_NO_DAY 275
#define TIMELIB_SHORTDATE_WITH_TIME 276
#define TIMELIB_DATE_FULL_POINTED 277
#define TIMELIB_TIME24_WITH_ZONE 278
#define TIMELIB_ISO_WEEK 279
#define TIMELIB_LF_DAY_OF_MONTH 280
#define TIMELIB_WEEK_DAY_OF_MONTH 281
#define TIMELIB_TIMEZONE 300
#define TIMELIB_AGO 301
#define TIMELIB_RELATIVE 310
#define TIMELIB_ERROR 999
/* Some compilers like AIX, defines uchar in sys/types.h */
#undef uchar
typedef unsigned char uchar;
#define BSIZE 8192
#define YYCTYPE uchar
#define YYCURSOR cursor
#define YYLIMIT s->lim
#define YYMARKER s->ptr
#define YYFILL(n) return EOI;
#define RET(i) {s->cur = cursor; return i;}
#define timelib_string_free timelib_free
#define TIMELIB_HAVE_TIME() { if (s->time->have_time) { add_error(s, TIMELIB_ERR_DOUBLE_TIME, "Double time specification"); timelib_string_free(str); return TIMELIB_ERROR; } else { s->time->have_time = 1; s->time->h = 0; s->time->i = 0; s->time->s = 0; s->time->us = 0; } }
#define TIMELIB_UNHAVE_TIME() { s->time->have_time = 0; s->time->h = 0; s->time->i = 0; s->time->s = 0; s->time->us = 0; }
#define TIMELIB_HAVE_DATE() { if (s->time->have_date) { add_error(s, TIMELIB_ERR_DOUBLE_DATE, "Double date specification"); timelib_string_free(str); return TIMELIB_ERROR; } else { s->time->have_date = 1; } }
#define TIMELIB_UNHAVE_DATE() { s->time->have_date = 0; s->time->d = 0; s->time->m = 0; s->time->y = 0; }
#define TIMELIB_HAVE_RELATIVE() { s->time->have_relative = 1; }
#define TIMELIB_HAVE_WEEKDAY_RELATIVE() { s->time->have_relative = 1; s->time->relative.have_weekday_relative = 1; }
#define TIMELIB_HAVE_SPECIAL_RELATIVE() { s->time->have_relative = 1; s->time->relative.have_special_relative = 1; }
#define TIMELIB_HAVE_TZ() { s->cur = cursor; if (s->time->have_zone) { s->time->have_zone > 1 ? add_error(s, TIMELIB_ERR_DOUBLE_TZ, "Double timezone specification") : add_warning(s, TIMELIB_WARN_DOUBLE_TZ, "Double timezone specification"); timelib_string_free(str); s->time->have_zone++; return TIMELIB_ERROR; } else { s->time->have_zone++; } }
#define TIMELIB_INIT s->cur = cursor; str = timelib_string(s); ptr = str
#define TIMELIB_DEINIT timelib_string_free(str)
#define TIMELIB_ADJUST_RELATIVE_WEEKDAY() if (in->time.have_weekday_relative && (in.rel.d > 0)) { in.rel.d -= 7; }
#define TIMELIB_PROCESS_YEAR(x, l) { \
if (((x) == TIMELIB_UNSET) || ((l) >= 4)) { \
/* (x) = 0; */ \
} else if ((x) < 100) { \
if ((x) < 70) { \
(x) += 2000; \
} else { \
(x) += 1900; \
} \
} \
}
#ifdef DEBUG_PARSER
#define DEBUG_OUTPUT(s) printf("%s\n", s);
#define YYDEBUG(s,c) { if (s != -1) { printf("state: %d ", s); printf("[%c]\n", c); } }
#else
#define DEBUG_OUTPUT(s)
#define YYDEBUG(s,c)
#endif
typedef struct _timelib_elems {
unsigned int c; /* Number of elements */
char **v; /* Values */
} timelib_elems;
typedef struct _Scanner {
int fd;
uchar *lim, *str, *ptr, *cur, *tok, *pos;
unsigned int line, len;
timelib_error_container *errors;
timelib_time *time;
const timelib_tzdb *tzdb;
} Scanner;
typedef struct _timelib_lookup_table {
const char *name;
int type;
int value;
} timelib_lookup_table;
typedef struct _timelib_relunit {
const char *name;
int unit;
int multiplier;
} timelib_relunit;
/* The timezone table. */
static const timelib_tz_lookup_table timelib_timezone_lookup[] = {
#include "timezonemap.h"
{ NULL, 0, 0, NULL },
};
static const timelib_tz_lookup_table timelib_timezone_fallbackmap[] = {
#include "fallbackmap.h"
{ NULL, 0, 0, NULL },
};
static const timelib_tz_lookup_table timelib_timezone_utc[] = {
{ "utc", 0, 0, "UTC" },
};
static timelib_relunit const timelib_relunit_lookup[] = {
{ "ms", TIMELIB_MICROSEC, 1000 },
{ "msec", TIMELIB_MICROSEC, 1000 },
{ "msecs", TIMELIB_MICROSEC, 1000 },
{ "millisecond", TIMELIB_MICROSEC, 1000 },
{ "milliseconds", TIMELIB_MICROSEC, 1000 },
{ "µs", TIMELIB_MICROSEC, 1 },
{ "usec", TIMELIB_MICROSEC, 1 },
{ "usecs", TIMELIB_MICROSEC, 1 },
{ "µsec", TIMELIB_MICROSEC, 1 },
{ "µsecs", TIMELIB_MICROSEC, 1 },
{ "microsecond", TIMELIB_MICROSEC, 1 },
{ "microseconds", TIMELIB_MICROSEC, 1 },
{ "sec", TIMELIB_SECOND, 1 },
{ "secs", TIMELIB_SECOND, 1 },
{ "second", TIMELIB_SECOND, 1 },
{ "seconds", TIMELIB_SECOND, 1 },
{ "min", TIMELIB_MINUTE, 1 },
{ "mins", TIMELIB_MINUTE, 1 },
{ "minute", TIMELIB_MINUTE, 1 },
{ "minutes", TIMELIB_MINUTE, 1 },
{ "hour", TIMELIB_HOUR, 1 },
{ "hours", TIMELIB_HOUR, 1 },
{ "day", TIMELIB_DAY, 1 },
{ "days", TIMELIB_DAY, 1 },
{ "week", TIMELIB_DAY, 7 },
{ "weeks", TIMELIB_DAY, 7 },
{ "fortnight", TIMELIB_DAY, 14 },
{ "fortnights", TIMELIB_DAY, 14 },
{ "forthnight", TIMELIB_DAY, 14 },
{ "forthnights", TIMELIB_DAY, 14 },
{ "month", TIMELIB_MONTH, 1 },
{ "months", TIMELIB_MONTH, 1 },
{ "year", TIMELIB_YEAR, 1 },
{ "years", TIMELIB_YEAR, 1 },
{ "monday", TIMELIB_WEEKDAY, 1 },
{ "mon", TIMELIB_WEEKDAY, 1 },
{ "tuesday", TIMELIB_WEEKDAY, 2 },
{ "tue", TIMELIB_WEEKDAY, 2 },
{ "wednesday", TIMELIB_WEEKDAY, 3 },
{ "wed", TIMELIB_WEEKDAY, 3 },
{ "thursday", TIMELIB_WEEKDAY, 4 },
{ "thu", TIMELIB_WEEKDAY, 4 },
{ "friday", TIMELIB_WEEKDAY, 5 },
{ "fri", TIMELIB_WEEKDAY, 5 },
{ "saturday", TIMELIB_WEEKDAY, 6 },
{ "sat", TIMELIB_WEEKDAY, 6 },
{ "sunday", TIMELIB_WEEKDAY, 0 },
{ "sun", TIMELIB_WEEKDAY, 0 },
{ "weekday", TIMELIB_SPECIAL, TIMELIB_SPECIAL_WEEKDAY },
{ "weekdays", TIMELIB_SPECIAL, TIMELIB_SPECIAL_WEEKDAY },
{ NULL, 0, 0 }
};
/* The relative text table. */
static timelib_lookup_table const timelib_reltext_lookup[] = {
{ "first", 0, 1 },
{ "next", 0, 1 },
{ "second", 0, 2 },
{ "third", 0, 3 },
{ "fourth", 0, 4 },
{ "fifth", 0, 5 },
{ "sixth", 0, 6 },
{ "seventh", 0, 7 },
{ "eight", 0, 8 },
{ "eighth", 0, 8 },
{ "ninth", 0, 9 },
{ "tenth", 0, 10 },
{ "eleventh", 0, 11 },
{ "twelfth", 0, 12 },
{ "last", 0, -1 },
{ "previous", 0, -1 },
{ "this", 1, 0 },
{ NULL, 1, 0 }
};
/* The month table. */
static timelib_lookup_table const timelib_month_lookup[] = {
{ "jan", 0, 1 },
{ "feb", 0, 2 },
{ "mar", 0, 3 },
{ "apr", 0, 4 },
{ "may", 0, 5 },
{ "jun", 0, 6 },
{ "jul", 0, 7 },
{ "aug", 0, 8 },
{ "sep", 0, 9 },
{ "sept", 0, 9 },
{ "oct", 0, 10 },
{ "nov", 0, 11 },
{ "dec", 0, 12 },
{ "i", 0, 1 },
{ "ii", 0, 2 },
{ "iii", 0, 3 },
{ "iv", 0, 4 },
{ "v", 0, 5 },
{ "vi", 0, 6 },
{ "vii", 0, 7 },
{ "viii", 0, 8 },
{ "ix", 0, 9 },
{ "x", 0, 10 },
{ "xi", 0, 11 },
{ "xii", 0, 12 },
{ "january", 0, 1 },
{ "february", 0, 2 },
{ "march", 0, 3 },
{ "april", 0, 4 },
{ "may", 0, 5 },
{ "june", 0, 6 },
{ "july", 0, 7 },
{ "august", 0, 8 },
{ "september", 0, 9 },
{ "october", 0, 10 },
{ "november", 0, 11 },
{ "december", 0, 12 },
{ NULL, 0, 0 }
};
#if 0
static char* timelib_ltrim(char *s)
{
char *ptr = s;
while (ptr[0] == ' ' || ptr[0] == '\t') {
ptr++;
}
return ptr;
}
#endif
#if 0
uchar *fill(Scanner *s, uchar *cursor){
if(!s->eof){
unsigned int cnt = s->tok - s->bot;
if(cnt){
memcpy(s->bot, s->tok, s->lim - s->tok);
s->tok = s->bot;
s->ptr -= cnt;
cursor -= cnt;
s->pos -= cnt;
s->lim -= cnt;
}
if((s->top - s->lim) < BSIZE){
uchar *buf = (uchar*) timelib_malloc(((s->lim - s->bot) + BSIZE)*sizeof(uchar));
memcpy(buf, s->tok, s->lim - s->tok);
s->tok = buf;
s->ptr = &buf[s->ptr - s->bot];
cursor = &buf[cursor - s->bot];
s->pos = &buf[s->pos - s->bot];
s->lim = &buf[s->lim - s->bot];
s->top = &s->lim[BSIZE];
timelib_free(s->bot);
s->bot = buf;
}
if((cnt = read(s->fd, (char*) s->lim, BSIZE)) != BSIZE){
s->eof = &s->lim[cnt]; *(s->eof)++ = '\n';
}
s->lim += cnt;
}
return cursor;
}
#endif
static void add_warning(Scanner *s, int error_code, char *error)
{
s->errors->warning_count++;
s->errors->warning_messages = timelib_realloc(s->errors->warning_messages, s->errors->warning_count * sizeof(timelib_error_message));
s->errors->warning_messages[s->errors->warning_count - 1].error_code = error_code;
s->errors->warning_messages[s->errors->warning_count - 1].position = s->tok ? s->tok - s->str : 0;
s->errors->warning_messages[s->errors->warning_count - 1].character = s->tok ? *s->tok : 0;
s->errors->warning_messages[s->errors->warning_count - 1].message = timelib_strdup(error);
}
static void add_error(Scanner *s, int error_code, char *error)
{
s->errors->error_count++;
s->errors->error_messages = timelib_realloc(s->errors->error_messages, s->errors->error_count * sizeof(timelib_error_message));
s->errors->error_messages[s->errors->error_count - 1].error_code = error_code;
s->errors->error_messages[s->errors->error_count - 1].position = s->tok ? s->tok - s->str : 0;
s->errors->error_messages[s->errors->error_count - 1].character = s->tok ? *s->tok : 0;
s->errors->error_messages[s->errors->error_count - 1].message = timelib_strdup(error);
}
static void add_pbf_warning(Scanner *s, int error_code, char *error, char *sptr, char *cptr)
{
s->errors->warning_count++;
s->errors->warning_messages = timelib_realloc(s->errors->warning_messages, s->errors->warning_count * sizeof(timelib_error_message));
s->errors->warning_messages[s->errors->warning_count - 1].error_code = error_code;
s->errors->warning_messages[s->errors->warning_count - 1].position = cptr - sptr;
s->errors->warning_messages[s->errors->warning_count - 1].character = *cptr;
s->errors->warning_messages[s->errors->warning_count - 1].message = timelib_strdup(error);
}
static void add_pbf_error(Scanner *s, int error_code, char *error, char *sptr, char *cptr)
{
s->errors->error_count++;
s->errors->error_messages = timelib_realloc(s->errors->error_messages, s->errors->error_count * sizeof(timelib_error_message));
s->errors->error_messages[s->errors->error_count - 1].error_code = error_code;
s->errors->error_messages[s->errors->error_count - 1].position = cptr - sptr;
s->errors->error_messages[s->errors->error_count - 1].character = *cptr;
s->errors->error_messages[s->errors->error_count - 1].message = timelib_strdup(error);
}
static timelib_sll timelib_meridian(char **ptr, timelib_sll h)
{
timelib_sll retval = 0;
while (!strchr("AaPp", **ptr)) {
++*ptr;
}
if (**ptr == 'a' || **ptr == 'A') {
if (h == 12) {
retval = -12;
}
} else if (h != 12) {
retval = 12;
}
++*ptr;
if (**ptr == '.') {
++*ptr;
}
if (**ptr == 'M' || **ptr == 'm') {
++*ptr;
}
if (**ptr == '.') {
++*ptr;
}
return retval;
}
static timelib_sll timelib_meridian_with_check(char **ptr, timelib_sll h)
{
timelib_sll retval = 0;
while (**ptr && !strchr("AaPp", **ptr)) {
++*ptr;
}
if(!**ptr) {
return TIMELIB_UNSET;
}
if (**ptr == 'a' || **ptr == 'A') {
if (h == 12) {
retval = -12;
}
} else if (h != 12) {
retval = 12;
}
++*ptr;
if (**ptr == '.') {
++*ptr;
if (**ptr != 'm' && **ptr != 'M') {
return TIMELIB_UNSET;
}
++*ptr;
if (**ptr != '.' ) {
return TIMELIB_UNSET;
}
++*ptr;
} else if (**ptr == 'm' || **ptr == 'M') {
++*ptr;
} else {
return TIMELIB_UNSET;
}
return retval;
}
static char *timelib_string(Scanner *s)
{
char *tmp = timelib_calloc(1, s->cur - s->tok + 1);
memcpy(tmp, s->tok, s->cur - s->tok);
return tmp;
}
static timelib_sll timelib_get_nr_ex(char **ptr, int max_length, int *scanned_length)
{
char *begin, *end, *str;
timelib_sll tmp_nr = TIMELIB_UNSET;
int len = 0;
while ((**ptr < '0') || (**ptr > '9')) {
if (**ptr == '\0') {
return TIMELIB_UNSET;
}
++*ptr;
}
begin = *ptr;
while ((**ptr >= '0') && (**ptr <= '9') && len < max_length) {
++*ptr;
++len;
}
end = *ptr;
if (scanned_length) {
*scanned_length = end - begin;
}
str = timelib_calloc(1, end - begin + 1);
memcpy(str, begin, end - begin);
tmp_nr = strtoll(str, NULL, 10);
timelib_free(str);
return tmp_nr;
}
static timelib_sll timelib_get_nr(char **ptr, int max_length)
{
return timelib_get_nr_ex(ptr, max_length, NULL);
}
static void timelib_skip_day_suffix(char **ptr)
{
if (isspace(**ptr)) {
return;
}
if (!timelib_strncasecmp(*ptr, "nd", 2) || !timelib_strncasecmp(*ptr, "rd", 2) ||!timelib_strncasecmp(*ptr, "st", 2) || !timelib_strncasecmp(*ptr, "th", 2)) {
*ptr += 2;
}
}
static timelib_sll timelib_get_frac_nr(char **ptr, int max_length)
{
char *begin, *end, *str;
double tmp_nr = TIMELIB_UNSET;
int len = 0;
while ((**ptr != '.') && (**ptr != ':') && ((**ptr < '0') || (**ptr > '9'))) {
if (**ptr == '\0') {
return TIMELIB_UNSET;
}
++*ptr;
}
begin = *ptr;
while (((**ptr == '.') || (**ptr == ':') || ((**ptr >= '0') && (**ptr <= '9'))) && len < max_length) {
++*ptr;
++len;
}
end = *ptr;
str = timelib_calloc(1, end - begin);
memcpy(str, begin + 1, end - begin - 1);
tmp_nr = strtod(str, NULL) * pow(10, 7 - (end - begin));
timelib_free(str);
return tmp_nr;
}
static timelib_ull timelib_get_unsigned_nr(char **ptr, int max_length)
{
timelib_ull dir = 1;
while (((**ptr < '0') || (**ptr > '9')) && (**ptr != '+') && (**ptr != '-')) {
if (**ptr == '\0') {
return TIMELIB_UNSET;
}
++*ptr;
}
while (**ptr == '+' || **ptr == '-')
{
if (**ptr == '-') {
dir *= -1;
}
++*ptr;
}
return dir * timelib_get_nr(ptr, max_length);
}
static timelib_sll timelib_lookup_relative_text(char **ptr, int *behavior)
{
char *word;
char *begin = *ptr, *end;
timelib_sll value = 0;
const timelib_lookup_table *tp;
while ((**ptr >= 'A' && **ptr <= 'Z') || (**ptr >= 'a' && **ptr <= 'z')) {
++*ptr;
}
end = *ptr;
word = timelib_calloc(1, end - begin + 1);
memcpy(word, begin, end - begin);
for (tp = timelib_reltext_lookup; tp->name; tp++) {
if (timelib_strcasecmp(word, tp->name) == 0) {
value = tp->value;
*behavior = tp->type;
}
}
timelib_free(word);
return value;
}
static timelib_sll timelib_get_relative_text(char **ptr, int *behavior)
{
while (**ptr == ' ' || **ptr == '\t' || **ptr == '-' || **ptr == '/') {
++*ptr;
}
return timelib_lookup_relative_text(ptr, behavior);
}
static timelib_long timelib_lookup_month(char **ptr)
{
char *word;
char *begin = *ptr, *end;
timelib_long value = 0;
const timelib_lookup_table *tp;
while ((**ptr >= 'A' && **ptr <= 'Z') || (**ptr >= 'a' && **ptr <= 'z')) {
++*ptr;
}
end = *ptr;
word = timelib_calloc(1, end - begin + 1);
memcpy(word, begin, end - begin);
for (tp = timelib_month_lookup; tp->name; tp++) {
if (timelib_strcasecmp(word, tp->name) == 0) {
value = tp->value;
}
}
timelib_free(word);
return value;
}
static timelib_long timelib_get_month(char **ptr)
{
while (**ptr == ' ' || **ptr == '\t' || **ptr == '-' || **ptr == '.' || **ptr == '/') {
++*ptr;
}
return timelib_lookup_month(ptr);
}
static void timelib_eat_spaces(char **ptr)
{
while (**ptr == ' ' || **ptr == '\t') {
++*ptr;
}
}
static void timelib_eat_until_separator(char **ptr)
{
++*ptr;
while (strchr(" \t.,:;/-0123456789", **ptr) == NULL) {
++*ptr;
}
}
static const timelib_relunit* timelib_lookup_relunit(char **ptr)
{
char *word;
char *begin = *ptr, *end;
const timelib_relunit *tp, *value = NULL;
while (**ptr != '\0' && **ptr != ' ' && **ptr != ',' && **ptr != '\t' && **ptr != ';' && **ptr != ':' &&
**ptr != '/' && **ptr != '.' && **ptr != '-' && **ptr != '(' && **ptr != ')' ) {
++*ptr;
}
end = *ptr;
word = timelib_calloc(1, end - begin + 1);
memcpy(word, begin, end - begin);
for (tp = timelib_relunit_lookup; tp->name; tp++) {
if (timelib_strcasecmp(word, tp->name) == 0) {
value = tp;
break;
}
}
timelib_free(word);
return value;
}
static void timelib_set_relative(char **ptr, timelib_sll amount, int behavior, Scanner *s)
{
const timelib_relunit* relunit;
if (!(relunit = timelib_lookup_relunit(ptr))) {
return;
}
switch (relunit->unit) {
case TIMELIB_MICROSEC: s->time->relative.us += amount * relunit->multiplier; break;
case TIMELIB_SECOND: s->time->relative.s += amount * relunit->multiplier; break;
case TIMELIB_MINUTE: s->time->relative.i += amount * relunit->multiplier; break;
case TIMELIB_HOUR: s->time->relative.h += amount * relunit->multiplier; break;
case TIMELIB_DAY: s->time->relative.d += amount * relunit->multiplier; break;
case TIMELIB_MONTH: s->time->relative.m += amount * relunit->multiplier; break;
case TIMELIB_YEAR: s->time->relative.y += amount * relunit->multiplier; break;
case TIMELIB_WEEKDAY:
TIMELIB_HAVE_WEEKDAY_RELATIVE();
TIMELIB_UNHAVE_TIME();
s->time->relative.d += (amount > 0 ? amount - 1 : amount) * 7;
s->time->relative.weekday = relunit->multiplier;
s->time->relative.weekday_behavior = behavior;
break;
case TIMELIB_SPECIAL:
TIMELIB_HAVE_SPECIAL_RELATIVE();
TIMELIB_UNHAVE_TIME();
s->time->relative.special.type = relunit->multiplier;
s->time->relative.special.amount = amount;
}
}
static const timelib_tz_lookup_table* abbr_search(const char *word, timelib_long gmtoffset, int isdst)
{
int first_found = 0;
const timelib_tz_lookup_table *tp, *first_found_elem = NULL;
const timelib_tz_lookup_table *fmp;
if (timelib_strcasecmp("utc", word) == 0 || timelib_strcasecmp("gmt", word) == 0) {
return timelib_timezone_utc;
}
for (tp = timelib_timezone_lookup; tp->name; tp++) {
if (timelib_strcasecmp(word, tp->name) == 0) {
if (!first_found) {
first_found = 1;
first_found_elem = tp;
if (gmtoffset == -1) {
return tp;
}
}
if (tp->gmtoffset == gmtoffset) {
return tp;
}
}
}
if (first_found) {
return first_found_elem;
}
/* Still didn't find anything, let's find the zone solely based on
* offset/isdst then */
for (fmp = timelib_timezone_fallbackmap; fmp->name; fmp++) {
if (fmp->gmtoffset == gmtoffset && fmp->type == isdst) {
return fmp;
}
}
return NULL;
}
static timelib_long timelib_lookup_abbr(char **ptr, int *dst, char **tz_abbr, int *found)
{
char *word;
char *begin = *ptr, *end;
timelib_long value = 0;
const timelib_tz_lookup_table *tp;
while (**ptr != '\0' && **ptr != ')' && **ptr != ' ') {
++*ptr;
}
end = *ptr;
word = timelib_calloc(1, end - begin + 1);
memcpy(word, begin, end - begin);
if ((tp = abbr_search(word, -1, 0))) {
value = tp->gmtoffset;
*dst = tp->type;
value -= tp->type * 3600;
*found = 1;
} else {
*found = 0;
}
*tz_abbr = word;
return value;
}
#define sHOUR(a) (int)(a * 3600)
#define sMIN(a) (int)(a * 60)
static timelib_long timelib_parse_tz_cor(char **ptr)
{
char *begin = *ptr, *end;
timelib_long tmp;
while (isdigit(**ptr) || **ptr == ':') {
++*ptr;
}
end = *ptr;
switch (end - begin) {
case 1: /* H */
case 2: /* HH */
return sHOUR(strtol(begin, NULL, 10));
break;
case 3: /* H:M */
case 4: /* H:MM, HH:M, HHMM */
if (begin[1] == ':') {
tmp = sHOUR(strtol(begin, NULL, 10)) + sMIN(strtol(begin + 2, NULL, 10));
return tmp;
} else if (begin[2] == ':') {
tmp = sHOUR(strtol(begin, NULL, 10)) + sMIN(strtol(begin + 3, NULL, 10));
return tmp;
} else {
tmp = strtol(begin, NULL, 10);
return sHOUR(tmp / 100) + sMIN(tmp % 100);
}
case 5: /* HH:MM */
tmp = sHOUR(strtol(begin, NULL, 10)) + sMIN(strtol(begin + 3, NULL, 10));
return tmp;
}
return 0;
}
static timelib_long timelib_parse_tz_minutes(char **ptr, timelib_time *t)
{
timelib_long retval = TIMELIB_UNSET;
char *begin = *ptr;
/* First character must be +/- */
if (**ptr != '+' && **ptr != '-') {
return retval;
}
++*ptr;
while (isdigit(**ptr)) {
++*ptr;
}
if (*begin == '+') {
t->is_localtime = 1;
t->zone_type = TIMELIB_ZONETYPE_OFFSET;
t->dst = 0;
retval = sMIN(strtol(begin + 1, NULL, 10));
} else if (*begin == '-') {
t->is_localtime = 1;
t->zone_type = TIMELIB_ZONETYPE_OFFSET;
t->dst = 0;
retval = -1 * sMIN(strtol(begin + 1, NULL, 10));
}
return retval;
}
timelib_long timelib_parse_zone(char **ptr, int *dst, timelib_time *t, int *tz_not_found, const timelib_tzdb *tzdb, timelib_tz_get_wrapper tz_wrapper)
{
timelib_tzinfo *res;
timelib_long retval = 0;
*tz_not_found = 0;
while (**ptr == ' ' || **ptr == '\t' || **ptr == '(') {
++*ptr;
}
if ((*ptr)[0] == 'G' && (*ptr)[1] == 'M' && (*ptr)[2] == 'T' && ((*ptr)[3] == '+' || (*ptr)[3] == '-')) {
*ptr += 3;
}
if (**ptr == '+') {
++*ptr;
t->is_localtime = 1;
t->zone_type = TIMELIB_ZONETYPE_OFFSET;
*tz_not_found = 0;
t->dst = 0;
retval = timelib_parse_tz_cor(ptr);
} else if (**ptr == '-') {
++*ptr;
t->is_localtime = 1;
t->zone_type = TIMELIB_ZONETYPE_OFFSET;
*tz_not_found = 0;
t->dst = 0;
retval = -1 * timelib_parse_tz_cor(ptr);
} else {
int found = 0;
timelib_long offset = 0;
char *tz_abbr;
t->is_localtime = 1;
/* First, we lookup by abbreviation only */
offset = timelib_lookup_abbr(ptr, dst, &tz_abbr, &found);
if (found) {
t->zone_type = TIMELIB_ZONETYPE_ABBR;
timelib_time_tz_abbr_update(t, tz_abbr);
}
/* Otherwise, we look if we have a TimeZone identifier */
if (!found || strcmp("UTC", tz_abbr) == 0) {
int dummy_error_code;
if ((res = tz_wrapper(tz_abbr, tzdb, &dummy_error_code)) != NULL) {
t->tz_info = res;
t->zone_type = TIMELIB_ZONETYPE_ID;
found++;
}
}
timelib_free(tz_abbr);
*tz_not_found = (found == 0);
retval = offset;
}
while (**ptr == ')') {
++*ptr;
}
return retval;
}
#define timelib_split_free(arg) { \
int i; \
for (i = 0; i < arg.c; i++) { \
timelib_free(arg.v[i]); \
} \
if (arg.v) { \
timelib_free(arg.v); \
} \
}
static int scan(Scanner *s, timelib_tz_get_wrapper tz_get_wrapper)
{
uchar *cursor = s->cur;
char *str, *ptr = NULL;
std:
s->tok = cursor;
s->len = 0;
/*!re2c
any = [\000-\377];
space = [ \t]+;
frac = "."[0-9]+;
ago = 'ago';
hour24 = [01]?[0-9] | "2"[0-4];
hour24lz = [01][0-9] | "2"[0-4];
hour12 = "0"?[1-9] | "1"[0-2];
minute = [0-5]?[0-9];
minutelz = [0-5][0-9];
second = minute | "60";
secondlz = minutelz | "60";
meridian = ([AaPp] "."? [Mm] "."?) [\000\t ];
tz = "("? [A-Za-z]{1,6} ")"? | [A-Z][a-z]+([_/-][A-Za-z]+)+;
tzcorrection = "GMT"? [+-] hour24 ":"? minute?;
daysuf = "st" | "nd" | "rd" | "th";
month = "0"? [0-9] | "1"[0-2];
day = (([0-2]?[0-9]) | ("3"[01])) daysuf?;
year = [0-9]{1,4};
year2 = [0-9]{2};
year4 = [0-9]{4};
year4withsign = [+-]? [0-9]{4};
yearx = [+-] [0-9]{5,19};
dayofyear = "00"[1-9] | "0"[1-9][0-9] | [1-2][0-9][0-9] | "3"[0-5][0-9] | "36"[0-6];
weekofyear = "0"[1-9] | [1-4][0-9] | "5"[0-3];
monthlz = "0" [0-9] | "1" [0-2];
daylz = "0" [0-9] | [1-2][0-9] | "3" [01];
dayfull = 'sunday' | 'monday' | 'tuesday' | 'wednesday' | 'thursday' | 'friday' | 'saturday';
dayabbr = 'sun' | 'mon' | 'tue' | 'wed' | 'thu' | 'fri' | 'sat' | 'sun';
dayspecial = 'weekday' | 'weekdays';
daytext = dayfull | dayabbr | dayspecial;
monthfull = 'january' | 'february' | 'march' | 'april' | 'may' | 'june' | 'july' | 'august' | 'september' | 'october' | 'november' | 'december';
monthabbr = 'jan' | 'feb' | 'mar' | 'apr' | 'may' | 'jun' | 'jul' | 'aug' | 'sep' | 'sept' | 'oct' | 'nov' | 'dec';
monthroman = "I" | "II" | "III" | "IV" | "V" | "VI" | "VII" | "VIII" | "IX" | "X" | "XI" | "XII";
monthtext = monthfull | monthabbr | monthroman;
/* Time formats */
timetiny12 = hour12 space? meridian;
timeshort12 = hour12[:.]minutelz space? meridian;
timelong12 = hour12[:.]minute[:.]secondlz space? meridian;
timeshort24 = 't'? hour24[:.]minute;
timelong24 = 't'? hour24[:.]minute[:.]second;
iso8601long = 't'? hour24 [:.] minute [:.] second frac;
/* iso8601shorttz = hour24 [:] minutelz space? (tzcorrection | tz); */
iso8601normtz = 't'? hour24 [:.] minute [:.] secondlz space? (tzcorrection | tz);
/* iso8601longtz = hour24 [:] minute [:] secondlz frac space? (tzcorrection | tz); */
gnunocolon = 't'? hour24lz minutelz;
/* gnunocolontz = hour24lz minutelz space? (tzcorrection | tz); */
iso8601nocolon = 't'? hour24lz minutelz secondlz;
/* iso8601nocolontz = hour24lz minutelz secondlz space? (tzcorrection | tz); */
/* Date formats */
americanshort = month "/" day;
american = month "/" day "/" year;
iso8601dateslash = year4 "/" monthlz "/" daylz "/"?;
dateslash = year4 "/" month "/" day;
iso8601date4 = year4withsign "-" monthlz "-" daylz;
iso8601date2 = year2 "-" monthlz "-" daylz;
iso8601datex = yearx "-" monthlz "-" daylz;
gnudateshorter = year4 "-" month;
gnudateshort = year "-" month "-" day;
pointeddate4 = day [.\t-] month [.-] year4;
pointeddate2 = day [.\t] month "." year2;
datefull = day ([ \t.-])* monthtext ([ \t.-])* year;
datenoday = monthtext ([ .\t-])* year4;
datenodayrev = year4 ([ .\t-])* monthtext;
datetextual = monthtext ([ .\t-])* day [,.stndrh\t ]+ year;
datenoyear = monthtext ([ .\t-])* day ([,.stndrh\t ]+|[\000]);
datenoyearrev = day ([ .\t-])* monthtext;
datenocolon = year4 monthlz daylz;
/* Special formats */
soap = year4 "-" monthlz "-" daylz "T" hour24lz ":" minutelz ":" secondlz frac tzcorrection?;
xmlrpc = year4 monthlz daylz "T" hour24 ":" minutelz ":" secondlz;
xmlrpcnocolon = year4 monthlz daylz 't' hour24 minutelz secondlz;
wddx = year4 "-" month "-" day "T" hour24 ":" minute ":" second;
pgydotd = year4 "."? dayofyear;
pgtextshort = monthabbr "-" daylz "-" year;
pgtextreverse = year "-" monthabbr "-" daylz;
mssqltime = hour12 ":" minutelz ":" secondlz [:.] [0-9]+ meridian;
isoweekday = year4 "-"? "W" weekofyear "-"? [0-7];
isoweek = year4 "-"? "W" weekofyear;
exif = year4 ":" monthlz ":" daylz " " hour24lz ":" minutelz ":" secondlz;
firstdayof = 'first day of';
lastdayof = 'last day of';
backof = 'back of ' hour24 (space? meridian)?;
frontof = 'front of ' hour24 (space? meridian)?;
/* Common Log Format: 10/Oct/2000:13:55:36 -0700 */
clf = day "/" monthabbr "/" year4 ":" hour24lz ":" minutelz ":" secondlz space tzcorrection;
/* Timestamp format: @1126396800 */
timestamp = "@" "-"? [0-9]+;
timestampms = "@" "-"? [0-9]+ "." [0-9]{6};
/* To fix some ambiguities */
dateshortwithtimeshort12 = datenoyear timeshort12;
dateshortwithtimelong12 = datenoyear timelong12;
dateshortwithtimeshort = datenoyear timeshort24;
dateshortwithtimelong = datenoyear timelong24;
dateshortwithtimelongtz = datenoyear iso8601normtz;
/*
* Relative regexps