Debian Codegraph @

time/date/observe.ha

raw
1 // SPDX-License-Identifier: MPL-2.0
2 // (c) Hare authors <https://harelang.org>
3
4 // Observes a [[date]]'s [[zonephase]].
5 export fn zone(d: *date) zonephase = {
6 match (d.zonephase) {
7 case let z: *zonephase =>
8 return *z;
9 case null =>
10 let lookup = match (d.loc.lookupzone) {
11 case let l: *zonelookuper =>
12 yield l;
13 case null =>
14 yield &lookupzone: *zonelookuper;
15 };
16 const z = lookup(d.loc, d);
17 d.zonephase = z;
18 return *z;
19 };
20 };
21
22 // Observes a [[date]]'s observed daydate (day number since epoch).
23 //
24 // For dates with a [[locality]] based on the UTC, TAI, GPS, and similar
25 // [[time::chrono::timescale]]s, their epoch date should be interpreted as the
26 // Unix epoch (1970 Janurary 1st). Other timescales may suggest their own
27 // interpretations applicable to other chronologies.
28 export fn daydate(d: *date) i64 = {
29 match (d.daydate) {
30 case let dd: i64 =>
31 return dd;
32 case void =>
33 const (dd, dt) = calc_datetime(
34 d.loc, to_instant(*d), zone(d).zoff,
35 );
36 d.daytime = dt;
37 d.daydate = dd;
38 return dd;
39 };
40 };
41
42 // Observes a [[date]]'s observed time-of-day (amount of daytime progressed in
43 // a day) as nanoseconds.
44 export fn daytime(d: *date) i64 = {
45 match (d.daytime) {
46 case let dt: i64 =>
47 return dt;
48 case void =>
49 const (dd, dt) = calc_datetime(
50 d.loc, to_instant(*d), zone(d).zoff,
51 );
52 d.daytime = dt;
53 d.daydate = dd;
54 return dt;
55 };
56 };
57
58 // These functions are renamed to avoid namespace conflicts, like in the
59 // parameters of the [[new]] function.
60
61 // Observes a [[date]]'s era.
62 export fn era(d: *date) int = _era(d);
63
64 // Observes a [[date]]'s year.
65 export fn year(d: *date) int = _year(d);
66
67 // Observes a [[date]]'s month of the year. Range January=1 to December=12.
68 export fn month(d: *date) int = _month(d);
69
70 // Observes a [[date]]'s day of the month. Range 1 to 31.
71 export fn day(d: *date) int = _day(d);
72
73 // Observes a [[date]]'s day of the week. Range Monday=0 to Sunday=6.
74 export fn weekday(d: *date) int = _weekday(d);
75
76 // Observes a [[date]]'s ordinal day of the year. Range 1 to 366.
77 export fn yearday(d: *date) int = _yearday(d);
78
79 // Observes a [[date]]'s ISO week-numbering year.
80 export fn isoweekyear(d: *date) int = _isoweekyear(d);
81
82 // Observes a [[date]]'s Gregorian week starting Monday. Range 0 to 53.
83 // All days in a year before the year's first Monday belong to week 0.
84 export fn week(d: *date) int = _week(d);
85
86 // Observes a [[date]]'s Gregorian week starting Sunday. Range 0 to 53.
87 // All days in a year before the year's first Sunday belong to week 0.
88 export fn sundayweek(d: *date) int = _sundayweek(d);
89
90 // Observes a [[date]]'s ISO week-numbering week. Range 0 to 53.
91 export fn isoweek(d: *date) int = _isoweek(d);
92
93 // Observes a [[date]]'s hour of the day.
94 export fn hour(d: *date) int = _hour(d);
95
96 // Observes a [[date]]'s minute of the hour.
97 export fn minute(d: *date) int = _minute(d);
98
99 // Observes a [[date]]'s second of the minute.
100 export fn second(d: *date) int = _second(d);
101
102 // Observes a [[date]]'s nanosecond of the second.
103 export fn nanosecond(d: *date) int = _nanosecond(d);
104
105 fn _era(d: *date) int = {
106 match (d.era) {
107 case void =>
108 d.era = calc_era(
109 _year(d),
110 );
111 return d.era: int;
112 case let a: int =>
113 return a;
114 };
115 };
116
117 fn _year(d: *date) int = {
118 match (d.year) {
119 case void =>
120 const ymd = calc_ymd(
121 daydate(d),
122 );
123 d.year = ymd.0;
124 d.month = ymd.1;
125 d.day = ymd.2;
126 return d.year: int;
127 case let y: int =>
128 return y;
129 };
130 };
131
132 fn _month(d: *date) int = {
133 match (d.month) {
134 case void =>
135 const ymd = calc_ymd(
136 daydate(d),
137 );
138 d.year = ymd.0;
139 d.month = ymd.1;
140 d.day = ymd.2;
141 return d.month: int;
142 case let y: int =>
143 return y;
144 };
145 };
146
147 fn _day(d: *date) int = {
148 match (d.day) {
149 case void =>
150 const ymd = calc_ymd(
151 daydate(d),
152 );
153 d.year = ymd.0;
154 d.month = ymd.1;
155 d.day = ymd.2;
156 return d.day: int;
157 case let y: int =>
158 return y;
159 };
160 };
161
162 fn _weekday(d: *date) int = {
163 match (d.weekday) {
164 case void =>
165 d.weekday = calc_weekday(
166 daydate(d),
167 );
168 return d.weekday: int;
169 case let y: int =>
170 return y;
171 };
172 };
173
174 fn _yearday(d: *date) int = {
175 match (d.yearday) {
176 case void =>
177 d.yearday = calc_yearday(
178 _year(d),
179 _month(d),
180 _day(d),
181 );
182 return d.yearday: int;
183 case let yd: int =>
184 return yd;
185 };
186 };
187
188 fn _isoweekyear(d: *date) int = {
189 match (d.isoweekyear) {
190 case void =>
191 d.isoweekyear = calc_isoweekyear(
192 _year(d),
193 _month(d),
194 _day(d),
195 _weekday(d),
196 );
197 return d.isoweekyear: int;
198 case let iwy: int =>
199 return iwy;
200 };
201 };
202
203 fn _week(d: *date) int = {
204 match (d.week) {
205 case void =>
206 d.week = calc_week(
207 _yearday(d),
208 _weekday(d),
209 );
210 return d.week: int;
211 case let w: int =>
212 return w;
213 };
214 };
215
216 fn _sundayweek(d: *date) int = {
217 match (d.sundayweek) {
218 case void =>
219 d.sundayweek = calc_sundayweek(
220 _yearday(d),
221 _weekday(d),
222 );
223 return d.sundayweek: int;
224 case let w: int =>
225 return w;
226 };
227 };
228
229 fn _isoweek(d: *date) int = {
230 match (d.isoweek) {
231 case void =>
232 d.isoweek = calc_isoweek(
233 _year(d),
234 _week(d),
235 );
236 return d.isoweek: int;
237 case let iw: int =>
238 return iw;
239 };
240 };
241
242 fn _hour(d: *date) int = {
243 match (d.hour) {
244 case void =>
245 const hmsn = calc_hmsn(
246 daytime(d),
247 );
248 d.hour = hmsn.0;
249 d.minute = hmsn.1;
250 d.second = hmsn.2;
251 d.nanosecond = hmsn.3;
252 return d.hour: int;
253 case let h: int =>
254 return h;
255 };
256 };
257
258 fn _minute(d: *date) int = {
259 match (d.minute) {
260 case void =>
261 const hmsn = calc_hmsn(
262 daytime(d),
263 );
264 d.hour = hmsn.0;
265 d.minute = hmsn.1;
266 d.second = hmsn.2;
267 d.nanosecond = hmsn.3;
268 return d.minute: int;
269 case let m: int =>
270 return m;
271 };
272 };
273
274 fn _second(d: *date) int = {
275 match (d.second) {
276 case void =>
277 const hmsn = calc_hmsn(
278 daytime(d),
279 );
280 d.hour = hmsn.0;
281 d.minute = hmsn.1;
282 d.second = hmsn.2;
283 d.nanosecond = hmsn.3;
284 return d.second: int;
285 case let s: int =>
286 return s;
287 };
288 };
289
290 fn _nanosecond(d: *date) int = {
291 match (d.nanosecond) {
292 case void =>
293 const hmsn = calc_hmsn(
294 daytime(d),
295 );
296 d.hour = hmsn.0;
297 d.minute = hmsn.1;
298 d.second = hmsn.2;
299 d.nanosecond = hmsn.3;
300 return d.nanosecond: int;
301 case let n: int =>
302 return n;
303 };
304 };