Debian Codegraph @

js/src/builtin/temporal/TemporalUnit.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_TemporalUnit_h
8 #define builtin_temporal_TemporalUnit_h
9
10 #include "mozilla/Assertions.h"
11
12 #include <stdint.h>
13
14 #include "builtin/temporal/Crash.h"
15
16 namespace js::temporal {
17 enum class TemporalUnit {
18 Auto,
19 Year,
20 Month,
21 Week,
22 Day,
23 Hour,
24 Minute,
25 Second,
26 Millisecond,
27 Microsecond,
28 Nanosecond
29 };
30
31 constexpr int64_t ToNanoseconds(TemporalUnit unit) {
32 switch (unit) {
33 case TemporalUnit::Day:
34 return 86'400'000'000'000;
35 case TemporalUnit::Hour:
36 return 3'600'000'000'000;
37 case TemporalUnit::Minute:
38 return 60'000'000'000;
39 case TemporalUnit::Second:
40 return 1'000'000'000;
41 case TemporalUnit::Millisecond:
42 return 1'000'000;
43 case TemporalUnit::Microsecond:
44 return 1'000;
45 case TemporalUnit::Nanosecond:
46 return 1;
47
48 case TemporalUnit::Auto:
49 case TemporalUnit::Year:
50 case TemporalUnit::Month:
51 case TemporalUnit::Week:
52 break;
53 }
54 JS_CONSTEXPR_CRASH("Unexpected temporal unit");
55 }
56
57 constexpr int64_t ToMicroseconds(TemporalUnit unit) {
58 switch (unit) {
59 case TemporalUnit::Day:
60 return 86'400'000'000;
61 case TemporalUnit::Hour:
62 return 3'600'000'000;
63 case TemporalUnit::Minute:
64 return 60'000'000;
65 case TemporalUnit::Second:
66 return 1'000'000;
67 case TemporalUnit::Millisecond:
68 return 1'000;
69 case TemporalUnit::Microsecond:
70 return 1;
71
72 case TemporalUnit::Auto:
73 case TemporalUnit::Year:
74 case TemporalUnit::Month:
75 case TemporalUnit::Week:
76 case TemporalUnit::Nanosecond:
77 break;
78 }
79 JS_CONSTEXPR_CRASH("Unexpected temporal unit");
80 }
81
82 constexpr int64_t ToMilliseconds(TemporalUnit unit) {
83 switch (unit) {
84 case TemporalUnit::Day:
85 return 86'400'000;
86 case TemporalUnit::Hour:
87 return 3'600'000;
88 case TemporalUnit::Minute:
89 return 60'000;
90 case TemporalUnit::Second:
91 return 1'000;
92 case TemporalUnit::Millisecond:
93 return 1;
94
95 case TemporalUnit::Auto:
96 case TemporalUnit::Year:
97 case TemporalUnit::Month:
98 case TemporalUnit::Week:
99 case TemporalUnit::Microsecond:
100 case TemporalUnit::Nanosecond:
101 break;
102 }
103 JS_CONSTEXPR_CRASH("Unexpected temporal unit");
104 }
105
106 constexpr int64_t ToSeconds(TemporalUnit unit) {
107 switch (unit) {
108 case TemporalUnit::Day:
109 return 86'400;
110 case TemporalUnit::Hour:
111 return 3'600;
112 case TemporalUnit::Minute:
113 return 60;
114 case TemporalUnit::Second:
115 return 1;
116
117 case TemporalUnit::Auto:
118 case TemporalUnit::Year:
119 case TemporalUnit::Month:
120 case TemporalUnit::Week:
121 case TemporalUnit::Millisecond:
122 case TemporalUnit::Microsecond:
123 case TemporalUnit::Nanosecond:
124 break;
125 }
126 JS_CONSTEXPR_CRASH("Unexpected temporal unit");
127 }
128
129 constexpr int64_t UnitsPerDay(TemporalUnit unit) {
130 switch (unit) {
131 case TemporalUnit::Day:
132 return 1;
133 case TemporalUnit::Hour:
134 return 24;
135 case TemporalUnit::Minute:
136 return 1440;
137 case TemporalUnit::Second:
138 return 86'400;
139 case TemporalUnit::Millisecond:
140 return 86'400'000;
141 case TemporalUnit::Microsecond:
142 return 86'400'000'000;
143 case TemporalUnit::Nanosecond:
144 return 86'400'000'000'000;
145
146 case TemporalUnit::Auto:
147 case TemporalUnit::Year:
148 case TemporalUnit::Month:
149 case TemporalUnit::Week:
150 break;
151 }
152 JS_CONSTEXPR_CRASH("Unexpected temporal unit");
153 }
154
155 constexpr const char* TemporalUnitToString(TemporalUnit unit) {
156 switch (unit) {
157 case TemporalUnit::Auto:
158 return "auto";
159 case TemporalUnit::Year:
160 return "year";
161 case TemporalUnit::Month:
162 return "month";
163 case TemporalUnit::Week:
164 return "week";
165 case TemporalUnit::Day:
166 return "day";
167 case TemporalUnit::Hour:
168 return "hour";
169 case TemporalUnit::Minute:
170 return "minute";
171 case TemporalUnit::Second:
172 return "second";
173 case TemporalUnit::Millisecond:
174 return "millisecond";
175 case TemporalUnit::Microsecond:
176 return "microsecond";
177 case TemporalUnit::Nanosecond:
178 return "nanosecond";
179 }
180 JS_CONSTEXPR_CRASH("Unexpected temporal unit");
181 }
182
183 } /* namespace js::temporal */
184
185 #endif /* builtin_temporal_TemporalUnit_h */