forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduk_bi_date.c
1769 lines (1524 loc) · 60 KB
/
duk_bi_date.c
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
/*
* Date built-ins
*
* Unlike most built-ins, Date has some platform dependencies for getting
* UTC time, converting between UTC and local time, and parsing and
* formatting time values. These are all abstracted behind DUK_USE_xxx
* config options. There are built-in platform specific providers for
* POSIX and Windows, but external providers can also be used.
*
* See doc/datetime.rst.
*
*/
#include "duk_internal.h"
/* XXX: currently defines unnecessary symbols when DUK_USE_DATE_BUILTIN is disabled. */
/*
* Forward declarations
*/
DUK_LOCAL_DECL duk_double_t duk__push_this_get_timeval_tzoffset(duk_hthread *thr, duk_small_uint_t flags, duk_int_t *out_tzoffset);
DUK_LOCAL_DECL duk_double_t duk__push_this_get_timeval(duk_hthread *thr, duk_small_uint_t flags);
DUK_LOCAL_DECL void duk__twodigit_year_fixup(duk_hthread *thr, duk_idx_t idx_val);
DUK_LOCAL_DECL duk_ret_t duk__set_this_timeval_from_dparts(duk_hthread *thr, duk_double_t *dparts, duk_small_uint_t flags);
/*
* Other file level defines
*/
/* Debug macro to print all parts and dparts (used manually because of debug level). */
#define DUK__DPRINT_PARTS_AND_DPARTS(parts,dparts) do { \
DUK_D(DUK_DPRINT("parts: %ld %ld %ld %ld %ld %ld %ld %ld, dparts: %lf %lf %lf %lf %lf %lf %lf %lf", \
(long) (parts)[0], (long) (parts)[1], \
(long) (parts)[2], (long) (parts)[3], \
(long) (parts)[4], (long) (parts)[5], \
(long) (parts)[6], (long) (parts)[7], \
(double) (dparts)[0], (double) (dparts)[1], \
(double) (dparts)[2], (double) (dparts)[3], \
(double) (dparts)[4], (double) (dparts)[5], \
(double) (dparts)[6], (double) (dparts)[7])); \
} while (0)
#define DUK__DPRINT_PARTS(parts) do { \
DUK_D(DUK_DPRINT("parts: %ld %ld %ld %ld %ld %ld %ld %ld", \
(long) (parts)[0], (long) (parts)[1], \
(long) (parts)[2], (long) (parts)[3], \
(long) (parts)[4], (long) (parts)[5], \
(long) (parts)[6], (long) (parts)[7])); \
} while (0)
#define DUK__DPRINT_DPARTS(dparts) do { \
DUK_D(DUK_DPRINT("dparts: %lf %lf %lf %lf %lf %lf %lf %lf", \
(double) (dparts)[0], (double) (dparts)[1], \
(double) (dparts)[2], (double) (dparts)[3], \
(double) (dparts)[4], (double) (dparts)[5], \
(double) (dparts)[6], (double) (dparts)[7])); \
} while (0)
/* Equivalent year for DST calculations outside [1970,2038[ range, see
* E5 Section 15.9.1.8. Equivalent year has the same leap-year-ness and
* starts with the same weekday on Jan 1.
* https://bugzilla.mozilla.org/show_bug.cgi?id=351066
*/
#define DUK__YEAR(x) ((duk_uint8_t) ((x) - 1970))
DUK_LOCAL duk_uint8_t duk__date_equivyear[14] = {
#if 1
/* This is based on V8 EquivalentYear() algorithm (see util/genequivyear.py):
* http://code.google.com/p/v8/source/browse/trunk/src/date.h#146
*/
/* non-leap year: sunday, monday, ... */
DUK__YEAR(2023), DUK__YEAR(2035), DUK__YEAR(2019), DUK__YEAR(2031),
DUK__YEAR(2015), DUK__YEAR(2027), DUK__YEAR(2011),
/* leap year: sunday, monday, ... */
DUK__YEAR(2012), DUK__YEAR(2024), DUK__YEAR(2008), DUK__YEAR(2020),
DUK__YEAR(2032), DUK__YEAR(2016), DUK__YEAR(2028)
#endif
#if 0
/* This is based on Rhino EquivalentYear() algorithm:
* https://github.com/mozilla/rhino/blob/f99cc11d616f0cdda2c42bde72b3484df6182947/src/org/mozilla/javascript/NativeDate.java
*/
/* non-leap year: sunday, monday, ... */
DUK__YEAR(1978), DUK__YEAR(1973), DUK__YEAR(1985), DUK__YEAR(1986),
DUK__YEAR(1981), DUK__YEAR(1971), DUK__YEAR(1977),
/* leap year: sunday, monday, ... */
DUK__YEAR(1984), DUK__YEAR(1996), DUK__YEAR(1980), DUK__YEAR(1992),
DUK__YEAR(1976), DUK__YEAR(1988), DUK__YEAR(1972)
#endif
};
/*
* ISO 8601 subset parser.
*/
/* Parser part count. */
#define DUK__NUM_ISO8601_PARSER_PARTS 9
/* Parser part indices. */
#define DUK__PI_YEAR 0
#define DUK__PI_MONTH 1
#define DUK__PI_DAY 2
#define DUK__PI_HOUR 3
#define DUK__PI_MINUTE 4
#define DUK__PI_SECOND 5
#define DUK__PI_MILLISECOND 6
#define DUK__PI_TZHOUR 7
#define DUK__PI_TZMINUTE 8
/* Parser part masks. */
#define DUK__PM_YEAR (1 << DUK__PI_YEAR)
#define DUK__PM_MONTH (1 << DUK__PI_MONTH)
#define DUK__PM_DAY (1 << DUK__PI_DAY)
#define DUK__PM_HOUR (1 << DUK__PI_HOUR)
#define DUK__PM_MINUTE (1 << DUK__PI_MINUTE)
#define DUK__PM_SECOND (1 << DUK__PI_SECOND)
#define DUK__PM_MILLISECOND (1 << DUK__PI_MILLISECOND)
#define DUK__PM_TZHOUR (1 << DUK__PI_TZHOUR)
#define DUK__PM_TZMINUTE (1 << DUK__PI_TZMINUTE)
/* Parser separator indices. */
#define DUK__SI_PLUS 0
#define DUK__SI_MINUS 1
#define DUK__SI_T 2
#define DUK__SI_SPACE 3
#define DUK__SI_COLON 4
#define DUK__SI_PERIOD 5
#define DUK__SI_Z 6
#define DUK__SI_NUL 7
/* Parser separator masks. */
#define DUK__SM_PLUS (1 << DUK__SI_PLUS)
#define DUK__SM_MINUS (1 << DUK__SI_MINUS)
#define DUK__SM_T (1 << DUK__SI_T)
#define DUK__SM_SPACE (1 << DUK__SI_SPACE)
#define DUK__SM_COLON (1 << DUK__SI_COLON)
#define DUK__SM_PERIOD (1 << DUK__SI_PERIOD)
#define DUK__SM_Z (1 << DUK__SI_Z)
#define DUK__SM_NUL (1 << DUK__SI_NUL)
/* Rule control flags. */
#define DUK__CF_NEG (1 << 0) /* continue matching, set neg_tzoffset flag */
#define DUK__CF_ACCEPT (1 << 1) /* accept string */
#define DUK__CF_ACCEPT_NUL (1 << 2) /* accept string if next char is NUL (otherwise reject) */
#define DUK__PACK_RULE(partmask,sepmask,nextpart,flags) \
((duk_uint32_t) (partmask) + \
(((duk_uint32_t) (sepmask)) << 9) + \
(((duk_uint32_t) (nextpart)) << 17) + \
(((duk_uint32_t) (flags)) << 21))
#define DUK__UNPACK_RULE(rule,var_nextidx,var_flags) do { \
(var_nextidx) = (duk_small_uint_t) (((rule) >> 17) & 0x0f); \
(var_flags) = (duk_small_uint_t) ((rule) >> 21); \
} while (0)
#define DUK__RULE_MASK_PART_SEP 0x1ffffUL
/* Matching separator index is used in the control table */
DUK_LOCAL const duk_uint8_t duk__parse_iso8601_seps[] = {
DUK_ASC_PLUS /*0*/, DUK_ASC_MINUS /*1*/, DUK_ASC_UC_T /*2*/, DUK_ASC_SPACE /*3*/,
DUK_ASC_COLON /*4*/, DUK_ASC_PERIOD /*5*/, DUK_ASC_UC_Z /*6*/, DUK_ASC_NUL /*7*/
};
/* Rule table: first matching rule is used to determine what to do next. */
DUK_LOCAL const duk_uint32_t duk__parse_iso8601_control[] = {
DUK__PACK_RULE(DUK__PM_YEAR, DUK__SM_MINUS, DUK__PI_MONTH, 0),
DUK__PACK_RULE(DUK__PM_MONTH, DUK__SM_MINUS, DUK__PI_DAY, 0),
DUK__PACK_RULE(DUK__PM_YEAR | DUK__PM_MONTH | DUK__PM_DAY, DUK__SM_T | DUK__SM_SPACE, DUK__PI_HOUR, 0),
DUK__PACK_RULE(DUK__PM_HOUR, DUK__SM_COLON, DUK__PI_MINUTE, 0),
DUK__PACK_RULE(DUK__PM_MINUTE, DUK__SM_COLON, DUK__PI_SECOND, 0),
DUK__PACK_RULE(DUK__PM_SECOND, DUK__SM_PERIOD, DUK__PI_MILLISECOND, 0),
DUK__PACK_RULE(DUK__PM_TZHOUR, DUK__SM_COLON, DUK__PI_TZMINUTE, 0),
DUK__PACK_RULE(DUK__PM_YEAR | DUK__PM_MONTH | DUK__PM_DAY | DUK__PM_HOUR /*Note1*/ | DUK__PM_MINUTE | DUK__PM_SECOND | DUK__PM_MILLISECOND, DUK__SM_PLUS, DUK__PI_TZHOUR, 0),
DUK__PACK_RULE(DUK__PM_YEAR | DUK__PM_MONTH | DUK__PM_DAY | DUK__PM_HOUR /*Note1*/ | DUK__PM_MINUTE | DUK__PM_SECOND | DUK__PM_MILLISECOND, DUK__SM_MINUS, DUK__PI_TZHOUR, DUK__CF_NEG),
DUK__PACK_RULE(DUK__PM_YEAR | DUK__PM_MONTH | DUK__PM_DAY | DUK__PM_HOUR /*Note1*/ | DUK__PM_MINUTE | DUK__PM_SECOND | DUK__PM_MILLISECOND, DUK__SM_Z, 0, DUK__CF_ACCEPT_NUL),
DUK__PACK_RULE(DUK__PM_YEAR | DUK__PM_MONTH | DUK__PM_DAY | DUK__PM_HOUR /*Note1*/ | DUK__PM_MINUTE | DUK__PM_SECOND | DUK__PM_MILLISECOND | DUK__PM_TZHOUR /*Note2*/ | DUK__PM_TZMINUTE, DUK__SM_NUL, 0, DUK__CF_ACCEPT)
/* Note1: the specification doesn't require matching a time form with
* just hours ("HH"), but we accept it here, e.g. "2012-01-02T12Z".
*
* Note2: the specification doesn't require matching a timezone offset
* with just hours ("HH"), but accept it here, e.g. "2012-01-02T03:04:05+02"
*/
};
DUK_LOCAL duk_bool_t duk__parse_string_iso8601_subset(duk_hthread *thr, const char *str) {
duk_int_t parts[DUK__NUM_ISO8601_PARSER_PARTS];
duk_double_t dparts[DUK_DATE_IDX_NUM_PARTS];
duk_double_t d;
const duk_uint8_t *p;
duk_small_uint_t part_idx = 0;
duk_int_t accum = 0;
duk_small_uint_t ndigits = 0;
duk_bool_t neg_year = 0;
duk_bool_t neg_tzoffset = 0;
duk_uint_fast8_t ch;
duk_small_uint_t i;
/* During parsing, month and day are one-based; set defaults here. */
duk_memzero(parts, sizeof(parts));
DUK_ASSERT(parts[DUK_DATE_IDX_YEAR] == 0); /* don't care value, year is mandatory */
parts[DUK_DATE_IDX_MONTH] = 1;
parts[DUK_DATE_IDX_DAY] = 1;
/* Special handling for year sign. */
p = (const duk_uint8_t *) str;
ch = p[0];
if (ch == DUK_ASC_PLUS) {
p++;
} else if (ch == DUK_ASC_MINUS) {
neg_year = 1;
p++;
}
for (;;) {
ch = *p++;
DUK_DDD(DUK_DDDPRINT("parsing, part_idx=%ld, char=%ld ('%c')",
(long) part_idx, (long) ch,
(int) ((ch >= 0x20 && ch <= 0x7e) ? ch : DUK_ASC_QUESTION)));
if (ch >= DUK_ASC_0 && ch <= DUK_ASC_9) {
if (ndigits >= 9) {
DUK_DDD(DUK_DDDPRINT("too many digits -> reject"));
goto reject;
}
if (part_idx == DUK__PI_MILLISECOND && ndigits >= 3) {
/* ignore millisecond fractions after 3 */
} else {
accum = accum * 10 + ((duk_int_t) ch) - ((duk_int_t) DUK_ASC_0) + 0x00;
ndigits++;
}
} else {
duk_uint_fast32_t match_val;
duk_small_uint_t sep_idx;
if (ndigits <= 0) {
goto reject;
}
if (part_idx == DUK__PI_MILLISECOND) {
/* complete the millisecond field */
while (ndigits < 3) {
accum *= 10;
ndigits++;
}
}
parts[part_idx] = accum;
DUK_DDD(DUK_DDDPRINT("wrote part %ld -> value %ld", (long) part_idx, (long) accum));
accum = 0;
ndigits = 0;
for (i = 0; i < (duk_small_uint_t) (sizeof(duk__parse_iso8601_seps) / sizeof(duk_uint8_t)); i++) {
if (duk__parse_iso8601_seps[i] == ch) {
break;
}
}
if (i == (duk_small_uint_t) (sizeof(duk__parse_iso8601_seps) / sizeof(duk_uint8_t))) {
DUK_DDD(DUK_DDDPRINT("separator character doesn't match -> reject"));
goto reject;
}
sep_idx = i;
match_val = (1UL << part_idx) + (1UL << (sep_idx + 9)); /* match against rule part/sep bits */
for (i = 0; i < (duk_small_uint_t) (sizeof(duk__parse_iso8601_control) / sizeof(duk_uint32_t)); i++) {
duk_uint_fast32_t rule = duk__parse_iso8601_control[i];
duk_small_uint_t nextpart;
duk_small_uint_t cflags;
DUK_DDD(DUK_DDDPRINT("part_idx=%ld, sep_idx=%ld, match_val=0x%08lx, considering rule=0x%08lx",
(long) part_idx, (long) sep_idx,
(unsigned long) match_val, (unsigned long) rule));
if ((rule & match_val) != match_val) {
continue;
}
DUK__UNPACK_RULE(rule, nextpart, cflags);
DUK_DDD(DUK_DDDPRINT("rule match -> part_idx=%ld, sep_idx=%ld, match_val=0x%08lx, "
"rule=0x%08lx -> nextpart=%ld, cflags=0x%02lx",
(long) part_idx, (long) sep_idx,
(unsigned long) match_val, (unsigned long) rule,
(long) nextpart, (unsigned long) cflags));
if (cflags & DUK__CF_NEG) {
neg_tzoffset = 1;
}
if (cflags & DUK__CF_ACCEPT) {
goto accept;
}
if (cflags & DUK__CF_ACCEPT_NUL) {
DUK_ASSERT(*(p - 1) != (char) 0);
if (*p == DUK_ASC_NUL) {
goto accept;
}
goto reject;
}
part_idx = nextpart;
break;
} /* rule match */
if (i == (duk_small_uint_t) (sizeof(duk__parse_iso8601_control) / sizeof(duk_uint32_t))) {
DUK_DDD(DUK_DDDPRINT("no rule matches -> reject"));
goto reject;
}
if (ch == 0) {
/* This shouldn't be necessary, but check just in case
* to avoid any chance of overruns.
*/
DUK_DDD(DUK_DDDPRINT("NUL after rule matching (should not happen) -> reject"));
goto reject;
}
} /* if-digit-else-ctrl */
} /* char loop */
/* We should never exit the loop above. */
DUK_UNREACHABLE();
reject:
DUK_DDD(DUK_DDDPRINT("reject"));
return 0;
accept:
DUK_DDD(DUK_DDDPRINT("accept"));
/* Apply timezone offset to get the main parts in UTC */
if (neg_year) {
parts[DUK__PI_YEAR] = -parts[DUK__PI_YEAR];
}
if (neg_tzoffset) {
parts[DUK__PI_HOUR] += parts[DUK__PI_TZHOUR];
parts[DUK__PI_MINUTE] += parts[DUK__PI_TZMINUTE];
} else {
parts[DUK__PI_HOUR] -= parts[DUK__PI_TZHOUR];
parts[DUK__PI_MINUTE] -= parts[DUK__PI_TZMINUTE];
}
parts[DUK__PI_MONTH] -= 1; /* zero-based month */
parts[DUK__PI_DAY] -= 1; /* zero-based day */
/* Use double parts, they tolerate unnormalized time.
*
* Note: DUK_DATE_IDX_WEEKDAY is initialized with a bogus value (DUK__PI_TZHOUR)
* on purpose. It won't be actually used by duk_bi_date_get_timeval_from_dparts(),
* but will make the value initialized just in case, and avoid any
* potential for Valgrind issues.
*/
for (i = 0; i < DUK_DATE_IDX_NUM_PARTS; i++) {
DUK_DDD(DUK_DDDPRINT("part[%ld] = %ld", (long) i, (long) parts[i]));
dparts[i] = parts[i];
}
d = duk_bi_date_get_timeval_from_dparts(dparts, 0 /*flags*/);
duk_push_number(thr, d);
return 1;
}
/*
* Date/time parsing helper.
*
* Parse a datetime string into a time value. We must first try to parse
* the input according to the standard format in E5.1 Section 15.9.1.15.
* If that fails, we can try to parse using custom parsing, which can
* either be platform neutral (custom code) or platform specific (using
* existing platform API calls).
*
* Note in particular that we must parse whatever toString(), toUTCString(),
* and toISOString() can produce; see E5.1 Section 15.9.4.2.
*
* Returns 1 to allow tail calling.
*
* There is much room for improvement here with respect to supporting
* alternative datetime formats. For instance, V8 parses '2012-01-01' as
* UTC and '2012/01/01' as local time.
*/
DUK_LOCAL duk_ret_t duk__parse_string(duk_hthread *thr, const char *str) {
/* XXX: there is a small risk here: because the ISO 8601 parser is
* very loose, it may end up parsing some datetime values which
* would be better parsed with a platform specific parser.
*/
DUK_ASSERT(str != NULL);
DUK_DDD(DUK_DDDPRINT("parse datetime from string '%s'", (const char *) str));
if (duk__parse_string_iso8601_subset(thr, str) != 0) {
return 1;
}
#if defined(DUK_USE_DATE_PARSE_STRING)
/* Contract, either:
* - Push value on stack and return 1
* - Don't push anything on stack and return 0
*/
if (DUK_USE_DATE_PARSE_STRING(thr, str) != 0) {
return 1;
}
#else
/* No platform-specific parsing, this is not an error. */
#endif
duk_push_nan(thr);
return 1;
}
/*
* Calendar helpers
*
* Some helpers are used for getters and can operate on normalized values
* which can be represented with 32-bit signed integers. Other helpers are
* needed by setters and operate on un-normalized double values, must watch
* out for non-finite numbers etc.
*/
DUK_LOCAL duk_uint8_t duk__days_in_month[12] = {
(duk_uint8_t) 31, (duk_uint8_t) 28, (duk_uint8_t) 31, (duk_uint8_t) 30,
(duk_uint8_t) 31, (duk_uint8_t) 30, (duk_uint8_t) 31, (duk_uint8_t) 31,
(duk_uint8_t) 30, (duk_uint8_t) 31, (duk_uint8_t) 30, (duk_uint8_t) 31
};
/* Maximum iteration count for computing UTC-to-local time offset when
* creating an ECMAScript time value from local parts.
*/
#define DUK__LOCAL_TZOFFSET_MAXITER 4
/* Because 'day since epoch' can be negative and is used to compute weekday
* using a modulo operation, add this multiple of 7 to avoid negative values
* when year is below 1970 epoch. ECMAScript time values are restricted to
* +/- 100 million days from epoch, so this adder fits nicely into 32 bits.
* Round to a multiple of 7 (= floor(100000000 / 7) * 7) and add margin.
*/
#define DUK__WEEKDAY_MOD_ADDER (20000000 * 7) /* 0x08583b00 */
DUK_INTERNAL duk_bool_t duk_bi_date_is_leap_year(duk_int_t year) {
if ((year % 4) != 0) {
return 0;
}
if ((year % 100) != 0) {
return 1;
}
if ((year % 400) != 0) {
return 0;
}
return 1;
}
DUK_INTERNAL duk_bool_t duk_bi_date_timeval_in_valid_range(duk_double_t x) {
return (x >= -DUK_DATE_MSEC_100M_DAYS && x <= DUK_DATE_MSEC_100M_DAYS);
}
DUK_INTERNAL duk_bool_t duk_bi_date_timeval_in_leeway_range(duk_double_t x) {
return (x >= -DUK_DATE_MSEC_100M_DAYS_LEEWAY && x <= DUK_DATE_MSEC_100M_DAYS_LEEWAY);
}
DUK_INTERNAL duk_bool_t duk_bi_date_year_in_valid_range(duk_double_t x) {
return (x >= DUK_DATE_MIN_ECMA_YEAR && x <= DUK_DATE_MAX_ECMA_YEAR);
}
DUK_LOCAL duk_double_t duk__timeclip(duk_double_t x) {
if (!DUK_ISFINITE(x)) {
return DUK_DOUBLE_NAN;
}
if (!duk_bi_date_timeval_in_valid_range(x)) {
return DUK_DOUBLE_NAN;
}
x = duk_js_tointeger_number(x);
/* Here we'd have the option to normalize -0 to +0. */
return x;
}
/* Integer division which floors also negative values correctly. */
DUK_LOCAL duk_int_t duk__div_floor(duk_int_t a, duk_int_t b) {
DUK_ASSERT(b > 0);
if (a >= 0) {
return a / b;
} else {
/* e.g. a = -4, b = 5 --> -4 - 5 + 1 / 5 --> -8 / 5 --> -1
* a = -5, b = 5 --> -5 - 5 + 1 / 5 --> -9 / 5 --> -1
* a = -6, b = 5 --> -6 - 5 + 1 / 5 --> -10 / 5 --> -2
*/
return (a - b + 1) / b;
}
}
/* Compute day number of the first day of a given year. */
DUK_LOCAL duk_int_t duk__day_from_year(duk_int_t year) {
/* Note: in integer arithmetic, (x / 4) is same as floor(x / 4) for non-negative
* values, but is incorrect for negative ones.
*/
return 365 * (year - 1970)
+ duk__div_floor(year - 1969, 4)
- duk__div_floor(year - 1901, 100)
+ duk__div_floor(year - 1601, 400);
}
/* Given a day number, determine year and day-within-year. */
DUK_LOCAL duk_int_t duk__year_from_day(duk_int_t day, duk_small_int_t *out_day_within_year) {
duk_int_t year;
duk_int_t diff_days;
/* estimate year upwards (towards positive infinity), then back down;
* two iterations should be enough
*/
if (day >= 0) {
year = 1970 + day / 365;
} else {
year = 1970 + day / 366;
}
for (;;) {
diff_days = duk__day_from_year(year) - day;
DUK_DDD(DUK_DDDPRINT("year=%ld day=%ld, diff_days=%ld", (long) year, (long) day, (long) diff_days));
if (diff_days <= 0) {
DUK_ASSERT(-diff_days < 366); /* fits into duk_small_int_t */
*out_day_within_year = -diff_days;
DUK_DDD(DUK_DDDPRINT("--> year=%ld, day-within-year=%ld",
(long) year, (long) *out_day_within_year));
DUK_ASSERT(*out_day_within_year >= 0);
DUK_ASSERT(*out_day_within_year < (duk_bi_date_is_leap_year(year) ? 366 : 365));
return year;
}
/* Note: this is very tricky; we must never 'overshoot' the
* correction downwards.
*/
year -= 1 + (diff_days - 1) / 366; /* conservative */
}
}
/* Given a (year, month, day-within-month) triple, compute day number.
* The input triple is un-normalized and may contain non-finite values.
*/
DUK_LOCAL duk_double_t duk__make_day(duk_double_t year, duk_double_t month, duk_double_t day) {
duk_int_t day_num;
duk_bool_t is_leap;
duk_small_int_t i, n;
/* Assume that year, month, day are all coerced to whole numbers.
* They may also be NaN or infinity, in which case this function
* must return NaN or infinity to ensure time value becomes NaN.
* If 'day' is NaN, the final return will end up returning a NaN,
* so it doesn't need to be checked here.
*/
if (!DUK_ISFINITE(year) || !DUK_ISFINITE(month)) {
return DUK_DOUBLE_NAN;
}
year += DUK_FLOOR(month / 12.0);
month = DUK_FMOD(month, 12.0);
if (month < 0.0) {
/* handle negative values */
month += 12.0;
}
/* The algorithm in E5.1 Section 15.9.1.12 normalizes month, but
* does not normalize the day-of-month (nor check whether or not
* it is finite) because it's not necessary for finding the day
* number which matches the (year,month) pair.
*
* We assume that duk__day_from_year() is exact here.
*
* Without an explicit infinity / NaN check in the beginning,
* day_num would be a bogus integer here.
*
* It's possible for 'year' to be out of integer range here.
* If so, we need to return NaN without integer overflow.
* This fixes test-bug-setyear-overflow.js.
*/
if (!duk_bi_date_year_in_valid_range(year)) {
DUK_DD(DUK_DDPRINT("year not in ecmascript valid range, avoid integer overflow: %lf", (double) year));
return DUK_DOUBLE_NAN;
}
day_num = duk__day_from_year((duk_int_t) year);
is_leap = duk_bi_date_is_leap_year((duk_int_t) year);
n = (duk_small_int_t) month;
for (i = 0; i < n; i++) {
day_num += duk__days_in_month[i];
if (i == 1 && is_leap) {
day_num++;
}
}
/* If 'day' is NaN, returns NaN. */
return (duk_double_t) day_num + day;
}
/* Split time value into parts. The time value may contain fractions (it may
* come from duk_time_to_components() API call) which are truncated. Possible
* local time adjustment has already been applied when reading the time value.
*/
DUK_INTERNAL void duk_bi_date_timeval_to_parts(duk_double_t d, duk_int_t *parts, duk_double_t *dparts, duk_small_uint_t flags) {
duk_double_t d1, d2;
duk_int_t t1, t2;
duk_int_t day_since_epoch;
duk_int_t year; /* does not fit into 16 bits */
duk_small_int_t day_in_year;
duk_small_int_t month;
duk_small_int_t day;
duk_small_int_t dim;
duk_int_t jan1_since_epoch;
duk_small_int_t jan1_weekday;
duk_int_t equiv_year;
duk_small_uint_t i;
duk_bool_t is_leap;
duk_small_int_t arridx;
DUK_ASSERT(DUK_ISFINITE(d)); /* caller checks */
d = DUK_FLOOR(d); /* remove fractions if present */
DUK_ASSERT(duk_double_equals(DUK_FLOOR(d), d));
/* The timevalue must be in valid ECMAScript range, but since a local
* time offset can be applied, we need to allow a +/- 24h leeway to
* the value. In other words, although the UTC time is within the
* ECMAScript range, the local part values can be just outside of it.
*/
DUK_UNREF(duk_bi_date_timeval_in_leeway_range);
DUK_ASSERT(duk_bi_date_timeval_in_leeway_range(d));
/* These computations are guaranteed to be exact for the valid
* E5 time value range, assuming milliseconds without fractions.
*/
d1 = (duk_double_t) DUK_FMOD(d, (double) DUK_DATE_MSEC_DAY);
if (d1 < 0.0) {
/* deal with negative values */
d1 += (duk_double_t) DUK_DATE_MSEC_DAY;
}
d2 = DUK_FLOOR((double) (d / (duk_double_t) DUK_DATE_MSEC_DAY));
DUK_ASSERT(duk_double_equals(d2 * ((duk_double_t) DUK_DATE_MSEC_DAY) + d1, d));
/* now expected to fit into a 32-bit integer */
t1 = (duk_int_t) d1;
t2 = (duk_int_t) d2;
day_since_epoch = t2;
DUK_ASSERT(duk_double_equals((duk_double_t) t1, d1));
DUK_ASSERT(duk_double_equals((duk_double_t) t2, d2));
/* t1 = milliseconds within day (fits 32 bit)
* t2 = day number from epoch (fits 32 bit, may be negative)
*/
parts[DUK_DATE_IDX_MILLISECOND] = t1 % 1000; t1 /= 1000;
parts[DUK_DATE_IDX_SECOND] = t1 % 60; t1 /= 60;
parts[DUK_DATE_IDX_MINUTE] = t1 % 60; t1 /= 60;
parts[DUK_DATE_IDX_HOUR] = t1;
DUK_ASSERT(parts[DUK_DATE_IDX_MILLISECOND] >= 0 && parts[DUK_DATE_IDX_MILLISECOND] <= 999);
DUK_ASSERT(parts[DUK_DATE_IDX_SECOND] >= 0 && parts[DUK_DATE_IDX_SECOND] <= 59);
DUK_ASSERT(parts[DUK_DATE_IDX_MINUTE] >= 0 && parts[DUK_DATE_IDX_MINUTE] <= 59);
DUK_ASSERT(parts[DUK_DATE_IDX_HOUR] >= 0 && parts[DUK_DATE_IDX_HOUR] <= 23);
DUK_DDD(DUK_DDDPRINT("d=%lf, d1=%lf, d2=%lf, t1=%ld, t2=%ld, parts: hour=%ld min=%ld sec=%ld msec=%ld",
(double) d, (double) d1, (double) d2, (long) t1, (long) t2,
(long) parts[DUK_DATE_IDX_HOUR],
(long) parts[DUK_DATE_IDX_MINUTE],
(long) parts[DUK_DATE_IDX_SECOND],
(long) parts[DUK_DATE_IDX_MILLISECOND]));
/* This assert depends on the input parts representing time inside
* the ECMAScript range.
*/
DUK_ASSERT(t2 + DUK__WEEKDAY_MOD_ADDER >= 0);
parts[DUK_DATE_IDX_WEEKDAY] = (t2 + 4 + DUK__WEEKDAY_MOD_ADDER) % 7; /* E5.1 Section 15.9.1.6 */
DUK_ASSERT(parts[DUK_DATE_IDX_WEEKDAY] >= 0 && parts[DUK_DATE_IDX_WEEKDAY] <= 6);
year = duk__year_from_day(t2, &day_in_year);
day = day_in_year;
is_leap = duk_bi_date_is_leap_year(year);
for (month = 0; month < 12; month++) {
dim = duk__days_in_month[month];
if (month == 1 && is_leap) {
dim++;
}
DUK_DDD(DUK_DDDPRINT("month=%ld, dim=%ld, day=%ld",
(long) month, (long) dim, (long) day));
if (day < dim) {
break;
}
day -= dim;
}
DUK_DDD(DUK_DDDPRINT("final month=%ld", (long) month));
DUK_ASSERT(month >= 0 && month <= 11);
DUK_ASSERT(day >= 0 && day <= 31);
/* Equivalent year mapping, used to avoid DST trouble when platform
* may fail to provide reasonable DST answers for dates outside the
* ordinary range (e.g. 1970-2038). An equivalent year has the same
* leap-year-ness as the original year and begins on the same weekday
* (Jan 1).
*
* The year 2038 is avoided because there seem to be problems with it
* on some platforms. The year 1970 is also avoided as there were
* practical problems with it; an equivalent year is used for it too,
* which breaks some DST computations for 1970 right now, see e.g.
* test-bi-date-tzoffset-brute-fi.js.
*/
if ((flags & DUK_DATE_FLAG_EQUIVYEAR) && (year < 1971 || year > 2037)) {
DUK_ASSERT(is_leap == 0 || is_leap == 1);
jan1_since_epoch = day_since_epoch - day_in_year; /* day number for Jan 1 since epoch */
DUK_ASSERT(jan1_since_epoch + DUK__WEEKDAY_MOD_ADDER >= 0);
jan1_weekday = (jan1_since_epoch + 4 + DUK__WEEKDAY_MOD_ADDER) % 7; /* E5.1 Section 15.9.1.6 */
DUK_ASSERT(jan1_weekday >= 0 && jan1_weekday <= 6);
arridx = jan1_weekday;
if (is_leap) {
arridx += 7;
}
DUK_ASSERT(arridx >= 0 && arridx < (duk_small_int_t) (sizeof(duk__date_equivyear) / sizeof(duk_uint8_t)));
equiv_year = (duk_int_t) duk__date_equivyear[arridx] + 1970;
year = equiv_year;
DUK_DDD(DUK_DDDPRINT("equiv year mapping, year=%ld, day_in_year=%ld, day_since_epoch=%ld, "
"jan1_since_epoch=%ld, jan1_weekday=%ld -> equiv year %ld",
(long) year, (long) day_in_year, (long) day_since_epoch,
(long) jan1_since_epoch, (long) jan1_weekday, (long) equiv_year));
}
parts[DUK_DATE_IDX_YEAR] = year;
parts[DUK_DATE_IDX_MONTH] = month;
parts[DUK_DATE_IDX_DAY] = day;
if (flags & DUK_DATE_FLAG_ONEBASED) {
parts[DUK_DATE_IDX_MONTH]++; /* zero-based -> one-based */
parts[DUK_DATE_IDX_DAY]++; /* -""- */
}
if (dparts != NULL) {
for (i = 0; i < DUK_DATE_IDX_NUM_PARTS; i++) {
dparts[i] = (duk_double_t) parts[i];
}
}
}
/* Compute time value from (double) parts. The parts can be either UTC
* or local time; if local, they need to be (conceptually) converted into
* UTC time. The parts may represent valid or invalid time, and may be
* wildly out of range (but may cancel each other and still come out in
* the valid Date range).
*/
DUK_INTERNAL duk_double_t duk_bi_date_get_timeval_from_dparts(duk_double_t *dparts, duk_small_uint_t flags) {
#if defined(DUK_USE_PARANOID_DATE_COMPUTATION)
/* See comments below on MakeTime why these are volatile. */
volatile duk_double_t tmp_time;
volatile duk_double_t tmp_day;
volatile duk_double_t d;
#else
duk_double_t tmp_time;
duk_double_t tmp_day;
duk_double_t d;
#endif
duk_small_uint_t i;
duk_int_t tzoff, tzoffprev1, tzoffprev2;
/* Expects 'this' at top of stack on entry. */
/* Coerce all finite parts with ToInteger(). ToInteger() must not
* be called for NaN/Infinity because it will convert e.g. NaN to
* zero. If ToInteger() has already been called, this has no side
* effects and is idempotent.
*
* Don't read dparts[DUK_DATE_IDX_WEEKDAY]; it will cause Valgrind
* issues if the value is uninitialized.
*/
for (i = 0; i <= DUK_DATE_IDX_MILLISECOND; i++) {
/* SCANBUILD: scan-build complains here about assigned value
* being garbage or undefined. This is correct but operating
* on undefined values has no ill effect and is ignored by the
* caller in the case where this happens.
*/
d = dparts[i];
if (DUK_ISFINITE(d)) {
dparts[i] = duk_js_tointeger_number(d);
}
}
/* Use explicit steps in computation to try to ensure that
* computation happens with intermediate results coerced to
* double values (instead of using something more accurate).
* E.g. E5.1 Section 15.9.1.11 requires use of IEEE 754
* rules (= ECMAScript '+' and '*' operators).
*
* Without 'volatile' even this approach fails on some platform
* and compiler combinations. For instance, gcc 4.8.1 on Ubuntu
* 64-bit, with -m32 and without -std=c99, test-bi-date-canceling.js
* would fail because of some optimizations when computing tmp_time
* (MakeTime below). Adding 'volatile' to tmp_time solved this
* particular problem (annoyingly, also adding debug prints or
* running the executable under valgrind hides it).
*/
/* MakeTime */
tmp_time = 0.0;
tmp_time += dparts[DUK_DATE_IDX_HOUR] * ((duk_double_t) DUK_DATE_MSEC_HOUR);
tmp_time += dparts[DUK_DATE_IDX_MINUTE] * ((duk_double_t) DUK_DATE_MSEC_MINUTE);
tmp_time += dparts[DUK_DATE_IDX_SECOND] * ((duk_double_t) DUK_DATE_MSEC_SECOND);
tmp_time += dparts[DUK_DATE_IDX_MILLISECOND];
/* MakeDay */
tmp_day = duk__make_day(dparts[DUK_DATE_IDX_YEAR], dparts[DUK_DATE_IDX_MONTH], dparts[DUK_DATE_IDX_DAY]);
/* MakeDate */
d = tmp_day * ((duk_double_t) DUK_DATE_MSEC_DAY) + tmp_time;
DUK_DDD(DUK_DDDPRINT("time=%lf day=%lf --> timeval=%lf",
(double) tmp_time, (double) tmp_day, (double) d));
/* Optional UTC conversion. */
if (flags & DUK_DATE_FLAG_LOCALTIME) {
/* DUK_USE_DATE_GET_LOCAL_TZOFFSET() needs to be called with a
* time value computed from UTC parts. At this point we only
* have 'd' which is a time value computed from local parts, so
* it is off by the UTC-to-local time offset which we don't know
* yet. The current solution for computing the UTC-to-local
* time offset is to iterate a few times and detect a fixed
* point or a two-cycle loop (or a sanity iteration limit),
* see test-bi-date-local-parts.js and test-bi-date-tzoffset-basic-fi.js.
*
* E5.1 Section 15.9.1.9:
* UTC(t) = t - LocalTZA - DaylightSavingTA(t - LocalTZA)
*
* For NaN/inf, DUK_USE_DATE_GET_LOCAL_TZOFFSET() returns 0.
*/
#if 0
/* Old solution: don't iterate, incorrect */
tzoff = DUK_USE_DATE_GET_LOCAL_TZOFFSET(d);
DUK_DDD(DUK_DDDPRINT("tzoffset w/o iteration, tzoff=%ld", (long) tzoff));
d -= tzoff * 1000L;
DUK_UNREF(tzoffprev1);
DUK_UNREF(tzoffprev2);
#endif
/* Iteration solution */
tzoff = 0;
tzoffprev1 = 999999999L; /* invalid value which never matches */
for (i = 0; i < DUK__LOCAL_TZOFFSET_MAXITER; i++) {
tzoffprev2 = tzoffprev1;
tzoffprev1 = tzoff;
tzoff = DUK_USE_DATE_GET_LOCAL_TZOFFSET(d - tzoff * 1000L);
DUK_DDD(DUK_DDDPRINT("tzoffset iteration, i=%d, tzoff=%ld, tzoffprev1=%ld tzoffprev2=%ld",
(int) i, (long) tzoff, (long) tzoffprev1, (long) tzoffprev2));
if (tzoff == tzoffprev1) {
DUK_DDD(DUK_DDDPRINT("tzoffset iteration finished, i=%d, tzoff=%ld, tzoffprev1=%ld, tzoffprev2=%ld",
(int) i, (long) tzoff, (long) tzoffprev1, (long) tzoffprev2));
break;
} else if (tzoff == tzoffprev2) {
/* Two value cycle, see e.g. test-bi-date-tzoffset-basic-fi.js.
* In these cases, favor a higher tzoffset to get a consistent
* result which is independent of iteration count. Not sure if
* this is a generically correct solution.
*/
DUK_DDD(DUK_DDDPRINT("tzoffset iteration two-value cycle, i=%d, tzoff=%ld, tzoffprev1=%ld, tzoffprev2=%ld",
(int) i, (long) tzoff, (long) tzoffprev1, (long) tzoffprev2));
if (tzoffprev1 > tzoff) {
tzoff = tzoffprev1;
}
break;
}
}
DUK_DDD(DUK_DDDPRINT("tzoffset iteration, tzoff=%ld", (long) tzoff));
d -= tzoff * 1000L;
}
/* TimeClip(), which also handles Infinity -> NaN conversion */
d = duk__timeclip(d);
return d;
}
/*
* API oriented helpers
*/
/* Push 'this' binding, check that it is a Date object; then push the
* internal time value. At the end, stack is: [ ... this timeval ].
* Returns the time value. Local time adjustment is done if requested.
*/
DUK_LOCAL duk_double_t duk__push_this_get_timeval_tzoffset(duk_hthread *thr, duk_small_uint_t flags, duk_int_t *out_tzoffset) {
duk_hobject *h;
duk_double_t d;
duk_int_t tzoffset = 0;
duk_push_this(thr);
h = duk_get_hobject(thr, -1); /* XXX: getter with class check, useful in built-ins */
if (h == NULL || DUK_HOBJECT_GET_CLASS_NUMBER(h) != DUK_HOBJECT_CLASS_DATE) {
DUK_ERROR_TYPE(thr, "expected Date");
DUK_WO_NORETURN(return 0.0;);
}
duk_xget_owndataprop_stridx_short(thr, -1, DUK_STRIDX_INT_VALUE);
d = duk_to_number_m1(thr);
duk_pop(thr);
if (DUK_ISNAN(d)) {
if (flags & DUK_DATE_FLAG_NAN_TO_ZERO) {
d = 0.0;
}
if (flags & DUK_DATE_FLAG_NAN_TO_RANGE_ERROR) {
DUK_ERROR_RANGE(thr, "Invalid Date");
DUK_WO_NORETURN(return 0.0;);
}
}
/* if no NaN handling flag, may still be NaN here, but not Inf */
DUK_ASSERT(!DUK_ISINF(d));
if (flags & DUK_DATE_FLAG_LOCALTIME) {
/* Note: DST adjustment is determined using UTC time.
* If 'd' is NaN, tzoffset will be 0.
*/
tzoffset = DUK_USE_DATE_GET_LOCAL_TZOFFSET(d); /* seconds */
d += tzoffset * 1000L;
}
if (out_tzoffset) {
*out_tzoffset = tzoffset;
}
/* [ ... this ] */
return d;
}
DUK_LOCAL duk_double_t duk__push_this_get_timeval(duk_hthread *thr, duk_small_uint_t flags) {
return duk__push_this_get_timeval_tzoffset(thr, flags, NULL);
}
/* Set timeval to 'this' from dparts, push the new time value onto the
* value stack and return 1 (caller can then tail call us). Expects
* the value stack to contain 'this' on the stack top.
*/
DUK_LOCAL duk_ret_t duk__set_this_timeval_from_dparts(duk_hthread *thr, duk_double_t *dparts, duk_small_uint_t flags) {
duk_double_t d;
/* [ ... this ] */
d = duk_bi_date_get_timeval_from_dparts(dparts, flags);
duk_push_number(thr, d); /* -> [ ... this timeval_new ] */
duk_dup_top(thr); /* -> [ ... this timeval_new timeval_new ] */
/* Must force write because e.g. .setYear() must work even when
* the Date instance is frozen.
*/
duk_xdef_prop_stridx_short(thr, -3, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_W);
/* Stack top: new time value, return 1 to allow tail calls. */
return 1;
}
/* 'out_buf' must be at least DUK_BI_DATE_ISO8601_BUFSIZE long. */
DUK_LOCAL void duk__format_parts_iso8601(duk_int_t *parts, duk_int_t tzoffset, duk_small_uint_t flags, duk_uint8_t *out_buf) {
char yearstr[8]; /* "-123456\0" */
char tzstr[8]; /* "+11:22\0" */
char sep = (flags & DUK_DATE_FLAG_SEP_T) ? DUK_ASC_UC_T : DUK_ASC_SPACE;
DUK_ASSERT(parts[DUK_DATE_IDX_MONTH] >= 1 && parts[DUK_DATE_IDX_MONTH] <= 12);
DUK_ASSERT(parts[DUK_DATE_IDX_DAY] >= 1 && parts[DUK_DATE_IDX_DAY] <= 31);
DUK_ASSERT(parts[DUK_DATE_IDX_YEAR] >= -999999 && parts[DUK_DATE_IDX_YEAR] <= 999999);
/* Note: %06d for positive value, %07d for negative value to include
* sign and 6 digits.
*/
DUK_SNPRINTF(yearstr,
sizeof(yearstr),
(parts[DUK_DATE_IDX_YEAR] >= 0 && parts[DUK_DATE_IDX_YEAR] <= 9999) ? "%04ld" :
((parts[DUK_DATE_IDX_YEAR] >= 0) ? "+%06ld" : "%07ld"),
(long) parts[DUK_DATE_IDX_YEAR]);
yearstr[sizeof(yearstr) - 1] = (char) 0;
if (flags & DUK_DATE_FLAG_LOCALTIME) {
/* tzoffset seconds are dropped; 16 bits suffice for
* time offset in minutes
*/
const char *fmt;
duk_small_int_t tmp, arg_hours, arg_minutes;
if (tzoffset >= 0) {
tmp = tzoffset;
fmt = "+%02d:%02d";
} else {
tmp = -tzoffset;
fmt = "-%02d:%02d";
}
tmp = tmp / 60;
arg_hours = tmp / 60;
arg_minutes = tmp % 60;
DUK_ASSERT(arg_hours <= 24); /* Even less is actually guaranteed for a valid tzoffset. */
arg_hours = arg_hours & 0x3f; /* For [0,24] this is a no-op, but fixes GCC 7 warning, see https://github.com/svaarala/duktape/issues/1602. */
DUK_SNPRINTF(tzstr, sizeof(tzstr), fmt, (int) arg_hours, (int) arg_minutes);