Debian Codegraph @

js/src/builtin/temporal/MonthCode.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef builtin_temporal_MonthCode_h
8 #define builtin_temporal_MonthCode_h
9
10 #include "mozilla/Assertions.h"
11 #include "mozilla/EnumSet.h"
12
13 #include <initializer_list>
14 #include <stddef.h>
15 #include <stdint.h>
16 #include <string_view>
17 #include <utility>
18
19 #include "jstypes.h"
20
21 #include "builtin/temporal/Calendar.h"
22 #include "builtin/temporal/Crash.h"
23
24 namespace js::temporal {
25
26 class MonthCode final {
27 public:
28 enum class Code {
29 Invalid = 0,
30
31 // Months 01 - M12.
32 M01 = 1,
33 M02,
34 M03,
35 M04,
36 M05,
37 M06,
38 M07,
39 M08,
40 M09,
41 M10,
42 M11,
43 M12,
44
45 // Epagomenal month M13.
46 M13,
47
48 // Leap months M01 - M12.
49 M01L,
50 M02L,
51 M03L,
52 M04L,
53 M05L,
54 M06L,
55 M07L,
56 M08L,
57 M09L,
58 M10L,
59 M11L,
60 M12L,
61 };
62
63 private:
64 static constexpr int32_t toLeapMonth =
65 static_cast<int32_t>(Code::M01L) - static_cast<int32_t>(Code::M01);
66
67 Code code_ = Code::Invalid;
68
69 public:
70 constexpr MonthCode() = default;
71
72 constexpr explicit MonthCode(Code code) : code_(code) {}
73
74 constexpr explicit MonthCode(int32_t month, bool isLeapMonth = false) {
75 MOZ_ASSERT(1 <= month && month <= 13);
76 MOZ_ASSERT_IF(isLeapMonth, 1 <= month && month <= 12);
77
78 code_ = static_cast<Code>(month + (isLeapMonth ? toLeapMonth : 0));
79 }
80
81 constexpr auto code() const { return code_; }
82
83 constexpr int32_t ordinal() const {
84 int32_t ordinal = static_cast<int32_t>(code_);
85 if (isLeapMonth()) {
86 ordinal -= toLeapMonth;
87 }
88 return ordinal;
89 }
90
91 constexpr bool isLeapMonth() const { return code_ >= Code::M01L; }
92
93 constexpr bool operator==(const MonthCode& other) const {
94 return other.code_ == code_;
95 }
96
97 constexpr bool operator!=(const MonthCode& other) const {
98 return !(*this == other);
99 }
100
101 constexpr bool operator<(const MonthCode& other) const {
102 if (ordinal() != other.ordinal()) {
103 return ordinal() < other.ordinal();
104 }
105 return code_ < other.code_;
106 }
107
108 constexpr bool operator>(const MonthCode& other) const {
109 return other < *this;
110 }
111
112 constexpr bool operator<=(const MonthCode& other) const {
113 return !(other < *this);
114 }
115
116 constexpr bool operator>=(const MonthCode& other) const {
117 return !(*this < other);
118 }
119
120 constexpr explicit operator std::string_view() const {
121 constexpr const char* name =
122 "M01L"
123 "M02L"
124 "M03L"
125 "M04L"
126 "M05L"
127 "M06L"
128 "M07L"
129 "M08L"
130 "M09L"
131 "M10L"
132 "M11L"
133 "M12L"
134 "M13";
135 size_t index = (ordinal() - 1) * 4;
136 size_t length = 3 + isLeapMonth();
137 return {name + index, length};
138 }
139
140 /**
141 * Returns the maximum non-leap month. This is the epagomenal month "M13".
142 */
143 constexpr static auto maxNonLeapMonth() { return MonthCode{Code::M13}; }
144
145 /**
146 * Returns the maximum leap month.
147 */
148 constexpr static auto maxLeapMonth() { return MonthCode{Code::M12L}; }
149 };
150
151 class MonthCodes final {
152 mozilla::EnumSet<MonthCode::Code> monthCodes_{
153 MonthCode::Code::M01, MonthCode::Code::M02, MonthCode::Code::M03,
154 MonthCode::Code::M04, MonthCode::Code::M05, MonthCode::Code::M06,
155 MonthCode::Code::M07, MonthCode::Code::M08, MonthCode::Code::M09,
156 MonthCode::Code::M10, MonthCode::Code::M11, MonthCode::Code::M12,
157 };
158
159 public:
160 constexpr MOZ_IMPLICIT MonthCodes(std::initializer_list<MonthCode> list) {
161 for (auto value : list) {
162 monthCodes_ += value.code();
163 }
164 }
165
166 bool contains(MonthCode monthCode) const {
167 return monthCodes_.contains(monthCode.code());
168 }
169
170 bool contains(const MonthCodes& monthCodes) const {
171 return monthCodes_.contains(monthCodes.monthCodes_);
172 }
173 };
174
175 // static variables in constexpr functions requires C++23 support, so we can't
176 // declare the month codes directly in CalendarMonthCodes.
177 //
178 // https://tc39.es/proposal-intl-era-monthcode/#table-additional-month-codes
179 //
180 // https://docs.rs/icu/latest/icu/calendar/buddhist/struct.Buddhist.html#month-codes
181 // https://docs.rs/icu/latest/icu/calendar/indian/struct.Indian.html#month-codes
182 // https://docs.rs/icu/latest/icu/calendar/islamic/struct.IslamicCivil.html#month-codes
183 // https://docs.rs/icu/latest/icu/calendar/islamic/struct.IslamicObservational.html#month-codes
184 // https://docs.rs/icu/latest/icu/calendar/islamic/struct.IslamicTabular.html#month-codes
185 // https://docs.rs/icu/latest/icu/calendar/islamic/struct.IslamicUmmAlQura.html#month-codes
186 // https://docs.rs/icu/latest/icu/calendar/japanese/struct.Japanese.html#month-codes
187 // https://docs.rs/icu/latest/icu/calendar/persian/struct.Persian.html#month-codes
188 // https://docs.rs/icu/latest/icu/calendar/roc/struct.Roc.html#month-codes
189 //
190 // https://docs.rs/icu/latest/icu/calendar/chinese/struct.Chinese.html#month-codes
191 // https://docs.rs/icu/latest/icu/calendar/dangi/struct.Dangi.html#month-codes
192 //
193 // https://docs.rs/icu/latest/icu/calendar/coptic/struct.Coptic.html#month-codes
194 //
195 // https://docs.rs/icu/latest/icu/calendar/ethiopian/struct.Ethiopian.html#month-codes
196 // https://docs.rs/icu/latest/icu/calendar/hebrew/struct.Hebrew.html#month-codes
197 namespace monthcodes {
198 inline constexpr MonthCodes ISO8601 = {};
199
200 inline constexpr MonthCodes ChineseOrDangi = {
201 // Leap months.
202 MonthCode{1, /* isLeapMonth = */ true},
203 MonthCode{2, /* isLeapMonth = */ true},
204 MonthCode{3, /* isLeapMonth = */ true},
205 MonthCode{4, /* isLeapMonth = */ true},
206 MonthCode{5, /* isLeapMonth = */ true},
207 MonthCode{6, /* isLeapMonth = */ true},
208 MonthCode{7, /* isLeapMonth = */ true},
209 MonthCode{8, /* isLeapMonth = */ true},
210 MonthCode{9, /* isLeapMonth = */ true},
211 MonthCode{10, /* isLeapMonth = */ true},
212 MonthCode{11, /* isLeapMonth = */ true},
213 MonthCode{12, /* isLeapMonth = */ true},
214 };
215
216 inline constexpr MonthCodes CopticOrEthiopian = {
217 // Short epagomenal month.
218 MonthCode{13},
219 };
220
221 inline constexpr MonthCodes Hebrew = {
222 // Leap month Adar I.
223 MonthCode{5, /* isLeapMonth = */ true},
224 };
225 } // namespace monthcodes
226
227 constexpr auto& CalendarMonthCodes(CalendarId id) {
228 switch (id) {
229 case CalendarId::ISO8601:
230 case CalendarId::Buddhist:
231 case CalendarId::Gregorian:
232 case CalendarId::Indian:
233 case CalendarId::Islamic:
234 case CalendarId::IslamicCivil:
235 case CalendarId::IslamicRGSA:
236 case CalendarId::IslamicTabular:
237 case CalendarId::IslamicUmmAlQura:
238 case CalendarId::Persian:
239 case CalendarId::Japanese:
240 case CalendarId::ROC:
241 return monthcodes::ISO8601;
242
243 case CalendarId::Chinese:
244 case CalendarId::Dangi:
245 return monthcodes::ChineseOrDangi;
246
247 case CalendarId::Coptic:
248 case CalendarId::Ethiopian:
249 case CalendarId::EthiopianAmeteAlem:
250 return monthcodes::CopticOrEthiopian;
251
252 case CalendarId::Hebrew:
253 return monthcodes::Hebrew;
254 }
255 JS_CONSTEXPR_CRASH("invalid calendar id");
256 }
257
258 constexpr bool CalendarHasLeapMonths(CalendarId id) {
259 switch (id) {
260 case CalendarId::ISO8601:
261 case CalendarId::Buddhist:
262 case CalendarId::Coptic:
263 case CalendarId::Ethiopian:
264 case CalendarId::EthiopianAmeteAlem:
265 case CalendarId::Gregorian:
266 case CalendarId::Indian:
267 case CalendarId::Islamic:
268 case CalendarId::IslamicCivil:
269 case CalendarId::IslamicRGSA:
270 case CalendarId::IslamicTabular:
271 case CalendarId::IslamicUmmAlQura:
272 case CalendarId::Japanese:
273 case CalendarId::Persian:
274 case CalendarId::ROC:
275 return false;
276
277 case CalendarId::Chinese:
278 case CalendarId::Dangi:
279 case CalendarId::Hebrew:
280 return true;
281 }
282 JS_CONSTEXPR_CRASH("invalid calendar id");
283 }
284
285 constexpr bool CalendarHasEpagomenalMonths(CalendarId id) {
286 switch (id) {
287 case CalendarId::ISO8601:
288 case CalendarId::Buddhist:
289 case CalendarId::Chinese:
290 case CalendarId::Dangi:
291 case CalendarId::Gregorian:
292 case CalendarId::Hebrew:
293 case CalendarId::Indian:
294 case CalendarId::Islamic:
295 case CalendarId::IslamicCivil:
296 case CalendarId::IslamicRGSA:
297 case CalendarId::IslamicTabular:
298 case CalendarId::IslamicUmmAlQura:
299 case CalendarId::Japanese:
300 case CalendarId::Persian:
301 case CalendarId::ROC:
302 return false;
303
304 case CalendarId::Coptic:
305 case CalendarId::Ethiopian:
306 case CalendarId::EthiopianAmeteAlem:
307 return true;
308 }
309 JS_CONSTEXPR_CRASH("invalid calendar id");
310 }
311
312 constexpr int32_t CalendarMonthsPerYear(CalendarId id) {
313 if (CalendarHasLeapMonths(id) || CalendarHasEpagomenalMonths(id)) {
314 return 13;
315 }
316 return 12;
317 }
318
319 constexpr std::pair<int32_t, int32_t> CalendarDaysInMonth(CalendarId id) {
320 switch (id) {
321 // ISO8601 calendar.
322 // M02: 28-29 days
323 // M04, M06, M09, M11: 30 days
324 // M01, M03, M05, M07, M08, M10, M12: 31 days
325 case CalendarId::ISO8601:
326 case CalendarId::Buddhist:
327 case CalendarId::Gregorian:
328 case CalendarId::Japanese:
329 case CalendarId::ROC:
330 return {28, 31};
331
332 // Chinese/Dangi calendars have 29-30 days per month.
333 //
334 // Hebrew:
335 // M01, M05, M07, M09, M11: 30 days.
336 // M02, M03: 29-30 days.
337 // M04, M06, M08, M10, M12: 29 days.
338 // M05L: 30 days
339 //
340 // Islamic calendars have 29-30 days.
341 //
342 // IslamicCivil, IslamicTabular:
343 // M01, M03, M05, M07, M09, M11: 30 days
344 // M02, M04, M06, M08, M10: 29 days
345 // M12: 29-30 days.
346 case CalendarId::Chinese:
347 case CalendarId::Dangi:
348 case CalendarId::Hebrew:
349 case CalendarId::Islamic:
350 case CalendarId::IslamicCivil:
351 case CalendarId::IslamicRGSA:
352 case CalendarId::IslamicTabular:
353 case CalendarId::IslamicUmmAlQura:
354 return {29, 30};
355
356 // Coptic, Ethiopian, EthiopianAmeteAlem:
357 // M01..M12: 30 days.
358 // M13: 5-6 days.
359 case CalendarId::Coptic:
360 case CalendarId::Ethiopian:
361 case CalendarId::EthiopianAmeteAlem:
362 return {5, 30};
363
364 // Indian:
365 // M1: 30-31 days.
366 // M02..M06: 31 days
367 // M07..M12: 30 days
368 case CalendarId::Indian:
369 return {30, 31};
370
371 // Persian:
372 // M01..M06: 31 days
373 // M07..M11: 30 days
374 // M12: 29-30 days
375 case CalendarId::Persian:
376 return {29, 31};
377 }
378 JS_CONSTEXPR_CRASH("invalid calendar id");
379 }
380
381 // ISO8601 calendar.
382 // M02: 28-29 days
383 // M04, M06, M09, M11: 30 days
384 // M01, M03, M05, M07, M08, M10, M12: 31 days
385 constexpr std::pair<int32_t, int32_t> ISODaysInMonth(MonthCode monthCode) {
386 int32_t ordinal = monthCode.ordinal();
387 if (ordinal == 2) {
388 return {28, 29};
389 }
390 if (ordinal == 4 || ordinal == 6 || ordinal == 9 || ordinal == 11) {
391 return {30, 30};
392 }
393 return {31, 31};
394 }
395
396 constexpr std::pair<int32_t, int32_t> CalendarDaysInMonth(CalendarId id,
397 MonthCode monthCode) {
398 switch (id) {
399 case CalendarId::ISO8601:
400 case CalendarId::Buddhist:
401 case CalendarId::Gregorian:
402 case CalendarId::Japanese:
403 case CalendarId::ROC:
404 return ISODaysInMonth(monthCode);
405
406 // Chinese/Dangi calendars have 29-30 days per month.
407 case CalendarId::Chinese:
408 case CalendarId::Dangi:
409 return {29, 30};
410
411 // Coptic, Ethiopian, EthiopianAmeteAlem:
412 // M01..M12: 30 days.
413 // M13: 5-6 days.
414 case CalendarId::Coptic:
415 case CalendarId::Ethiopian:
416 case CalendarId::EthiopianAmeteAlem: {
417 if (monthCode.ordinal() <= 12) {
418 return {30, 30};
419 }
420 return {5, 6};
421 }
422
423 // Hebrew:
424 // M01, M05, M07, M09, M11: 30 days.
425 // M02, M03: 29-30 days.
426 // M04, M06, M08, M10, M12: 29 days.
427 // M05L: 30 days
428 case CalendarId::Hebrew: {
429 int32_t ordinal = monthCode.ordinal();
430 if (ordinal == 2 || ordinal == 3) {
431 return {29, 30};
432 }
433 if ((ordinal & 1) == 1 || monthCode.isLeapMonth()) {
434 return {30, 30};
435 }
436 return {29, 29};
437 }
438
439 // Indian:
440 // M1: 30-31 days.
441 // M02..M06: 31 days
442 // M07..M12: 30 days
443 case CalendarId::Indian: {
444 int32_t ordinal = monthCode.ordinal();
445 if (ordinal == 1) {
446 return {30, 31};
447 }
448 if (ordinal <= 6) {
449 return {31, 31};
450 }
451 return {30, 30};
452 }
453
454 // Islamic calendars have 29-30 days per month.
455 case CalendarId::Islamic:
456 case CalendarId::IslamicRGSA:
457 case CalendarId::IslamicUmmAlQura:
458 return {29, 30};
459
460 // IslamicCivil, IslamicTabular:
461 // M01, M03, M05, M07, M09, M11: 30 days
462 // M02, M04, M06, M08, M10: 29 days
463 // M12: 29-30 days.
464 case CalendarId::IslamicCivil:
465 case CalendarId::IslamicTabular: {
466 int32_t ordinal = monthCode.ordinal();
467 if ((ordinal & 1) == 1) {
468 return {30, 30};
469 }
470 if (ordinal < 12) {
471 return {29, 29};
472 }
473 return {29, 30};
474 }
475
476 // Persian:
477 // M01..M06: 31 days
478 // M07..M11: 30 days
479 // M12: 29-30 days
480 case CalendarId::Persian: {
481 int32_t ordinal = monthCode.ordinal();
482 if (ordinal <= 6) {
483 return {31, 31};
484 }
485 if (ordinal <= 11) {
486 return {30, 30};
487 }
488 return {29, 30};
489 }
490 }
491 JS_CONSTEXPR_CRASH("invalid calendar id");
492 }
493
494 } // namespace js::temporal
495
496 #endif /* builtin_temporal_MonthCode_h */