forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jsdate.cpp
3143 lines (2691 loc) · 85.4 KB
/
jsdate.cpp
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sts=4 et sw=4 tw=99:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* JS date methods.
*
* "For example, OS/360 devotes 26 bytes of the permanently
* resident date-turnover routine to the proper handling of
* December 31 on leap years (when it is Day 366). That
* might have been left to the operator."
*
* Frederick Brooks, 'The Second-System Effect'.
*/
#include "jsdate.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/FloatingPoint.h"
#include <ctype.h>
#include <math.h>
#include <string.h>
#include "jsapi.h"
#include "jscntxt.h"
#include "jsnum.h"
#include "jsobj.h"
#include "jsprf.h"
#include "jsstr.h"
#include "jstypes.h"
#include "jsutil.h"
#include "jswrapper.h"
#include "prmjtime.h"
#include "js/Date.h"
#include "vm/DateTime.h"
#include "vm/GlobalObject.h"
#include "vm/Interpreter.h"
#include "vm/NumericConversions.h"
#include "vm/String.h"
#include "vm/StringBuffer.h"
#include "jsobjinlines.h"
using namespace js;
using namespace js::types;
using mozilla::ArrayLength;
using mozilla::IsFinite;
using mozilla::IsNaN;
using JS::AutoCheckCannotGC;
using JS::GenericNaN;
/*
* The JS 'Date' object is patterned after the Java 'Date' object.
* Here is a script:
*
* today = new Date();
*
* print(today.toLocaleString());
*
* weekDay = today.getDay();
*
*
* These Java (and ECMA-262) methods are supported:
*
* UTC
* getDate (getUTCDate)
* getDay (getUTCDay)
* getHours (getUTCHours)
* getMinutes (getUTCMinutes)
* getMonth (getUTCMonth)
* getSeconds (getUTCSeconds)
* getMilliseconds (getUTCMilliseconds)
* getTime
* getTimezoneOffset
* getYear
* getFullYear (getUTCFullYear)
* parse
* setDate (setUTCDate)
* setHours (setUTCHours)
* setMinutes (setUTCMinutes)
* setMonth (setUTCMonth)
* setSeconds (setUTCSeconds)
* setMilliseconds (setUTCMilliseconds)
* setTime
* setYear (setFullYear, setUTCFullYear)
* toGMTString (toUTCString)
* toLocaleString
* toString
*
*
* These Java methods are not supported
*
* setDay
* before
* after
* equals
* hashCode
*/
static inline double
Day(double t)
{
return floor(t / msPerDay);
}
static double
TimeWithinDay(double t)
{
double result = fmod(t, msPerDay);
if (result < 0)
result += msPerDay;
return result;
}
/* ES5 15.9.1.3. */
static inline bool
IsLeapYear(double year)
{
JS_ASSERT(ToInteger(year) == year);
return fmod(year, 4) == 0 && (fmod(year, 100) != 0 || fmod(year, 400) == 0);
}
static inline double
DaysInYear(double year)
{
if (!IsFinite(year))
return GenericNaN();
return IsLeapYear(year) ? 366 : 365;
}
static inline double
DayFromYear(double y)
{
return 365 * (y - 1970) +
floor((y - 1969) / 4.0) -
floor((y - 1901) / 100.0) +
floor((y - 1601) / 400.0);
}
static inline double
TimeFromYear(double y)
{
return DayFromYear(y) * msPerDay;
}
static double
YearFromTime(double t)
{
if (!IsFinite(t))
return GenericNaN();
JS_ASSERT(ToInteger(t) == t);
double y = floor(t / (msPerDay * 365.2425)) + 1970;
double t2 = TimeFromYear(y);
/*
* Adjust the year if the approximation was wrong. Since the year was
* computed using the average number of ms per year, it will usually
* be wrong for dates within several hours of a year transition.
*/
if (t2 > t) {
y--;
} else {
if (t2 + msPerDay * DaysInYear(y) <= t)
y++;
}
return y;
}
static inline int
DaysInFebruary(double year)
{
return IsLeapYear(year) ? 29 : 28;
}
/* ES5 15.9.1.4. */
static inline double
DayWithinYear(double t, double year)
{
JS_ASSERT_IF(IsFinite(t), YearFromTime(t) == year);
return Day(t) - DayFromYear(year);
}
static double
MonthFromTime(double t)
{
if (!IsFinite(t))
return GenericNaN();
double year = YearFromTime(t);
double d = DayWithinYear(t, year);
int step;
if (d < (step = 31))
return 0;
if (d < (step += DaysInFebruary(year)))
return 1;
if (d < (step += 31))
return 2;
if (d < (step += 30))
return 3;
if (d < (step += 31))
return 4;
if (d < (step += 30))
return 5;
if (d < (step += 31))
return 6;
if (d < (step += 31))
return 7;
if (d < (step += 30))
return 8;
if (d < (step += 31))
return 9;
if (d < (step += 30))
return 10;
return 11;
}
/* ES5 15.9.1.5. */
static double
DateFromTime(double t)
{
if (!IsFinite(t))
return GenericNaN();
double year = YearFromTime(t);
double d = DayWithinYear(t, year);
int next;
if (d <= (next = 30))
return d + 1;
int step = next;
if (d <= (next += DaysInFebruary(year)))
return d - step;
step = next;
if (d <= (next += 31))
return d - step;
step = next;
if (d <= (next += 30))
return d - step;
step = next;
if (d <= (next += 31))
return d - step;
step = next;
if (d <= (next += 30))
return d - step;
step = next;
if (d <= (next += 31))
return d - step;
step = next;
if (d <= (next += 31))
return d - step;
step = next;
if (d <= (next += 30))
return d - step;
step = next;
if (d <= (next += 31))
return d - step;
step = next;
if (d <= (next += 30))
return d - step;
step = next;
return d - step;
}
/* ES5 15.9.1.6. */
static int
WeekDay(double t)
{
/*
* We can't assert TimeClip(t) == t because we call this function with
* local times, which can be offset outside TimeClip's permitted range.
*/
JS_ASSERT(ToInteger(t) == t);
int result = (int(Day(t)) + 4) % 7;
if (result < 0)
result += 7;
return result;
}
static inline int
DayFromMonth(int month, bool isLeapYear)
{
/*
* The following array contains the day of year for the first day of
* each month, where index 0 is January, and day 0 is January 1.
*/
static const int firstDayOfMonth[2][13] = {
{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
{0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
};
JS_ASSERT(0 <= month && month <= 12);
return firstDayOfMonth[isLeapYear][month];
}
template<typename T>
static inline int
DayFromMonth(T month, bool isLeapYear) MOZ_DELETE;
/* ES5 15.9.1.12 (out of order to accommodate DaylightSavingTA). */
static double
MakeDay(double year, double month, double date)
{
/* Step 1. */
if (!IsFinite(year) || !IsFinite(month) || !IsFinite(date))
return GenericNaN();
/* Steps 2-4. */
double y = ToInteger(year);
double m = ToInteger(month);
double dt = ToInteger(date);
/* Step 5. */
double ym = y + floor(m / 12);
/* Step 6. */
int mn = int(fmod(m, 12.0));
if (mn < 0)
mn += 12;
/* Steps 7-8. */
bool leap = IsLeapYear(ym);
double yearday = floor(TimeFromYear(ym) / msPerDay);
double monthday = DayFromMonth(mn, leap);
return yearday + monthday + dt - 1;
}
/* ES5 15.9.1.13 (out of order to accommodate DaylightSavingTA). */
static inline double
MakeDate(double day, double time)
{
/* Step 1. */
if (!IsFinite(day) || !IsFinite(time))
return GenericNaN();
/* Step 2. */
return day * msPerDay + time;
}
JS_PUBLIC_API(double)
JS::MakeDate(double year, unsigned month, unsigned day)
{
return TimeClip(::MakeDate(MakeDay(year, month, day), 0));
}
JS_PUBLIC_API(double)
JS::YearFromTime(double time)
{
return ::YearFromTime(time);
}
JS_PUBLIC_API(double)
JS::MonthFromTime(double time)
{
return ::MonthFromTime(time);
}
JS_PUBLIC_API(double)
JS::DayFromTime(double time)
{
return DateFromTime(time);
}
/*
* Find a year for which any given date will fall on the same weekday.
*
* This function should be used with caution when used other than
* for determining DST; it hasn't been proven not to produce an
* incorrect year for times near year boundaries.
*/
static int
EquivalentYearForDST(int year)
{
/*
* Years and leap years on which Jan 1 is a Sunday, Monday, etc.
*
* yearStartingWith[0][i] is an example non-leap year where
* Jan 1 appears on Sunday (i == 0), Monday (i == 1), etc.
*
* yearStartingWith[1][i] is an example leap year where
* Jan 1 appears on Sunday (i == 0), Monday (i == 1), etc.
*/
static const int yearStartingWith[2][7] = {
{1978, 1973, 1974, 1975, 1981, 1971, 1977},
{1984, 1996, 1980, 1992, 1976, 1988, 1972}
};
int day = int(DayFromYear(year) + 4) % 7;
if (day < 0)
day += 7;
return yearStartingWith[IsLeapYear(year)][day];
}
/* ES5 15.9.1.8. */
static double
DaylightSavingTA(double t, DateTimeInfo *dtInfo)
{
if (!IsFinite(t))
return GenericNaN();
/*
* If earlier than 1970 or after 2038, potentially beyond the ken of
* many OSes, map it to an equivalent year before asking.
*/
if (t < 0.0 || t > 2145916800000.0) {
int year = EquivalentYearForDST(int(YearFromTime(t)));
double day = MakeDay(year, MonthFromTime(t), DateFromTime(t));
t = MakeDate(day, TimeWithinDay(t));
}
int64_t utcMilliseconds = static_cast<int64_t>(t);
int64_t offsetMilliseconds = dtInfo->getDSTOffsetMilliseconds(utcMilliseconds);
return static_cast<double>(offsetMilliseconds);
}
static double
AdjustTime(double date, DateTimeInfo *dtInfo)
{
double t = DaylightSavingTA(date, dtInfo) + dtInfo->localTZA();
t = (dtInfo->localTZA() >= 0) ? fmod(t, msPerDay) : -fmod(msPerDay - t, msPerDay);
return t;
}
/* ES5 15.9.1.9. */
static double
LocalTime(double t, DateTimeInfo *dtInfo)
{
return t + AdjustTime(t, dtInfo);
}
static double
UTC(double t, DateTimeInfo *dtInfo)
{
return t - AdjustTime(t - dtInfo->localTZA(), dtInfo);
}
/* ES5 15.9.1.10. */
static double
HourFromTime(double t)
{
double result = fmod(floor(t/msPerHour), HoursPerDay);
if (result < 0)
result += HoursPerDay;
return result;
}
static double
MinFromTime(double t)
{
double result = fmod(floor(t / msPerMinute), MinutesPerHour);
if (result < 0)
result += MinutesPerHour;
return result;
}
static double
SecFromTime(double t)
{
double result = fmod(floor(t / msPerSecond), SecondsPerMinute);
if (result < 0)
result += SecondsPerMinute;
return result;
}
static double
msFromTime(double t)
{
double result = fmod(t, msPerSecond);
if (result < 0)
result += msPerSecond;
return result;
}
/* ES5 15.9.1.11. */
static double
MakeTime(double hour, double min, double sec, double ms)
{
/* Step 1. */
if (!IsFinite(hour) ||
!IsFinite(min) ||
!IsFinite(sec) ||
!IsFinite(ms))
{
return GenericNaN();
}
/* Step 2. */
double h = ToInteger(hour);
/* Step 3. */
double m = ToInteger(min);
/* Step 4. */
double s = ToInteger(sec);
/* Step 5. */
double milli = ToInteger(ms);
/* Steps 6-7. */
return h * msPerHour + m * msPerMinute + s * msPerSecond + milli;
}
/**
* end of ECMA 'support' functions
*/
static bool
date_convert(JSContext *cx, HandleObject obj, JSType hint, MutableHandleValue vp)
{
JS_ASSERT(hint == JSTYPE_NUMBER || hint == JSTYPE_STRING || hint == JSTYPE_VOID);
JS_ASSERT(obj->is<DateObject>());
return DefaultValue(cx, obj, (hint == JSTYPE_VOID) ? JSTYPE_STRING : hint, vp);
}
/* for use by date_parse */
static const char* const wtb[] = {
"am", "pm",
"monday", "tuesday", "wednesday", "thursday", "friday",
"saturday", "sunday",
"january", "february", "march", "april", "may", "june",
"july", "august", "september", "october", "november", "december",
"gmt", "ut", "utc",
"est", "edt",
"cst", "cdt",
"mst", "mdt",
"pst", "pdt"
/* time zone table needs to be expanded */
};
static const int ttb[] = {
-1, -2, 0, 0, 0, 0, 0, 0, 0, /* AM/PM */
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
10000 + 0, 10000 + 0, 10000 + 0, /* GMT/UT/UTC */
10000 + 5 * 60, 10000 + 4 * 60, /* EST/EDT */
10000 + 6 * 60, 10000 + 5 * 60, /* CST/CDT */
10000 + 7 * 60, 10000 + 6 * 60, /* MST/MDT */
10000 + 8 * 60, 10000 + 7 * 60 /* PST/PDT */
};
template <typename CharT>
static bool
RegionMatches(const char *s1, int s1off, const CharT *s2, int s2off, int count)
{
while (count > 0 && s1[s1off] && s2[s2off]) {
if (unicode::ToLowerCase(s1[s1off]) != unicode::ToLowerCase(s2[s2off]))
break;
s1off++;
s2off++;
count--;
}
return count == 0;
}
/* find UTC time from given date... no 1900 correction! */
static double
date_msecFromDate(double year, double mon, double mday, double hour,
double min, double sec, double msec)
{
return MakeDate(MakeDay(year, mon, mday), MakeTime(hour, min, sec, msec));
}
/* compute the time in msec (unclipped) from the given args */
#define MAXARGS 7
static bool
date_msecFromArgs(JSContext *cx, CallArgs args, double *rval)
{
unsigned loop;
double array[MAXARGS];
double msec_time;
for (loop = 0; loop < MAXARGS; loop++) {
if (loop < args.length()) {
double d;
if (!ToNumber(cx, args[loop], &d))
return false;
/* return NaN if any arg is not finite */
if (!IsFinite(d)) {
*rval = GenericNaN();
return true;
}
array[loop] = ToInteger(d);
} else {
if (loop == 2) {
array[loop] = 1; /* Default the date argument to 1. */
} else {
array[loop] = 0;
}
}
}
/* adjust 2-digit years into the 20th century */
if (array[0] >= 0 && array[0] <= 99)
array[0] += 1900;
msec_time = date_msecFromDate(array[0], array[1], array[2],
array[3], array[4], array[5], array[6]);
*rval = msec_time;
return true;
}
/*
* See ECMA 15.9.4.[3-10];
*/
static bool
date_UTC(JSContext *cx, unsigned argc, Value *vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
double msec_time;
if (!date_msecFromArgs(cx, args, &msec_time))
return false;
msec_time = TimeClip(msec_time);
args.rval().setNumber(msec_time);
return true;
}
/*
* Read and convert decimal digits from s[*i] into *result
* while *i < limit.
*
* Succeed if any digits are converted. Advance *i only
* as digits are consumed.
*/
template <typename CharT>
static bool
ParseDigits(size_t *result, const CharT *s, size_t *i, size_t limit)
{
size_t init = *i;
*result = 0;
while (*i < limit && ('0' <= s[*i] && s[*i] <= '9')) {
*result *= 10;
*result += (s[*i] - '0');
++(*i);
}
return *i != init;
}
/*
* Read and convert decimal digits to the right of a decimal point,
* representing a fractional integer, from s[*i] into *result
* while *i < limit.
*
* Succeed if any digits are converted. Advance *i only
* as digits are consumed.
*/
template <typename CharT>
static bool
ParseFractional(double *result, const CharT *s, size_t *i, size_t limit)
{
double factor = 0.1;
size_t init = *i;
*result = 0.0;
while (*i < limit && ('0' <= s[*i] && s[*i] <= '9')) {
*result += (s[*i] - '0') * factor;
factor *= 0.1;
++(*i);
}
return *i != init;
}
/*
* Read and convert exactly n decimal digits from s[*i]
* to s[min(*i+n,limit)] into *result.
*
* Succeed if exactly n digits are converted. Advance *i only
* on success.
*/
template <typename CharT>
static bool
ParseDigitsN(size_t n, size_t *result, const CharT *s, size_t *i, size_t limit)
{
size_t init = *i;
if (ParseDigits(result, s, i, Min(limit, init + n)))
return (*i - init) == n;
*i = init;
return false;
}
static int
DaysInMonth(int year, int month)
{
bool leap = IsLeapYear(year);
int result = int(DayFromMonth(month, leap) - DayFromMonth(month - 1, leap));
return result;
}
/*
* Parse a string in one of the date-time formats given by the W3C
* "NOTE-datetime" specification. These formats make up a restricted
* profile of the ISO 8601 format. Quoted here:
*
* The formats are as follows. Exactly the components shown here
* must be present, with exactly this punctuation. Note that the "T"
* appears literally in the string, to indicate the beginning of the
* time element, as specified in ISO 8601.
*
* Any combination of the date formats with the time formats is
* allowed, and also either the date or the time can be missing.
*
* The specification is silent on the meaning when fields are
* ommitted so the interpretations are a guess, but hopefully a
* reasonable one. We default the month to January, the day to the
* 1st, and hours minutes and seconds all to 0. If the date is
* missing entirely then we assume 1970-01-01 so that the time can
* be aded to a date later. If the time is missing then we assume
* 00:00 UTC. If the time is present but the time zone field is
* missing then we use local time.
*
* Date part:
*
* Year:
* YYYY (eg 1997)
*
* Year and month:
* YYYY-MM (eg 1997-07)
*
* Complete date:
* YYYY-MM-DD (eg 1997-07-16)
*
* Time part:
*
* Hours and minutes:
* Thh:mmTZD (eg T19:20+01:00)
*
* Hours, minutes and seconds:
* Thh:mm:ssTZD (eg T19:20:30+01:00)
*
* Hours, minutes, seconds and a decimal fraction of a second:
* Thh:mm:ss.sTZD (eg T19:20:30.45+01:00)
*
* where:
*
* YYYY = four-digit year or six digit year as +YYYYYY or -YYYYYY
* MM = two-digit month (01=January, etc.)
* DD = two-digit day of month (01 through 31)
* hh = two digits of hour (00 through 23) (am/pm NOT allowed)
* mm = two digits of minute (00 through 59)
* ss = two digits of second (00 through 59)
* s = one or more digits representing a decimal fraction of a second
* TZD = time zone designator (Z or +hh:mm or -hh:mm or missing for local)
*/
template <typename CharT>
static bool
ParseISODate(const CharT *s, size_t length, double *result, DateTimeInfo *dtInfo)
{
size_t i = 0;
int tzMul = 1;
int dateMul = 1;
size_t year = 1970;
size_t month = 1;
size_t day = 1;
size_t hour = 0;
size_t min = 0;
size_t sec = 0;
double frac = 0;
bool isLocalTime = false;
size_t tzHour = 0;
size_t tzMin = 0;
#define PEEK(ch) (i < length && s[i] == ch)
#define NEED(ch) \
if (i >= length || s[i] != ch) { return false; } else { ++i; }
#define DONE_DATE_UNLESS(ch) \
if (i >= length || s[i] != ch) { goto done_date; } else { ++i; }
#define DONE_UNLESS(ch) \
if (i >= length || s[i] != ch) { goto done; } else { ++i; }
#define NEED_NDIGITS(n, field) \
if (!ParseDigitsN(n, &field, s, &i, length)) { return false; }
if (PEEK('+') || PEEK('-')) {
if (PEEK('-'))
dateMul = -1;
++i;
NEED_NDIGITS(6, year);
} else if (!PEEK('T')) {
NEED_NDIGITS(4, year);
}
DONE_DATE_UNLESS('-');
NEED_NDIGITS(2, month);
DONE_DATE_UNLESS('-');
NEED_NDIGITS(2, day);
done_date:
DONE_UNLESS('T');
NEED_NDIGITS(2, hour);
NEED(':');
NEED_NDIGITS(2, min);
if (PEEK(':')) {
++i;
NEED_NDIGITS(2, sec);
if (PEEK('.')) {
++i;
if (!ParseFractional(&frac, s, &i, length))
return false;
}
}
if (PEEK('Z')) {
++i;
} else if (PEEK('+') || PEEK('-')) {
if (PEEK('-'))
tzMul = -1;
++i;
NEED_NDIGITS(2, tzHour);
/*
* Non-standard extension to the ISO date format (permitted by ES5):
* allow "-0700" as a time zone offset, not just "-07:00".
*/
if (PEEK(':'))
++i;
NEED_NDIGITS(2, tzMin);
} else {
isLocalTime = true;
}
done:
if (year > 275943 // ceil(1e8/365) + 1970
|| (month == 0 || month > 12)
|| (day == 0 || day > size_t(DaysInMonth(year,month)))
|| hour > 24
|| ((hour == 24) && (min > 0 || sec > 0))
|| min > 59
|| sec > 59
|| tzHour > 23
|| tzMin > 59)
{
return false;
}
if (i != length)
return false;
month -= 1; /* convert month to 0-based */
double msec = date_msecFromDate(dateMul * double(year), month, day,
hour, min, sec, frac * 1000.0);
if (isLocalTime)
msec = UTC(msec, dtInfo);
else
msec -= tzMul * (tzHour * msPerHour + tzMin * msPerMinute);
if (msec < -8.64e15 || msec > 8.64e15)
return false;
*result = msec;
return true;
#undef PEEK
#undef NEED
#undef DONE_UNLESS
#undef NEED_NDIGITS
}
template <typename CharT>
static bool
ParseDate(const CharT *s, size_t length, double *result, DateTimeInfo *dtInfo)
{
if (ParseISODate(s, length, result, dtInfo))
return true;
if (length == 0)
return false;
int year = -1;
int mon = -1;
int mday = -1;
int hour = -1;
int min = -1;
int sec = -1;
int tzOffset = -1;
int prevc = 0;
bool seenPlusMinus = false;
bool seenMonthName = false;
size_t i = 0;
while (i < length) {
int c = s[i];
i++;
if (c <= ' ' || c == ',' || c == '-') {
if (c == '-' && '0' <= s[i] && s[i] <= '9')
prevc = c;
continue;
}
if (c == '(') { /* comments) */
int depth = 1;
while (i < length) {
c = s[i];
i++;
if (c == '(') {
depth++;
} else if (c == ')') {
if (--depth <= 0)
break;
}
}
continue;
}
if ('0' <= c && c <= '9') {
int n = c - '0';
while (i < length && '0' <= (c = s[i]) && c <= '9') {
n = n * 10 + c - '0';
i++;
}
/*
* Allow TZA before the year, so 'Wed Nov 05 21:49:11 GMT-0800 1997'
* works.
*
* Uses of seenPlusMinus allow ':' in TZA, so Java no-timezone style
* of GMT+4:30 works.
*/
if ((prevc == '+' || prevc == '-')/* && year>=0 */) {
/* Make ':' case below change tzOffset. */
seenPlusMinus = true;
/* offset */
if (n < 24)
n = n * 60; /* EG. "GMT-3" */
else
n = n % 100 + n / 100 * 60; /* eg "GMT-0430" */
if (prevc == '+') /* plus means east of GMT */
n = -n;
if (tzOffset != 0 && tzOffset != -1)
return false;
tzOffset = n;
} else if (prevc == '/' && mon >= 0 && mday >= 0 && year < 0) {
if (c <= ' ' || c == ',' || c == '/' || i >= length)
year = n;
else
return false;
} else if (c == ':') {
if (hour < 0)
hour = /*byte*/ n;
else if (min < 0)
min = /*byte*/ n;
else
return false;
} else if (c == '/') {
/*
* Until it is determined that mon is the actual month, keep
* it as 1-based rather than 0-based.
*/
if (mon < 0)
mon = /*byte*/ n;
else if (mday < 0)
mday = /*byte*/ n;
else
return false;
} else if (i < length && c != ',' && c > ' ' && c != '-' && c != '(') {
return false;
} else if (seenPlusMinus && n < 60) { /* handle GMT-3:30 */
if (tzOffset < 0)
tzOffset -= n;
else
tzOffset += n;
} else if (hour >= 0 && min < 0) {
min = /*byte*/ n;
} else if (prevc == ':' && min >= 0 && sec < 0) {
sec = /*byte*/ n;
} else if (mon < 0) {
mon = /*byte*/n;
} else if (mon >= 0 && mday < 0) {
mday = /*byte*/ n;
} else if (mon >= 0 && mday >= 0 && year < 0) {
year = n;
} else {
return false;
}