Bug 1901983 - Remove usage of searchForm from ProviderTabToSearch. r=Standard8,mak
[gecko.git] / js / src / builtin / temporal / PlainTime.cpp
blob4202945cacf6af6d3cf75f19ff43c7f6557c76d8
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"
13 #include <algorithm>
14 #include <cmath>
15 #include <cstdlib>
16 #include <type_traits>
17 #include <utility>
19 #include "jsnum.h"
20 #include "jspubtd.h"
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"
41 #include "js/Class.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"
47 #include "js/Value.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"
60 using namespace js;
61 using namespace js::temporal;
63 static inline bool IsPlainTime(Handle<Value> v) {
64 return v.isObject() && v.toObject().is<PlainTimeObject>();
67 #ifdef DEBUG
68 /**
69 * IsValidTime ( hour, minute, second, millisecond, microsecond, nanosecond )
71 template <typename T>
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>);
76 // Step 1.
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));
84 // Step 2.
85 if (hour < 0 || hour > 23) {
86 return false;
89 // Step 3.
90 if (minute < 0 || minute > 59) {
91 return false;
94 // Step 4.
95 if (second < 0 || second > 59) {
96 return false;
99 // Step 5.
100 if (millisecond < 0 || millisecond > 999) {
101 return false;
104 // Step 6.
105 if (microsecond < 0 || microsecond > 999) {
106 return false;
109 // Step 7.
110 if (nanosecond < 0 || nanosecond > 999) {
111 return false;
114 // Step 8.
115 return true;
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] =
123 time;
124 return ::IsValidTime(hour, minute, second, millisecond, microsecond,
125 nanosecond);
129 * IsValidTime ( hour, minute, second, millisecond, microsecond, nanosecond )
131 bool js::temporal::IsValidTime(double hour, double minute, double second,
132 double millisecond, double microsecond,
133 double nanosecond) {
134 return ::IsValidTime(hour, minute, second, millisecond, microsecond,
135 nanosecond);
137 #endif
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) {
159 return true;
161 ReportInvalidTimeValue(cx, name, min, max, num);
162 return false;
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>);
173 // Step 1.
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));
181 // Step 2.
182 if (!ThrowIfInvalidTimeValue(cx, "hour", 0, 23, hour)) {
183 return false;
186 // Step 3.
187 if (!ThrowIfInvalidTimeValue(cx, "minute", 0, 59, minute)) {
188 return false;
191 // Step 4.
192 if (!ThrowIfInvalidTimeValue(cx, "second", 0, 59, second)) {
193 return false;
196 // Step 5.
197 if (!ThrowIfInvalidTimeValue(cx, "millisecond", 0, 999, millisecond)) {
198 return false;
201 // Step 6.
202 if (!ThrowIfInvalidTimeValue(cx, "microsecond", 0, 999, microsecond)) {
203 return false;
206 // Step 7.
207 if (!ThrowIfInvalidTimeValue(cx, "nanosecond", 0, 999, nanosecond)) {
208 return false;
211 // Step 8.
212 return true;
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] =
220 time;
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,
240 double nanosecond) {
241 // Step 1.
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));
249 // Steps 2-8.
250 return {
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,
262 * overflow )
264 bool js::temporal::RegulateTime(JSContext* cx, const TemporalTimeLike& time,
265 TemporalOverflow overflow, PlainTime* result) {
266 const auto& [hour, minute, second, millisecond, microsecond, nanosecond] =
267 time;
269 // Step 1.
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.)
279 // Step 3.
280 if (overflow == TemporalOverflow::Constrain) {
281 *result = ConstrainTime(hour, minute, second, millisecond, microsecond,
282 nanosecond);
283 return true;
286 // Step 4.a.
287 MOZ_ASSERT(overflow == TemporalOverflow::Reject);
289 // Step 4.b.
290 if (!ThrowIfInvalidTime(cx, hour, minute, second, millisecond, microsecond,
291 nanosecond)) {
292 return false;
295 // Step 4.c.
296 *result = {
297 int32_t(hour), int32_t(minute), int32_t(second),
298 int32_t(millisecond), int32_t(microsecond), int32_t(nanosecond),
300 return true;
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,
310 double microsecond,
311 double nanosecond) {
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));
319 // Step 1.
320 if (!ThrowIfInvalidTime(cx, hour, minute, second, millisecond, microsecond,
321 nanosecond)) {
322 return nullptr;
325 // Steps 2-3.
326 Rooted<JSObject*> proto(cx);
327 if (!GetPrototypeFromBuiltinConstructor(cx, args, JSProto_PlainTime,
328 &proto)) {
329 return nullptr;
332 auto* object = NewObjectWithClassProto<PlainTimeObject>(cx, proto);
333 if (!object) {
334 return nullptr;
337 // Step 4.
338 object->setFixedSlot(PlainTimeObject::ISO_HOUR_SLOT,
339 Int32Value(int32_t(hour)));
341 // Step 5.
342 object->setFixedSlot(PlainTimeObject::ISO_MINUTE_SLOT,
343 Int32Value(int32_t(minute)));
345 // Step 6.
346 object->setFixedSlot(PlainTimeObject::ISO_SECOND_SLOT,
347 Int32Value(int32_t(second)));
349 // Step 7.
350 object->setFixedSlot(PlainTimeObject::ISO_MILLISECOND_SLOT,
351 Int32Value(int32_t(millisecond)));
353 // Step 8.
354 object->setFixedSlot(PlainTimeObject::ISO_MICROSECOND_SLOT,
355 Int32Value(int32_t(microsecond)));
357 // Step 9.
358 object->setFixedSlot(PlainTimeObject::ISO_NANOSECOND_SLOT,
359 Int32Value(int32_t(nanosecond)));
361 // Step 10.
362 return object;
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] =
372 time;
374 // Step 1.
375 if (!ThrowIfInvalidTime(cx, time)) {
376 return nullptr;
379 // Steps 2-3.
380 auto* object = NewBuiltinClassInstance<PlainTimeObject>(cx);
381 if (!object) {
382 return nullptr;
385 // Step 4.
386 object->setFixedSlot(PlainTimeObject::ISO_HOUR_SLOT, Int32Value(hour));
388 // Step 5.
389 object->setFixedSlot(PlainTimeObject::ISO_MINUTE_SLOT, Int32Value(minute));
391 // Step 6.
392 object->setFixedSlot(PlainTimeObject::ISO_SECOND_SLOT, Int32Value(second));
394 // Step 7.
395 object->setFixedSlot(PlainTimeObject::ISO_MILLISECOND_SLOT,
396 Int32Value(millisecond));
398 // Step 8.
399 object->setFixedSlot(PlainTimeObject::ISO_MICROSECOND_SLOT,
400 Int32Value(microsecond));
402 // Step 9.
403 object->setFixedSlot(PlainTimeObject::ISO_NANOSECOND_SLOT,
404 Int32Value(nanosecond));
406 // Step 10.
407 return object;
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,
416 IntT nanosecond) {
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;
428 quotient -= 1;
431 return quotient;
434 PlainTime time = {};
436 // Steps 1-2.
437 microsecond += divmod(nanosecond, 1000, &time.nanosecond);
439 // Steps 3-4.
440 millisecond += divmod(microsecond, 1000, &time.microsecond);
442 // Steps 5-6.
443 second += divmod(millisecond, 1000, &time.millisecond);
445 // Steps 7-8.
446 minute += divmod(second, 60, &time.second);
448 // Steps 9-10.
449 hour += divmod(minute, 60, &time.minute);
451 // Steps 11-12.
452 int32_t days = divmod(hour, 24, &time.hour);
454 // Step 13.
455 MOZ_ASSERT(IsValidTime(time));
456 return {days, 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,
473 nanosecond);
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));
497 // Step 1.
498 int32_t hours = time2.hour - time1.hour;
500 // Step 2.
501 int32_t minutes = time2.minute - time1.minute;
503 // Step 3.
504 int32_t seconds = time2.second - time1.second;
506 // Step 4.
507 int32_t milliseconds = time2.millisecond - time1.millisecond;
509 // Step 5.
510 int32_t microseconds = time2.microsecond - time1.microsecond;
512 // Step 6.
513 int32_t nanoseconds = time2.nanosecond - time1.nanosecond;
515 // Step 7.
516 auto result = NormalizeTimeDuration(hours, minutes, seconds, milliseconds,
517 microseconds, nanoseconds);
519 // Step 8.
520 MOZ_ASSERT(result.abs().toNanoseconds() <
521 Int128{ToNanoseconds(TemporalUnit::Day)});
523 // Step 9.
524 return result;
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.)
534 // Steps 3-4.
535 if (item.isObject()) {
536 // Step 3.
537 Rooted<JSObject*> itemObj(cx, &item.toObject());
539 // Step 3.a.
540 if (auto* time = itemObj->maybeUnwrapIf<PlainTimeObject>()) {
541 *result = ToPlainTime(time);
542 return true;
545 // Step 3.b.
546 if (auto* zonedDateTime = itemObj->maybeUnwrapIf<ZonedDateTimeObject>()) {
547 auto epochInstant = ToInstant(zonedDateTime);
548 Rooted<TimeZoneValue> timeZone(cx, zonedDateTime->timeZone());
550 if (!timeZone.wrap(cx)) {
551 return false;
554 // Steps 3.b.i-iii.
555 PlainDateTime dateTime;
556 if (!GetPlainDateTimeFor(cx, timeZone, epochInstant, &dateTime)) {
557 return false;
560 // Step 3.b.iv.
561 *result = dateTime.time;
562 return true;
565 // Step 3.c.
566 if (auto* dateTime = itemObj->maybeUnwrapIf<PlainDateTimeObject>()) {
567 *result = ToPlainTime(dateTime);
568 return true;
571 // Step 3.d.
572 TemporalTimeLike timeResult;
573 if (!ToTemporalTimeRecord(cx, itemObj, &timeResult)) {
574 return false;
577 // Step 3.e.
578 if (!RegulateTime(cx, timeResult, overflow, result)) {
579 return false;
581 } else {
582 // Step 4.
584 // Step 4.a.
585 if (!item.isString()) {
586 ReportValueError(cx, JSMSG_UNEXPECTED_TYPE, JSDVG_IGNORE_STACK, item,
587 nullptr, "not a string");
588 return false;
590 Rooted<JSString*> string(cx, item.toString());
592 // Step 4.b.
593 if (!ParseTemporalTimeString(cx, string, result)) {
594 return false;
597 // Step 4.c.
598 MOZ_ASSERT(IsValidTime(*result));
601 // Step 5.
602 return true;
606 * ToTemporalTime ( item [ , overflow ] )
608 static PlainTimeObject* ToTemporalTime(JSContext* cx, Handle<Value> item,
609 TemporalOverflow overflow) {
610 PlainTime time;
611 if (!ToTemporalTime(cx, item, overflow, &time)) {
612 return nullptr;
614 MOZ_ASSERT(IsValidTime(time));
616 return CreateTemporalTime(cx, time);
620 * ToTemporalTime ( item [ , overflow ] )
622 bool js::temporal::ToTemporalTime(JSContext* cx, Handle<Value> item,
623 PlainTime* result) {
624 return ToTemporalTime(cx, item, TemporalOverflow::Constrain, result);
628 * CompareTemporalTime ( h1, min1, s1, ms1, mus1, ns1, h2, min2, s2, ms2, mus2,
629 * ns2 )
631 int32_t js::temporal::CompareTemporalTime(const PlainTime& one,
632 const PlainTime& two) {
633 // Steps 1-2.
634 if (int32_t diff = one.hour - two.hour) {
635 return diff < 0 ? -1 : 1;
638 // Steps 3-4.
639 if (int32_t diff = one.minute - two.minute) {
640 return diff < 0 ? -1 : 1;
643 // Steps 5-6.
644 if (int32_t diff = one.second - two.second) {
645 return diff < 0 ? -1 : 1;
648 // Steps 7-8.
649 if (int32_t diff = one.millisecond - two.millisecond) {
650 return diff < 0 ? -1 : 1;
653 // Steps 9-10.
654 if (int32_t diff = one.microsecond - two.microsecond) {
655 return diff < 0 ? -1 : 1;
658 // Steps 11-12.
659 if (int32_t diff = one.nanosecond - two.nanosecond) {
660 return diff < 0 ? -1 : 1;
663 // Step 13.
664 return 0;
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.
679 bool any = false;
681 // PrepareTemporalFields, steps 3-4. (Loop unrolled)
682 Rooted<Value> value(cx);
683 auto getTimeProperty = [&](Handle<PropertyName*> property, const char* name,
684 double* num) {
685 // Step 4.a.
686 if (!GetProperty(cx, temporalTimeLike, temporalTimeLike, property,
687 &value)) {
688 return false;
691 // Step 4.b.
692 if (!value.isUndefined()) {
693 // Step 4.b.i.
694 any = true;
696 // Step 4.b.ii.2.
697 if (!ToIntegerWithTruncation(cx, value, name, num)) {
698 return false;
701 return true;
704 if (!getTimeProperty(cx->names().hour, "hour", &result->hour)) {
705 return false;
707 if (!getTimeProperty(cx->names().microsecond, "microsecond",
708 &result->microsecond)) {
709 return false;
711 if (!getTimeProperty(cx->names().millisecond, "millisecond",
712 &result->millisecond)) {
713 return false;
715 if (!getTimeProperty(cx->names().minute, "minute", &result->minute)) {
716 return false;
718 if (!getTimeProperty(cx->names().nanosecond, "nanosecond",
719 &result->nanosecond)) {
720 return false;
722 if (!getTimeProperty(cx->names().second, "second", &result->second)) {
723 return false;
726 // PrepareTemporalFields, step 5.
727 if (!any) {
728 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
729 JSMSG_TEMPORAL_PLAIN_TIME_MISSING_UNIT);
730 return false;
733 // Steps 5-16. (Performed implicitly in our implementation.)
735 // Step 17.
736 return true;
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.)
746 *result = {};
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,
772 TemporalUnit unit,
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});
780 int32_t days = 0;
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.
786 // Steps 1-6.
787 PlainTime quantity;
788 int32_t* result;
789 switch (unit) {
790 case TemporalUnit::Day:
791 quantity = time;
792 result = &days;
793 break;
794 case TemporalUnit::Hour:
795 quantity = time;
796 result = &hour;
797 minute = 0;
798 second = 0;
799 millisecond = 0;
800 microsecond = 0;
801 nanosecond = 0;
802 break;
803 case TemporalUnit::Minute:
804 quantity = {0, minute, second, millisecond, microsecond, nanosecond};
805 result = &minute;
806 second = 0;
807 millisecond = 0;
808 microsecond = 0;
809 nanosecond = 0;
810 break;
811 case TemporalUnit::Second:
812 quantity = {0, 0, second, millisecond, microsecond, nanosecond};
813 result = &second;
814 millisecond = 0;
815 microsecond = 0;
816 nanosecond = 0;
817 break;
818 case TemporalUnit::Millisecond:
819 quantity = {0, 0, 0, millisecond, microsecond, nanosecond};
820 result = &millisecond;
821 microsecond = 0;
822 nanosecond = 0;
823 break;
824 case TemporalUnit::Microsecond:
825 quantity = {0, 0, 0, 0, microsecond, nanosecond};
826 result = &microsecond;
827 nanosecond = 0;
828 break;
829 case TemporalUnit::Nanosecond:
830 quantity = {0, 0, 0, 0, 0, nanosecond};
831 result = &nanosecond;
832 break;
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));
844 // Step 7.
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");
850 // Step 8.
851 int64_t r = RoundNumberToIncrement(quantityNs, incrementNs, roundingMode) /
852 unitLength;
853 MOZ_ASSERT(r == int64_t(int32_t(r)),
854 "can't overflow when inputs are all in range");
856 *result = int32_t(r);
858 // Step 9.
859 if (unit == TemporalUnit::Day) {
860 return {int64_t(days), {0, 0, 0, 0, 0, 0}};
863 // Steps 10-16.
864 auto balanced =
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) {
879 seconds += 1;
880 nanoseconds -= 1'000'000'000;
882 MOZ_ASSERT(std::abs(nanoseconds) <= 999'999'999);
884 // Step 1.
885 int64_t second = time.second + seconds;
887 // Step 2.
888 int32_t nanosecond = time.nanosecond + nanoseconds;
890 // Step 3.
891 auto balanced =
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) {
903 auto temporalTime =
904 ToPlainTime(&args.thisv().toObject().as<PlainTimeObject>());
906 // Step 1. (Not applicable in our implementation.)
908 // Step 2.
909 PlainTime other;
910 if (!ToTemporalTime(cx, args.get(0), &other)) {
911 return false;
914 // Steps 3-4.
915 DifferenceSettings settings;
916 if (args.hasDefined(1)) {
917 Rooted<JSObject*> options(
918 cx, RequireObjectArg(cx, "options", ToName(operation), args[1]));
919 if (!options) {
920 return false;
923 // Step 3.
924 Rooted<PlainObject*> resolvedOptions(cx,
925 SnapshotOwnProperties(cx, options));
926 if (!resolvedOptions) {
927 return false;
930 // Step 4.
931 if (!GetDifferenceSettings(
932 cx, operation, resolvedOptions, TemporalUnitGroup::Time,
933 TemporalUnit::Nanosecond, TemporalUnit::Hour, &settings)) {
934 return false;
936 } else {
937 // Steps 3-4.
938 settings = {
939 TemporalUnit::Nanosecond,
940 TemporalUnit::Hour,
941 TemporalRoundingMode::Trunc,
942 Increment{1},
946 // Step 5.
947 auto diff = DifferenceTime(temporalTime, other);
949 // Step 6.
950 if (settings.smallestUnit != TemporalUnit::Nanosecond ||
951 settings.roundingIncrement != Increment{1}) {
952 // Steps 6.a-b.
953 diff = RoundDuration(diff, settings.roundingIncrement,
954 settings.smallestUnit, settings.roundingMode);
957 // Step 7.
958 TimeDuration balancedDuration;
959 if (!BalanceTimeDuration(cx, diff, settings.largestUnit, &balancedDuration)) {
960 return false;
963 // Step 8.
964 auto duration = balancedDuration.toDuration();
965 if (operation == TemporalDifference::Since) {
966 duration = duration.negate();
969 auto* result = CreateTemporalDuration(cx, duration);
970 if (!result) {
971 return false;
974 args.rval().setObject(*result);
975 return true;
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.)
991 // Step 2.
992 Duration duration;
993 if (!ToTemporalDurationRecord(cx, args.get(0), &duration)) {
994 return false;
997 // Step 3.
998 if (operation == PlainTimeDuration::Subtract) {
999 duration = duration.negate();
1001 auto timeDuration = NormalizeTimeDuration(duration);
1003 // Step 4.
1004 auto result = AddTime(time, timeDuration);
1005 MOZ_ASSERT(IsValidTime(result.time));
1007 // Step 5.
1008 auto* obj = CreateTemporalTime(cx, result.time);
1009 if (!obj) {
1010 return false;
1013 args.rval().setObject(*obj);
1014 return true;
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);
1024 // Step 1.
1025 if (!ThrowIfNotConstructing(cx, args, "Temporal.PlainTime")) {
1026 return false;
1029 // Step 2.
1030 double hour = 0;
1031 if (args.hasDefined(0)) {
1032 if (!ToIntegerWithTruncation(cx, args[0], "hour", &hour)) {
1033 return false;
1037 // Step 3.
1038 double minute = 0;
1039 if (args.hasDefined(1)) {
1040 if (!ToIntegerWithTruncation(cx, args[1], "minute", &minute)) {
1041 return false;
1045 // Step 4.
1046 double second = 0;
1047 if (args.hasDefined(2)) {
1048 if (!ToIntegerWithTruncation(cx, args[2], "second", &second)) {
1049 return false;
1053 // Step 5.
1054 double millisecond = 0;
1055 if (args.hasDefined(3)) {
1056 if (!ToIntegerWithTruncation(cx, args[3], "millisecond", &millisecond)) {
1057 return false;
1061 // Step 6.
1062 double microsecond = 0;
1063 if (args.hasDefined(4)) {
1064 if (!ToIntegerWithTruncation(cx, args[4], "microsecond", &microsecond)) {
1065 return false;
1069 // Step 7.
1070 double nanosecond = 0;
1071 if (args.hasDefined(5)) {
1072 if (!ToIntegerWithTruncation(cx, args[5], "nanosecond", &nanosecond)) {
1073 return false;
1077 // Step 8.
1078 auto* temporalTime = CreateTemporalTime(cx, args, hour, minute, second,
1079 millisecond, microsecond, nanosecond);
1080 if (!temporalTime) {
1081 return false;
1084 args.rval().setObject(*temporalTime);
1085 return true;
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)) {
1098 // Step 2.
1099 Rooted<JSObject*> options(cx,
1100 RequireObjectArg(cx, "options", "from", args[1]));
1101 if (!options) {
1102 return false;
1105 // Step 3.
1106 if (!GetTemporalOverflowOption(cx, options, &overflow)) {
1107 return false;
1111 // Steps 4-5.
1112 auto* result = ToTemporalTime(cx, args.get(0), overflow);
1113 if (!result) {
1114 return false;
1117 args.rval().setObject(*result);
1118 return true;
1122 * Temporal.PlainTime.compare ( one, two )
1124 static bool PlainTime_compare(JSContext* cx, unsigned argc, Value* vp) {
1125 CallArgs args = CallArgsFromVp(argc, vp);
1127 // Step 1.
1128 PlainTime one;
1129 if (!ToTemporalTime(cx, args.get(0), &one)) {
1130 return false;
1133 // Step 2.
1134 PlainTime two;
1135 if (!ToTemporalTime(cx, args.get(1), &two)) {
1136 return false;
1139 // Step 3.
1140 args.rval().setInt32(CompareTemporalTime(one, two));
1141 return true;
1145 * get Temporal.PlainTime.prototype.hour
1147 static bool PlainTime_hour(JSContext* cx, const CallArgs& args) {
1148 // Step 3.
1149 auto* temporalTime = &args.thisv().toObject().as<PlainTimeObject>();
1150 args.rval().setInt32(temporalTime->isoHour());
1151 return true;
1155 * get Temporal.PlainTime.prototype.hour
1157 static bool PlainTime_hour(JSContext* cx, unsigned argc, Value* vp) {
1158 // Steps 1-2.
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) {
1167 // Step 3.
1168 auto* temporalTime = &args.thisv().toObject().as<PlainTimeObject>();
1169 args.rval().setInt32(temporalTime->isoMinute());
1170 return true;
1174 * get Temporal.PlainTime.prototype.minute
1176 static bool PlainTime_minute(JSContext* cx, unsigned argc, Value* vp) {
1177 // Steps 1-2.
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) {
1186 // Step 3.
1187 auto* temporalTime = &args.thisv().toObject().as<PlainTimeObject>();
1188 args.rval().setInt32(temporalTime->isoSecond());
1189 return true;
1193 * get Temporal.PlainTime.prototype.second
1195 static bool PlainTime_second(JSContext* cx, unsigned argc, Value* vp) {
1196 // Steps 1-2.
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) {
1205 // Step 3.
1206 auto* temporalTime = &args.thisv().toObject().as<PlainTimeObject>();
1207 args.rval().setInt32(temporalTime->isoMillisecond());
1208 return true;
1212 * get Temporal.PlainTime.prototype.millisecond
1214 static bool PlainTime_millisecond(JSContext* cx, unsigned argc, Value* vp) {
1215 // Steps 1-2.
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) {
1224 // Step 3.
1225 auto* temporalTime = &args.thisv().toObject().as<PlainTimeObject>();
1226 args.rval().setInt32(temporalTime->isoMicrosecond());
1227 return true;
1231 * get Temporal.PlainTime.prototype.microsecond
1233 static bool PlainTime_microsecond(JSContext* cx, unsigned argc, Value* vp) {
1234 // Steps 1-2.
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) {
1243 // Step 3.
1244 auto* temporalTime = &args.thisv().toObject().as<PlainTimeObject>();
1245 args.rval().setInt32(temporalTime->isoNanosecond());
1246 return true;
1250 * get Temporal.PlainTime.prototype.nanosecond
1252 static bool PlainTime_nanosecond(JSContext* cx, unsigned argc, Value* vp) {
1253 // Steps 1-2.
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) {
1262 // Step 3.
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) {
1271 // Steps 1-2.
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) {
1280 // Step 3.
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) {
1289 // Steps 1-2.
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);
1301 // Step 3.
1302 Rooted<JSObject*> temporalTimeLike(
1303 cx, RequireObjectArg(cx, "temporalTimeLike", "with", args.get(0)));
1304 if (!temporalTimeLike) {
1305 return false;
1307 if (!ThrowIfTemporalLikeObject(cx, temporalTimeLike)) {
1308 return false;
1311 auto overflow = TemporalOverflow::Constrain;
1312 if (args.hasDefined(1)) {
1313 // Step 4.
1314 Rooted<JSObject*> options(cx,
1315 RequireObjectArg(cx, "options", "with", args[1]));
1316 if (!options) {
1317 return false;
1320 // Step 5.
1321 if (!GetTemporalOverflowOption(cx, options, &overflow)) {
1322 return false;
1326 // Steps 6-18.
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)) {
1333 return false;
1336 // Step 19.
1337 PlainTime result;
1338 if (!RegulateTime(cx, partialTime, overflow, &result)) {
1339 return false;
1342 // Step 20.
1343 auto* obj = CreateTemporalTime(cx, result);
1344 if (!obj) {
1345 return false;
1348 args.rval().setObject(*obj);
1349 return true;
1353 * Temporal.PlainTime.prototype.with ( temporalTimeLike [ , options ] )
1355 static bool PlainTime_with(JSContext* cx, unsigned argc, Value* vp) {
1356 // Steps 1-2.
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) {
1365 // Step 3.
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) {
1373 // Steps 1-2.
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) {
1382 // Step 3.
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) {
1390 // Steps 1-2.
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);
1402 // Steps 3-12.
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.)
1409 // Step 9.
1410 Rooted<JSString*> paramString(cx, args[0].toString());
1411 if (!GetTemporalUnitValuedOption(cx, paramString,
1412 TemporalUnitKey::SmallestUnit,
1413 TemporalUnitGroup::Time, &smallestUnit)) {
1414 return false;
1417 // Steps 6-8 and 10-12. (Implicit)
1418 } else {
1419 // Steps 3 and 5.
1420 Rooted<JSObject*> options(
1421 cx, RequireObjectArg(cx, "roundTo", "round", args.get(0)));
1422 if (!options) {
1423 return false;
1426 // Steps 6-7.
1427 if (!GetRoundingIncrementOption(cx, options, &roundingIncrement)) {
1428 return false;
1431 // Step 8.
1432 if (!GetRoundingModeOption(cx, options, &roundingMode)) {
1433 return false;
1436 // Step 9.
1437 if (!GetTemporalUnitValuedOption(cx, options, TemporalUnitKey::SmallestUnit,
1438 TemporalUnitGroup::Time, &smallestUnit)) {
1439 return false;
1442 if (smallestUnit == TemporalUnit::Auto) {
1443 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
1444 JSMSG_TEMPORAL_MISSING_OPTION, "smallestUnit");
1445 return false;
1448 // Steps 10-11.
1449 auto maximum = MaximumTemporalDurationRoundingIncrement(smallestUnit);
1451 // Step 12.
1452 if (!ValidateTemporalRoundingIncrement(cx, roundingIncrement, maximum,
1453 false)) {
1454 return false;
1458 // Step 13.
1459 auto result = RoundTime(time, roundingIncrement, smallestUnit, roundingMode);
1461 // Step 14.
1462 auto* obj = CreateTemporalTime(cx, result.time);
1463 if (!obj) {
1464 return false;
1467 args.rval().setObject(*obj);
1468 return true;
1472 * Temporal.PlainTime.prototype.round ( roundTo )
1474 static bool PlainTime_round(JSContext* cx, unsigned argc, Value* vp) {
1475 // Steps 1-2.
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) {
1484 auto temporalTime =
1485 ToPlainTime(&args.thisv().toObject().as<PlainTimeObject>());
1487 // Step 3.
1488 PlainTime other;
1489 if (!ToTemporalTime(cx, args.get(0), &other)) {
1490 return false;
1493 // Steps 4-10.
1494 args.rval().setBoolean(temporalTime == other);
1495 return true;
1499 * Temporal.PlainTime.prototype.equals ( other )
1501 static bool PlainTime_equals(JSContext* cx, unsigned argc, Value* vp) {
1502 // Steps 1-2.
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);
1514 // Step 3.
1515 Rooted<PlainDateWithCalendar> plainDate(cx);
1516 if (!ToTemporalDate(cx, args.get(0), &plainDate)) {
1517 return false;
1519 auto date = plainDate.date();
1520 auto calendar = plainDate.calendar();
1522 // Step 4.
1523 auto* result = CreateTemporalDateTime(cx, {date, time}, calendar);
1524 if (!result) {
1525 return false;
1528 args.rval().setObject(*result);
1529 return true;
1533 * Temporal.PlainTime.prototype.toPlainDateTime ( temporalDate )
1535 static bool PlainTime_toPlainDateTime(JSContext* cx, unsigned argc, Value* vp) {
1536 // Steps 1-2.
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);
1550 // Step 3.
1551 Rooted<JSObject*> itemObj(
1552 cx, RequireObjectArg(cx, "item", "toZonedDateTime", args.get(0)));
1553 if (!itemObj) {
1554 return false;
1557 // Step 4.
1558 Rooted<Value> temporalDateLike(cx);
1559 if (!GetProperty(cx, itemObj, args[0], cx->names().plainDate,
1560 &temporalDateLike)) {
1561 return false;
1564 // Step 5.
1565 if (temporalDateLike.isUndefined()) {
1566 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
1567 JSMSG_TEMPORAL_MISSING_PROPERTY, "plainDate");
1568 return false;
1571 // Step 6.
1572 Rooted<PlainDateWithCalendar> plainDate(cx);
1573 if (!ToTemporalDate(cx, temporalDateLike, &plainDate)) {
1574 return false;
1576 auto date = plainDate.date();
1577 auto calendar = plainDate.calendar();
1579 // Step 7.
1580 Rooted<Value> temporalTimeZoneLike(cx);
1581 if (!GetProperty(cx, itemObj, itemObj, cx->names().timeZone,
1582 &temporalTimeZoneLike)) {
1583 return false;
1586 // Step 8.
1587 if (temporalTimeZoneLike.isUndefined()) {
1588 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
1589 JSMSG_TEMPORAL_MISSING_PROPERTY, "timeZone");
1590 return false;
1593 // Step 9.
1594 Rooted<TimeZoneValue> timeZone(cx);
1595 if (!ToTemporalTimeZone(cx, temporalTimeZoneLike, &timeZone)) {
1596 return false;
1599 // Step 10.
1600 Rooted<PlainDateTimeWithCalendar> temporalDateTime(cx);
1601 if (!CreateTemporalDateTime(cx, {date, time}, calendar, &temporalDateTime)) {
1602 return false;
1605 // Steps 11-12.
1606 Instant instant;
1607 if (!GetInstantFor(cx, timeZone, temporalDateTime,
1608 TemporalDisambiguation::Compatible, &instant)) {
1609 return false;
1612 // Step 13.
1613 auto* result = CreateTemporalZonedDateTime(cx, instant, timeZone, calendar);
1614 if (!result) {
1615 return false;
1618 args.rval().setObject(*result);
1619 return true;
1623 * Temporal.PlainTime.prototype.toZonedDateTime ( item )
1625 static bool PlainTime_toZonedDateTime(JSContext* cx, unsigned argc, Value* vp) {
1626 // Steps 1-2.
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);
1639 // Step 3.
1640 Rooted<IdValueVector> fields(cx, IdValueVector(cx));
1642 // Step 4.
1643 if (!fields.emplaceBack(NameToId(cx->names().isoHour),
1644 Int32Value(time.hour))) {
1645 return false;
1648 // Step 5.
1649 if (!fields.emplaceBack(NameToId(cx->names().isoMicrosecond),
1650 Int32Value(time.microsecond))) {
1651 return false;
1654 // Step 6.
1655 if (!fields.emplaceBack(NameToId(cx->names().isoMillisecond),
1656 Int32Value(time.millisecond))) {
1657 return false;
1660 // Step 7.
1661 if (!fields.emplaceBack(NameToId(cx->names().isoMinute),
1662 Int32Value(time.minute))) {
1663 return false;
1666 // Step 8.
1667 if (!fields.emplaceBack(NameToId(cx->names().isoNanosecond),
1668 Int32Value(time.nanosecond))) {
1669 return false;
1672 // Step 9.
1673 if (!fields.emplaceBack(NameToId(cx->names().isoSecond),
1674 Int32Value(time.second))) {
1675 return false;
1678 // Step 10.
1679 auto* obj = NewPlainObjectWithUniqueNames(cx, fields);
1680 if (!obj) {
1681 return false;
1684 args.rval().setObject(*obj);
1685 return true;
1689 * Temporal.PlainTime.prototype.getISOFields ( )
1691 static bool PlainTime_getISOFields(JSContext* cx, unsigned argc, Value* vp) {
1692 // Steps 1-2.
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)) {
1708 // Step 3.
1709 Rooted<JSObject*> options(
1710 cx, RequireObjectArg(cx, "options", "toString", args[0]));
1711 if (!options) {
1712 return false;
1715 // Steps 4-5.
1716 auto digits = Precision::Auto();
1717 if (!GetTemporalFractionalSecondDigitsOption(cx, options, &digits)) {
1718 return false;
1721 // Step 6.
1722 if (!GetRoundingModeOption(cx, options, &roundingMode)) {
1723 return false;
1726 // Step 7.
1727 auto smallestUnit = TemporalUnit::Auto;
1728 if (!GetTemporalUnitValuedOption(cx, options, TemporalUnitKey::SmallestUnit,
1729 TemporalUnitGroup::Time, &smallestUnit)) {
1730 return false;
1733 // Step 8.
1734 if (smallestUnit == TemporalUnit::Hour) {
1735 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
1736 JSMSG_TEMPORAL_INVALID_UNIT_OPTION, "hour",
1737 "smallestUnit");
1738 return false;
1741 // Step 9.
1742 precision = ToSecondsStringPrecision(smallestUnit, digits);
1745 // Step 10.
1746 auto roundedTime =
1747 RoundTime(time, precision.increment, precision.unit, roundingMode);
1749 // Step 11.
1750 JSString* str =
1751 TemporalTimeToString(cx, roundedTime.time, precision.precision);
1752 if (!str) {
1753 return false;
1756 args.rval().setString(str);
1757 return true;
1761 * Temporal.PlainTime.prototype.toString ( [ options ] )
1763 static bool PlainTime_toString(JSContext* cx, unsigned argc, Value* vp) {
1764 // Steps 1-2.
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);
1776 // Step 3.
1777 JSString* str = TemporalTimeToString(cx, time, Precision::Auto());
1778 if (!str) {
1779 return false;
1782 args.rval().setString(str);
1783 return true;
1787 * Temporal.PlainTime.prototype.toLocaleString ( [ locales [ , options ] ] )
1789 static bool PlainTime_toLocaleString(JSContext* cx, unsigned argc, Value* vp) {
1790 // Steps 1-2.
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);
1802 // Step 3.
1803 JSString* str = TemporalTimeToString(cx, time, Precision::Auto());
1804 if (!str) {
1805 return false;
1808 args.rval().setString(str);
1809 return true;
1813 * Temporal.PlainTime.prototype.toJSON ( )
1815 static bool PlainTime_toJSON(JSContext* cx, unsigned argc, Value* vp) {
1816 // Steps 1-2.
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");
1827 return false;
1830 const JSClass PlainTimeObject::class_ = {
1831 "Temporal.PlainTime",
1832 JSCLASS_HAS_RESERVED_SLOTS(PlainTimeObject::SLOT_COUNT) |
1833 JSCLASS_HAS_CACHED_PROTO(JSProto_PlainTime),
1834 JS_NULL_CLASS_OPS,
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),
1843 JS_FS_END,
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),
1861 JS_FS_END,
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),
1872 JS_PS_END,
1875 const ClassSpec PlainTimeObject::classSpec_ = {
1876 GenericCreateConstructor<PlainTimeConstructor, 0, gc::AllocKind::FUNCTION>,
1877 GenericCreatePrototype<PlainTimeObject>,
1878 PlainTime_methods,
1879 nullptr,
1880 PlainTime_prototype_methods,
1881 PlainTime_prototype_properties,
1882 nullptr,
1883 ClassSpec::DontDefineConstructor,