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/. */
7 #ifndef builtin_temporal_Instant_h
8 #define builtin_temporal_Instant_h
10 #include "mozilla/Assertions.h"
14 #include "builtin/temporal/TemporalTypes.h"
15 #include "builtin/temporal/Wrapped.h"
16 #include "js/TypeDecls.h"
18 #include "vm/NativeObject.h"
24 namespace js::temporal
{
26 class InstantObject
: public NativeObject
{
28 static const JSClass class_
;
29 static const JSClass
& protoClass_
;
31 static constexpr uint32_t SECONDS_SLOT
= 0;
32 static constexpr uint32_t NANOSECONDS_SLOT
= 1;
33 static constexpr uint32_t SLOT_COUNT
= 2;
35 int64_t seconds() const {
36 double seconds
= getFixedSlot(SECONDS_SLOT
).toNumber();
37 MOZ_ASSERT(-8'640'000'000'000 <= seconds
&& seconds
<= 8'640'000'000'000);
38 return int64_t(seconds
);
41 int32_t nanoseconds() const {
42 int32_t nanoseconds
= getFixedSlot(NANOSECONDS_SLOT
).toInt32();
43 MOZ_ASSERT(0 <= nanoseconds
&& nanoseconds
<= 999'999'999);
48 static const ClassSpec classSpec_
;
52 * Extract the instant fields from the Instant object.
54 inline Instant
ToInstant(const InstantObject
* instant
) {
55 return {instant
->seconds(), instant
->nanoseconds()};
59 enum class TemporalUnit
;
60 enum class TemporalRoundingMode
;
63 * IsValidEpochNanoseconds ( epochNanoseconds )
65 bool IsValidEpochNanoseconds(const JS::BigInt
* epochNanoseconds
);
68 * IsValidEpochNanoseconds ( epochNanoseconds )
70 bool IsValidEpochInstant(const Instant
& instant
);
74 * Return true if the input is within the valid instant span limits.
76 bool IsValidInstantSpan(const InstantSpan
& span
);
80 * Convert a BigInt to an instant. The input must be a valid epoch nanoseconds
83 Instant
ToInstant(const JS::BigInt
* epochNanoseconds
);
86 * Convert an instant to a BigInt. The input must be a valid epoch instant.
88 JS::BigInt
* ToEpochNanoseconds(JSContext
* cx
, const Instant
& instant
);
91 * ToTemporalInstant ( item )
93 Wrapped
<InstantObject
*> ToTemporalInstant(JSContext
* cx
,
94 JS::Handle
<JS::Value
> item
);
97 * ToTemporalInstant ( item )
99 bool ToTemporalInstant(JSContext
* cx
, JS::Handle
<JS::Value
> item
,
103 * CreateTemporalInstant ( epochNanoseconds [ , newTarget ] )
105 InstantObject
* CreateTemporalInstant(JSContext
* cx
, const Instant
& instant
);
108 * GetUTCEpochNanoseconds ( year, month, day, hour, minute, second, millisecond,
109 * microsecond, nanosecond [ , offsetNanoseconds ] )
111 Instant
GetUTCEpochNanoseconds(const PlainDateTime
& dateTime
);
114 * GetUTCEpochNanoseconds ( year, month, day, hour, minute, second, millisecond,
115 * microsecond, nanosecond [ , offsetNanoseconds ] )
117 Instant
GetUTCEpochNanoseconds(const PlainDateTime
& dateTime
,
118 const InstantSpan
& offsetNanoseconds
);
121 * RoundTemporalInstant ( ns, increment, unit, roundingMode )
123 Instant
RoundTemporalInstant(const Instant
& ns
, Increment increment
,
125 TemporalRoundingMode roundingMode
);
128 * AddNormalizedTimeDurationToEpochNanoseconds ( d, epochNs )
130 Instant
AddNormalizedTimeDurationToEpochNanoseconds(
131 const NormalizedTimeDuration
& d
, const Instant
& epochNs
);
134 * AddInstant ( epochNanoseconds, norm )
136 bool AddInstant(JSContext
* cx
, const Instant
& instant
,
137 const NormalizedTimeDuration
& duration
, Instant
* result
);
140 * DifferenceInstant ( ns1, ns2, roundingIncrement, smallestUnit, roundingMode )
142 NormalizedTimeDuration
DifferenceInstant(const Instant
& ns1
, const Instant
& ns2
,
143 Increment roundingIncrement
,
144 TemporalUnit smallestUnit
,
145 TemporalRoundingMode roundingMode
);
147 } /* namespace js::temporal */
149 #endif /* builtin_temporal_Instant_h */