summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/Temporal/Instant.cpp
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-08-07 00:31:08 +0100
committerLinus Groh <mail@linusgroh.de>2021-08-07 13:10:35 +0100
commitb38f1fb0718cd3d9f58854064b0c538b27aa4a5a (patch)
treefe6ac4cbd6152c7993684fb494802609740a98a2 /Userland/Libraries/LibJS/Runtime/Temporal/Instant.cpp
parent8ffad70504a2d882b28137a7cf6c27571173314b (diff)
downloadserenity-b38f1fb0718cd3d9f58854064b0c538b27aa4a5a.zip
LibJS: Implement Temporal.Instant.prototype.add()
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime/Temporal/Instant.cpp')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/Instant.cpp29
1 files changed, 29 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/Instant.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/Instant.cpp
index 02f88828de..834d3a7cbe 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/Instant.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/Instant.cpp
@@ -160,6 +160,35 @@ i32 compare_epoch_nanoseconds(BigInt const& epoch_nanoseconds_one, BigInt const&
return 0;
}
+// 8.5.6 AddInstant ( epochNanoseconds, hours, minutes, seconds, milliseconds, microseconds, nanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal-addinstant
+BigInt* add_instant(GlobalObject& global_object, BigInt const& epoch_nanoseconds, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds)
+{
+ auto& vm = global_object.vm();
+
+ // 1. Assert: hours, minutes, seconds, milliseconds, microseconds, and nanoseconds are integer Number values.
+ VERIFY(hours == trunc(hours) && minutes == trunc(minutes) && seconds == trunc(seconds) && milliseconds == trunc(milliseconds) && microseconds == trunc(microseconds) && nanoseconds == trunc(nanoseconds));
+
+ // 2. Let result be epochNanoseconds + ℤ(nanoseconds) + ℤ(microseconds) × 1000ℤ + ℤ(milliseconds) × 10^6ℤ + ℤ(seconds) × 10^9ℤ + ℤ(minutes) × 60ℤ × 10^9ℤ + ℤ(hours) × 3600ℤ × 10^9ℤ.
+ // FIXME: Pretty sure i64's are not sufficient for the extreme cases.
+ auto* result = js_bigint(vm,
+ epoch_nanoseconds.big_integer()
+ .plus(Crypto::SignedBigInteger::create_from((i64)nanoseconds))
+ .plus(Crypto::SignedBigInteger::create_from((i64)microseconds).multiplied_by(Crypto::SignedBigInteger { 1'000 }))
+ .plus(Crypto::SignedBigInteger::create_from((i64)milliseconds).multiplied_by(Crypto::SignedBigInteger { 1'000'000 }))
+ .plus(Crypto::SignedBigInteger::create_from((i64)seconds).multiplied_by(Crypto::SignedBigInteger { 1'000'000'000 }))
+ .plus(Crypto::SignedBigInteger::create_from((i64)minutes).multiplied_by(Crypto::SignedBigInteger { 60 }).multiplied_by(Crypto::SignedBigInteger { 1'000'000'000 }))
+ .plus(Crypto::SignedBigInteger::create_from((i64)hours).multiplied_by(Crypto::SignedBigInteger { 3600 }).multiplied_by(Crypto::SignedBigInteger { 1'000'000'000 })));
+
+ // If ! IsValidEpochNanoseconds(result) is false, throw a RangeError exception.
+ if (!is_valid_epoch_nanoseconds(*result)) {
+ vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidEpochNanoseconds);
+ return {};
+ }
+
+ // 4. Return result.
+ return result;
+}
+
// 8.5.8 RoundTemporalInstant ( ns, increment, unit, roundingMode ), https://tc39.es/proposal-temporal/#sec-temporal-roundtemporalinstant
BigInt* round_temporal_instant(GlobalObject& global_object, BigInt const& nanoseconds, u64 increment, String const& unit, String const& rounding_mode)
{