-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
parse_date.re
2755 lines (2437 loc) · 76.6 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-2023 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 <errno.h>
#include <math.h>
#include <assert.h>
#include <limits.h>
#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" },
};
#if defined(_POSIX_TZNAME_MAX)
# define MAX_ABBR_LEN _POSIX_TZNAME_MAX
#elif defined(TZNAME_MAX)
# define MAX_ABBR_LEN TZNAME_MAX
#else
# define MAX_ABBR_LEN 6
#endif
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 },
{ "mondays", TIMELIB_WEEKDAY, 1 },
{ "monday", TIMELIB_WEEKDAY, 1 },
{ "mon", TIMELIB_WEEKDAY, 1 },
{ "tuesdays", TIMELIB_WEEKDAY, 2 },
{ "tuesday", TIMELIB_WEEKDAY, 2 },
{ "tue", TIMELIB_WEEKDAY, 2 },
{ "wednesdays", TIMELIB_WEEKDAY, 3 },
{ "wednesday", TIMELIB_WEEKDAY, 3 },
{ "wed", TIMELIB_WEEKDAY, 3 },
{ "thursdays", TIMELIB_WEEKDAY, 4 },
{ "thursday", TIMELIB_WEEKDAY, 4 },
{ "thu", TIMELIB_WEEKDAY, 4 },
{ "fridays", TIMELIB_WEEKDAY, 5 },
{ "friday", TIMELIB_WEEKDAY, 5 },
{ "fri", TIMELIB_WEEKDAY, 5 },
{ "saturdays", TIMELIB_WEEKDAY, 6 },
{ "saturday", TIMELIB_WEEKDAY, 6 },
{ "sat", TIMELIB_WEEKDAY, 6 },
{ "sundays", TIMELIB_WEEKDAY, 0 },
{ "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 timelib_error_message *alloc_error_message(timelib_error_message **messages, int *count)
{
/* Realloc in power of two increments */
int is_pow2 = (*count & (*count - 1)) == 0;
if (is_pow2) {
size_t alloc_size = *count ? (*count * 2) : 1;
*messages = timelib_realloc(*messages, alloc_size * sizeof(timelib_error_message));
}
return *messages + (*count)++;
}
static void add_warning(Scanner *s, int error_code, const char *error)
{
timelib_error_message *message = alloc_error_message(&s->errors->warning_messages, &s->errors->warning_count);
message->error_code = error_code;
message->position = s->tok ? s->tok - s->str : 0;
message->character = s->tok ? *s->tok : 0;
message->message = timelib_strdup(error);
}
static void add_error(Scanner *s, int error_code, const char *error)
{
timelib_error_message *message = alloc_error_message(&s->errors->error_messages, &s->errors->error_count);
message->error_code = error_code;
message->position = s->tok ? s->tok - s->str : 0;
message->character = s->tok ? *s->tok : 0;
message->message = timelib_strdup(error);
}
static void add_pbf_warning(Scanner *s, int error_code, const char *error, const char *sptr, const char *cptr)
{
timelib_error_message *message = alloc_error_message(&s->errors->warning_messages, &s->errors->warning_count);
message->error_code = error_code;
message->position = cptr - sptr;
message->character = *cptr;
message->message = timelib_strdup(error);
}
static void add_pbf_error(Scanner *s, int error_code, const char *error, const char *sptr, const char *cptr)
{
timelib_error_message *message = alloc_error_message(&s->errors->error_messages, &s->errors->error_count);
message->error_code = error_code;
message->position = cptr - sptr;
message->character = *cptr;
message->message = timelib_strdup(error);
}
static timelib_sll timelib_meridian(const 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(const 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(const char **ptr, int max_length, int *scanned_length)
{
const char *begin, *end;
char *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(const char **ptr, int max_length)
{
return timelib_get_nr_ex(ptr, max_length, NULL);
}
static void timelib_skip_day_suffix(const 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(const char **ptr)
{
const char *begin, *end;
char *str;
double tmp_nr = TIMELIB_UNSET;
while ((**ptr != '.') && (**ptr != ':') && ((**ptr < '0') || (**ptr > '9'))) {
if (**ptr == '\0') {
return TIMELIB_UNSET;
}
++*ptr;
}
begin = *ptr;
while ((**ptr == '.') || (**ptr == ':') || ((**ptr >= '0') && (**ptr <= '9'))) {
++*ptr;
}
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_signed_nr(Scanner *s, const char **ptr, int max_length)
{
char *str, *str_ptr;
timelib_sll tmp_nr = 0;
int len = 0;
/* Skip over non-numeric chars */
while (((**ptr < '0') || (**ptr > '9')) && (**ptr != '+') && (**ptr != '-')) {
if (**ptr == '\0') {
add_error(s, TIMELIB_ERR_UNEXPECTED_DATA, "Found unexpected data");
return 0;
}
++*ptr;
}
/* Allocate string to feed to strtoll(): sign + length + '\0' */
str = timelib_calloc(1, max_length + 2);
str[0] = '+'; /* First position is the sign */
str_ptr = str + 1;
while ((**ptr == '+') || (**ptr == '-')) {
if (**ptr == '-') {
str[0] = str[0] == '+' ? '-' : '+';
}
++*ptr;
}
while (((**ptr < '0') || (**ptr > '9'))) {
if (**ptr == '\0') {
timelib_free(str);
add_error(s, TIMELIB_ERR_UNEXPECTED_DATA, "Found unexpected data");
return 0;
}
++*ptr;
}
while ((**ptr >= '0') && (**ptr <= '9') && len < max_length) {
*str_ptr = **ptr;
++*ptr;
++str_ptr;
++len;
}
errno = 0;
tmp_nr = strtoll(str, NULL, 10);
if (errno == ERANGE) {
timelib_free(str);
add_error(s, TIMELIB_ERR_NUMBER_OUT_OF_RANGE, "Number out of range");
return 0;
}
timelib_free(str);
return tmp_nr;
}
static timelib_sll timelib_lookup_relative_text(const char **ptr, int *behavior)
{
char *word;
const 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(const char **ptr, int *behavior)
{
while (**ptr == ' ' || **ptr == '\t' || **ptr == '-' || **ptr == '/') {
++*ptr;
}
return timelib_lookup_relative_text(ptr, behavior);
}
static timelib_long timelib_lookup_month(const char **ptr)
{
char *word;
const 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(const char **ptr)
{
while (**ptr == ' ' || **ptr == '\t' || **ptr == '-' || **ptr == '.' || **ptr == '/') {
++*ptr;
}
return timelib_lookup_month(ptr);
}
static void timelib_eat_spaces(const char **ptr)
{
do {
if (**ptr == ' ' || **ptr == '\t') {
++*ptr;
continue;
}
if ((*ptr)[0] == '\xe2' && (*ptr)[1] == '\x80' && (*ptr)[2] == '\xaf') { // NNBSP
*ptr += 3;
continue;
}
if ((*ptr)[0] == '\xc2' && (*ptr)[1] == '\xa0') { // NBSP
*ptr += 2;
continue;
}
break;
} while (true);
}
static void timelib_eat_until_separator(const char **ptr)
{
++*ptr;
while (strchr(" \t.,:;/-0123456789", **ptr) == NULL) {
++*ptr;
}
}
static const timelib_relunit* timelib_lookup_relunit(const char **ptr)
{
char *word;
const 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 add_with_overflow(Scanner *s, timelib_sll *e, timelib_sll amount, int multiplier)
{
#if TIMELIB_HAVE_BUILTIN_SADDLL_OVERFLOW
if (__builtin_saddll_overflow(*e, amount * multiplier, e)) {
add_error(s, TIMELIB_ERR_NUMBER_OUT_OF_RANGE, "Number out of range");
}
#else
*e += (amount * multiplier);
#endif
}
/**
* The time_part parameter is a flag. It can be TIMELIB_TIME_PART_KEEP in case
* the time portion should not be reset to midnight, or
* TIMELIB_TIME_PART_DONT_KEEP in case it does need to be reset. This is used
* for not overwriting the time portion for 'X weekday'.
*/
static void timelib_set_relative(const char **ptr, timelib_sll amount, int behavior, Scanner *s, int time_part)
{
const timelib_relunit* relunit;
if (!(relunit = timelib_lookup_relunit(ptr))) {
return;
}
switch (relunit->unit) {
case TIMELIB_MICROSEC: add_with_overflow(s, &s->time->relative.us, amount, relunit->multiplier); break;
case TIMELIB_SECOND: add_with_overflow(s, &s->time->relative.s, amount, relunit->multiplier); break;
case TIMELIB_MINUTE: add_with_overflow(s, &s->time->relative.i, amount, relunit->multiplier); break;
case TIMELIB_HOUR: add_with_overflow(s, &s->time->relative.h, amount, relunit->multiplier); break;
case TIMELIB_DAY: add_with_overflow(s, &s->time->relative.d, amount, relunit->multiplier); break;
case TIMELIB_MONTH: add_with_overflow(s, &s->time->relative.m, amount, relunit->multiplier); break;
case TIMELIB_YEAR: add_with_overflow(s, &s->time->relative.y, amount, relunit->multiplier); break;
case TIMELIB_WEEKDAY:
TIMELIB_HAVE_WEEKDAY_RELATIVE();
if (time_part != TIMELIB_TIME_PART_KEEP) {
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();
if (time_part != TIMELIB_TIME_PART_KEEP) {
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(const char **ptr, int *dst, char **tz_abbr, int *found)
{
char *word;
const char *begin = *ptr, *end;
timelib_long value = 0;
const timelib_tz_lookup_table *tp;
/* Only include A-Z, a-z, 0-9, /, _, and - in abbreviations/TZ IDs */
while (
(**ptr >= 'A' && **ptr <= 'Z') ||
(**ptr >= 'a' && **ptr <= 'z') ||
(**ptr >= '0' && **ptr <= '9') ||
**ptr == '/' || **ptr == '_' || **ptr == '-' || **ptr == '+'
) {
++*ptr;
}
end = *ptr;
word = timelib_calloc(1, end - begin + 1);
memcpy(word, begin, end - begin);
if (end - begin < MAX_ABBR_LEN && (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(const char **ptr, int *tz_not_found)
{
const char *begin = *ptr, *end;
timelib_long tmp;
*tz_not_found = 1;
while (isdigit(**ptr) || **ptr == ':') {
++*ptr;
}
end = *ptr;
switch (end - begin) {
case 1: /* H */
case 2: /* HH */
*tz_not_found = 0;
return sHOUR(strtol(begin, NULL, 10));
case 3: /* H:M */
case 4: /* H:MM, HH:M, HHMM */
if (begin[1] == ':') {
*tz_not_found = 0;
tmp = sHOUR(strtol(begin, NULL, 10)) + sMIN(strtol(begin + 2, NULL, 10));
return tmp;
} else if (begin[2] == ':') {
*tz_not_found = 0;
tmp = sHOUR(strtol(begin, NULL, 10)) + sMIN(strtol(begin + 3, NULL, 10));
return tmp;
} else {
*tz_not_found = 0;
tmp = strtol(begin, NULL, 10);
return sHOUR(tmp / 100) + sMIN(tmp % 100);
}
case 5: /* HH:MM */
if (begin[2] != ':') {
break;
}
*tz_not_found = 0;
tmp = sHOUR(strtol(begin, NULL, 10)) + sMIN(strtol(begin + 3, NULL, 10));
return tmp;
case 6: /* HHMMSS */
*tz_not_found = 0;
tmp = strtol(begin, NULL, 10);
tmp = sHOUR(tmp / 10000) + sMIN((tmp / 100) % 100) + (tmp % 100);
return tmp;
case 8: /* HH:MM:SS */
if (begin[2] != ':' || begin[5] != ':') {
break;
}
*tz_not_found = 0;
tmp = sHOUR(strtol(begin, NULL, 10)) + sMIN(strtol(begin + 3, NULL, 10)) + strtol(begin + 6, NULL, 10);
return tmp;
}
return 0;
}
static timelib_long timelib_parse_tz_minutes(const char **ptr, timelib_time *t)
{
timelib_long retval = TIMELIB_UNSET;
const 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(const 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;
t->dst = 0;
retval = timelib_parse_tz_cor(ptr, tz_not_found);
} else if (**ptr == '-') {
++*ptr;
t->is_localtime = 1;
t->zone_type = TIMELIB_ZONETYPE_OFFSET;
t->dst = 0;
retval = -1 * timelib_parse_tz_cor(ptr, tz_not_found);
} 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;
t->dst = *dst;
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)