Instant.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Optional.h>
  9. #include <AK/Variant.h>
  10. #include <LibJS/Runtime/BigInt.h>
  11. #include <LibJS/Runtime/Completion.h>
  12. #include <LibJS/Runtime/Object.h>
  13. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  14. namespace JS::Temporal {
  15. class Instant final : public Object {
  16. JS_OBJECT(Instant, Object);
  17. JS_DECLARE_ALLOCATOR(Instant);
  18. public:
  19. virtual ~Instant() override = default;
  20. [[nodiscard]] BigInt const& nanoseconds() const { return m_nanoseconds; }
  21. private:
  22. Instant(BigInt const& nanoseconds, Object& prototype);
  23. virtual void visit_edges(Visitor&) override;
  24. // 8.4 Properties of Temporal.Instant Instances, https://tc39.es/proposal-temporal/#sec-properties-of-temporal-instant-instances
  25. NonnullGCPtr<BigInt const> m_nanoseconds; // [[Nanoseconds]]
  26. };
  27. // https://tc39.es/proposal-temporal/#eqn-nsMaxInstant
  28. // nsMaxInstant = 10^8 × nsPerDay = 8.64 × 10^21
  29. static auto const ns_max_instant = "8640000000000000000000"_sbigint;
  30. // https://tc39.es/proposal-temporal/#eqn-nsMinInstant
  31. // nsMinInstant = -nsMaxInstant = -8.64 × 10^21
  32. static auto const ns_min_instant = "-8640000000000000000000"_sbigint;
  33. bool is_valid_epoch_nanoseconds(BigInt const& epoch_nanoseconds);
  34. bool is_valid_epoch_nanoseconds(Crypto::SignedBigInteger const& epoch_nanoseconds);
  35. ThrowCompletionOr<Instant*> create_temporal_instant(VM&, BigInt const& nanoseconds, FunctionObject const* new_target = nullptr);
  36. ThrowCompletionOr<Instant*> to_temporal_instant(VM&, Value item);
  37. ThrowCompletionOr<BigInt*> parse_temporal_instant(VM&, StringView iso_string);
  38. i32 compare_epoch_nanoseconds(BigInt const&, BigInt const&);
  39. ThrowCompletionOr<BigInt*> add_instant(VM&, BigInt const& epoch_nanoseconds, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds);
  40. TimeDurationRecord difference_instant(VM&, BigInt const& nanoseconds1, BigInt const& nanoseconds2, u64 rounding_increment, StringView smallest_unit, StringView largest_unit, StringView rounding_mode);
  41. BigInt* round_temporal_instant(VM&, BigInt const& nanoseconds, u64 increment, StringView unit, StringView rounding_mode);
  42. ThrowCompletionOr<String> temporal_instant_to_string(VM&, Instant&, Value time_zone, Variant<StringView, u8> const& precision);
  43. ThrowCompletionOr<NonnullGCPtr<Duration>> difference_temporal_instant(VM&, DifferenceOperation, Instant const&, Value other, Value options);
  44. ThrowCompletionOr<Instant*> add_duration_to_or_subtract_duration_from_instant(VM&, ArithmeticOperation, Instant const&, Value temporal_duration_like);
  45. }