Debian Codegraph @

time/date/truncate.ha

raw
1 // SPDX-License-Identifier: MPL-2.0
2 // (c) Hare authors <https://harelang.org>
3
4 use time::chrono;
5
6 // Truncates the given [[date]] at the provided nominal [[step]].
7 // The [[zflag]] parameter affects the final result. Example:
8 //
9 // // On this day in Sao Paulo, a +1 hour jump occurs at 00:00.
10 // // The time range 00:00..00:59 is never observed.
11 // //
12 // // 2000-10-08 12:00:00.000000000 -0200 -02 America/Sao_Paulo
13 // let a = date::new(chrono::tz("America/Sao_Paulo")!, -2 * time::HOUR,
14 // 2000, 10, 8, 12)!
15 // //
16 // // 2000-10-08 01:00:00.000000000 -0200 -02 America/Sao_Paulo
17 // let b = date::truncate(a, date::zflag::GAP_END, date::step::DAY)!;
18 //
19 export fn truncate(d: date, zf: zflag, u: step) (date | invalid | zfunresolved) = {
20 switch (u) {
21 case step::ERA =>
22 return new(d.loc, zf,
23 1, 1, 1,
24 0, 0, 0, 0,
25 );
26 case step::YEAR =>
27 return new(d.loc, zf,
28 _year(&d), 1, 1,
29 0, 0, 0, 0,
30 );
31 case step::MONTH =>
32 return new(d.loc, zf,
33 _year(&d), _month(&d), 1,
34 0, 0, 0, 0,
35 );
36 case step::WEEK =>
37 const dd = daydate(&d) - _weekday(&d);
38 const ymd = calc_ymd(dd);
39 return new(d.loc, zf,
40 ymd.0, ymd.1, ymd.2,
41 0, 0, 0, 0,
42 );
43 case step::DAY =>
44 return new(d.loc, zf,
45 _year(&d), _month(&d), _day(&d),
46 0, 0, 0, 0,
47 );
48 case step::HOUR =>
49 return new(d.loc, zf,
50 _year(&d), _month(&d), _day(&d),
51 _hour(&d), 0, 0, 0,
52 );
53 case step::MINUTE =>
54 return new(d.loc, zf,
55 _year(&d), _month(&d), _day(&d),
56 _hour(&d), _minute(&d), 0, 0,
57 );
58 case step::SECOND =>
59 return new(d.loc, zf,
60 _year(&d), _month(&d), _day(&d),
61 _hour(&d), _minute(&d), _second(&d), 0,
62 );
63 case step::NANOSECOND =>
64 return d;
65 };
66 };
67
68 @test fn truncate() void = {
69 const d = new(UTC, 0, 1994, 8, 27, 11, 20, 1, 2)!;
70
71 assert(0 == chrono::compare(
72 &truncate(d, zflag::CONTIG, step::ERA)!,
73 &new(UTC, 0, 1, 1, 1, 0, 0, 0, 0)!)!,
74 "invalid truncate() result 01");
75
76 assert(0 == chrono::compare(
77 &truncate(d, zflag::CONTIG, step::YEAR)!,
78 &new(UTC, 0, 1994, 1, 1, 0, 0, 0, 0)!)!,
79 "invalid truncate() result 02");
80
81 assert(0 == chrono::compare(
82 &truncate(d, zflag::CONTIG, step::MONTH)!,
83 &new(UTC, 0, 1994, 8, 1, 0, 0, 0, 0)!)!,
84 "invalid truncate() result 03");
85
86 assert(0 == chrono::compare(
87 &truncate(d, zflag::CONTIG, step::WEEK)!,
88 &new(UTC, 0, 1994, 8, 22, 0, 0, 0, 0)!)!,
89 "invalid truncate() result 04");
90
91 assert(0 == chrono::compare(
92 &truncate(d, zflag::CONTIG, step::DAY)!,
93 &new(UTC, 0, 1994, 8, 27, 0, 0, 0, 0)!)!,
94 "invalid truncate() result 05");
95
96 assert(0 == chrono::compare(
97 &truncate(d, zflag::CONTIG, step::HOUR)!,
98 &new(UTC, 0, 1994, 8, 27, 11, 0, 0, 0)!)!,
99 "invalid truncate() result 06");
100
101 assert(0 == chrono::compare(
102 &truncate(d, zflag::CONTIG, step::MINUTE)!,
103 &new(UTC, 0, 1994, 8, 27, 11, 20, 0, 0)!)!,
104 "invalid truncate() result 07");
105
106 assert(0 == chrono::compare(
107 &truncate(d, zflag::CONTIG, step::SECOND)!,
108 &new(UTC, 0, 1994, 8, 27, 11, 20, 1, 0)!)!,
109 "invalid truncate() result 08");
110
111 assert(0 == chrono::compare(
112 &truncate(d, zflag::CONTIG, step::NANOSECOND)!,
113 &d)!,
114 "invalid truncate() result 09");
115 };