PlainDateTime.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/Date.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/Temporal/PlainDate.h>
  9. #include <LibJS/Runtime/Temporal/PlainDateTime.h>
  10. #include <LibJS/Runtime/Temporal/PlainTime.h>
  11. namespace JS::Temporal {
  12. // 5.5.1 GetEpochFromISOParts ( year, month, day, hour, minute, second, millisecond, microsecond, nanosecond ), https://tc39.es/proposal-temporal/#sec-temporal-getepochfromisoparts
  13. BigInt* get_epoch_from_iso_parts(GlobalObject& global_object, i32 year, i32 month, i32 day, i32 hour, i32 minute, i32 second, i32 millisecond, i32 microsecond, i32 nanosecond)
  14. {
  15. auto& vm = global_object.vm();
  16. // 1. Assert: year, month, day, hour, minute, second, millisecond, microsecond, and nanosecond are integers.
  17. // 2. Assert: ! IsValidISODate(year, month, day) is true.
  18. VERIFY(is_valid_iso_date(year, month, day));
  19. // 3. Assert: ! IsValidTime(hour, minute, second, millisecond, microsecond, nanosecond) is true.
  20. VERIFY(is_valid_time(hour, minute, second, millisecond, microsecond, nanosecond));
  21. // 4. Let date be ! MakeDay(𝔽(year), 𝔽(month − 1), 𝔽(day)).
  22. auto date = make_day(global_object, Value(year), Value(month - 1), Value(day));
  23. // 5. Let time be ! MakeTime(𝔽(hour), 𝔽(minute), 𝔽(second), 𝔽(millisecond)).
  24. auto time = make_time(global_object, Value(hour), Value(minute), Value(second), Value(millisecond));
  25. // 6. Let ms be ! MakeDate(date, time).
  26. auto ms = make_date(date, time);
  27. // 7. Assert: ms is finite.
  28. VERIFY(ms.is_finite_number());
  29. // 8. Return ℝ(ms) × 10^6 + microsecond × 10^3 + nanosecond.
  30. return js_bigint(vm.heap(), Crypto::SignedBigInteger::create_from(static_cast<i64>(ms.as_double())).multiplied_by(Crypto::UnsignedBigInteger { 1'000'000 }).plus(Crypto::SignedBigInteger::create_from((i64)microsecond * 1000)).plus(Crypto::SignedBigInteger(nanosecond)));
  31. }
  32. }