Instant.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright (c) 2021-2022, 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. public:
  18. Instant(BigInt const& nanoseconds, Object& prototype);
  19. virtual ~Instant() override = default;
  20. [[nodiscard]] BigInt const& nanoseconds() const { return m_nanoseconds; }
  21. private:
  22. virtual void visit_edges(Visitor&) override;
  23. // 8.4 Properties of Temporal.Instant Instances, https://tc39.es/proposal-temporal/#sec-properties-of-temporal-instant-instances
  24. BigInt const& m_nanoseconds; // [[Nanoseconds]]
  25. };
  26. // -86400 * 10^17
  27. auto const INSTANT_NANOSECONDS_MIN = "-8640000000000000000000"_sbigint;
  28. // +86400 * 10^17
  29. auto const INSTANT_NANOSECONDS_MAX = "8640000000000000000000"_sbigint;
  30. bool is_valid_epoch_nanoseconds(BigInt const& epoch_nanoseconds);
  31. ThrowCompletionOr<Instant*> create_temporal_instant(GlobalObject&, BigInt const& nanoseconds, FunctionObject const* new_target = nullptr);
  32. ThrowCompletionOr<Instant*> to_temporal_instant(GlobalObject&, Value item);
  33. ThrowCompletionOr<BigInt*> parse_temporal_instant(GlobalObject&, String const& iso_string);
  34. i32 compare_epoch_nanoseconds(BigInt const&, BigInt const&);
  35. ThrowCompletionOr<BigInt*> add_instant(GlobalObject&, BigInt const& epoch_nanoseconds, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds);
  36. BigInt* difference_instant(GlobalObject&, BigInt const& nanoseconds1, BigInt const& nanoseconds2, u64 rounding_increment, StringView smallest_unit, StringView rounding_mode);
  37. BigInt* round_temporal_instant(GlobalObject&, BigInt const& nanoseconds, u64 increment, StringView unit, StringView rounding_mode);
  38. ThrowCompletionOr<String> temporal_instant_to_string(GlobalObject&, Instant&, Value time_zone, Variant<StringView, u8> const& precision);
  39. ThrowCompletionOr<Instant*> add_duration_to_or_subtract_duration_from_instant(GlobalObject&, ArithmeticOperation, Instant const&, Value temporal_duration_like);
  40. }