Debian Codegraph @

js/src/builtin/temporal/Instant.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_Instant_h
8 #define builtin_temporal_Instant_h
9
10 #include "mozilla/Assertions.h"
11
12 #include <stdint.h>
13
14 #include "builtin/temporal/TemporalTypes.h"
15 #include "js/TypeDecls.h"
16 #include "js/Value.h"
17 #include "vm/NativeObject.h"
18
19 namespace js {
20 struct ClassSpec;
21 }
22
23 namespace js::temporal {
24
25 class InstantObject : public NativeObject {
26 public:
27 static const JSClass class_;
28 static const JSClass& protoClass_;
29
30 static constexpr uint32_t SECONDS_SLOT = 0;
31 static constexpr uint32_t NANOSECONDS_SLOT = 1;
32 static constexpr uint32_t SLOT_COUNT = 2;
33
34 /**
35 * Extract the epoch nanoseconds fields from this ZonedDateTime object.
36 */
37 EpochNanoseconds epochNanoseconds() const {
38 double seconds = getFixedSlot(SECONDS_SLOT).toNumber();
39 MOZ_ASSERT(-8'640'000'000'000 <= seconds && seconds <= 8'640'000'000'000);
40
41 int32_t nanoseconds = getFixedSlot(NANOSECONDS_SLOT).toInt32();
42 MOZ_ASSERT(0 <= nanoseconds && nanoseconds <= 999'999'999);
43
44 return {{int64_t(seconds), nanoseconds}};
45 }
46
47 private:
48 static const ClassSpec classSpec_;
49 };
50
51 class Increment;
52 enum class TemporalUnit;
53 enum class TemporalRoundingMode;
54
55 /**
56 * IsValidEpochNanoseconds ( epochNanoseconds )
57 */
58 bool IsValidEpochNanoseconds(const JS::BigInt* epochNanoseconds);
59
60 /**
61 * IsValidEpochNanoseconds ( epochNanoseconds )
62 */
63 bool IsValidEpochNanoseconds(const EpochNanoseconds& epochNanoseconds);
64
65 #ifdef DEBUG
66 /**
67 * Return true if the input is within the valid epoch duration limits.
68 */
69 bool IsValidEpochDuration(const EpochDuration& duration);
70 #endif
71
72 /**
73 * Convert a BigInt to epoch nanoseconds. The input must be a valid epoch
74 * nanoseconds value.
75 */
76 EpochNanoseconds ToEpochNanoseconds(const JS::BigInt* epochNanoseconds);
77
78 /**
79 * Convert epoch nanoseconds to a BigInt. The input must be valid epoch
80 * nanoseconds.
81 */
82 JS::BigInt* ToBigInt(JSContext* cx, const EpochNanoseconds& epochNanoseconds);
83
84 /**
85 * CreateTemporalInstant ( epochNanoseconds [ , newTarget ] )
86 */
87 InstantObject* CreateTemporalInstant(JSContext* cx,
88 const EpochNanoseconds& epochNanoseconds);
89
90 /**
91 * GetUTCEpochNanoseconds ( isoDateTime )
92 */
93 EpochNanoseconds GetUTCEpochNanoseconds(const ISODateTime& isoDateTime);
94
95 /**
96 * RoundTemporalInstant ( ns, increment, unit, roundingMode )
97 */
98 EpochNanoseconds RoundTemporalInstant(const EpochNanoseconds& ns,
99 Increment increment, TemporalUnit unit,
100 TemporalRoundingMode roundingMode);
101
102 /**
103 * AddInstant ( epochNanoseconds, norm )
104 */
105 bool AddInstant(JSContext* cx, const EpochNanoseconds& epochNanoseconds,
106 const TimeDuration& duration, EpochNanoseconds* result);
107
108 /**
109 * DifferenceInstant ( ns1, ns2, roundingIncrement, smallestUnit, roundingMode )
110 */
111 TimeDuration DifferenceInstant(const EpochNanoseconds& ns1,
112 const EpochNanoseconds& ns2,
113 Increment roundingIncrement,
114 TemporalUnit smallestUnit,
115 TemporalRoundingMode roundingMode);
116
117 } /* namespace js::temporal */
118
119 #endif /* builtin_temporal_Instant_h */