-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatetime.zig
1702 lines (1457 loc) · 59.9 KB
/
datetime.zig
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
// -------------------------------------------------------------------------- //
// Copyright (c) 2019-2022, Jairus Martin. //
// Distributed under the terms of the MIT License. //
// The full license is in the file LICENSE, distributed with this software. //
// -------------------------------------------------------------------------- //
// Some of this is ported from cpython's datetime module
const std = @import("std");
const time = std.time;
const math = std.math;
const ascii = std.ascii;
const Allocator = std.mem.Allocator;
const Order = std.math.Order;
pub const timezones = @import("timezones.zig");
const testing = std.testing;
const assert = std.debug.assert;
// Number of days in each month not accounting for leap year
pub const Weekday = enum(u3) {
Monday = 1,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday,
};
pub const Month = enum(u4) {
January = 1,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December,
// Convert an abbreviation, eg Jan to the enum value
pub fn parseAbbr(month: []const u8) !Month {
if (month.len == 3) {
inline for (std.meta.fields(Month)) |f| {
if (ascii.eqlIgnoreCase(f.name[0..3], month)) {
return @enumFromInt(f.value);
}
}
}
return error.InvalidFormat;
}
pub fn parseName(month: []const u8) !Month {
inline for (std.meta.fields(Month)) |f| {
if (ascii.eqlIgnoreCase(f.name, month)) {
return @enumFromInt(f.value);
}
}
return error.InvalidFormat;
}
};
test "month-parse-abbr" {
try testing.expectEqual(try Month.parseAbbr("Jan"), .January);
try testing.expectEqual(try Month.parseAbbr("Oct"), .October);
try testing.expectEqual(try Month.parseAbbr("sep"), .September);
try testing.expectError(error.InvalidFormat, Month.parseAbbr("cra"));
}
test "month-parse" {
try testing.expectEqual(try Month.parseName("January"), .January);
try testing.expectEqual(try Month.parseName("OCTOBER"), .October);
try testing.expectEqual(try Month.parseName("july"), .July);
try testing.expectError(error.InvalidFormat, Month.parseName("NoShaveNov"));
}
pub const MIN_YEAR: u16 = 1;
pub const MAX_YEAR: u16 = 9999;
pub const MAX_ORDINAL: u32 = 3652059;
const DAYS_IN_MONTH = [12]u8{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
const DAYS_BEFORE_MONTH = [12]u16{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
pub fn isLeapYear(year: u32) bool {
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0);
}
pub fn isLeapDay(year: u32, month: u32, day: u32) bool {
return isLeapYear(year) and month == 2 and day == 29;
}
test "leapyear" {
try testing.expect(isLeapYear(2019) == false);
try testing.expect(isLeapYear(2018) == false);
try testing.expect(isLeapYear(2017) == false);
try testing.expect(isLeapYear(2016) == true);
try testing.expect(isLeapYear(2000) == true);
try testing.expect(isLeapYear(1900) == false);
}
// Number of days before Jan 1st of year
pub fn daysBeforeYear(year: u32) u32 {
const y: u32 = year - 1;
return y * 365 + @divFloor(y, 4) - @divFloor(y, 100) + @divFloor(y, 400);
}
// Days before 1 Jan 1970
const EPOCH = daysBeforeYear(1970) + 1;
test "daysBeforeYear" {
try testing.expect(daysBeforeYear(1996) == 728658);
try testing.expect(daysBeforeYear(2019) == 737059);
}
// Number of days in that month for the year
pub fn daysInMonth(year: u32, month: u32) u8 {
assert(1 <= month and month <= 12);
if (month == 2 and isLeapYear(year)) return 29;
return DAYS_IN_MONTH[month - 1];
}
test "daysInMonth" {
try testing.expect(daysInMonth(2019, 1) == 31);
try testing.expect(daysInMonth(2019, 2) == 28);
try testing.expect(daysInMonth(2016, 2) == 29);
}
// Number of days in year preceding the first day of month
pub fn daysBeforeMonth(year: u32, month: u32) u32 {
assert(month >= 1 and month <= 12);
var d = DAYS_BEFORE_MONTH[month - 1];
if (month > 2 and isLeapYear(year)) d += 1;
return d;
}
// Return number of days since 01-Jan-0001
fn ymd2ord(year: u16, month: u8, day: u8) u32 {
assert(month >= 1 and month <= 12);
assert(day >= 1 and day <= daysInMonth(year, month));
return daysBeforeYear(year) + daysBeforeMonth(year, month) + day;
}
test "ymd2ord" {
try testing.expect(ymd2ord(1970, 1, 1) == 719163);
try testing.expect(ymd2ord(28, 2, 29) == 9921);
try testing.expect(ymd2ord(2019, 11, 27) == 737390);
try testing.expect(ymd2ord(2019, 11, 28) == 737391);
}
test "days-before-year" {
const DI400Y = daysBeforeYear(401); // Num of days in 400 years
const DI100Y = daysBeforeYear(101); // Num of days in 100 years
const DI4Y = daysBeforeYear(5); // Num of days in 4 years
// A 4-year cycle has an extra leap day over what we'd get from pasting
// together 4 single years.
try testing.expect(DI4Y == 4 * 365 + 1);
// Similarly, a 400-year cycle has an extra leap day over what we'd get from
// pasting together 4 100-year cycles.
try testing.expect(DI400Y == 4 * DI100Y + 1);
// OTOH, a 100-year cycle has one fewer leap day than we'd get from
// pasting together 25 4-year cycles.
try testing.expect(DI100Y == 25 * DI4Y - 1);
}
// Calculate the number of days of the first monday for week 1 iso calendar
// for the given year since 01-Jan-0001
pub fn daysBeforeFirstMonday(year: u16) u32 {
// From cpython/datetime.py _isoweek1monday
const THURSDAY = 3;
const first_day = ymd2ord(year, 1, 1);
const first_weekday = (first_day + 6) % 7;
var week1_monday = first_day - first_weekday;
if (first_weekday > THURSDAY) {
week1_monday += 7;
}
return week1_monday;
}
test "iso-first-monday" {
// Created using python
const years = [20]u16{ 1816, 1823, 1839, 1849, 1849, 1870, 1879, 1882, 1909, 1910, 1917, 1934, 1948, 1965, 1989, 2008, 2064, 2072, 2091, 2096 };
const output = [20]u32{ 662915, 665470, 671315, 674969, 674969, 682641, 685924, 687023, 696886, 697250, 699805, 706014, 711124, 717340, 726104, 733041, 753495, 756421, 763358, 765185 };
for (years, 0..) |year, i| {
try testing.expectEqual(daysBeforeFirstMonday(year), output[i]);
}
}
pub const ISOCalendar = struct {
year: u16,
week: u6, // Week of year 1-53
weekday: u3, // Day of week 1-7
};
pub const Date = struct {
year: u16,
month: u4 = 1, // Month of year
day: u8 = 1, // Day of month
// Create and validate the date
pub fn create(year: u32, month: u32, day: u32) !Date {
if (year < MIN_YEAR or year > MAX_YEAR) return error.InvalidDate;
if (month < 1 or month > 12) return error.InvalidDate;
if (day < 1 or day > daysInMonth(year, month)) return error.InvalidDate;
// Since we just validated the ranges we can now savely cast
return Date{
.year = @intCast(year),
.month = @intCast(month),
.day = @intCast(day),
};
}
// Return a copy of the date
pub fn copy(self: Date) !Date {
return Date.create(self.year, self.month, self.day);
}
// Create a Date from the number of days since 01-Jan-0001
pub fn fromOrdinal(ordinal: u32) Date {
// n is a 1-based index, starting at 1-Jan-1. The pattern of leap years
// repeats exactly every 400 years. The basic strategy is to find the
// closest 400-year boundary at or before n, then work with the offset
// from that boundary to n. Life is much clearer if we subtract 1 from
// n first -- then the values of n at 400-year boundaries are exactly
// those divisible by DI400Y:
//
// D M Y n n-1
// -- --- ---- ---------- ----------------
// 31 Dec -400 -DI400Y -DI400Y -1
// 1 Jan -399 -DI400Y +1 -DI400Y 400-year boundary
// ...
// 30 Dec 000 -1 -2
// 31 Dec 000 0 -1
// 1 Jan 001 1 0 400-year boundary
// 2 Jan 001 2 1
// 3 Jan 001 3 2
// ...
// 31 Dec 400 DI400Y DI400Y -1
// 1 Jan 401 DI400Y +1 DI400Y 400-year boundary
assert(ordinal >= 1 and ordinal <= MAX_ORDINAL);
var n = ordinal - 1;
const DI400Y = comptime daysBeforeYear(401); // Num of days in 400 years
const DI100Y = comptime daysBeforeYear(101); // Num of days in 100 years
const DI4Y = comptime daysBeforeYear(5); // Num of days in 4 years
const n400 = @divFloor(n, DI400Y);
n = @mod(n, DI400Y);
var year = n400 * 400 + 1; // ..., -399, 1, 401, ...
// Now n is the (non-negative) offset, in days, from January 1 of year, to
// the desired date. Now compute how many 100-year cycles precede n.
// Note that it's possible for n100 to equal 4! In that case 4 full
// 100-year cycles precede the desired day, which implies the desired
// day is December 31 at the end of a 400-year cycle.
const n100 = @divFloor(n, DI100Y);
n = @mod(n, DI100Y);
// Now compute how many 4-year cycles precede it.
const n4 = @divFloor(n, DI4Y);
n = @mod(n, DI4Y);
// And now how many single years. Again n1 can be 4, and again meaning
// that the desired day is December 31 at the end of the 4-year cycle.
const n1 = @divFloor(n, 365);
n = @mod(n, 365);
year += n100 * 100 + n4 * 4 + n1;
if (n1 == 4 or n100 == 4) {
assert(n == 0);
return Date.create(year - 1, 12, 31) catch unreachable;
}
// Now the year is correct, and n is the offset from January 1. We find
// the month via an estimate that's either exact or one too large.
const leapyear = (n1 == 3) and (n4 != 24 or n100 == 3);
assert(leapyear == isLeapYear(year));
var month = (n + 50) >> 5;
if (month == 0) month = 12; // Loop around
var preceding = daysBeforeMonth(year, month);
if (preceding > n) { // estimate is too large
month -= 1;
if (month == 0) month = 12; // Loop around
preceding -= daysInMonth(year, month);
}
n -= preceding;
// assert(n > 0 and n < daysInMonth(year, month));
// Now the year and month are correct, and n is the offset from the
// start of that month: we're done!
return Date.create(year, month, n + 1) catch unreachable;
}
// Return proleptic Gregorian ordinal for the year, month and day.
// January 1 of year 1 is day 1. Only the year, month and day values
// contribute to the result.
pub fn toOrdinal(self: Date) u32 {
return ymd2ord(self.year, self.month, self.day);
}
// Returns todays date
pub fn now() Date {
return Date.fromTimestamp(time.milliTimestamp());
}
// Create a date from the number of seconds since 1 Jan 1970
pub fn fromSeconds(seconds: f64) Date {
const r = math.modf(seconds);
const timestamp: i64 = @intFromFloat(r.ipart); // Seconds
const days = @divFloor(timestamp, time.s_per_day) + @as(i64, EPOCH);
assert(days >= 0 and days <= MAX_ORDINAL);
return Date.fromOrdinal(@intCast(days));
}
// Return the number of seconds since 1 Jan 1970
pub fn toSeconds(self: Date) f64 {
const days = @as(i64, @intCast(self.toOrdinal())) - @as(i64, EPOCH);
return @floatFromInt(days * time.s_per_day);
}
// Create a date from a UTC timestamp in milliseconds relative to Jan 1st 1970
pub fn fromTimestamp(timestamp: i64) Date {
const days = @divFloor(timestamp, time.ms_per_day) + @as(i64, EPOCH);
assert(days >= 0 and days <= MAX_ORDINAL);
return Date.fromOrdinal(@intCast(days));
}
// Create a UTC timestamp in milliseconds relative to Jan 1st 1970
pub fn toTimestamp(self: Date) i64 {
const d: i64 = @intCast(daysBeforeYear(self.year));
const days = d - @as(i64, EPOCH) + @as(i64, @intCast(self.dayOfYear()));
return days * time.ms_per_day;
}
// Convert to an ISOCalendar date containing the year, week number, and
// weekday. First week is 1. Monday is 1, Sunday is 7.
pub fn isoCalendar(self: Date) ISOCalendar {
// Ported from python's isocalendar.
var y = self.year;
var first_monday = daysBeforeFirstMonday(y);
const today = ymd2ord(self.year, self.month, self.day);
if (today < first_monday) {
y -= 1;
first_monday = daysBeforeFirstMonday(y);
}
const days_between = today - first_monday;
var week = @divFloor(days_between, 7);
const day = @mod(days_between, 7);
if (week >= 52 and today >= daysBeforeFirstMonday(y + 1)) {
y += 1;
week = 0;
}
assert(week >= 0 and week < 53);
assert(day >= 0 and day < 8);
return ISOCalendar{ .year = y, .week = @intCast(week + 1), .weekday = @intCast(day + 1) };
}
// ------------------------------------------------------------------------
// Comparisons
// ------------------------------------------------------------------------
pub fn eql(self: Date, other: Date) bool {
return self.cmp(other) == .eq;
}
pub fn cmp(self: Date, other: Date) Order {
if (self.year > other.year) return .gt;
if (self.year < other.year) return .lt;
if (self.month > other.month) return .gt;
if (self.month < other.month) return .lt;
if (self.day > other.day) return .gt;
if (self.day < other.day) return .lt;
return .eq;
}
pub fn gt(self: Date, other: Date) bool {
return self.cmp(other) == .gt;
}
pub fn gte(self: Date, other: Date) bool {
const r = self.cmp(other);
return r == .eq or r == .gt;
}
pub fn lt(self: Date, other: Date) bool {
return self.cmp(other) == .lt;
}
pub fn lte(self: Date, other: Date) bool {
const r = self.cmp(other);
return r == .eq or r == .lt;
}
// ------------------------------------------------------------------------
// Parsing
// ------------------------------------------------------------------------
// Parse date in format YYYY-MM-DD. Numbers must be zero padded.
pub fn parseIso(ymd: []const u8) !Date {
const value = std.mem.trim(u8, ymd, " ");
if (value.len != 10) return error.InvalidFormat;
const year = std.fmt.parseInt(u16, value[0..4], 10) catch return error.InvalidFormat;
const month = std.fmt.parseInt(u8, value[5..7], 10) catch return error.InvalidFormat;
const day = std.fmt.parseInt(u8, value[8..10], 10) catch return error.InvalidFormat;
return Date.create(year, month, day);
}
// TODO: Parsing
// ------------------------------------------------------------------------
// Formatting
// ------------------------------------------------------------------------
// Return date in ISO format YYYY-MM-DD
const ISO_DATE_FMT = "{:0>4}-{:0>2}-{:0>2}";
pub fn formatIso(self: Date, allocator: Allocator) ![]u8 {
return std.fmt.allocPrint(allocator, ISO_DATE_FMT, .{ self.year, self.month, self.day });
}
pub fn formatIsoBuf(self: Date, buf: []u8) ![]u8 {
return std.fmt.bufPrint(buf, ISO_DATE_FMT, .{ self.year, self.month, self.day });
}
pub fn writeIso(self: Date, writer: anytype) !void {
try std.fmt.format(writer, ISO_DATE_FMT, .{ self.year, self.month, self.day });
}
// ------------------------------------------------------------------------
// Properties
// ------------------------------------------------------------------------
// Return day of year starting with 1
pub fn dayOfYear(self: Date) u16 {
const d = self.toOrdinal() - daysBeforeYear(self.year);
assert(d >= 1 and d <= 366);
return @intCast(d);
}
// Return day of week starting with Monday = 1 and Sunday = 7
pub fn dayOfWeek(self: Date) Weekday {
const dow: u3 = @intCast(self.toOrdinal() % 7);
return @enumFromInt(if (dow == 0) 7 else dow);
}
// Return the ISO calendar based week of year. With 1 being the first week.
pub fn weekOfYear(self: Date) u8 {
return self.isoCalendar().week;
}
// Return day of week starting with Monday = 0 and Sunday = 6
pub fn weekday(self: Date) u4 {
return @intFromEnum(self.dayOfWeek()) - 1;
}
// Return whether the date is a weekend (Saturday or Sunday)
pub fn isWeekend(self: Date) bool {
return self.weekday() >= 5;
}
// Return the name of the day of the week, eg "Sunday"
pub fn weekdayName(self: Date) []const u8 {
return @tagName(self.dayOfWeek());
}
// Return the name of the day of the month, eg "January"
pub fn monthName(self: Date) []const u8 {
assert(self.month >= 1 and self.month <= 12);
return @tagName(@as(Month, @enumFromInt(self.month)));
}
// ------------------------------------------------------------------------
// Operations
// ------------------------------------------------------------------------
// Return a copy of the date shifted by the given number of days
pub fn shiftDays(self: Date, days: i32) Date {
return self.shift(Delta{ .days = days });
}
// Return a copy of the date shifted by the given number of years
pub fn shiftYears(self: Date, years: i16) Date {
return self.shift(Delta{ .years = years });
}
pub const Delta = struct {
years: i16 = 0,
days: i32 = 0,
};
// Return a copy of the date shifted in time by the delta
pub fn shift(self: Date, delta: Delta) Date {
if (delta.years == 0 and delta.days == 0) {
return self.copy() catch unreachable;
}
// Shift year
var year = self.year;
if (delta.years < 0) {
year -= @intCast(-delta.years);
} else {
year += @intCast(delta.years);
}
var ord = daysBeforeYear(year);
var days = self.dayOfYear();
const from_leap = isLeapYear(self.year);
const to_leap = isLeapYear(year);
if (days == 59 and from_leap and to_leap) {
// No change before leap day
} else if (days < 59) {
// No change when jumping from leap day to leap day
} else if (to_leap and !from_leap) {
// When jumping to a leap year to non-leap year
// we have to add a leap day to the day of year
days += 1;
} else if (from_leap and !to_leap) {
// When jumping from leap year to non-leap year we have to undo
// the leap day added to the day of yearear
days -= 1;
}
ord += days;
// Shift days
if (delta.days < 0) {
ord -= @intCast(-delta.days);
} else {
ord += @intCast(delta.days);
}
return Date.fromOrdinal(ord);
}
};
test "date-now" {
_ = Date.now();
}
test "date-compare" {
const d1 = try Date.create(2019, 7, 3);
const d2 = try Date.create(2019, 7, 3);
const d3 = try Date.create(2019, 6, 3);
const d4 = try Date.create(2020, 7, 3);
try testing.expect(d1.eql(d2));
try testing.expect(d1.gt(d3));
try testing.expect(d3.lt(d2));
try testing.expect(d4.gt(d2));
}
test "date-from-ordinal" {
var date = Date.fromOrdinal(9921);
try testing.expectEqual(date.year, 28);
try testing.expectEqual(date.month, 2);
try testing.expectEqual(date.day, 29);
try testing.expectEqual(date.toOrdinal(), 9921);
date = Date.fromOrdinal(737390);
try testing.expectEqual(date.year, 2019);
try testing.expectEqual(date.month, 11);
try testing.expectEqual(date.day, 27);
try testing.expectEqual(date.toOrdinal(), 737390);
date = Date.fromOrdinal(719163);
try testing.expectEqual(date.year, 1970);
try testing.expectEqual(date.month, 1);
try testing.expectEqual(date.day, 1);
try testing.expectEqual(date.toOrdinal(), 719163);
}
test "date-from-seconds" {
var seconds: f64 = 0;
var date = Date.fromSeconds(seconds);
try testing.expectEqual(date, try Date.create(1970, 1, 1));
try testing.expectEqual(date.toSeconds(), seconds);
seconds = -@as(f64, EPOCH - 1) * time.s_per_day;
date = Date.fromSeconds(seconds);
try testing.expectEqual(date, try Date.create(1, 1, 1));
try testing.expectEqual(date.toSeconds(), seconds);
seconds = @as(f64, MAX_ORDINAL - EPOCH) * time.s_per_day;
date = Date.fromSeconds(seconds);
try testing.expectEqual(date, try Date.create(9999, 12, 31));
try testing.expectEqual(date.toSeconds(), seconds);
//
//
// const t = 63710928000.000;
// date = Date.fromSeconds(t);
// try testing.expectEqual(date.year, 2019);
// try testing.expectEqual(date.month, 12);
// try testing.expectEqual(date.day, 3);
// try testing.expectEqual(date.toSeconds(), t);
//
// Max check
// var max_date = try Date.create(9999, 12, 31);
// const tmax: f64 = @intToFloat(f64, MAX_ORDINAL-1) * time.s_per_day;
// date = Date.fromSeconds(tmax);
// try testing.expect(date.eql(max_date));
// try testing.expectEqual(date.toSeconds(), tmax);
}
test "date-day-of-year" {
var date = try Date.create(1970, 1, 1);
try testing.expect(date.dayOfYear() == 1);
}
test "date-day-of-week" {
var date = try Date.create(2019, 11, 27);
try testing.expectEqual(date.weekday(), 2);
try testing.expectEqual(date.dayOfWeek(), .Wednesday);
try testing.expectEqualSlices(u8, date.monthName(), "November");
try testing.expectEqualSlices(u8, date.weekdayName(), "Wednesday");
try testing.expect(!date.isWeekend());
date = try Date.create(1776, 6, 4);
try testing.expectEqual(date.weekday(), 1);
try testing.expectEqual(date.dayOfWeek(), .Tuesday);
try testing.expectEqualSlices(u8, date.monthName(), "June");
try testing.expectEqualSlices(u8, date.weekdayName(), "Tuesday");
try testing.expect(!date.isWeekend());
date = try Date.create(2019, 12, 1);
try testing.expectEqualSlices(u8, date.monthName(), "December");
try testing.expectEqualSlices(u8, date.weekdayName(), "Sunday");
try testing.expect(date.isWeekend());
}
test "date-shift-days" {
var date = try Date.create(2019, 11, 27);
var d = date.shiftDays(-2);
try testing.expectEqual(d.day, 25);
try testing.expectEqualSlices(u8, d.weekdayName(), "Monday");
// Ahead one week
d = date.shiftDays(7);
try testing.expectEqualSlices(u8, d.weekdayName(), date.weekdayName());
try testing.expectEqual(d.month, 12);
try testing.expectEqualSlices(u8, d.monthName(), "December");
try testing.expectEqual(d.day, 4);
d = date.shiftDays(0);
try testing.expect(date.eql(d));
}
test "date-shift-years" {
// Shift including a leap year
var date = try Date.create(2019, 11, 27);
var d = date.shiftYears(-4);
try testing.expect(d.eql(try Date.create(2015, 11, 27)));
d = date.shiftYears(15);
try testing.expect(d.eql(try Date.create(2034, 11, 27)));
// Shifting from leap day
var leap_day = try Date.create(2020, 2, 29);
d = leap_day.shiftYears(1);
try testing.expect(d.eql(try Date.create(2021, 2, 28)));
// Before leap day
date = try Date.create(2020, 2, 2);
d = date.shiftYears(1);
try testing.expect(d.eql(try Date.create(2021, 2, 2)));
// After leap day
date = try Date.create(2020, 3, 1);
d = date.shiftYears(1);
try testing.expect(d.eql(try Date.create(2021, 3, 1)));
// From leap day to leap day
d = leap_day.shiftYears(4);
try testing.expect(d.eql(try Date.create(2024, 2, 29)));
}
test "date-create" {
try testing.expectError(error.InvalidDate, Date.create(2019, 2, 29));
var date = Date.fromTimestamp(1574908586928);
try testing.expect(date.eql(try Date.create(2019, 11, 28)));
}
test "date-copy" {
const d1 = try Date.create(2020, 1, 1);
const d2 = try d1.copy();
try testing.expect(d1.eql(d2));
}
test "date-parse-iso" {
try testing.expectEqual(try Date.parseIso("2018-12-15"), try Date.create(2018, 12, 15));
try testing.expectEqual(try Date.parseIso("2021-01-07"), try Date.create(2021, 1, 7));
try testing.expectError(error.InvalidDate, Date.parseIso("2021-13-01"));
try testing.expectError(error.InvalidFormat, Date.parseIso("20-01-01"));
try testing.expectError(error.InvalidFormat, Date.parseIso("2000-1-1"));
}
test "date-format-iso" {
const date_strs = [_][]const u8{
"0959-02-05",
"2018-12-15",
};
for (date_strs) |date_str| {
var d = try Date.parseIso(date_str);
const parsed_date_str = try d.formatIso(std.testing.allocator);
defer std.testing.allocator.free(parsed_date_str);
try testing.expectEqualStrings(date_str, parsed_date_str);
}
}
test "date-format-iso-buf" {
const date_strs = [_][]const u8{
"0959-02-05",
"2018-12-15",
};
for (date_strs) |date_str| {
var d = try Date.parseIso(date_str);
var buf: [32]u8 = undefined;
try testing.expectEqualStrings(date_str, try d.formatIsoBuf(buf[0..]));
}
}
test "date-write-iso" {
const date_strs = [_][]const u8{
"0959-02-05",
"2018-12-15",
};
for (date_strs) |date_str| {
var buf: [32]u8 = undefined;
var stream = std.io.fixedBufferStream(buf[0..]);
var d = try Date.parseIso(date_str);
try d.writeIso(stream.writer());
try testing.expectEqualStrings(date_str, stream.getWritten());
}
}
test "date-isocalendar" {
const today = try Date.create(2021, 8, 12);
try testing.expectEqual(today.isoCalendar(), ISOCalendar{ .year = 2021, .week = 32, .weekday = 4 });
// Some random dates and outputs generated with python
const dates = [15][]const u8{
"2018-12-15",
"2019-01-19",
"2019-10-14",
"2020-09-26",
// Border cases
"2020-12-27",
"2020-12-30",
"2020-12-31",
"2021-01-01",
"2021-01-03",
"2021-01-04",
"2021-01-10",
"2021-09-14",
"2022-09-12",
"2023-04-10",
"2024-01-16",
};
const expect = [15]ISOCalendar{
ISOCalendar{ .year = 2018, .week = 50, .weekday = 6 },
ISOCalendar{ .year = 2019, .week = 3, .weekday = 6 },
ISOCalendar{ .year = 2019, .week = 42, .weekday = 1 },
ISOCalendar{ .year = 2020, .week = 39, .weekday = 6 },
ISOCalendar{ .year = 2020, .week = 52, .weekday = 7 },
ISOCalendar{ .year = 2020, .week = 53, .weekday = 3 },
ISOCalendar{ .year = 2020, .week = 53, .weekday = 4 },
ISOCalendar{ .year = 2020, .week = 53, .weekday = 5 },
ISOCalendar{ .year = 2020, .week = 53, .weekday = 7 },
ISOCalendar{ .year = 2021, .week = 1, .weekday = 1 },
ISOCalendar{ .year = 2021, .week = 1, .weekday = 7 },
ISOCalendar{ .year = 2021, .week = 37, .weekday = 2 },
ISOCalendar{ .year = 2022, .week = 37, .weekday = 1 },
ISOCalendar{ .year = 2023, .week = 15, .weekday = 1 },
ISOCalendar{ .year = 2024, .week = 3, .weekday = 2 },
};
for (dates, 0..) |d, i| {
const date = try Date.parseIso(d);
const cal = date.isoCalendar();
try testing.expectEqual(cal, expect[i]);
try testing.expectEqual(date.weekOfYear(), expect[i].week);
}
}
pub const Timezone = struct {
offset: i16, // In minutes
name: []const u8,
// Auto register timezones
pub fn create(name: []const u8, offset: i16) Timezone {
const self = Timezone{ .offset = offset, .name = name };
return self;
}
pub fn offsetSeconds(self: Timezone) i32 {
return @as(i32, self.offset) * time.s_per_min;
}
};
pub const Time = struct {
hour: u8 = 0, // 0 to 23
minute: u8 = 0, // 0 to 59
second: u8 = 0, // 0 to 59
nanosecond: u32 = 0, // 0 to 999999999 TODO: Should this be u20?
// ------------------------------------------------------------------------
// Constructors
// ------------------------------------------------------------------------
pub fn now() Time {
return Time.fromTimestamp(time.milliTimestamp());
}
// Create a Time struct and validate that all fields are in range
pub fn create(hour: u32, minute: u32, second: u32, nanosecond: u32) !Time {
if (hour > 23 or minute > 59 or second > 59 or nanosecond > 999999999) {
return error.InvalidTime;
}
return Time{
.hour = @intCast(hour),
.minute = @intCast(minute),
.second = @intCast(second),
.nanosecond = nanosecond,
};
}
// Create a copy of the Time
pub fn copy(self: Time) !Time {
return Time.create(self.hour, self.minute, self.second, self.nanosecond);
}
// Create Time from a UTC Timestamp in milliseconds
pub fn fromTimestamp(timestamp: i64) Time {
const remainder = @mod(timestamp, time.ms_per_day);
var t: u64 = @abs(remainder);
// t is now only the time part of the day
const h: u32 = @intCast(@divFloor(t, time.ms_per_hour));
t -= h * time.ms_per_hour;
const m: u32 = @intCast(@divFloor(t, time.ms_per_min));
t -= m * time.ms_per_min;
const s: u32 = @intCast(@divFloor(t, time.ms_per_s));
t -= s * time.ms_per_s;
const ns: u32 = @intCast(t * time.ns_per_ms);
return Time.create(h, m, s, ns) catch unreachable;
}
// From seconds since the start of the day
pub fn fromSeconds(seconds: f64) Time {
assert(seconds >= 0);
// Convert to s and us
const r = math.modf(seconds);
var s: u32 = @intFromFloat(@mod(r.ipart, time.s_per_day)); // s
const h = @divFloor(s, time.s_per_hour);
s -= h * time.s_per_hour;
const m = @divFloor(s, time.s_per_min);
s -= m * time.s_per_min;
// Rounding seems to only be accurate to within 100ns
// for normal timestamps
var frac = math.round(r.fpart * time.ns_per_s / 100) * 100;
if (frac >= time.ns_per_s) {
s += 1;
frac -= time.ns_per_s;
} else if (frac < 0) {
s -= 1;
frac += time.ns_per_s;
}
const ns: u32 = @intFromFloat(frac);
return Time.create(h, m, s, ns) catch unreachable; // If this fails it's a bug
}
// Convert to a time in seconds relative to the UTC timezones
// including the nanosecond component
pub fn toSeconds(self: Time) f64 {
const s: f64 = @floatFromInt(self.totalSeconds());
const ns = @as(f64, @floatFromInt(self.nanosecond)) / time.ns_per_s;
return s + ns;
}
// Convert to a timestamp in milliseconds from UTC
pub fn toTimestamp(self: Time) i64 {
const h = @as(i64, @intCast(self.hour)) * time.ms_per_hour;
const m = @as(i64, @intCast(self.minute)) * time.ms_per_min;
const s = @as(i64, @intCast(self.second)) * time.ms_per_s;
const ms: i64 = @intCast(self.nanosecond / time.ns_per_ms);
return h + m + s + ms;
}
// Total seconds from the start of day
pub fn totalSeconds(self: Time) i32 {
const h = @as(i32, @intCast(self.hour)) * time.s_per_hour;
const m = @as(i32, @intCast(self.minute)) * time.s_per_min;
const s: i32 = @intCast(self.second);
return h + m + s;
}
// -----------------------------------------------------------------------
// Comparisons
// -----------------------------------------------------------------------
pub fn eql(self: Time, other: Time) bool {
return self.cmp(other) == .eq;
}
pub fn cmp(self: Time, other: Time) Order {
const t1 = self.totalSeconds();
const t2 = other.totalSeconds();
if (t1 > t2) return .gt;
if (t1 < t2) return .lt;
if (self.nanosecond > other.nanosecond) return .gt;
if (self.nanosecond < other.nanosecond) return .lt;
return .eq;
}
pub fn gt(self: Time, other: Time) bool {
return self.cmp(other) == .gt;
}
pub fn gte(self: Time, other: Time) bool {
const r = self.cmp(other);
return r == .eq or r == .gt;
}
pub fn lt(self: Time, other: Time) bool {
return self.cmp(other) == .lt;
}
pub fn lte(self: Time, other: Time) bool {
const r = self.cmp(other);
return r == .eq or r == .lt;
}
// -----------------------------------------------------------------------
// Methods
// -----------------------------------------------------------------------
pub fn amOrPm(self: Time) []const u8 {
return if (self.hour > 12) return "PM" else "AM";
}
// -----------------------------------------------------------------------
// Formatting Methods
// -----------------------------------------------------------------------
const ISO_HM_FORMAT = "T{d:0>2}:{d:0>2}";
const ISO_HMS_FORMAT = "T{d:0>2}:{d:0>2}:{d:0>2}";
pub fn writeIsoHM(self: Time, writer: anytype) !void {
try std.fmt.format(writer, ISO_HM_FORMAT, .{ self.hour, self.minute });
}
pub fn writeIsoHMS(self: Time, writer: anytype) !void {
try std.fmt.format(writer, ISO_HMS_FORMAT, .{ self.hour, self.minute, self.second });
}
};
test "time-create" {
const t = Time.fromTimestamp(1574908586928);
try testing.expect(t.hour == 2);
try testing.expect(t.minute == 36);
try testing.expect(t.second == 26);
try testing.expect(t.nanosecond == 928000000);
try testing.expectError(error.InvalidTime, Time.create(25, 1, 1, 0));
try testing.expectError(error.InvalidTime, Time.create(1, 60, 1, 0));
try testing.expectError(error.InvalidTime, Time.create(12, 30, 281, 0));
try testing.expectError(error.InvalidTime, Time.create(12, 30, 28, 1000000000));
}
test "time-now" {
_ = Time.now();
}
test "time-from-seconds" {
var seconds: f64 = 15.12;
var t = Time.fromSeconds(seconds);
try testing.expect(t.hour == 0);
try testing.expect(t.minute == 0);
try testing.expect(t.second == 15);
try testing.expect(t.nanosecond == 120000000);
try testing.expect(t.toSeconds() == seconds);
seconds = 315.12; // + 5 min
t = Time.fromSeconds(seconds);
try testing.expect(t.hour == 0);
try testing.expect(t.minute == 5);
try testing.expect(t.second == 15);
try testing.expect(t.nanosecond == 120000000);
try testing.expect(t.toSeconds() == seconds);
seconds = 36000 + 315.12; // + 10 hr
t = Time.fromSeconds(seconds);
try testing.expect(t.hour == 10);