-
-
Notifications
You must be signed in to change notification settings - Fork 601
Expand file tree
/
Copy pathutils.rs
More file actions
1078 lines (954 loc) · 33.4 KB
/
utils.rs
File metadata and controls
1078 lines (954 loc) · 33.4 KB
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
use crate::{JsStr, JsString, context::HostHooks, js_string, value::IntegerOrInfinity};
use boa_macros::js_str;
use boa_string::JsStrVariant;
use std::slice::Iter;
use std::str;
use std::{borrow::Cow, iter::Peekable};
use time::{OffsetDateTime, PrimitiveDateTime, macros::format_description};
// Time-related Constants
//
// More info:
// - [ECMAScript reference][spec]
//
// https://tc39.es/ecma262/#sec-time-related-constants
// HoursPerDay = 24
const HOURS_PER_DAY: f64 = 24.0;
// MinutesPerHour = 60
const MINUTES_PER_HOUR: f64 = 60.0;
// SecondsPerMinute = 60
const SECONDS_PER_MINUTE: f64 = 60.0;
// msPerSecond = 1000𝔽
const MS_PER_SECOND: f64 = 1000.0;
// msPerMinute = 60000𝔽 = msPerSecond × 𝔽(SecondsPerMinute)
pub(super) const MS_PER_MINUTE: f64 = MS_PER_SECOND * SECONDS_PER_MINUTE;
// msPerHour = 3600000𝔽 = msPerMinute × 𝔽(MinutesPerHour)
const MS_PER_HOUR: f64 = MS_PER_MINUTE * MINUTES_PER_HOUR;
// msPerDay = 86400000𝔽 = msPerHour × 𝔽(HoursPerDay)
const MS_PER_DAY: f64 = MS_PER_HOUR * HOURS_PER_DAY;
/// Abstract operation `Day ( t )`
///
/// More info:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-day
pub(super) fn day(t: f64) -> f64 {
// 1. Return 𝔽(floor(ℝ(t / msPerDay))).
(t / MS_PER_DAY).floor()
}
/// Abstract operation `TimeWithinDay ( t )`
///
/// More info:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-timewithinday
pub(super) fn time_within_day(t: f64) -> f64 {
// 1. Return 𝔽(ℝ(t) modulo ℝ(msPerDay)).
t.rem_euclid(MS_PER_DAY)
}
/// Abstract operation `DaysInYear ( y )`
///
/// More info:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-daysinyear
fn days_in_year(y: f64) -> u16 {
// 1. Let ry be ℝ(y).
let ry = y;
// 2. If (ry modulo 400) = 0, return 366𝔽.
if ry.rem_euclid(400.0) == 0.0 {
return 366;
}
// 3. If (ry modulo 100) = 0, return 365𝔽.
if ry.rem_euclid(100.0) == 0.0 {
return 365;
}
// 4. If (ry modulo 4) = 0, return 366𝔽.
if ry.rem_euclid(4.0) == 0.0 {
return 366;
}
// 5. Return 365𝔽.
365
}
/// Abstract operation `DayFromYear ( y )`
///
/// More info:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-dayfromyear
fn day_from_year(y: f64) -> f64 {
// 1. Let ry be ℝ(y).
// 2. NOTE: In the following steps, each _numYearsN_ is the number of years divisible by N
// that occur between the epoch and the start of year y.
// (The number is negative if y is before the epoch.)
// 3. Let numYears1 be (ry - 1970).
let num_years_1 = y - 1970.0;
// 4. Let numYears4 be floor((ry - 1969) / 4).
let num_years_4 = ((y - 1969.0) / 4.0).floor();
// 5. Let numYears100 be floor((ry - 1901) / 100).
let num_years_100 = ((y - 1901.0) / 100.0).floor();
// 6. Let numYears400 be floor((ry - 1601) / 400).
let num_years_400 = ((y - 1601.0) / 400.0).floor();
// 7. Return 𝔽(365 × numYears1 + numYears4 - numYears100 + numYears400).
365.0 * num_years_1 + num_years_4 - num_years_100 + num_years_400
}
/// Abstract operation `TimeFromYear ( y )`
///
/// More info:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-timefromyear
fn time_from_year(y: f64) -> f64 {
// 1. Return msPerDay × DayFromYear(y).
MS_PER_DAY * day_from_year(y)
}
/// Abstract operation `YearFromTime ( t )`
///
/// More info:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-yearfromtime
pub(crate) fn year_from_time(t: f64) -> i32 {
const MS_PER_AVERAGE_YEAR: f64 = 12.0 * 30.436_875 * MS_PER_DAY;
// 1. Return the largest integral Number y (closest to +∞) such that TimeFromYear(y) ≤ t.
let mut year = (((t + MS_PER_AVERAGE_YEAR / 2.0) / MS_PER_AVERAGE_YEAR).floor()) as i32 + 1970;
if time_from_year(year.into()) > t {
year -= 1;
}
year
}
/// Abstract operation `DayWithinYear ( t )`
///
/// More info:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-daywithinyear
fn day_within_year(t: f64) -> u16 {
// 1. Return Day(t) - DayFromYear(YearFromTime(t)).
(day(t) - day_from_year(year_from_time(t).into())) as u16
}
/// Abstract operation `InLeapYear ( t )`
///
/// More info:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-inleapyear
fn in_leap_year(t: f64) -> u16 {
// 1. If DaysInYear(YearFromTime(t)) is 366𝔽, return 1𝔽; else return +0𝔽.
(days_in_year(year_from_time(t).into()) == 366).into()
}
/// Abstract operation `MonthFromTime ( t )`
///
/// More info:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-monthfromtime
pub(crate) fn month_from_time(t: f64) -> u8 {
// 1. Let inLeapYear be InLeapYear(t).
let in_leap_year = in_leap_year(t);
// 2. Let dayWithinYear be DayWithinYear(t).
let day_within_year = day_within_year(t);
match day_within_year {
// 3. If dayWithinYear < 31𝔽, return +0𝔽.
t if t < 31 => 0,
// 4. If dayWithinYear < 59𝔽 + inLeapYear, return 1𝔽.
t if t < 59 + in_leap_year => 1,
// 5. If dayWithinYear < 90𝔽 + inLeapYear, return 2𝔽.
t if t < 90 + in_leap_year => 2,
// 6. If dayWithinYear < 120𝔽 + inLeapYear, return 3𝔽.
t if t < 120 + in_leap_year => 3,
// 7. If dayWithinYear < 151𝔽 + inLeapYear, return 4𝔽.
t if t < 151 + in_leap_year => 4,
// 8. If dayWithinYear < 181𝔽 + inLeapYear, return 5𝔽.
t if t < 181 + in_leap_year => 5,
// 9. If dayWithinYear < 212𝔽 + inLeapYear, return 6𝔽.
t if t < 212 + in_leap_year => 6,
// 10. If dayWithinYear < 243𝔽 + inLeapYear, return 7𝔽.
t if t < 243 + in_leap_year => 7,
// 11. If dayWithinYear < 273𝔽 + inLeapYear, return 8𝔽.
t if t < 273 + in_leap_year => 8,
// 12. If dayWithinYear < 304𝔽 + inLeapYear, return 9𝔽.
t if t < 304 + in_leap_year => 9,
// 13. If dayWithinYear < 334𝔽 + inLeapYear, return 10𝔽.
t if t < 334 + in_leap_year => 10,
// 14. Assert: dayWithinYear < 365𝔽 + inLeapYear.
// 15. Return 11𝔽.
_ => 11,
}
}
/// Abstract operation `DateFromTime ( t )`
///
/// More info:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-datefromtime
pub(crate) fn date_from_time(t: f64) -> u8 {
// 1. Let inLeapYear be InLeapYear(t).
let in_leap_year = in_leap_year(t);
// 2. Let dayWithinYear be DayWithinYear(t).
let day_within_year = day_within_year(t);
// 3. Let month be MonthFromTime(t).
let month = month_from_time(t);
let date = match month {
// 4. If month is +0𝔽, return dayWithinYear + 1𝔽.
0 => day_within_year + 1,
// 5. If month is 1𝔽, return dayWithinYear - 30𝔽.
1 => day_within_year - 30,
// 6. If month is 2𝔽, return dayWithinYear - 58𝔽 - inLeapYear.
2 => day_within_year - 58 - in_leap_year,
// 7. If month is 3𝔽, return dayWithinYear - 89𝔽 - inLeapYear.
3 => day_within_year - 89 - in_leap_year,
// 8. If month is 4𝔽, return dayWithinYear - 119𝔽 - inLeapYear.
4 => day_within_year - 119 - in_leap_year,
// 9. If month is 5𝔽, return dayWithinYear - 150𝔽 - inLeapYear.
5 => day_within_year - 150 - in_leap_year,
// 10. If month is 6𝔽, return dayWithinYear - 180𝔽 - inLeapYear.
6 => day_within_year - 180 - in_leap_year,
// 11. If month is 7𝔽, return dayWithinYear - 211𝔽 - inLeapYear.
7 => day_within_year - 211 - in_leap_year,
// 12. If month is 8𝔽, return dayWithinYear - 242𝔽 - inLeapYear.
8 => day_within_year - 242 - in_leap_year,
// 13. If month is 9𝔽, return dayWithinYear - 272𝔽 - inLeapYear.
9 => day_within_year - 272 - in_leap_year,
// 14. If month is 10𝔽, return dayWithinYear - 303𝔽 - inLeapYear.
10 => day_within_year - 303 - in_leap_year,
// 15. Assert: month is 11𝔽.
// 16. Return dayWithinYear - 333𝔽 - inLeapYear.
_ => day_within_year - 333 - in_leap_year,
};
date as u8
}
/// Abstract operation `WeekDay ( t )`
///
/// More info:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-weekday
pub(super) fn week_day(t: f64) -> u8 {
// 1. Return 𝔽(ℝ(Day(t) + 4𝔽) modulo 7).
(day(t) + 4.0).rem_euclid(7.0) as u8
}
/// Abstract operation `HourFromTime ( t )`
///
/// More info:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-hourfromtime
pub(crate) fn hour_from_time(t: f64) -> u8 {
// 1. Return 𝔽(floor(ℝ(t / msPerHour)) modulo HoursPerDay).
((t / MS_PER_HOUR).floor()).rem_euclid(HOURS_PER_DAY) as u8
}
/// Abstract operation `MinFromTime ( t )`
///
/// More info:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-minfromtime
pub(crate) fn min_from_time(t: f64) -> u8 {
// 1. Return 𝔽(floor(ℝ(t / msPerMinute)) modulo MinutesPerHour).
((t / MS_PER_MINUTE).floor()).rem_euclid(MINUTES_PER_HOUR) as u8
}
/// Abstract operation `SecFromTime ( t )`
///
/// More info:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-secfromtime
pub(crate) fn sec_from_time(t: f64) -> u8 {
// 1. Return 𝔽(floor(ℝ(t / msPerSecond)) modulo SecondsPerMinute).
((t / MS_PER_SECOND).floor()).rem_euclid(SECONDS_PER_MINUTE) as u8
}
/// Abstract operation `msFromTime ( t )`
///
/// More info:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-msfromtime
pub(crate) fn ms_from_time(t: f64) -> u16 {
// 1. Return 𝔽(ℝ(t) modulo ℝ(msPerSecond)).
t.rem_euclid(MS_PER_SECOND) as u16
}
/// Abstract operation `LocalTime ( t )`
///
/// More info:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-localtime
pub(super) fn local_time(t: f64, hooks: &dyn HostHooks) -> f64 {
t + f64::from(local_timezone_offset_seconds(t, hooks)) * MS_PER_SECOND
}
/// Abstract operation `UTC ( t )`
///
/// More info:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-utc-t
pub(super) fn utc_t(t: f64, hooks: &dyn HostHooks) -> f64 {
// 1. If t is not finite, return NaN.
if !t.is_finite() {
return f64::NAN;
}
t - f64::from(local_timezone_offset_seconds(t, hooks)) * MS_PER_SECOND
}
/// Abstract operation `MakeTime ( hour, min, sec, ms )`
///
/// More info:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-maketime
pub(super) fn make_time(hour: f64, min: f64, sec: f64, ms: f64) -> f64 {
// 1. If hour is not finite, min is not finite, sec is not finite, or ms is not finite, return NaN.
if !hour.is_finite() || !min.is_finite() || !sec.is_finite() || !ms.is_finite() {
return f64::NAN;
}
// 2. Let h be 𝔽(! ToIntegerOrInfinity(hour)).
let h = hour.abs().floor().copysign(hour);
// 3. Let m be 𝔽(! ToIntegerOrInfinity(min)).
let m = min.abs().floor().copysign(min);
// 4. Let s be 𝔽(! ToIntegerOrInfinity(sec)).
let s = sec.abs().floor().copysign(sec);
// 5. Let milli be 𝔽(! ToIntegerOrInfinity(ms)).
let milli = ms.abs().floor().copysign(ms);
// 6. Return ((h × msPerHour + m × msPerMinute) + s × msPerSecond) + milli.
((h * MS_PER_HOUR + m * MS_PER_MINUTE) + s * MS_PER_SECOND) + milli
}
/// Abstract operation `MakeDay ( year, month, date )`
///
/// More info:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-makeday
pub(super) fn make_day(year: f64, month: f64, date: f64) -> f64 {
// 1. If year is not finite, month is not finite, or date is not finite, return NaN.
if !year.is_finite() || !month.is_finite() || !date.is_finite() {
return f64::NAN;
}
// 2. Let y be 𝔽(! ToIntegerOrInfinity(year)).
let y = year.abs().floor().copysign(year);
// 3. Let m be 𝔽(! ToIntegerOrInfinity(month)).
let m = month.abs().floor().copysign(month);
// 4. Let dt be 𝔽(! ToIntegerOrInfinity(date)).
let dt = date.abs().floor().copysign(date);
// 5. Let ym be y + 𝔽(floor(ℝ(m) / 12)).
let ym = y + (m / 12.0).floor();
// 6. If ym is not finite, return NaN.
if !ym.is_finite() {
return f64::NAN;
}
// 7. Let mn be 𝔽(ℝ(m) modulo 12).
let mn = m.rem_euclid(12.0) as u8;
// 8. Find a finite time value t such that YearFromTime(t) is ym, MonthFromTime(t) is mn,
// and DateFromTime(t) is 1𝔽;
// but if this is not possible (because some argument is out of range), return NaN.
let rest = if mn > 1 { 1.0 } else { 0.0 };
let days_within_year_to_end_of_month = match mn {
0 => 0.0,
1 => 31.0,
2 => 59.0,
3 => 90.0,
4 => 120.0,
5 => 151.0,
6 => 181.0,
7 => 212.0,
8 => 243.0,
9 => 273.0,
10 => 304.0,
11 => 334.0,
12 => 365.0,
_ => unreachable!(),
};
let t =
(day_from_year(ym + rest) - 365.0 * rest + days_within_year_to_end_of_month) * MS_PER_DAY;
// 9. Return Day(t) + dt - 1𝔽.
day(t) + dt - 1.0
}
/// Abstract operation `MakeDate ( day, time )`
///
/// More info:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-makedate
pub(super) fn make_date(day: f64, time: f64) -> f64 {
// 1. If day is not finite or time is not finite, return NaN.
if !day.is_finite() || !time.is_finite() {
return f64::NAN;
}
// 2. Let tv be day × msPerDay + time.
let tv = day * MS_PER_DAY + time;
// 3. If tv is not finite, return NaN.
if !tv.is_finite() {
return f64::NAN;
}
// 4. Return tv.
tv
}
#[cfg(feature = "intl")]
pub(crate) fn timestamp_for_first_of_month_utc(year: i32, month: u8) -> f64 {
debug_assert!((1..=12).contains(&month));
let day = make_day(f64::from(year), f64::from(month - 1), 1.0);
let time = make_time(0.0, 0.0, 0.0, 0.0);
time_clip(make_date(day, time))
}
/// Abstract operation `MakeFullYear ( year )`
///
/// More info:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-makefullyear
pub(super) fn make_full_year(year: f64) -> f64 {
// 1. If year is NaN, return NaN.
if year.is_nan() {
return f64::NAN;
}
// 2. Let truncated be ! ToIntegerOrInfinity(year).
let truncated = IntegerOrInfinity::from(year);
// 3. If truncated is in the inclusive interval from 0 to 99, return 1900𝔽 + 𝔽(truncated).
// 4. Return 𝔽(truncated).
match truncated {
IntegerOrInfinity::Integer(i) if (0..=99).contains(&i) => 1900.0 + i as f64,
IntegerOrInfinity::Integer(i) => i as f64,
IntegerOrInfinity::PositiveInfinity => f64::INFINITY,
IntegerOrInfinity::NegativeInfinity => f64::NEG_INFINITY,
}
}
/// Abstract operation `TimeClip ( time )`
///
/// More info:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-timeclip
pub(crate) fn time_clip(time: f64) -> f64 {
// 1. If time is not finite, return NaN.
if !time.is_finite() {
return f64::NAN;
}
// 2. If abs(ℝ(time)) > 8.64 × 10**15, return NaN.
if time.abs() > 8.64e15 {
return f64::NAN;
}
// 3. Return 𝔽(! ToIntegerOrInfinity(time)).
let time = time.trunc();
if time.abs() == 0.0 {
return 0.0;
}
time
}
/// Abstract operation `TimeString ( tv )`
///
/// More info:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-timestring
pub(super) fn time_string(tv: f64) -> JsString {
// 1. Let hour be ToZeroPaddedDecimalString(ℝ(HourFromTime(tv)), 2).
let mut binding = [0; 2];
let hour = pad_two(hour_from_time(tv), &mut binding);
// 2. Let minute be ToZeroPaddedDecimalString(ℝ(MinFromTime(tv)), 2).
let mut binding = [0; 2];
let minute = pad_two(min_from_time(tv), &mut binding);
// 3. Let second be ToZeroPaddedDecimalStringbindingFromTime(tv)), 2).
let mut binding = [0; 2];
let second = pad_two(sec_from_time(tv), &mut binding);
// 4. Return the string-concatenation of
// hour,
// ":",
// minute,
// ":",
// second,
// the code unit 0x0020 (SPACE),
// and "GMT".
js_string!(
hour,
js_str!(":"),
minute,
js_str!(":"),
second,
js_str!(" GMT")
)
}
/// Abstract operation `DateString ( tv )`
///
/// More info:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-datestring
pub(super) fn date_string(tv: f64) -> JsString {
// 1. Let weekday be the Name of the entry in Table 63 with the Number WeekDay(tv).
let weekday = match week_day(tv) {
0 => js_str!("Sun"),
1 => js_str!("Mon"),
2 => js_str!("Tue"),
3 => js_str!("Wed"),
4 => js_str!("Thu"),
5 => js_str!("Fri"),
6 => js_str!("Sat"),
_ => unreachable!(),
};
// 2. Let month be the Name of the entry in Table 64 with the Number MonthFromTime(tv).
let month = match month_from_time(tv) {
0 => js_str!("Jan"),
1 => js_str!("Feb"),
2 => js_str!("Mar"),
3 => js_str!("Apr"),
4 => js_str!("May"),
5 => js_str!("Jun"),
6 => js_str!("Jul"),
7 => js_str!("Aug"),
8 => js_str!("Sep"),
9 => js_str!("Oct"),
10 => js_str!("Nov"),
11 => js_str!("Dec"),
_ => unreachable!(),
};
// 3. Let day be ToZeroPaddedDecimalString(ℝ(DateFromTime(tv)), 2).
let mut binding = [0; 2];
let day = pad_two(date_from_time(tv), &mut binding);
// 4. Let yv be YearFromTime(tv).
let yv = year_from_time(tv);
// 5. If yv is +0𝔽 or yv > +0𝔽, let yearSign be the empty String; otherwise, let yearSign be "-".
let year_sign = if yv >= 0 { js_str!("") } else { js_str!("-") };
// 6. Let paddedYear be ToZeroPaddedDecimalString(abs(ℝ(yv)), 4).
let yv = yv.unsigned_abs();
let padded_year: JsString = if yv >= 100_000 {
pad_six(yv, &mut [0; 6]).into()
} else if yv >= 10000 {
pad_five(yv, &mut [0; 5]).into()
} else {
pad_four(yv, &mut [0; 4]).into()
};
// 7. Return the string-concatenation of
// weekday,
// the code unit 0x0020 (SPACE),
// month,
// the code unit 0x0020 (SPACE),
// day,
// the code unit 0x0020 (SPACE),
// yearSign,
// and paddedYear.
js_string!(
weekday,
js_str!(" "),
month,
js_str!(" "),
day,
js_str!(" "),
year_sign,
&padded_year
)
}
/// Abstract operation `TimeZoneString ( tv )`
///
/// More info:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-timezoneestring
pub(super) fn time_zone_string(t: f64, hooks: &dyn HostHooks) -> JsString {
// 1. Let systemTimeZoneIdentifier be SystemTimeZoneIdentifier().
// 2. If IsTimeZoneOffsetString(systemTimeZoneIdentifier) is true, then
// a. Let offsetNs be ParseTimeZoneOffsetString(systemTimeZoneIdentifier).
// 3. Else,
// a. Let offsetNs be GetNamedTimeZoneOffsetNanoseconds(systemTimeZoneIdentifier, ℤ(ℝ(tv) × 10**6)).
// 4. Let offset be 𝔽(truncate(offsetNs / 10**6)).
let offset = f64::from(local_timezone_offset_seconds(t, hooks)) * MS_PER_SECOND;
//let offset = hooks.local_timezone_offset_seconds((t / MS_PER_SECOND).floor() as i64);
// 5. If offset is +0𝔽 or offset > +0𝔽, then
let (offset_sign, abs_offset) = if offset >= 0.0 {
// a. Let offsetSign be "+".
// b. Let absOffset be offset.
(js_str!("+"), offset)
}
// 6. Else,
else {
// a. Let offsetSign be "-".
// b. Let absOffset be -offset.
(js_str!("-"), -offset)
};
// 7. Let offsetMin be ToZeroPaddedDecimalString(ℝ(MinFromTime(absOffset)), 2).
let mut binding = [0; 2];
let offset_min = pad_two(min_from_time(abs_offset), &mut binding);
// 8. Let offsetHour be ToZeroPaddedDecimalString(ℝ(HourFromTime(absOffset)), 2).
let mut binding = [0; 2];
let offset_hour = pad_two(hour_from_time(abs_offset), &mut binding);
// 9. Let tzName be an implementation-defined string that is either the empty String or the
// string-concatenation of the code unit 0x0020 (SPACE), the code unit 0x0028 (LEFT PARENTHESIS),
// an implementation-defined timezone name, and the code unit 0x0029 (RIGHT PARENTHESIS).
// 10. Return the string-concatenation of offsetSign, offsetHour, offsetMin, and tzName.
js_string!(offset_sign, offset_hour, offset_min)
}
/// Abstract operation `ToDateString ( tv )`
///
/// More info:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-todatestring
pub(super) fn to_date_string_t(tv: f64, hooks: &dyn HostHooks) -> JsString {
// 1. If tv is NaN, return "Invalid Date".
if tv.is_nan() {
return js_string!("Invalid Date");
}
// 2. Let t be LocalTime(tv).
let t = local_time(tv, hooks);
// 3. Return the string-concatenation of
// DateString(t),
// the code unit 0x0020 (SPACE),
// TimeString(t),
// and TimeZoneString(tv).
js_string!(
&date_string(t),
js_str!(" "),
&time_string(t),
&time_zone_string(t, hooks)
)
}
fn local_timezone_offset_seconds(t: f64, hooks: &dyn HostHooks) -> i32 {
let millis = t.rem_euclid(MS_PER_SECOND);
let seconds = ((t - millis) / MS_PER_SECOND) as i64;
hooks.local_timezone_offset_seconds(seconds)
}
pub(super) fn pad_two(t: u8, output: &mut [u8; 2]) -> JsStr<'_> {
*output = if t < 10 {
[b'0', b'0' + t]
} else {
[b'0' + (t / 10), b'0' + (t % 10)]
};
debug_assert!(output.is_ascii());
JsStr::latin1(output)
}
pub(super) fn pad_three(t: u16, output: &mut [u8; 3]) -> JsStr<'_> {
*output = [
b'0' + (t / 100) as u8,
b'0' + ((t / 10) % 10) as u8,
b'0' + (t % 10) as u8,
];
JsStr::latin1(output)
}
pub(super) fn pad_four(t: u32, output: &mut [u8; 4]) -> JsStr<'_> {
*output = [
b'0' + (t / 1000) as u8,
b'0' + ((t / 100) % 10) as u8,
b'0' + ((t / 10) % 10) as u8,
b'0' + (t % 10) as u8,
];
JsStr::latin1(output)
}
pub(super) fn pad_five(t: u32, output: &mut [u8; 5]) -> JsStr<'_> {
*output = [
b'0' + (t / 10_000) as u8,
b'0' + ((t / 1000) % 10) as u8,
b'0' + ((t / 100) % 10) as u8,
b'0' + ((t / 10) % 10) as u8,
b'0' + (t % 10) as u8,
];
JsStr::latin1(output)
}
pub(super) fn pad_six(t: u32, output: &mut [u8; 6]) -> JsStr<'_> {
*output = [
b'0' + (t / 100_000) as u8,
b'0' + ((t / 10_000) % 10) as u8,
b'0' + ((t / 1000) % 10) as u8,
b'0' + ((t / 100) % 10) as u8,
b'0' + ((t / 10) % 10) as u8,
b'0' + (t % 10) as u8,
];
JsStr::latin1(output)
}
/// Parse a date string according to the steps specified in [`Date.parse`][spec].
///
/// We parse three different formats:
/// - The [`Date Time String Format`][spec-format] specified in the spec: `YYYY-MM-DDTHH:mm:ss.sssZ`
/// - The `toString` format: `Thu Jan 01 1970 00:00:00 GMT+0000`
/// - The `toUTCString` format: `Thu, 01 Jan 1970 00:00:00 GMT`
///
/// [spec]: https://tc39.es/ecma262/#sec-date.parse
/// [spec-format]: https://tc39.es/ecma262/#sec-date-time-string-format
pub(super) fn parse_date(date: &JsString, hooks: &dyn HostHooks) -> Option<i64> {
// All characters must be ASCII so we can return early if we find a non-ASCII character.
let owned_js_str = date.as_str();
let date = match owned_js_str.variant() {
JsStrVariant::Latin1(s) => {
if !s.is_ascii() {
return None;
}
// SAFETY: Since all characters are ASCII we can safely convert this into str.
Cow::Borrowed(unsafe { str::from_utf8_unchecked(s) })
}
JsStrVariant::Utf16(s) => {
let date = String::from_utf16(s).ok()?;
if !date.is_ascii() {
return None;
}
Cow::Owned(date)
}
};
// Date Time String Format: 'YYYY-MM-DDTHH:mm:ss.sssZ'
if let Some(dt) = DateParser::new(&date, hooks).parse() {
return Some(dt);
}
// `toString` format: `Thu Jan 01 1970 00:00:00 GMT+0000`
if let Ok(t) = OffsetDateTime::parse(
&date,
&format_description!(
"[weekday repr:short] [month repr:short] [day] [year] [hour]:[minute]:[second] GMT[offset_hour sign:mandatory][offset_minute][end]"
),
) {
return Some(t.unix_timestamp() * 1000 + i64::from(t.millisecond()));
}
// `toUTCString` format: `Thu, 01 Jan 1970 00:00:00 GMT`
if let Ok(t) = PrimitiveDateTime::parse(
&date,
&format_description!(
"[weekday repr:short], [day] [month repr:short] [year] [hour]:[minute]:[second] GMT[end]"
),
) {
let t = t.assume_utc();
return Some(t.unix_timestamp() * 1000 + i64::from(t.millisecond()));
}
None
}
/// Parses a date string according to the [`Date Time String Format`][spec].
///
/// [spec]: https://tc39.es/ecma262/#sec-date-time-string-format
struct DateParser<'a> {
hooks: &'a dyn HostHooks,
input: Peekable<Iter<'a, u8>>,
year: i32,
month: u32,
day: u32,
hour: u32,
minute: u32,
second: u32,
millisecond: u32,
offset: i64,
}
// Copied from https://github.com/RoDmitry/atoi_simd/blob/master/src/fallback.rs,
// which is based on https://rust-malaysia.github.io/code/2020/07/11/faster-integer-parsing.html.
#[doc(hidden)]
#[allow(clippy::inline_always)]
pub(in crate::builtins::date) mod fast_atoi {
#[inline(always)]
pub(in crate::builtins::date) const fn process_8(mut val: u64, len: usize) -> u64 {
val <<= 64_usize.saturating_sub(len << 3); // << 3 - same as mult by 8
val = (val & 0x0F0F_0F0F_0F0F_0F0F).wrapping_mul(0xA01) >> 8;
val = (val & 0x00FF_00FF_00FF_00FF).wrapping_mul(0x64_0001) >> 16;
(val & 0x0000_FFFF_0000_FFFF).wrapping_mul(0x2710_0000_0001) >> 32
}
#[inline(always)]
pub(in crate::builtins::date) const fn process_4(mut val: u32, len: usize) -> u32 {
val <<= 32_usize.saturating_sub(len << 3); // << 3 - same as mult by 8
val = (val & 0x0F0F_0F0F).wrapping_mul(0xA01) >> 8;
(val & 0x00FF_00FF).wrapping_mul(0x64_0001) >> 16
}
}
impl<'a> DateParser<'a> {
fn new(s: &'a str, hooks: &'a dyn HostHooks) -> Self {
Self {
hooks,
input: s.as_bytes().iter().peekable(),
year: 0,
month: 1,
day: 1,
hour: 0,
minute: 0,
second: 0,
millisecond: 0,
offset: 0,
}
}
fn next_expect(&mut self, expect: u8) -> Option<()> {
self.input
.next()
.and_then(|c| if *c == expect { Some(()) } else { None })
}
fn next_ascii_digit(&mut self) -> Option<u8> {
self.input
.next()
.and_then(|c| if c.is_ascii_digit() { Some(*c) } else { None })
}
fn next_n_ascii_digits<const N: usize>(&mut self) -> Option<[u8; N]> {
let mut digits = [0; N];
for digit in &mut digits {
*digit = self.next_ascii_digit()?;
}
Some(digits)
}
fn parse_n_ascii_digits<const N: usize>(&mut self) -> Option<u64> {
assert!(N <= 8, "parse_n_ascii_digits parses no more than 8 digits");
if N == 0 {
return None;
}
let ascii_digits = self.next_n_ascii_digits::<N>()?;
match N {
1..4 => {
// When N is small, process digits naively.
let mut res = 0;
for digit in ascii_digits {
res = res * 10 + u64::from(digit & 0xF);
}
Some(res)
}
4 => {
// Process digits as an u32 block.
let mut src = [0; 4];
src[..N].copy_from_slice(&ascii_digits);
let val = u32::from_le_bytes(src);
Some(u64::from(fast_atoi::process_4(val, N)))
}
_ => {
// Process digits as an u64 block.
let mut src = [0; 8];
src[..N].copy_from_slice(&ascii_digits);
let val = u64::from_le_bytes(src);
Some(fast_atoi::process_8(val, N))
}
}
}
fn finish(&mut self) -> Option<i64> {
if self.input.peek().is_some() {
return None;
}
// Hour 24 is only valid as 24:00:00.000
if self.hour == 24 && (self.minute != 0 || self.second != 0 || self.millisecond != 0) {
return None;
}
let date = make_date(
make_day(self.year.into(), (self.month - 1).into(), self.day.into()),
make_time(
self.hour.into(),
self.minute.into(),
self.second.into(),
self.millisecond.into(),
),
);
let date = date + (self.offset as f64) * MS_PER_MINUTE;
let t = time_clip(date);
if t.is_finite() { Some(t as i64) } else { None }
}
fn finish_local(&mut self) -> Option<i64> {
if self.input.peek().is_some() {
return None;
}
// Hour 24 is only valid as 24:00:00.000
if self.hour == 24 && (self.minute != 0 || self.second != 0 || self.millisecond != 0) {
return None;
}
let date = make_date(
make_day(self.year.into(), (self.month - 1).into(), self.day.into()),
make_time(
self.hour.into(),
self.minute.into(),
self.second.into(),
self.millisecond.into(),
),
);
let t = time_clip(utc_t(date, self.hooks));
if t.is_finite() { Some(t as i64) } else { None }
}
#[allow(clippy::as_conversions)]
fn parse(&mut self) -> Option<i64> {
self.parse_year()?;
match self.input.peek() {
Some(b'T') => return self.parse_time(),
None => return self.finish(),
_ => {}
}
self.next_expect(b'-')?;
self.month = self.parse_n_ascii_digits::<2>()? as u32;
if self.month < 1 || self.month > 12 {
return None;
}
match self.input.peek() {
Some(b'T') => return self.parse_time(),
None => return self.finish(),
_ => {}
}
self.next_expect(b'-')?;
self.day = self.parse_n_ascii_digits::<2>()? as u32;
if self.day < 1 || self.day > 31 {
return None;
}
match self.input.peek() {
Some(b'T') => self.parse_time(),
_ => self.finish(),
}
}
#[allow(clippy::as_conversions)]
fn parse_year(&mut self) -> Option<()> {
if let &&sign @ (b'+' | b'-') = self.input.peek()? {
// Consume the sign.
self.input.next();
let year = self.parse_n_ascii_digits::<6>()? as i32;