summaryrefslogtreecommitdiffstats
path: root/js/src/builtin/temporal/Instant.h
blob: edce677d102e9603d0676f9d26f8daaf8e96b221 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef builtin_temporal_Instant_h
#define builtin_temporal_Instant_h

#include "mozilla/Assertions.h"

#include <stdint.h>

#include "builtin/temporal/TemporalTypes.h"
#include "builtin/temporal/Wrapped.h"
#include "js/TypeDecls.h"
#include "js/Value.h"
#include "vm/NativeObject.h"

namespace js {
struct ClassSpec;
}

namespace js::temporal {

class InstantObject : public NativeObject {
 public:
  static const JSClass class_;
  static const JSClass& protoClass_;

  static constexpr uint32_t SECONDS_SLOT = 0;
  static constexpr uint32_t NANOSECONDS_SLOT = 1;
  static constexpr uint32_t SLOT_COUNT = 2;

  int64_t seconds() const {
    double seconds = getFixedSlot(SECONDS_SLOT).toNumber();
    MOZ_ASSERT(-8'640'000'000'000 <= seconds && seconds <= 8'640'000'000'000);
    return int64_t(seconds);
  }

  int32_t nanoseconds() const {
    int32_t nanoseconds = getFixedSlot(NANOSECONDS_SLOT).toInt32();
    MOZ_ASSERT(0 <= nanoseconds && nanoseconds <= 999'999'999);
    return nanoseconds;
  }

 private:
  static const ClassSpec classSpec_;
};

/**
 * Extract the instant fields from the Instant object.
 */
inline Instant ToInstant(const InstantObject* instant) {
  return {instant->seconds(), instant->nanoseconds()};
}

class Increment;
enum class TemporalUnit;
enum class TemporalRoundingMode;

/**
 * IsValidEpochNanoseconds ( epochNanoseconds )
 */
bool IsValidEpochNanoseconds(const JS::BigInt* epochNanoseconds);

/**
 * IsValidEpochNanoseconds ( epochNanoseconds )
 */
bool IsValidEpochInstant(const Instant& instant);

/**
 * Return true if the input is within the valid instant span limits.
 */
bool IsValidInstantSpan(const InstantSpan& span);

/**
 * Return true if the input is within the valid instant span limits.
 */
bool IsValidInstantSpan(const JS::BigInt* nanoseconds);

/**
 * Convert a BigInt to an instant. The input must be a valid epoch nanoseconds
 * value.
 */
Instant ToInstant(const JS::BigInt* epochNanoseconds);

/**
 * Convert a BigInt to an instant span. The input must be a valid epoch
 * nanoseconds span value.
 */
InstantSpan ToInstantSpan(const JS::BigInt* nanoseconds);

/**
 * Convert an instant to a BigInt. The input must be a valid epoch instant.
 */
JS::BigInt* ToEpochNanoseconds(JSContext* cx, const Instant& instant);

/**
 * Convert an instant span to a BigInt. The input must be a valid instant span.
 */
JS::BigInt* ToEpochNanoseconds(JSContext* cx, const InstantSpan& instant);

/**
 * ToTemporalInstant ( item )
 */
Wrapped<InstantObject*> ToTemporalInstant(JSContext* cx,
                                          JS::Handle<JS::Value> item);

/**
 * ToTemporalInstant ( item )
 */
bool ToTemporalInstant(JSContext* cx, JS::Handle<JS::Value> item,
                       Instant* result);

/**
 * CreateTemporalInstant ( epochNanoseconds [ , newTarget ] )
 */
InstantObject* CreateTemporalInstant(JSContext* cx, const Instant& instant);

/**
 * GetUTCEpochNanoseconds ( year, month, day, hour, minute, second, millisecond,
 * microsecond, nanosecond [ , offsetNanoseconds ] )
 */
Instant GetUTCEpochNanoseconds(const PlainDateTime& dateTime);

/**
 * GetUTCEpochNanoseconds ( year, month, day, hour, minute, second, millisecond,
 * microsecond, nanosecond [ , offsetNanoseconds ] )
 */
Instant GetUTCEpochNanoseconds(const PlainDateTime& dateTime,
                               const InstantSpan& offsetNanoseconds);

/**
 * RoundTemporalInstant ( ns, increment, unit, roundingMode )
 */
bool RoundTemporalInstant(JSContext* cx, const Instant& ns, Increment increment,
                          TemporalUnit unit, TemporalRoundingMode roundingMode,
                          Instant* result);

/**
 * AddInstant ( epochNanoseconds, hours, minutes, seconds, milliseconds,
 * microseconds, nanoseconds )
 */
bool AddInstant(JSContext* cx, const Instant& instant, const Duration& duration,
                Instant* result);

/**
 * DifferenceInstant ( ns1, ns2, roundingIncrement, smallestUnit, largestUnit,
 * roundingMode )
 */
bool DifferenceInstant(JSContext* cx, const Instant& ns1, const Instant& ns2,
                       Increment roundingIncrement, TemporalUnit smallestUnit,
                       TemporalUnit largestUnit,
                       TemporalRoundingMode roundingMode, Duration* result);

} /* namespace js::temporal */

#endif /* builtin_temporal_Instant_h */