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_PlainDateTime_h
8 #define builtin_temporal_PlainDateTime_h
10 #include "mozilla/Assertions.h"
14 #include "builtin/temporal/Calendar.h"
15 #include "builtin/temporal/TemporalTypes.h"
16 #include "builtin/temporal/Wrapped.h"
17 #include "js/RootingAPI.h"
18 #include "js/TypeDecls.h"
20 #include "vm/NativeObject.h"
22 class JS_PUBLIC_API JSTracer
;
29 namespace js::temporal
{
31 class PlainDateTimeObject
: public NativeObject
{
33 static const JSClass class_
;
34 static const JSClass
& protoClass_
;
36 // TODO: Consider compacting fields to reduce object size.
38 // See also PlainDateObject and PlainTimeObject.
40 static constexpr uint32_t ISO_YEAR_SLOT
= 0;
41 static constexpr uint32_t ISO_MONTH_SLOT
= 1;
42 static constexpr uint32_t ISO_DAY_SLOT
= 2;
43 static constexpr uint32_t ISO_HOUR_SLOT
= 3;
44 static constexpr uint32_t ISO_MINUTE_SLOT
= 4;
45 static constexpr uint32_t ISO_SECOND_SLOT
= 5;
46 static constexpr uint32_t ISO_MILLISECOND_SLOT
= 6;
47 static constexpr uint32_t ISO_MICROSECOND_SLOT
= 7;
48 static constexpr uint32_t ISO_NANOSECOND_SLOT
= 8;
49 static constexpr uint32_t CALENDAR_SLOT
= 9;
50 static constexpr uint32_t SLOT_COUNT
= 10;
52 int32_t isoYear() const { return getFixedSlot(ISO_YEAR_SLOT
).toInt32(); }
54 int32_t isoMonth() const { return getFixedSlot(ISO_MONTH_SLOT
).toInt32(); }
56 int32_t isoDay() const { return getFixedSlot(ISO_DAY_SLOT
).toInt32(); }
58 int32_t isoHour() const { return getFixedSlot(ISO_HOUR_SLOT
).toInt32(); }
60 int32_t isoMinute() const { return getFixedSlot(ISO_MINUTE_SLOT
).toInt32(); }
62 int32_t isoSecond() const { return getFixedSlot(ISO_SECOND_SLOT
).toInt32(); }
64 int32_t isoMillisecond() const {
65 return getFixedSlot(ISO_MILLISECOND_SLOT
).toInt32();
68 int32_t isoMicrosecond() const {
69 return getFixedSlot(ISO_MICROSECOND_SLOT
).toInt32();
72 int32_t isoNanosecond() const {
73 return getFixedSlot(ISO_NANOSECOND_SLOT
).toInt32();
76 CalendarValue
calendar() const {
77 return CalendarValue(getFixedSlot(CALENDAR_SLOT
));
81 static const ClassSpec classSpec_
;
85 * Extract the date fields from the PlainDateTime object.
87 inline PlainDate
ToPlainDate(const PlainDateTimeObject
* dateTime
) {
88 return {dateTime
->isoYear(), dateTime
->isoMonth(), dateTime
->isoDay()};
92 * Extract the time fields from the PlainDateTime object.
94 inline PlainTime
ToPlainTime(const PlainDateTimeObject
* dateTime
) {
95 return {dateTime
->isoHour(), dateTime
->isoMinute(),
96 dateTime
->isoSecond(), dateTime
->isoMillisecond(),
97 dateTime
->isoMicrosecond(), dateTime
->isoNanosecond()};
101 * Extract the date-time fields from the PlainDateTime object.
103 inline PlainDateTime
ToPlainDateTime(const PlainDateTimeObject
* dateTime
) {
104 return {ToPlainDate(dateTime
), ToPlainTime(dateTime
)};
107 enum class TemporalUnit
;
111 * IsValidISODateTime ( year, month, day, hour, minute, second, millisecond,
112 * microsecond, nanosecond )
114 bool IsValidISODateTime(const PlainDateTime
& dateTime
);
118 * ISODateTimeWithinLimits ( year, month, day, hour, minute, second,
119 * millisecond, microsecond, nanosecond )
121 bool ISODateTimeWithinLimits(const PlainDateTime
& dateTime
);
124 * ISODateTimeWithinLimits ( year, month, day, hour, minute, second,
125 * millisecond, microsecond, nanosecond )
127 bool ISODateTimeWithinLimits(const PlainDate
& date
);
130 * ISODateTimeWithinLimits ( year, month, day, hour, minute, second,
131 * millisecond, microsecond, nanosecond )
133 bool ISODateTimeWithinLimits(double year
, double month
, double day
);
136 * CreateTemporalDateTime ( isoYear, isoMonth, isoDay, hour, minute, second,
137 * millisecond, microsecond, nanosecond, calendar [ , newTarget ] )
139 PlainDateTimeObject
* CreateTemporalDateTime(JSContext
* cx
,
140 const PlainDateTime
& dateTime
,
141 JS::Handle
<CalendarValue
> calendar
);
144 * ToTemporalDateTime ( item [ , options ] )
146 Wrapped
<PlainDateTimeObject
*> ToTemporalDateTime(JSContext
* cx
,
147 JS::Handle
<JS::Value
> item
);
150 * ToTemporalDateTime ( item [ , options ] )
152 bool ToTemporalDateTime(JSContext
* cx
, JS::Handle
<JS::Value
> item
,
153 PlainDateTime
* result
);
156 * InterpretTemporalDateTimeFields ( calendarRec, fields, options )
158 bool InterpretTemporalDateTimeFields(JSContext
* cx
,
159 JS::Handle
<CalendarRecord
> calendar
,
160 JS::Handle
<PlainObject
*> fields
,
161 JS::Handle
<PlainObject
*> options
,
162 PlainDateTime
* result
);
165 * InterpretTemporalDateTimeFields ( calendarRec, fields, options )
167 bool InterpretTemporalDateTimeFields(JSContext
* cx
,
168 JS::Handle
<CalendarRecord
> calendar
,
169 JS::Handle
<PlainObject
*> fields
,
170 PlainDateTime
* result
);
173 * DifferenceISODateTime ( y1, mon1, d1, h1, min1, s1, ms1, mus1, ns1, y2, mon2,
174 * d2, h2, min2, s2, ms2, mus2, ns2, calendarRec, largestUnit, options )
176 bool DifferenceISODateTime(JSContext
* cx
, const PlainDateTime
& one
,
177 const PlainDateTime
& two
,
178 JS::Handle
<CalendarRecord
> calendar
,
179 TemporalUnit largestUnit
,
180 NormalizedDuration
* result
);
183 * DifferenceISODateTime ( y1, mon1, d1, h1, min1, s1, ms1, mus1, ns1, y2, mon2,
184 * d2, h2, min2, s2, ms2, mus2, ns2, calendarRec, largestUnit, options )
186 bool DifferenceISODateTime(JSContext
* cx
, const PlainDateTime
& one
,
187 const PlainDateTime
& two
,
188 JS::Handle
<CalendarRecord
> calendar
,
189 TemporalUnit largestUnit
,
190 JS::Handle
<PlainObject
*> options
,
191 NormalizedDuration
* result
);
193 class PlainDateTimeWithCalendar
{
194 PlainDateTime dateTime_
;
195 CalendarValue calendar_
;
198 PlainDateTimeWithCalendar() = default;
200 PlainDateTimeWithCalendar(const PlainDateTime
& dateTime
,
201 const CalendarValue
& calendar
)
202 : dateTime_(dateTime
), calendar_(calendar
) {
203 MOZ_ASSERT(ISODateTimeWithinLimits(dateTime
));
206 explicit PlainDateTimeWithCalendar(const PlainDateTimeObject
* dateTime
)
207 : PlainDateTimeWithCalendar(ToPlainDateTime(dateTime
),
208 dateTime
->calendar()) {}
210 const auto& dateTime() const { return dateTime_
; }
211 const auto& date() const { return dateTime_
.date
; }
212 const auto& time() const { return dateTime_
.time
; }
213 const auto& calendar() const { return calendar_
; }
215 // Allow implicit conversion to a calendar-less PlainDateTime.
216 operator const PlainDateTime
&() const { return dateTime(); }
218 void trace(JSTracer
* trc
) { calendar_
.trace(trc
); }
220 const auto* calendarDoNotUse() const { return &calendar_
; }
224 * Extract the date-time fields from the PlainDateTimeWithCalendar object.
226 inline const auto& ToPlainDateTime(const PlainDateTimeWithCalendar
& dateTime
) {
227 return dateTime
.dateTime();
231 * CreateTemporalDateTime ( isoYear, isoMonth, isoDay, hour, minute, second,
232 * millisecond, microsecond, nanosecond, calendar [ , newTarget ] )
234 bool CreateTemporalDateTime(
235 JSContext
* cx
, const PlainDateTime
& dateTime
,
236 JS::Handle
<CalendarValue
> calendar
,
237 JS::MutableHandle
<PlainDateTimeWithCalendar
> result
);
239 } /* namespace js::temporal */
243 template <typename Wrapper
>
244 class WrappedPtrOperations
<temporal::PlainDateTimeWithCalendar
, Wrapper
> {
245 const auto& container() const {
246 return static_cast<const Wrapper
*>(this)->get();
250 const auto& dateTime() const { return container().dateTime(); }
251 const auto& date() const { return container().date(); }
252 const auto& time() const { return container().time(); }
254 auto calendar() const {
255 return JS::Handle
<temporal::CalendarValue
>::fromMarkedLocation(
256 container().calendarDoNotUse());
259 // Allow implicit conversion to a calendar-less PlainDateTime.
260 operator const temporal::PlainDateTime
&() const { return dateTime(); }
265 #endif /* builtin_temporal_PlainDateTime_h */