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 #include "builtin/temporal/PlainTime.h"
9 #include "mozilla/Assertions.h"
10 #include "mozilla/FloatingPoint.h"
11 #include "mozilla/Maybe.h"
16 #include <type_traits>
21 #include "NamespaceImports.h"
23 #include "builtin/temporal/Duration.h"
24 #include "builtin/temporal/Instant.h"
25 #include "builtin/temporal/PlainDate.h"
26 #include "builtin/temporal/PlainDateTime.h"
27 #include "builtin/temporal/Temporal.h"
28 #include "builtin/temporal/TemporalParser.h"
29 #include "builtin/temporal/TemporalRoundingMode.h"
30 #include "builtin/temporal/TemporalTypes.h"
31 #include "builtin/temporal/TemporalUnit.h"
32 #include "builtin/temporal/TimeZone.h"
33 #include "builtin/temporal/ToString.h"
34 #include "builtin/temporal/ZonedDateTime.h"
35 #include "ds/IdValuePair.h"
36 #include "gc/AllocKind.h"
37 #include "gc/Barrier.h"
38 #include "js/AllocPolicy.h"
39 #include "js/CallArgs.h"
40 #include "js/CallNonGenericMethod.h"
42 #include "js/ErrorReport.h"
43 #include "js/friend/ErrorMessages.h"
44 #include "js/PropertyDescriptor.h"
45 #include "js/PropertySpec.h"
46 #include "js/RootingAPI.h"
48 #include "vm/BytecodeUtil.h"
49 #include "vm/GlobalObject.h"
50 #include "vm/JSAtomState.h"
51 #include "vm/JSContext.h"
52 #include "vm/JSObject.h"
53 #include "vm/PlainObject.h"
54 #include "vm/StringType.h"
56 #include "vm/JSObject-inl.h"
57 #include "vm/NativeObject-inl.h"
58 #include "vm/ObjectOperations-inl.h"
61 using namespace js::temporal
;
63 static inline bool IsPlainTime(Handle
<Value
> v
) {
64 return v
.isObject() && v
.toObject().is
<PlainTimeObject
>();
69 * IsValidTime ( hour, minute, second, millisecond, microsecond, nanosecond )
72 static bool IsValidTime(T hour
, T minute
, T second
, T millisecond
,
73 T microsecond
, T nanosecond
) {
74 static_assert(std::is_same_v
<T
, int32_t> || std::is_same_v
<T
, double>);
77 MOZ_ASSERT(IsInteger(hour
));
78 MOZ_ASSERT(IsInteger(minute
));
79 MOZ_ASSERT(IsInteger(second
));
80 MOZ_ASSERT(IsInteger(millisecond
));
81 MOZ_ASSERT(IsInteger(microsecond
));
82 MOZ_ASSERT(IsInteger(nanosecond
));
85 if (hour
< 0 || hour
> 23) {
90 if (minute
< 0 || minute
> 59) {
95 if (second
< 0 || second
> 59) {
100 if (millisecond
< 0 || millisecond
> 999) {
105 if (microsecond
< 0 || microsecond
> 999) {
110 if (nanosecond
< 0 || nanosecond
> 999) {
119 * IsValidTime ( hour, minute, second, millisecond, microsecond, nanosecond )
121 bool js::temporal::IsValidTime(const PlainTime
& time
) {
122 const auto& [hour
, minute
, second
, millisecond
, microsecond
, nanosecond
] =
124 return ::IsValidTime(hour
, minute
, second
, millisecond
, microsecond
,
129 * IsValidTime ( hour, minute, second, millisecond, microsecond, nanosecond )
131 bool js::temporal::IsValidTime(double hour
, double minute
, double second
,
132 double millisecond
, double microsecond
,
134 return ::IsValidTime(hour
, minute
, second
, millisecond
, microsecond
,
139 static void ReportInvalidTimeValue(JSContext
* cx
, const char* name
, int32_t min
,
140 int32_t max
, double num
) {
141 Int32ToCStringBuf minCbuf
;
142 const char* minStr
= Int32ToCString(&minCbuf
, min
);
144 Int32ToCStringBuf maxCbuf
;
145 const char* maxStr
= Int32ToCString(&maxCbuf
, max
);
147 ToCStringBuf numCbuf
;
148 const char* numStr
= NumberToCString(&numCbuf
, num
);
150 JS_ReportErrorNumberASCII(cx
, GetErrorMessage
, nullptr,
151 JSMSG_TEMPORAL_PLAIN_TIME_INVALID_VALUE
, name
,
152 minStr
, maxStr
, numStr
);
155 template <typename T
>
156 static inline bool ThrowIfInvalidTimeValue(JSContext
* cx
, const char* name
,
157 int32_t min
, int32_t max
, T num
) {
158 if (min
<= num
&& num
<= max
) {
161 ReportInvalidTimeValue(cx
, name
, min
, max
, num
);
166 * IsValidTime ( hour, minute, second, millisecond, microsecond, nanosecond )
168 template <typename T
>
169 static bool ThrowIfInvalidTime(JSContext
* cx
, T hour
, T minute
, T second
,
170 T millisecond
, T microsecond
, T nanosecond
) {
171 static_assert(std::is_same_v
<T
, int32_t> || std::is_same_v
<T
, double>);
174 MOZ_ASSERT(IsInteger(hour
));
175 MOZ_ASSERT(IsInteger(minute
));
176 MOZ_ASSERT(IsInteger(second
));
177 MOZ_ASSERT(IsInteger(millisecond
));
178 MOZ_ASSERT(IsInteger(microsecond
));
179 MOZ_ASSERT(IsInteger(nanosecond
));
182 if (!ThrowIfInvalidTimeValue(cx
, "hour", 0, 23, hour
)) {
187 if (!ThrowIfInvalidTimeValue(cx
, "minute", 0, 59, minute
)) {
192 if (!ThrowIfInvalidTimeValue(cx
, "second", 0, 59, second
)) {
197 if (!ThrowIfInvalidTimeValue(cx
, "millisecond", 0, 999, millisecond
)) {
202 if (!ThrowIfInvalidTimeValue(cx
, "microsecond", 0, 999, microsecond
)) {
207 if (!ThrowIfInvalidTimeValue(cx
, "nanosecond", 0, 999, nanosecond
)) {
216 * IsValidTime ( hour, minute, second, millisecond, microsecond, nanosecond )
218 bool js::temporal::ThrowIfInvalidTime(JSContext
* cx
, const PlainTime
& time
) {
219 const auto& [hour
, minute
, second
, millisecond
, microsecond
, nanosecond
] =
221 return ::ThrowIfInvalidTime(cx
, hour
, minute
, second
, millisecond
,
222 microsecond
, nanosecond
);
226 * IsValidTime ( hour, minute, second, millisecond, microsecond, nanosecond )
228 bool js::temporal::ThrowIfInvalidTime(JSContext
* cx
, double hour
, double minute
,
229 double second
, double millisecond
,
230 double microsecond
, double nanosecond
) {
231 return ::ThrowIfInvalidTime(cx
, hour
, minute
, second
, millisecond
,
232 microsecond
, nanosecond
);
236 * ConstrainTime ( hour, minute, second, millisecond, microsecond, nanosecond )
238 static PlainTime
ConstrainTime(double hour
, double minute
, double second
,
239 double millisecond
, double microsecond
,
242 MOZ_ASSERT(IsInteger(hour
));
243 MOZ_ASSERT(IsInteger(minute
));
244 MOZ_ASSERT(IsInteger(second
));
245 MOZ_ASSERT(IsInteger(millisecond
));
246 MOZ_ASSERT(IsInteger(microsecond
));
247 MOZ_ASSERT(IsInteger(nanosecond
));
251 int32_t(std::clamp(hour
, 0.0, 23.0)),
252 int32_t(std::clamp(minute
, 0.0, 59.0)),
253 int32_t(std::clamp(second
, 0.0, 59.0)),
254 int32_t(std::clamp(millisecond
, 0.0, 999.0)),
255 int32_t(std::clamp(microsecond
, 0.0, 999.0)),
256 int32_t(std::clamp(nanosecond
, 0.0, 999.0)),
261 * RegulateTime ( hour, minute, second, millisecond, microsecond, nanosecond,
264 bool js::temporal::RegulateTime(JSContext
* cx
, const TemporalTimeLike
& time
,
265 TemporalOverflow overflow
, PlainTime
* result
) {
266 const auto& [hour
, minute
, second
, millisecond
, microsecond
, nanosecond
] =
270 MOZ_ASSERT(IsInteger(hour
));
271 MOZ_ASSERT(IsInteger(minute
));
272 MOZ_ASSERT(IsInteger(second
));
273 MOZ_ASSERT(IsInteger(millisecond
));
274 MOZ_ASSERT(IsInteger(microsecond
));
275 MOZ_ASSERT(IsInteger(nanosecond
));
277 // Step 2. (Not applicable in our implementation.)
280 if (overflow
== TemporalOverflow::Constrain
) {
281 *result
= ConstrainTime(hour
, minute
, second
, millisecond
, microsecond
,
287 MOZ_ASSERT(overflow
== TemporalOverflow::Reject
);
290 if (!ThrowIfInvalidTime(cx
, hour
, minute
, second
, millisecond
, microsecond
,
297 int32_t(hour
), int32_t(minute
), int32_t(second
),
298 int32_t(millisecond
), int32_t(microsecond
), int32_t(nanosecond
),
304 * CreateTemporalTime ( hour, minute, second, millisecond, microsecond,
305 * nanosecond [ , newTarget ] )
307 static PlainTimeObject
* CreateTemporalTime(JSContext
* cx
, const CallArgs
& args
,
308 double hour
, double minute
,
309 double second
, double millisecond
,
312 MOZ_ASSERT(IsInteger(hour
));
313 MOZ_ASSERT(IsInteger(minute
));
314 MOZ_ASSERT(IsInteger(second
));
315 MOZ_ASSERT(IsInteger(millisecond
));
316 MOZ_ASSERT(IsInteger(microsecond
));
317 MOZ_ASSERT(IsInteger(nanosecond
));
320 if (!ThrowIfInvalidTime(cx
, hour
, minute
, second
, millisecond
, microsecond
,
326 Rooted
<JSObject
*> proto(cx
);
327 if (!GetPrototypeFromBuiltinConstructor(cx
, args
, JSProto_PlainTime
,
332 auto* object
= NewObjectWithClassProto
<PlainTimeObject
>(cx
, proto
);
338 object
->setFixedSlot(PlainTimeObject::ISO_HOUR_SLOT
,
339 Int32Value(int32_t(hour
)));
342 object
->setFixedSlot(PlainTimeObject::ISO_MINUTE_SLOT
,
343 Int32Value(int32_t(minute
)));
346 object
->setFixedSlot(PlainTimeObject::ISO_SECOND_SLOT
,
347 Int32Value(int32_t(second
)));
350 object
->setFixedSlot(PlainTimeObject::ISO_MILLISECOND_SLOT
,
351 Int32Value(int32_t(millisecond
)));
354 object
->setFixedSlot(PlainTimeObject::ISO_MICROSECOND_SLOT
,
355 Int32Value(int32_t(microsecond
)));
358 object
->setFixedSlot(PlainTimeObject::ISO_NANOSECOND_SLOT
,
359 Int32Value(int32_t(nanosecond
)));
366 * CreateTemporalTime ( hour, minute, second, millisecond, microsecond,
367 * nanosecond [ , newTarget ] )
369 PlainTimeObject
* js::temporal::CreateTemporalTime(JSContext
* cx
,
370 const PlainTime
& time
) {
371 const auto& [hour
, minute
, second
, millisecond
, microsecond
, nanosecond
] =
375 if (!ThrowIfInvalidTime(cx
, time
)) {
380 auto* object
= NewBuiltinClassInstance
<PlainTimeObject
>(cx
);
386 object
->setFixedSlot(PlainTimeObject::ISO_HOUR_SLOT
, Int32Value(hour
));
389 object
->setFixedSlot(PlainTimeObject::ISO_MINUTE_SLOT
, Int32Value(minute
));
392 object
->setFixedSlot(PlainTimeObject::ISO_SECOND_SLOT
, Int32Value(second
));
395 object
->setFixedSlot(PlainTimeObject::ISO_MILLISECOND_SLOT
,
396 Int32Value(millisecond
));
399 object
->setFixedSlot(PlainTimeObject::ISO_MICROSECOND_SLOT
,
400 Int32Value(microsecond
));
403 object
->setFixedSlot(PlainTimeObject::ISO_NANOSECOND_SLOT
,
404 Int32Value(nanosecond
));
411 * BalanceTime ( hour, minute, second, millisecond, microsecond, nanosecond )
413 template <typename IntT
>
414 static BalancedTime
BalanceTime(IntT hour
, IntT minute
, IntT second
,
415 IntT millisecond
, IntT microsecond
,
417 // Combined floor'ed division and modulo operation.
418 auto divmod
= [](IntT dividend
, int32_t divisor
, int32_t* remainder
) {
419 MOZ_ASSERT(divisor
> 0);
421 IntT quotient
= dividend
/ divisor
;
422 *remainder
= dividend
% divisor
;
424 // The remainder is negative, add the divisor and simulate a floor instead
425 // of trunc division.
426 if (*remainder
< 0) {
427 *remainder
+= divisor
;
437 microsecond
+= divmod(nanosecond
, 1000, &time
.nanosecond
);
440 millisecond
+= divmod(microsecond
, 1000, &time
.microsecond
);
443 second
+= divmod(millisecond
, 1000, &time
.millisecond
);
446 minute
+= divmod(second
, 60, &time
.second
);
449 hour
+= divmod(minute
, 60, &time
.minute
);
452 int32_t days
= divmod(hour
, 24, &time
.hour
);
455 MOZ_ASSERT(IsValidTime(time
));
460 * BalanceTime ( hour, minute, second, millisecond, microsecond, nanosecond )
462 static BalancedTime
BalanceTime(int32_t hour
, int32_t minute
, int32_t second
,
463 int32_t millisecond
, int32_t microsecond
,
464 int32_t nanosecond
) {
465 MOZ_ASSERT(-24 < hour
&& hour
< 2 * 24);
466 MOZ_ASSERT(-60 < minute
&& minute
< 2 * 60);
467 MOZ_ASSERT(-60 < second
&& second
< 2 * 60);
468 MOZ_ASSERT(-1000 < millisecond
&& millisecond
< 2 * 1000);
469 MOZ_ASSERT(-1000 < microsecond
&& microsecond
< 2 * 1000);
470 MOZ_ASSERT(-1000 < nanosecond
&& nanosecond
< 2 * 1000);
472 return BalanceTime
<int32_t>(hour
, minute
, second
, millisecond
, microsecond
,
477 * BalanceTime ( hour, minute, second, millisecond, microsecond, nanosecond )
479 BalancedTime
js::temporal::BalanceTime(const PlainTime
& time
,
480 int64_t nanoseconds
) {
481 MOZ_ASSERT(IsValidTime(time
));
482 MOZ_ASSERT(std::abs(nanoseconds
) <= ToNanoseconds(TemporalUnit::Day
));
484 return ::BalanceTime
<int64_t>(time
.hour
, time
.minute
, time
.second
,
485 time
.millisecond
, time
.microsecond
,
486 time
.nanosecond
+ nanoseconds
);
490 * DifferenceTime ( h1, min1, s1, ms1, mus1, ns1, h2, min2, s2, ms2, mus2, ns2 )
492 NormalizedTimeDuration
js::temporal::DifferenceTime(const PlainTime
& time1
,
493 const PlainTime
& time2
) {
494 MOZ_ASSERT(IsValidTime(time1
));
495 MOZ_ASSERT(IsValidTime(time2
));
498 int32_t hours
= time2
.hour
- time1
.hour
;
501 int32_t minutes
= time2
.minute
- time1
.minute
;
504 int32_t seconds
= time2
.second
- time1
.second
;
507 int32_t milliseconds
= time2
.millisecond
- time1
.millisecond
;
510 int32_t microseconds
= time2
.microsecond
- time1
.microsecond
;
513 int32_t nanoseconds
= time2
.nanosecond
- time1
.nanosecond
;
516 auto result
= NormalizeTimeDuration(hours
, minutes
, seconds
, milliseconds
,
517 microseconds
, nanoseconds
);
520 MOZ_ASSERT(result
.abs().toNanoseconds() <
521 Int128
{ToNanoseconds(TemporalUnit::Day
)});
528 * ToTemporalTime ( item [ , overflow ] )
530 static bool ToTemporalTime(JSContext
* cx
, Handle
<Value
> item
,
531 TemporalOverflow overflow
, PlainTime
* result
) {
532 // Steps 1-2. (Not applicable in our implementation.)
535 if (item
.isObject()) {
537 Rooted
<JSObject
*> itemObj(cx
, &item
.toObject());
540 if (auto* time
= itemObj
->maybeUnwrapIf
<PlainTimeObject
>()) {
541 *result
= ToPlainTime(time
);
546 if (auto* zonedDateTime
= itemObj
->maybeUnwrapIf
<ZonedDateTimeObject
>()) {
547 auto epochInstant
= ToInstant(zonedDateTime
);
548 Rooted
<TimeZoneValue
> timeZone(cx
, zonedDateTime
->timeZone());
550 if (!timeZone
.wrap(cx
)) {
555 PlainDateTime dateTime
;
556 if (!GetPlainDateTimeFor(cx
, timeZone
, epochInstant
, &dateTime
)) {
561 *result
= dateTime
.time
;
566 if (auto* dateTime
= itemObj
->maybeUnwrapIf
<PlainDateTimeObject
>()) {
567 *result
= ToPlainTime(dateTime
);
572 TemporalTimeLike timeResult
;
573 if (!ToTemporalTimeRecord(cx
, itemObj
, &timeResult
)) {
578 if (!RegulateTime(cx
, timeResult
, overflow
, result
)) {
585 if (!item
.isString()) {
586 ReportValueError(cx
, JSMSG_UNEXPECTED_TYPE
, JSDVG_IGNORE_STACK
, item
,
587 nullptr, "not a string");
590 Rooted
<JSString
*> string(cx
, item
.toString());
593 if (!ParseTemporalTimeString(cx
, string
, result
)) {
598 MOZ_ASSERT(IsValidTime(*result
));
606 * ToTemporalTime ( item [ , overflow ] )
608 static PlainTimeObject
* ToTemporalTime(JSContext
* cx
, Handle
<Value
> item
,
609 TemporalOverflow overflow
) {
611 if (!ToTemporalTime(cx
, item
, overflow
, &time
)) {
614 MOZ_ASSERT(IsValidTime(time
));
616 return CreateTemporalTime(cx
, time
);
620 * ToTemporalTime ( item [ , overflow ] )
622 bool js::temporal::ToTemporalTime(JSContext
* cx
, Handle
<Value
> item
,
624 return ToTemporalTime(cx
, item
, TemporalOverflow::Constrain
, result
);
628 * CompareTemporalTime ( h1, min1, s1, ms1, mus1, ns1, h2, min2, s2, ms2, mus2,
631 int32_t js::temporal::CompareTemporalTime(const PlainTime
& one
,
632 const PlainTime
& two
) {
634 if (int32_t diff
= one
.hour
- two
.hour
) {
635 return diff
< 0 ? -1 : 1;
639 if (int32_t diff
= one
.minute
- two
.minute
) {
640 return diff
< 0 ? -1 : 1;
644 if (int32_t diff
= one
.second
- two
.second
) {
645 return diff
< 0 ? -1 : 1;
649 if (int32_t diff
= one
.millisecond
- two
.millisecond
) {
650 return diff
< 0 ? -1 : 1;
654 if (int32_t diff
= one
.microsecond
- two
.microsecond
) {
655 return diff
< 0 ? -1 : 1;
659 if (int32_t diff
= one
.nanosecond
- two
.nanosecond
) {
660 return diff
< 0 ? -1 : 1;
668 * ToTemporalTimeRecord ( temporalTimeLike [ , completeness ] )
670 static bool ToTemporalTimeRecord(JSContext
* cx
,
671 Handle
<JSObject
*> temporalTimeLike
,
672 TemporalTimeLike
* result
) {
673 // Steps 1 and 3-4. (Not applicable in our implementation.)
675 // Step 2. (Inlined call to PrepareTemporalFields.)
676 // PrepareTemporalFields, step 1. (Not applicable in our implementation.)
678 // PrepareTemporalFields, step 2.
681 // PrepareTemporalFields, steps 3-4. (Loop unrolled)
682 Rooted
<Value
> value(cx
);
683 auto getTimeProperty
= [&](Handle
<PropertyName
*> property
, const char* name
,
686 if (!GetProperty(cx
, temporalTimeLike
, temporalTimeLike
, property
,
692 if (!value
.isUndefined()) {
697 if (!ToIntegerWithTruncation(cx
, value
, name
, num
)) {
704 if (!getTimeProperty(cx
->names().hour
, "hour", &result
->hour
)) {
707 if (!getTimeProperty(cx
->names().microsecond
, "microsecond",
708 &result
->microsecond
)) {
711 if (!getTimeProperty(cx
->names().millisecond
, "millisecond",
712 &result
->millisecond
)) {
715 if (!getTimeProperty(cx
->names().minute
, "minute", &result
->minute
)) {
718 if (!getTimeProperty(cx
->names().nanosecond
, "nanosecond",
719 &result
->nanosecond
)) {
722 if (!getTimeProperty(cx
->names().second
, "second", &result
->second
)) {
726 // PrepareTemporalFields, step 5.
728 JS_ReportErrorNumberASCII(cx
, GetErrorMessage
, nullptr,
729 JSMSG_TEMPORAL_PLAIN_TIME_MISSING_UNIT
);
733 // Steps 5-16. (Performed implicitly in our implementation.)
740 * ToTemporalTimeRecord ( temporalTimeLike [ , completeness ] )
742 bool js::temporal::ToTemporalTimeRecord(JSContext
* cx
,
743 Handle
<JSObject
*> temporalTimeLike
,
744 TemporalTimeLike
* result
) {
745 // Step 3.a. (Set all fields to zero.)
748 // Steps 1-2 and 4-17.
749 return ::ToTemporalTimeRecord(cx
, temporalTimeLike
, result
);
752 static int64_t TimeToNanos(const PlainTime
& time
) {
753 // No overflow possible because the input is a valid time.
754 MOZ_ASSERT(IsValidTime(time
));
756 int64_t hour
= time
.hour
;
757 int64_t minute
= time
.minute
;
758 int64_t second
= time
.second
;
759 int64_t millisecond
= time
.millisecond
;
760 int64_t microsecond
= time
.microsecond
;
761 int64_t nanosecond
= time
.nanosecond
;
763 int64_t millis
= ((hour
* 60 + minute
) * 60 + second
) * 1000 + millisecond
;
764 return (millis
* 1000 + microsecond
) * 1000 + nanosecond
;
768 * RoundTime ( hour, minute, second, millisecond, microsecond, nanosecond,
769 * increment, unit, roundingMode )
771 RoundedTime
js::temporal::RoundTime(const PlainTime
& time
, Increment increment
,
773 TemporalRoundingMode roundingMode
) {
774 MOZ_ASSERT(IsValidTime(time
));
775 MOZ_ASSERT(unit
>= TemporalUnit::Day
);
776 MOZ_ASSERT_IF(unit
> TemporalUnit::Day
,
777 increment
<= MaximumTemporalDurationRoundingIncrement(unit
));
778 MOZ_ASSERT_IF(unit
== TemporalUnit::Day
, increment
== Increment
{1});
781 auto [hour
, minute
, second
, millisecond
, microsecond
, nanosecond
] = time
;
783 // Take the same approach as used in RoundDuration() to perform exact
784 // mathematical operations without possible loss of precision.
790 case TemporalUnit::Day
:
794 case TemporalUnit::Hour
:
803 case TemporalUnit::Minute
:
804 quantity
= {0, minute
, second
, millisecond
, microsecond
, nanosecond
};
811 case TemporalUnit::Second
:
812 quantity
= {0, 0, second
, millisecond
, microsecond
, nanosecond
};
818 case TemporalUnit::Millisecond
:
819 quantity
= {0, 0, 0, millisecond
, microsecond
, nanosecond
};
820 result
= &millisecond
;
824 case TemporalUnit::Microsecond
:
825 quantity
= {0, 0, 0, 0, microsecond
, nanosecond
};
826 result
= µsecond
;
829 case TemporalUnit::Nanosecond
:
830 quantity
= {0, 0, 0, 0, 0, nanosecond
};
831 result
= &nanosecond
;
834 case TemporalUnit::Auto
:
835 case TemporalUnit::Year
:
836 case TemporalUnit::Month
:
837 case TemporalUnit::Week
:
838 MOZ_CRASH("unexpected temporal unit");
841 int64_t quantityNs
= TimeToNanos(quantity
);
842 MOZ_ASSERT(0 <= quantityNs
&& quantityNs
< ToNanoseconds(TemporalUnit::Day
));
845 int64_t unitLength
= ToNanoseconds(unit
);
846 int64_t incrementNs
= increment
.value() * unitLength
;
847 MOZ_ASSERT(incrementNs
<= ToNanoseconds(TemporalUnit::Day
),
848 "incrementNs doesn't overflow time resolution");
851 int64_t r
= RoundNumberToIncrement(quantityNs
, incrementNs
, roundingMode
) /
853 MOZ_ASSERT(r
== int64_t(int32_t(r
)),
854 "can't overflow when inputs are all in range");
856 *result
= int32_t(r
);
859 if (unit
== TemporalUnit::Day
) {
860 return {int64_t(days
), {0, 0, 0, 0, 0, 0}};
865 ::BalanceTime(hour
, minute
, second
, millisecond
, microsecond
, nanosecond
);
866 return {int64_t(balanced
.days
), balanced
.time
};
870 * AddTime ( hour, minute, second, millisecond, microsecond, nanosecond, norm )
872 AddedTime
js::temporal::AddTime(const PlainTime
& time
,
873 const NormalizedTimeDuration
& duration
) {
874 MOZ_ASSERT(IsValidTime(time
));
875 MOZ_ASSERT(IsValidNormalizedTimeDuration(duration
));
877 auto [seconds
, nanoseconds
] = duration
;
878 if (seconds
< 0 && nanoseconds
> 0) {
880 nanoseconds
-= 1'000'000'000;
882 MOZ_ASSERT(std::abs(nanoseconds
) <= 999'999'999);
885 int64_t second
= time
.second
+ seconds
;
888 int32_t nanosecond
= time
.nanosecond
+ nanoseconds
;
892 ::BalanceTime
<int64_t>(time
.hour
, time
.minute
, second
, time
.millisecond
,
893 time
.microsecond
, nanosecond
);
894 return {balanced
.days
, balanced
.time
};
898 * DifferenceTemporalPlainTime ( operation, temporalTime, other, options )
900 static bool DifferenceTemporalPlainTime(JSContext
* cx
,
901 TemporalDifference operation
,
902 const CallArgs
& args
) {
904 ToPlainTime(&args
.thisv().toObject().as
<PlainTimeObject
>());
906 // Step 1. (Not applicable in our implementation.)
910 if (!ToTemporalTime(cx
, args
.get(0), &other
)) {
915 DifferenceSettings settings
;
916 if (args
.hasDefined(1)) {
917 Rooted
<JSObject
*> options(
918 cx
, RequireObjectArg(cx
, "options", ToName(operation
), args
[1]));
924 Rooted
<PlainObject
*> resolvedOptions(cx
,
925 SnapshotOwnProperties(cx
, options
));
926 if (!resolvedOptions
) {
931 if (!GetDifferenceSettings(
932 cx
, operation
, resolvedOptions
, TemporalUnitGroup::Time
,
933 TemporalUnit::Nanosecond
, TemporalUnit::Hour
, &settings
)) {
939 TemporalUnit::Nanosecond
,
941 TemporalRoundingMode::Trunc
,
947 auto diff
= DifferenceTime(temporalTime
, other
);
950 if (settings
.smallestUnit
!= TemporalUnit::Nanosecond
||
951 settings
.roundingIncrement
!= Increment
{1}) {
953 diff
= RoundDuration(diff
, settings
.roundingIncrement
,
954 settings
.smallestUnit
, settings
.roundingMode
);
958 TimeDuration balancedDuration
;
959 if (!BalanceTimeDuration(cx
, diff
, settings
.largestUnit
, &balancedDuration
)) {
964 auto duration
= balancedDuration
.toDuration();
965 if (operation
== TemporalDifference::Since
) {
966 duration
= duration
.negate();
969 auto* result
= CreateTemporalDuration(cx
, duration
);
974 args
.rval().setObject(*result
);
978 enum class PlainTimeDuration
{ Add
, Subtract
};
981 * AddDurationToOrSubtractDurationFromPlainTime ( operation, temporalTime,
982 * temporalDurationLike )
984 static bool AddDurationToOrSubtractDurationFromPlainTime(
985 JSContext
* cx
, PlainTimeDuration operation
, const CallArgs
& args
) {
986 auto* temporalTime
= &args
.thisv().toObject().as
<PlainTimeObject
>();
987 auto time
= ToPlainTime(temporalTime
);
989 // Step 1. (Not applicable in our implementation.)
993 if (!ToTemporalDurationRecord(cx
, args
.get(0), &duration
)) {
998 if (operation
== PlainTimeDuration::Subtract
) {
999 duration
= duration
.negate();
1001 auto timeDuration
= NormalizeTimeDuration(duration
);
1004 auto result
= AddTime(time
, timeDuration
);
1005 MOZ_ASSERT(IsValidTime(result
.time
));
1008 auto* obj
= CreateTemporalTime(cx
, result
.time
);
1013 args
.rval().setObject(*obj
);
1018 * Temporal.PlainTime ( [ hour [ , minute [ , second [ , millisecond [ ,
1019 * microsecond [ , nanosecond ] ] ] ] ] ] )
1021 static bool PlainTimeConstructor(JSContext
* cx
, unsigned argc
, Value
* vp
) {
1022 CallArgs args
= CallArgsFromVp(argc
, vp
);
1025 if (!ThrowIfNotConstructing(cx
, args
, "Temporal.PlainTime")) {
1031 if (args
.hasDefined(0)) {
1032 if (!ToIntegerWithTruncation(cx
, args
[0], "hour", &hour
)) {
1039 if (args
.hasDefined(1)) {
1040 if (!ToIntegerWithTruncation(cx
, args
[1], "minute", &minute
)) {
1047 if (args
.hasDefined(2)) {
1048 if (!ToIntegerWithTruncation(cx
, args
[2], "second", &second
)) {
1054 double millisecond
= 0;
1055 if (args
.hasDefined(3)) {
1056 if (!ToIntegerWithTruncation(cx
, args
[3], "millisecond", &millisecond
)) {
1062 double microsecond
= 0;
1063 if (args
.hasDefined(4)) {
1064 if (!ToIntegerWithTruncation(cx
, args
[4], "microsecond", µsecond
)) {
1070 double nanosecond
= 0;
1071 if (args
.hasDefined(5)) {
1072 if (!ToIntegerWithTruncation(cx
, args
[5], "nanosecond", &nanosecond
)) {
1078 auto* temporalTime
= CreateTemporalTime(cx
, args
, hour
, minute
, second
,
1079 millisecond
, microsecond
, nanosecond
);
1080 if (!temporalTime
) {
1084 args
.rval().setObject(*temporalTime
);
1089 * Temporal.PlainTime.from ( item [ , options ] )
1091 static bool PlainTime_from(JSContext
* cx
, unsigned argc
, Value
* vp
) {
1092 CallArgs args
= CallArgsFromVp(argc
, vp
);
1094 // Step 1. (Not applicable)
1096 auto overflow
= TemporalOverflow::Constrain
;
1097 if (args
.hasDefined(1)) {
1099 Rooted
<JSObject
*> options(cx
,
1100 RequireObjectArg(cx
, "options", "from", args
[1]));
1106 if (!GetTemporalOverflowOption(cx
, options
, &overflow
)) {
1112 auto* result
= ToTemporalTime(cx
, args
.get(0), overflow
);
1117 args
.rval().setObject(*result
);
1122 * Temporal.PlainTime.compare ( one, two )
1124 static bool PlainTime_compare(JSContext
* cx
, unsigned argc
, Value
* vp
) {
1125 CallArgs args
= CallArgsFromVp(argc
, vp
);
1129 if (!ToTemporalTime(cx
, args
.get(0), &one
)) {
1135 if (!ToTemporalTime(cx
, args
.get(1), &two
)) {
1140 args
.rval().setInt32(CompareTemporalTime(one
, two
));
1145 * get Temporal.PlainTime.prototype.hour
1147 static bool PlainTime_hour(JSContext
* cx
, const CallArgs
& args
) {
1149 auto* temporalTime
= &args
.thisv().toObject().as
<PlainTimeObject
>();
1150 args
.rval().setInt32(temporalTime
->isoHour());
1155 * get Temporal.PlainTime.prototype.hour
1157 static bool PlainTime_hour(JSContext
* cx
, unsigned argc
, Value
* vp
) {
1159 CallArgs args
= CallArgsFromVp(argc
, vp
);
1160 return CallNonGenericMethod
<IsPlainTime
, PlainTime_hour
>(cx
, args
);
1164 * get Temporal.PlainTime.prototype.minute
1166 static bool PlainTime_minute(JSContext
* cx
, const CallArgs
& args
) {
1168 auto* temporalTime
= &args
.thisv().toObject().as
<PlainTimeObject
>();
1169 args
.rval().setInt32(temporalTime
->isoMinute());
1174 * get Temporal.PlainTime.prototype.minute
1176 static bool PlainTime_minute(JSContext
* cx
, unsigned argc
, Value
* vp
) {
1178 CallArgs args
= CallArgsFromVp(argc
, vp
);
1179 return CallNonGenericMethod
<IsPlainTime
, PlainTime_minute
>(cx
, args
);
1183 * get Temporal.PlainTime.prototype.second
1185 static bool PlainTime_second(JSContext
* cx
, const CallArgs
& args
) {
1187 auto* temporalTime
= &args
.thisv().toObject().as
<PlainTimeObject
>();
1188 args
.rval().setInt32(temporalTime
->isoSecond());
1193 * get Temporal.PlainTime.prototype.second
1195 static bool PlainTime_second(JSContext
* cx
, unsigned argc
, Value
* vp
) {
1197 CallArgs args
= CallArgsFromVp(argc
, vp
);
1198 return CallNonGenericMethod
<IsPlainTime
, PlainTime_second
>(cx
, args
);
1202 * get Temporal.PlainTime.prototype.millisecond
1204 static bool PlainTime_millisecond(JSContext
* cx
, const CallArgs
& args
) {
1206 auto* temporalTime
= &args
.thisv().toObject().as
<PlainTimeObject
>();
1207 args
.rval().setInt32(temporalTime
->isoMillisecond());
1212 * get Temporal.PlainTime.prototype.millisecond
1214 static bool PlainTime_millisecond(JSContext
* cx
, unsigned argc
, Value
* vp
) {
1216 CallArgs args
= CallArgsFromVp(argc
, vp
);
1217 return CallNonGenericMethod
<IsPlainTime
, PlainTime_millisecond
>(cx
, args
);
1221 * get Temporal.PlainTime.prototype.microsecond
1223 static bool PlainTime_microsecond(JSContext
* cx
, const CallArgs
& args
) {
1225 auto* temporalTime
= &args
.thisv().toObject().as
<PlainTimeObject
>();
1226 args
.rval().setInt32(temporalTime
->isoMicrosecond());
1231 * get Temporal.PlainTime.prototype.microsecond
1233 static bool PlainTime_microsecond(JSContext
* cx
, unsigned argc
, Value
* vp
) {
1235 CallArgs args
= CallArgsFromVp(argc
, vp
);
1236 return CallNonGenericMethod
<IsPlainTime
, PlainTime_microsecond
>(cx
, args
);
1240 * get Temporal.PlainTime.prototype.nanosecond
1242 static bool PlainTime_nanosecond(JSContext
* cx
, const CallArgs
& args
) {
1244 auto* temporalTime
= &args
.thisv().toObject().as
<PlainTimeObject
>();
1245 args
.rval().setInt32(temporalTime
->isoNanosecond());
1250 * get Temporal.PlainTime.prototype.nanosecond
1252 static bool PlainTime_nanosecond(JSContext
* cx
, unsigned argc
, Value
* vp
) {
1254 CallArgs args
= CallArgsFromVp(argc
, vp
);
1255 return CallNonGenericMethod
<IsPlainTime
, PlainTime_nanosecond
>(cx
, args
);
1259 * Temporal.PlainTime.prototype.add ( temporalDurationLike )
1261 static bool PlainTime_add(JSContext
* cx
, const CallArgs
& args
) {
1263 return AddDurationToOrSubtractDurationFromPlainTime(
1264 cx
, PlainTimeDuration::Add
, args
);
1268 * Temporal.PlainTime.prototype.add ( temporalDurationLike )
1270 static bool PlainTime_add(JSContext
* cx
, unsigned argc
, Value
* vp
) {
1272 CallArgs args
= CallArgsFromVp(argc
, vp
);
1273 return CallNonGenericMethod
<IsPlainTime
, PlainTime_add
>(cx
, args
);
1277 * Temporal.PlainTime.prototype.subtract ( temporalDurationLike )
1279 static bool PlainTime_subtract(JSContext
* cx
, const CallArgs
& args
) {
1281 return AddDurationToOrSubtractDurationFromPlainTime(
1282 cx
, PlainTimeDuration::Subtract
, args
);
1286 * Temporal.PlainTime.prototype.subtract ( temporalDurationLike )
1288 static bool PlainTime_subtract(JSContext
* cx
, unsigned argc
, Value
* vp
) {
1290 CallArgs args
= CallArgsFromVp(argc
, vp
);
1291 return CallNonGenericMethod
<IsPlainTime
, PlainTime_subtract
>(cx
, args
);
1295 * Temporal.PlainTime.prototype.with ( temporalTimeLike [ , options ] )
1297 static bool PlainTime_with(JSContext
* cx
, const CallArgs
& args
) {
1298 auto* temporalTime
= &args
.thisv().toObject().as
<PlainTimeObject
>();
1299 auto time
= ToPlainTime(temporalTime
);
1302 Rooted
<JSObject
*> temporalTimeLike(
1303 cx
, RequireObjectArg(cx
, "temporalTimeLike", "with", args
.get(0)));
1304 if (!temporalTimeLike
) {
1307 if (!ThrowIfTemporalLikeObject(cx
, temporalTimeLike
)) {
1311 auto overflow
= TemporalOverflow::Constrain
;
1312 if (args
.hasDefined(1)) {
1314 Rooted
<JSObject
*> options(cx
,
1315 RequireObjectArg(cx
, "options", "with", args
[1]));
1321 if (!GetTemporalOverflowOption(cx
, options
, &overflow
)) {
1327 TemporalTimeLike partialTime
= {
1328 double(time
.hour
), double(time
.minute
),
1329 double(time
.second
), double(time
.millisecond
),
1330 double(time
.microsecond
), double(time
.nanosecond
),
1332 if (!::ToTemporalTimeRecord(cx
, temporalTimeLike
, &partialTime
)) {
1338 if (!RegulateTime(cx
, partialTime
, overflow
, &result
)) {
1343 auto* obj
= CreateTemporalTime(cx
, result
);
1348 args
.rval().setObject(*obj
);
1353 * Temporal.PlainTime.prototype.with ( temporalTimeLike [ , options ] )
1355 static bool PlainTime_with(JSContext
* cx
, unsigned argc
, Value
* vp
) {
1357 CallArgs args
= CallArgsFromVp(argc
, vp
);
1358 return CallNonGenericMethod
<IsPlainTime
, PlainTime_with
>(cx
, args
);
1362 * Temporal.PlainTime.prototype.until ( other [ , options ] )
1364 static bool PlainTime_until(JSContext
* cx
, const CallArgs
& args
) {
1366 return DifferenceTemporalPlainTime(cx
, TemporalDifference::Until
, args
);
1370 * Temporal.PlainTime.prototype.until ( other [ , options ] )
1372 static bool PlainTime_until(JSContext
* cx
, unsigned argc
, Value
* vp
) {
1374 CallArgs args
= CallArgsFromVp(argc
, vp
);
1375 return CallNonGenericMethod
<IsPlainTime
, PlainTime_until
>(cx
, args
);
1379 * Temporal.PlainTime.prototype.since ( other [ , options ] )
1381 static bool PlainTime_since(JSContext
* cx
, const CallArgs
& args
) {
1383 return DifferenceTemporalPlainTime(cx
, TemporalDifference::Since
, args
);
1387 * Temporal.PlainTime.prototype.since ( other [ , options ] )
1389 static bool PlainTime_since(JSContext
* cx
, unsigned argc
, Value
* vp
) {
1391 CallArgs args
= CallArgsFromVp(argc
, vp
);
1392 return CallNonGenericMethod
<IsPlainTime
, PlainTime_since
>(cx
, args
);
1396 * Temporal.PlainTime.prototype.round ( roundTo )
1398 static bool PlainTime_round(JSContext
* cx
, const CallArgs
& args
) {
1399 auto* temporalTime
= &args
.thisv().toObject().as
<PlainTimeObject
>();
1400 auto time
= ToPlainTime(temporalTime
);
1403 auto smallestUnit
= TemporalUnit::Auto
;
1404 auto roundingMode
= TemporalRoundingMode::HalfExpand
;
1405 auto roundingIncrement
= Increment
{1};
1406 if (args
.get(0).isString()) {
1407 // Step 4. (Not applicable in our implementation.)
1410 Rooted
<JSString
*> paramString(cx
, args
[0].toString());
1411 if (!GetTemporalUnitValuedOption(cx
, paramString
,
1412 TemporalUnitKey::SmallestUnit
,
1413 TemporalUnitGroup::Time
, &smallestUnit
)) {
1417 // Steps 6-8 and 10-12. (Implicit)
1420 Rooted
<JSObject
*> options(
1421 cx
, RequireObjectArg(cx
, "roundTo", "round", args
.get(0)));
1427 if (!GetRoundingIncrementOption(cx
, options
, &roundingIncrement
)) {
1432 if (!GetRoundingModeOption(cx
, options
, &roundingMode
)) {
1437 if (!GetTemporalUnitValuedOption(cx
, options
, TemporalUnitKey::SmallestUnit
,
1438 TemporalUnitGroup::Time
, &smallestUnit
)) {
1442 if (smallestUnit
== TemporalUnit::Auto
) {
1443 JS_ReportErrorNumberASCII(cx
, GetErrorMessage
, nullptr,
1444 JSMSG_TEMPORAL_MISSING_OPTION
, "smallestUnit");
1449 auto maximum
= MaximumTemporalDurationRoundingIncrement(smallestUnit
);
1452 if (!ValidateTemporalRoundingIncrement(cx
, roundingIncrement
, maximum
,
1459 auto result
= RoundTime(time
, roundingIncrement
, smallestUnit
, roundingMode
);
1462 auto* obj
= CreateTemporalTime(cx
, result
.time
);
1467 args
.rval().setObject(*obj
);
1472 * Temporal.PlainTime.prototype.round ( roundTo )
1474 static bool PlainTime_round(JSContext
* cx
, unsigned argc
, Value
* vp
) {
1476 CallArgs args
= CallArgsFromVp(argc
, vp
);
1477 return CallNonGenericMethod
<IsPlainTime
, PlainTime_round
>(cx
, args
);
1481 * Temporal.PlainTime.prototype.equals ( other )
1483 static bool PlainTime_equals(JSContext
* cx
, const CallArgs
& args
) {
1485 ToPlainTime(&args
.thisv().toObject().as
<PlainTimeObject
>());
1489 if (!ToTemporalTime(cx
, args
.get(0), &other
)) {
1494 args
.rval().setBoolean(temporalTime
== other
);
1499 * Temporal.PlainTime.prototype.equals ( other )
1501 static bool PlainTime_equals(JSContext
* cx
, unsigned argc
, Value
* vp
) {
1503 CallArgs args
= CallArgsFromVp(argc
, vp
);
1504 return CallNonGenericMethod
<IsPlainTime
, PlainTime_equals
>(cx
, args
);
1508 * Temporal.PlainTime.prototype.toPlainDateTime ( temporalDate )
1510 static bool PlainTime_toPlainDateTime(JSContext
* cx
, const CallArgs
& args
) {
1511 auto* temporalTime
= &args
.thisv().toObject().as
<PlainTimeObject
>();
1512 auto time
= ToPlainTime(temporalTime
);
1515 Rooted
<PlainDateWithCalendar
> plainDate(cx
);
1516 if (!ToTemporalDate(cx
, args
.get(0), &plainDate
)) {
1519 auto date
= plainDate
.date();
1520 auto calendar
= plainDate
.calendar();
1523 auto* result
= CreateTemporalDateTime(cx
, {date
, time
}, calendar
);
1528 args
.rval().setObject(*result
);
1533 * Temporal.PlainTime.prototype.toPlainDateTime ( temporalDate )
1535 static bool PlainTime_toPlainDateTime(JSContext
* cx
, unsigned argc
, Value
* vp
) {
1537 CallArgs args
= CallArgsFromVp(argc
, vp
);
1538 return CallNonGenericMethod
<IsPlainTime
, PlainTime_toPlainDateTime
>(cx
, args
);
1542 * Temporal.PlainTime.prototype.toZonedDateTime ( item )
1544 * |item| is an options object with `plainDate` and `timeZone` properties.
1546 static bool PlainTime_toZonedDateTime(JSContext
* cx
, const CallArgs
& args
) {
1547 auto* temporalTime
= &args
.thisv().toObject().as
<PlainTimeObject
>();
1548 auto time
= ToPlainTime(temporalTime
);
1551 Rooted
<JSObject
*> itemObj(
1552 cx
, RequireObjectArg(cx
, "item", "toZonedDateTime", args
.get(0)));
1558 Rooted
<Value
> temporalDateLike(cx
);
1559 if (!GetProperty(cx
, itemObj
, args
[0], cx
->names().plainDate
,
1560 &temporalDateLike
)) {
1565 if (temporalDateLike
.isUndefined()) {
1566 JS_ReportErrorNumberASCII(cx
, GetErrorMessage
, nullptr,
1567 JSMSG_TEMPORAL_MISSING_PROPERTY
, "plainDate");
1572 Rooted
<PlainDateWithCalendar
> plainDate(cx
);
1573 if (!ToTemporalDate(cx
, temporalDateLike
, &plainDate
)) {
1576 auto date
= plainDate
.date();
1577 auto calendar
= plainDate
.calendar();
1580 Rooted
<Value
> temporalTimeZoneLike(cx
);
1581 if (!GetProperty(cx
, itemObj
, itemObj
, cx
->names().timeZone
,
1582 &temporalTimeZoneLike
)) {
1587 if (temporalTimeZoneLike
.isUndefined()) {
1588 JS_ReportErrorNumberASCII(cx
, GetErrorMessage
, nullptr,
1589 JSMSG_TEMPORAL_MISSING_PROPERTY
, "timeZone");
1594 Rooted
<TimeZoneValue
> timeZone(cx
);
1595 if (!ToTemporalTimeZone(cx
, temporalTimeZoneLike
, &timeZone
)) {
1600 Rooted
<PlainDateTimeWithCalendar
> temporalDateTime(cx
);
1601 if (!CreateTemporalDateTime(cx
, {date
, time
}, calendar
, &temporalDateTime
)) {
1607 if (!GetInstantFor(cx
, timeZone
, temporalDateTime
,
1608 TemporalDisambiguation::Compatible
, &instant
)) {
1613 auto* result
= CreateTemporalZonedDateTime(cx
, instant
, timeZone
, calendar
);
1618 args
.rval().setObject(*result
);
1623 * Temporal.PlainTime.prototype.toZonedDateTime ( item )
1625 static bool PlainTime_toZonedDateTime(JSContext
* cx
, unsigned argc
, Value
* vp
) {
1627 CallArgs args
= CallArgsFromVp(argc
, vp
);
1628 return CallNonGenericMethod
<IsPlainTime
, PlainTime_toZonedDateTime
>(cx
, args
);
1632 * Temporal.PlainTime.prototype.getISOFields ( )
1634 static bool PlainTime_getISOFields(JSContext
* cx
, const CallArgs
& args
) {
1635 Rooted
<PlainTimeObject
*> temporalTime(
1636 cx
, &args
.thisv().toObject().as
<PlainTimeObject
>());
1637 auto time
= ToPlainTime(temporalTime
);
1640 Rooted
<IdValueVector
> fields(cx
, IdValueVector(cx
));
1643 if (!fields
.emplaceBack(NameToId(cx
->names().isoHour
),
1644 Int32Value(time
.hour
))) {
1649 if (!fields
.emplaceBack(NameToId(cx
->names().isoMicrosecond
),
1650 Int32Value(time
.microsecond
))) {
1655 if (!fields
.emplaceBack(NameToId(cx
->names().isoMillisecond
),
1656 Int32Value(time
.millisecond
))) {
1661 if (!fields
.emplaceBack(NameToId(cx
->names().isoMinute
),
1662 Int32Value(time
.minute
))) {
1667 if (!fields
.emplaceBack(NameToId(cx
->names().isoNanosecond
),
1668 Int32Value(time
.nanosecond
))) {
1673 if (!fields
.emplaceBack(NameToId(cx
->names().isoSecond
),
1674 Int32Value(time
.second
))) {
1679 auto* obj
= NewPlainObjectWithUniqueNames(cx
, fields
);
1684 args
.rval().setObject(*obj
);
1689 * Temporal.PlainTime.prototype.getISOFields ( )
1691 static bool PlainTime_getISOFields(JSContext
* cx
, unsigned argc
, Value
* vp
) {
1693 CallArgs args
= CallArgsFromVp(argc
, vp
);
1694 return CallNonGenericMethod
<IsPlainTime
, PlainTime_getISOFields
>(cx
, args
);
1698 * Temporal.PlainTime.prototype.toString ( [ options ] )
1700 static bool PlainTime_toString(JSContext
* cx
, const CallArgs
& args
) {
1701 auto* temporalTime
= &args
.thisv().toObject().as
<PlainTimeObject
>();
1702 auto time
= ToPlainTime(temporalTime
);
1704 SecondsStringPrecision precision
= {Precision::Auto(),
1705 TemporalUnit::Nanosecond
, Increment
{1}};
1706 auto roundingMode
= TemporalRoundingMode::Trunc
;
1707 if (args
.hasDefined(0)) {
1709 Rooted
<JSObject
*> options(
1710 cx
, RequireObjectArg(cx
, "options", "toString", args
[0]));
1716 auto digits
= Precision::Auto();
1717 if (!GetTemporalFractionalSecondDigitsOption(cx
, options
, &digits
)) {
1722 if (!GetRoundingModeOption(cx
, options
, &roundingMode
)) {
1727 auto smallestUnit
= TemporalUnit::Auto
;
1728 if (!GetTemporalUnitValuedOption(cx
, options
, TemporalUnitKey::SmallestUnit
,
1729 TemporalUnitGroup::Time
, &smallestUnit
)) {
1734 if (smallestUnit
== TemporalUnit::Hour
) {
1735 JS_ReportErrorNumberASCII(cx
, GetErrorMessage
, nullptr,
1736 JSMSG_TEMPORAL_INVALID_UNIT_OPTION
, "hour",
1742 precision
= ToSecondsStringPrecision(smallestUnit
, digits
);
1747 RoundTime(time
, precision
.increment
, precision
.unit
, roundingMode
);
1751 TemporalTimeToString(cx
, roundedTime
.time
, precision
.precision
);
1756 args
.rval().setString(str
);
1761 * Temporal.PlainTime.prototype.toString ( [ options ] )
1763 static bool PlainTime_toString(JSContext
* cx
, unsigned argc
, Value
* vp
) {
1765 CallArgs args
= CallArgsFromVp(argc
, vp
);
1766 return CallNonGenericMethod
<IsPlainTime
, PlainTime_toString
>(cx
, args
);
1770 * Temporal.PlainTime.prototype.toLocaleString ( [ locales [ , options ] ] )
1772 static bool PlainTime_toLocaleString(JSContext
* cx
, const CallArgs
& args
) {
1773 auto* temporalTime
= &args
.thisv().toObject().as
<PlainTimeObject
>();
1774 auto time
= ToPlainTime(temporalTime
);
1777 JSString
* str
= TemporalTimeToString(cx
, time
, Precision::Auto());
1782 args
.rval().setString(str
);
1787 * Temporal.PlainTime.prototype.toLocaleString ( [ locales [ , options ] ] )
1789 static bool PlainTime_toLocaleString(JSContext
* cx
, unsigned argc
, Value
* vp
) {
1791 CallArgs args
= CallArgsFromVp(argc
, vp
);
1792 return CallNonGenericMethod
<IsPlainTime
, PlainTime_toLocaleString
>(cx
, args
);
1796 * Temporal.PlainTime.prototype.toJSON ( )
1798 static bool PlainTime_toJSON(JSContext
* cx
, const CallArgs
& args
) {
1799 auto* temporalTime
= &args
.thisv().toObject().as
<PlainTimeObject
>();
1800 auto time
= ToPlainTime(temporalTime
);
1803 JSString
* str
= TemporalTimeToString(cx
, time
, Precision::Auto());
1808 args
.rval().setString(str
);
1813 * Temporal.PlainTime.prototype.toJSON ( )
1815 static bool PlainTime_toJSON(JSContext
* cx
, unsigned argc
, Value
* vp
) {
1817 CallArgs args
= CallArgsFromVp(argc
, vp
);
1818 return CallNonGenericMethod
<IsPlainTime
, PlainTime_toJSON
>(cx
, args
);
1822 * Temporal.PlainTime.prototype.valueOf ( )
1824 static bool PlainTime_valueOf(JSContext
* cx
, unsigned argc
, Value
* vp
) {
1825 JS_ReportErrorNumberASCII(cx
, GetErrorMessage
, nullptr, JSMSG_CANT_CONVERT_TO
,
1826 "PlainTime", "primitive type");
1830 const JSClass
PlainTimeObject::class_
= {
1831 "Temporal.PlainTime",
1832 JSCLASS_HAS_RESERVED_SLOTS(PlainTimeObject::SLOT_COUNT
) |
1833 JSCLASS_HAS_CACHED_PROTO(JSProto_PlainTime
),
1835 &PlainTimeObject::classSpec_
,
1838 const JSClass
& PlainTimeObject::protoClass_
= PlainObject::class_
;
1840 static const JSFunctionSpec PlainTime_methods
[] = {
1841 JS_FN("from", PlainTime_from
, 1, 0),
1842 JS_FN("compare", PlainTime_compare
, 2, 0),
1846 static const JSFunctionSpec PlainTime_prototype_methods
[] = {
1847 JS_FN("add", PlainTime_add
, 1, 0),
1848 JS_FN("subtract", PlainTime_subtract
, 1, 0),
1849 JS_FN("with", PlainTime_with
, 1, 0),
1850 JS_FN("until", PlainTime_until
, 1, 0),
1851 JS_FN("since", PlainTime_since
, 1, 0),
1852 JS_FN("round", PlainTime_round
, 1, 0),
1853 JS_FN("equals", PlainTime_equals
, 1, 0),
1854 JS_FN("toPlainDateTime", PlainTime_toPlainDateTime
, 1, 0),
1855 JS_FN("toZonedDateTime", PlainTime_toZonedDateTime
, 1, 0),
1856 JS_FN("getISOFields", PlainTime_getISOFields
, 0, 0),
1857 JS_FN("toString", PlainTime_toString
, 0, 0),
1858 JS_FN("toLocaleString", PlainTime_toLocaleString
, 0, 0),
1859 JS_FN("toJSON", PlainTime_toJSON
, 0, 0),
1860 JS_FN("valueOf", PlainTime_valueOf
, 0, 0),
1864 static const JSPropertySpec PlainTime_prototype_properties
[] = {
1865 JS_PSG("hour", PlainTime_hour
, 0),
1866 JS_PSG("minute", PlainTime_minute
, 0),
1867 JS_PSG("second", PlainTime_second
, 0),
1868 JS_PSG("millisecond", PlainTime_millisecond
, 0),
1869 JS_PSG("microsecond", PlainTime_microsecond
, 0),
1870 JS_PSG("nanosecond", PlainTime_nanosecond
, 0),
1871 JS_STRING_SYM_PS(toStringTag
, "Temporal.PlainTime", JSPROP_READONLY
),
1875 const ClassSpec
PlainTimeObject::classSpec_
= {
1876 GenericCreateConstructor
<PlainTimeConstructor
, 0, gc::AllocKind::FUNCTION
>,
1877 GenericCreatePrototype
<PlainTimeObject
>,
1880 PlainTime_prototype_methods
,
1881 PlainTime_prototype_properties
,
1883 ClassSpec::DontDefineConstructor
,