PlainDateTime.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2024, Tim Flynn <trflynn89@ladybird.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  9. #include <LibJS/Runtime/Temporal/Instant.h>
  10. #include <LibJS/Runtime/Temporal/PlainDate.h>
  11. #include <LibJS/Runtime/Temporal/PlainDateTime.h>
  12. #include <LibJS/Runtime/Temporal/PlainTime.h>
  13. namespace JS::Temporal {
  14. // 5.5.3 CombineISODateAndTimeRecord ( isoDate, time ), https://tc39.es/proposal-temporal/#sec-temporal-combineisodateandtimerecord
  15. ISODateTime combine_iso_date_and_time_record(ISODate iso_date, Time time)
  16. {
  17. // 1. NOTE: time.[[Days]] is ignored.
  18. // 2. Return ISO Date-Time Record { [[ISODate]]: isoDate, [[Time]]: time }.
  19. return { .iso_date = iso_date, .time = time };
  20. }
  21. // nsMinInstant - nsPerDay
  22. static auto const DATETIME_NANOSECONDS_MIN = "-8640000086400000000000"_sbigint;
  23. // nsMaxInstant + nsPerDay
  24. static auto const DATETIME_NANOSECONDS_MAX = "8640000086400000000000"_sbigint;
  25. // 5.5.4 ISODateTimeWithinLimits ( isoDateTime ), https://tc39.es/proposal-temporal/#sec-temporal-isodatetimewithinlimits
  26. bool iso_date_time_within_limits(ISODateTime iso_date_time)
  27. {
  28. // 1. If abs(ISODateToEpochDays(isoDateTime.[[ISODate]].[[Year]], isoDateTime.[[ISODate]].[[Month]] - 1, isoDateTime.[[ISODate]].[[Day]])) > 10**8 + 1, return false.
  29. if (fabs(iso_date_to_epoch_days(iso_date_time.iso_date.year, iso_date_time.iso_date.month - 1, iso_date_time.iso_date.day)) > 100000001)
  30. return false;
  31. // 2. Let ns be ℝ(GetUTCEpochNanoseconds(isoDateTime)).
  32. auto nanoseconds = get_utc_epoch_nanoseconds(iso_date_time);
  33. // 3. If ns ≤ nsMinInstant - nsPerDay, then
  34. if (nanoseconds <= DATETIME_NANOSECONDS_MIN) {
  35. // a. Return false.
  36. return false;
  37. }
  38. // 4. If ns ≥ nsMaxInstant + nsPerDay, then
  39. if (nanoseconds >= DATETIME_NANOSECONDS_MAX) {
  40. // a. Return false.
  41. return false;
  42. }
  43. // 5. Return true.
  44. return true;
  45. }
  46. // 5.5.7 BalanceISODateTime ( year, month, day, hour, minute, second, millisecond, microsecond, nanosecond ), https://tc39.es/proposal-temporal/#sec-temporal-balanceisodatetime
  47. ISODateTime balance_iso_date_time(double year, double month, double day, double hour, double minute, double second, double millisecond, double microsecond, double nanosecond)
  48. {
  49. // 1. Let balancedTime be BalanceTime(hour, minute, second, millisecond, microsecond, nanosecond).
  50. auto balanced_time = balance_time(hour, minute, second, millisecond, microsecond, nanosecond);
  51. // 2. Let balancedDate be BalanceISODate(year, month, day + balancedTime.[[Days]]).
  52. auto balanced_date = balance_iso_date(year, month, day + balanced_time.days);
  53. // 3. Return CombineISODateAndTimeRecord(balancedDate, balancedTime).
  54. return combine_iso_date_and_time_record(balanced_date, balanced_time);
  55. }
  56. }