TimeZone.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Optional.h>
  8. #include <LibJS/Heap/MarkedVector.h>
  9. #include <LibJS/Runtime/Object.h>
  10. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  11. namespace JS::Temporal {
  12. class TimeZone final : public Object {
  13. JS_OBJECT(TimeZone, Object);
  14. JS_DECLARE_ALLOCATOR(TimeZone);
  15. public:
  16. // Needs to store values in the range -8.64 * 10^13 to 8.64 * 10^13
  17. using OffsetType = double;
  18. virtual ~TimeZone() override = default;
  19. [[nodiscard]] String const& identifier() const { return m_identifier; }
  20. [[nodiscard]] Optional<OffsetType> const& offset_nanoseconds() const { return m_offset_nanoseconds; }
  21. void set_identifier(String identifier) { m_identifier = move(identifier); }
  22. void set_offset_nanoseconds(OffsetType offset_nanoseconds) { m_offset_nanoseconds = offset_nanoseconds; }
  23. private:
  24. explicit TimeZone(Object& prototype);
  25. // 11.5 Properties of Temporal.TimeZone Instances, https://tc39.es/proposal-temporal/#sec-properties-of-temporal-timezone-instances
  26. String m_identifier; // [[Identifier]]
  27. Optional<OffsetType> m_offset_nanoseconds; // [[OffsetNanoseconds]]
  28. };
  29. bool is_available_time_zone_name(StringView time_zone);
  30. ThrowCompletionOr<String> canonicalize_time_zone_name(VM&, StringView time_zone);
  31. ThrowCompletionOr<TimeZone*> create_temporal_time_zone(VM&, StringView identifier, FunctionObject const* new_target = nullptr);
  32. ISODateTime get_iso_parts_from_epoch(VM&, Crypto::SignedBigInteger const& epoch_nanoseconds);
  33. BigInt* get_named_time_zone_next_transition(VM&, StringView time_zone_identifier, BigInt const& epoch_nanoseconds);
  34. BigInt* get_named_time_zone_previous_transition(VM&, StringView time_zone_identifier, BigInt const& epoch_nanoseconds);
  35. ThrowCompletionOr<String> format_time_zone_offset_string(VM&, double offset_nanoseconds);
  36. ThrowCompletionOr<String> format_iso_time_zone_offset_string(VM&, double offset_nanoseconds);
  37. ThrowCompletionOr<Object*> to_temporal_time_zone(VM&, Value temporal_time_zone_like);
  38. ThrowCompletionOr<double> get_offset_nanoseconds_for(VM&, Value time_zone, Instant&);
  39. ThrowCompletionOr<String> builtin_time_zone_get_offset_string_for(VM&, Value time_zone, Instant&);
  40. ThrowCompletionOr<PlainDateTime*> builtin_time_zone_get_plain_date_time_for(VM&, Value time_zone, Instant&, Object& calendar);
  41. ThrowCompletionOr<Instant*> builtin_time_zone_get_instant_for(VM&, Value time_zone, PlainDateTime&, StringView disambiguation);
  42. ThrowCompletionOr<Instant*> disambiguate_possible_instants(VM&, MarkedVector<Instant*> const& possible_instants, Value time_zone, PlainDateTime&, StringView disambiguation);
  43. ThrowCompletionOr<MarkedVector<Instant*>> get_possible_instants_for(VM&, Value time_zone, PlainDateTime&);
  44. ThrowCompletionOr<bool> time_zone_equals(VM&, Object& one, Object& two);
  45. }