Debian Codegraph @

js/src/builtin/temporal/TimeZone.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_TimeZone_h
8 #define builtin_temporal_TimeZone_h
9
10 #include "mozilla/Assertions.h"
11 #include "mozilla/Attributes.h"
12 #include "mozilla/Maybe.h"
13
14 #include <array>
15 #include <stddef.h>
16 #include <stdint.h>
17
18 #include "builtin/temporal/TemporalTypes.h"
19 #include "js/RootingAPI.h"
20 #include "js/TypeDecls.h"
21 #include "js/Value.h"
22 #include "vm/JSObject.h"
23 #include "vm/NativeObject.h"
24 #include "vm/StringType.h"
25
26 class JS_PUBLIC_API JSTracer;
27 struct JSClassOps;
28
29 namespace mozilla::intl {
30 class TimeZone;
31 }
32
33 namespace js::temporal {
34
35 class TimeZoneObject : public NativeObject {
36 public:
37 static const JSClass class_;
38
39 static constexpr uint32_t IDENTIFIER_SLOT = 0;
40 static constexpr uint32_t PRIMARY_IDENTIFIER_SLOT = 1;
41 static constexpr uint32_t OFFSET_MINUTES_SLOT = 2;
42 static constexpr uint32_t INTL_TIMEZONE_SLOT = 3;
43 static constexpr uint32_t SLOT_COUNT = 4;
44
45 // Estimated memory use for intl::TimeZone (see IcuMemoryUsage).
46 static constexpr size_t EstimatedMemoryUse = 6840;
47
48 bool isOffset() const { return getFixedSlot(OFFSET_MINUTES_SLOT).isInt32(); }
49
50 JSLinearString* identifier() const {
51 return &getFixedSlot(IDENTIFIER_SLOT).toString()->asLinear();
52 }
53
54 JSLinearString* primaryIdentifier() const {
55 MOZ_ASSERT(!isOffset());
56 return &getFixedSlot(PRIMARY_IDENTIFIER_SLOT).toString()->asLinear();
57 }
58
59 int32_t offsetMinutes() const {
60 MOZ_ASSERT(isOffset());
61 return getFixedSlot(OFFSET_MINUTES_SLOT).toInt32();
62 }
63
64 mozilla::intl::TimeZone* getTimeZone() const {
65 const auto& slot = getFixedSlot(INTL_TIMEZONE_SLOT);
66 if (slot.isUndefined()) {
67 return nullptr;
68 }
69 return static_cast<mozilla::intl::TimeZone*>(slot.toPrivate());
70 }
71
72 void setTimeZone(mozilla::intl::TimeZone* timeZone) {
73 setFixedSlot(INTL_TIMEZONE_SLOT, JS::PrivateValue(timeZone));
74 }
75
76 private:
77 static const JSClassOps classOps_;
78
79 static void finalize(JS::GCContext* gcx, JSObject* obj);
80 };
81
82 } /* namespace js::temporal */
83
84 namespace js::temporal {
85
86 /**
87 * Temporal time zones are either available named time zones or offset time
88 * zones.
89 *
90 * The identifier of an available named time zones is an available named
91 * time zone identifier, which is either a primary time zone identifier or a
92 * non-primary time zone identifier.
93 *
94 * The identifier of an offset time zone is an offset time zone identifier.
95 *
96 * Temporal methods always return the normalized format of a time zone
97 * identifier. Available named time zone identifier are always in normalized
98 * format.
99 *
100 * Examples of valid available time zone identifiers in normalized format:
101 * - "UTC" (primary identifier)
102 * - "Etc/UTC" (non-primary identifier)
103 * - "America/New_York" (primary identifier)
104 * - "+00:00"
105 *
106 * Examples of valid available time zone identifiers in non-normalized format:
107 * - "+00"
108 * - "-00:00"
109 *
110 * Examples of invalid available time zone identifiers:
111 * - "utc" (wrong case)
112 * - "+00:00:00" (sub-minute precision)
113 * - "+00:00:01" (sub-minute precision)
114 *
115 * The following two implementation approaches are possible:
116 *
117 * 1. Represent time zones as JSStrings. Additionally keep a mapping from
118 * JSString to `mozilla::intl::TimeZone` to avoid repeatedly creating new
119 * `mozilla::intl::TimeZone` for time zone operations. Offset string time
120 * zones have to be special cased, because they don't use
121 * `mozilla::intl::TimeZone`. Either detect offset strings by checking the
122 * time zone identifier or store offset strings as the offset in minutes
123 * value to avoid reparsing the offset string again and again.
124 * 2. Represent time zones as objects which hold `mozilla::intl::TimeZone` in
125 * an internal slot.
126 *
127 * Option 2 is a bit easier to implement, so we use this approach for now.
128 */
129 class MOZ_STACK_CLASS TimeZoneValue final {
130 TimeZoneObject* object_ = nullptr;
131
132 public:
133 /**
134 * Default initialize this TimeZoneValue.
135 */
136 TimeZoneValue() = default;
137
138 /**
139 * Initialize this TimeZoneValue with a time zone object.
140 */
141 explicit TimeZoneValue(TimeZoneObject* timeZone) : object_(timeZone) {
142 MOZ_ASSERT(object_);
143 }
144
145 /**
146 * Initialize this TimeZoneValue from a slot Value.
147 */
148 explicit TimeZoneValue(const JS::Value& value)
149 : object_(&value.toObject().as<TimeZoneObject>()) {}
150
151 /**
152 * Return true if this TimeZoneValue is not null.
153 */
154 explicit operator bool() const { return !!object_; }
155
156 /**
157 * Return true if this TimeZoneValue is an offset time zone.
158 */
159 bool isOffset() const {
160 MOZ_ASSERT(object_);
161 return object_->isOffset();
162 }
163
164 /**
165 * Return the offset of an offset time zone.
166 */
167 auto offsetMinutes() const {
168 MOZ_ASSERT(object_);
169 return object_->offsetMinutes();
170 }
171
172 /**
173 * Return the time zone identifier.
174 */
175 auto* identifier() const {
176 MOZ_ASSERT(object_);
177 return object_->identifier();
178 }
179
180 /**
181 * Return the primary time zone identifier of a named time zone.
182 */
183 auto* primaryIdentifier() const {
184 MOZ_ASSERT(object_);
185 return object_->primaryIdentifier();
186 }
187
188 /**
189 * Return the time zone implementation.
190 */
191 auto* getTimeZone() const {
192 MOZ_ASSERT(object_);
193 return object_->getTimeZone();
194 }
195
196 /**
197 * Return the underlying TimeZoneObject.
198 */
199 auto* toTimeZoneObject() const {
200 MOZ_ASSERT(object_);
201 return object_;
202 }
203
204 /**
205 * Return the slot Value representation of this TimeZoneValue.
206 */
207 JS::Value toSlotValue() const {
208 MOZ_ASSERT(object_);
209 return JS::ObjectValue(*object_);
210 }
211
212 // Helper methods for (Mutable)WrappedPtrOperations.
213 auto address() { return &object_; }
214 auto address() const { return &object_; }
215
216 // Trace implementation.
217 void trace(JSTracer* trc);
218 };
219
220 class PossibleEpochNanoseconds final {
221 // GetPossibleEpochNanoseconds can return up-to two elements.
222 static constexpr size_t MaxLength = 2;
223
224 std::array<EpochNanoseconds, MaxLength> array_ = {};
225 size_t length_ = 0;
226
227 void append(const EpochNanoseconds& epochNs) { array_[length_++] = epochNs; }
228
229 public:
230 PossibleEpochNanoseconds() = default;
231
232 explicit PossibleEpochNanoseconds(const EpochNanoseconds& epochNs) {
233 append(epochNs);
234 }
235
236 explicit PossibleEpochNanoseconds(const EpochNanoseconds& earlier,
237 const EpochNanoseconds& later) {
238 MOZ_ASSERT(earlier <= later);
239 append(earlier);
240 append(later);
241 }
242
243 size_t length() const { return length_; }
244 bool empty() const { return length_ == 0; }
245
246 const auto& operator[](size_t i) const { return array_[i]; }
247
248 auto begin() const { return array_.begin(); }
249 auto end() const { return array_.begin() + length_; }
250
251 const auto& front() const {
252 MOZ_ASSERT(length_ > 0);
253 return array_[0];
254 }
255 const auto& back() const {
256 MOZ_ASSERT(length_ > 0);
257 return array_[length_ - 1];
258 }
259 };
260
261 struct ParsedTimeZone;
262 enum class TemporalDisambiguation;
263
264 /**
265 * SystemTimeZoneIdentifier ( )
266 */
267 JSLinearString* SystemTimeZoneIdentifier(JSContext* cx);
268
269 /**
270 * SystemTimeZoneIdentifier ( )
271 */
272 bool SystemTimeZone(JSContext* cx, JS::MutableHandle<TimeZoneValue> result);
273
274 /**
275 * ToTemporalTimeZoneIdentifier ( temporalTimeZoneLike )
276 */
277 bool ToTemporalTimeZone(JSContext* cx,
278 JS::Handle<JS::Value> temporalTimeZoneLike,
279 JS::MutableHandle<TimeZoneValue> result);
280
281 /**
282 * ToTemporalTimeZoneIdentifier ( temporalTimeZoneLike )
283 */
284 bool ToTemporalTimeZone(JSContext* cx, JS::Handle<ParsedTimeZone> string,
285 JS::MutableHandle<TimeZoneValue> result);
286
287 /**
288 * TimeZoneEquals ( one, two )
289 */
290 bool TimeZoneEquals(const TimeZoneValue& one, const TimeZoneValue& two);
291
292 /**
293 * GetISODateTimeFor ( timeZone, epochNs )
294 */
295 ISODateTime GetISODateTimeFor(const EpochNanoseconds& epochNs,
296 int64_t offsetNanoseconds);
297
298 /**
299 * GetISODateTimeFor ( timeZone, epochNs )
300 */
301 bool GetISODateTimeFor(JSContext* cx, JS::Handle<TimeZoneValue> timeZone,
302 const EpochNanoseconds& epochNs, ISODateTime* result);
303
304 /**
305 * GetEpochNanosecondsFor ( timeZone, isoDateTime, disambiguation )
306 */
307 bool GetEpochNanosecondsFor(JSContext* cx, JS::Handle<TimeZoneValue> timeZone,
308 const ISODateTime& isoDateTime,
309 TemporalDisambiguation disambiguation,
310 EpochNanoseconds* result);
311
312 /**
313 * GetOffsetNanosecondsFor ( timeZone, epochNs )
314 */
315 bool GetOffsetNanosecondsFor(JSContext* cx, JS::Handle<TimeZoneValue> timeZone,
316 const EpochNanoseconds& epochNs,
317 int64_t* offsetNanoseconds);
318
319 /**
320 * GetPossibleEpochNanoseconds ( timeZone, isoDateTime )
321 */
322 bool GetPossibleEpochNanoseconds(JSContext* cx,
323 JS::Handle<TimeZoneValue> timeZone,
324 const ISODateTime& isoDateTime,
325 PossibleEpochNanoseconds* result);
326
327 /**
328 * DisambiguatePossibleEpochNanoseconds ( possibleEpochNs, timeZone,
329 * isoDateTime, disambiguation )
330 */
331 bool DisambiguatePossibleEpochNanoseconds(
332 JSContext* cx, const PossibleEpochNanoseconds& possibleEpochNs,
333 JS::Handle<TimeZoneValue> timeZone, const ISODateTime& isoDateTime,
334 TemporalDisambiguation disambiguation, EpochNanoseconds* result);
335
336 /**
337 * GetNamedTimeZoneNextTransition ( timeZoneIdentifier, epochNanoseconds )
338 */
339 bool GetNamedTimeZoneNextTransition(JSContext* cx,
340 JS::Handle<TimeZoneValue> timeZone,
341 const EpochNanoseconds& epochNanoseconds,
342 mozilla::Maybe<EpochNanoseconds>* result);
343
344 /**
345 * GetNamedTimeZonePreviousTransition ( timeZoneIdentifier, epochNanoseconds )
346 */
347 bool GetNamedTimeZonePreviousTransition(
348 JSContext* cx, JS::Handle<TimeZoneValue> timeZone,
349 const EpochNanoseconds& epochNanoseconds,
350 mozilla::Maybe<EpochNanoseconds>* result);
351
352 /**
353 * GetStartOfDay ( timeZone, isoDate )
354 */
355 bool GetStartOfDay(JSContext* cx, JS::Handle<TimeZoneValue> timeZone,
356 const ISODate& isoDate, EpochNanoseconds* result);
357
358 // Helper for MutableWrappedPtrOperations.
359 bool WrapTimeZoneValueObject(JSContext* cx,
360 JS::MutableHandle<TimeZoneObject*> timeZone);
361
362 } /* namespace js::temporal */
363
364 namespace js {
365
366 template <typename Wrapper>
367 class WrappedPtrOperations<temporal::TimeZoneValue, Wrapper> {
368 const auto& container() const {
369 return static_cast<const Wrapper*>(this)->get();
370 }
371
372 public:
373 explicit operator bool() const { return !!container(); }
374
375 bool isOffset() const { return container().isOffset(); }
376
377 auto offsetMinutes() const { return container().offsetMinutes(); }
378
379 auto* identifier() const { return container().identifier(); }
380
381 auto* primaryIdentifier() const { return container().primaryIdentifier(); }
382
383 auto* getTimeZone() const { return container().getTimeZone(); }
384
385 JS::Value toSlotValue() const { return container().toSlotValue(); }
386 };
387
388 template <typename Wrapper>
389 class MutableWrappedPtrOperations<temporal::TimeZoneValue, Wrapper>
390 : public WrappedPtrOperations<temporal::TimeZoneValue, Wrapper> {
391 auto& container() { return static_cast<Wrapper*>(this)->get(); }
392
393 public:
394 /**
395 * Wrap the time zone value into the current compartment.
396 */
397 bool wrap(JSContext* cx) {
398 MOZ_ASSERT(container());
399 auto mh = JS::MutableHandle<temporal::TimeZoneObject*>::fromMarkedLocation(
400 container().address());
401 return temporal::WrapTimeZoneValueObject(cx, mh);
402 }
403 };
404
405 } /* namespace js */
406
407 #endif /* builtin_temporal_TimeZone_h */