PlainTime.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Optional.h>
  9. #include <LibJS/Runtime/Object.h>
  10. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  11. #include <LibJS/Runtime/Temporal/Duration.h>
  12. #include <LibJS/Runtime/VM.h>
  13. namespace JS::Temporal {
  14. class PlainTime final : public Object {
  15. JS_OBJECT(PlainDateTime, Object);
  16. public:
  17. PlainTime(u8 iso_hour, u8 iso_minute, u8 iso_second, u16 iso_millisecond, u16 iso_microsecond, u16 iso_nanosecond, Calendar& calendar, Object& prototype);
  18. virtual ~PlainTime() override = default;
  19. [[nodiscard]] u8 iso_hour() const { return m_iso_hour; }
  20. [[nodiscard]] u8 iso_minute() const { return m_iso_minute; }
  21. [[nodiscard]] u8 iso_second() const { return m_iso_second; }
  22. [[nodiscard]] u16 iso_millisecond() const { return m_iso_millisecond; }
  23. [[nodiscard]] u16 iso_microsecond() const { return m_iso_microsecond; }
  24. [[nodiscard]] u16 iso_nanosecond() const { return m_iso_nanosecond; }
  25. [[nodiscard]] Calendar const& calendar() const { return m_calendar; }
  26. [[nodiscard]] Calendar& calendar() { return m_calendar; }
  27. private:
  28. virtual void visit_edges(Visitor&) override;
  29. // 4.4 Properties of Temporal.PlainTime Instances, https://tc39.es/proposal-temporal/#sec-properties-of-temporal-plaintime-instances
  30. u8 m_iso_hour { 0 }; // [[ISOHour]]
  31. u8 m_iso_minute { 0 }; // [[ISOMinute]]
  32. u8 m_iso_second { 0 }; // [[ISOSecond]]
  33. u16 m_iso_millisecond { 0 }; // [[ISOMillisecond]]
  34. u16 m_iso_microsecond { 0 }; // [[ISOMicrosecond]]
  35. u16 m_iso_nanosecond { 0 }; // [[ISONanosecond]]
  36. Calendar& m_calendar; // [[Calendar]] (always the built-in ISO 8601 calendar)
  37. };
  38. struct DaysAndTime {
  39. i32 days;
  40. u8 hour;
  41. u8 minute;
  42. u8 second;
  43. u16 millisecond;
  44. u16 microsecond;
  45. u16 nanosecond;
  46. };
  47. struct TemporalTimeLikeRecord {
  48. Optional<double> hour;
  49. Optional<double> minute;
  50. Optional<double> second;
  51. Optional<double> millisecond;
  52. Optional<double> microsecond;
  53. Optional<double> nanosecond;
  54. };
  55. // Table 4: TemporalTimeLike Record Fields, https://tc39.es/proposal-temporal/#table-temporal-temporaltimelike-properties
  56. template<typename StructT, typename ValueT>
  57. struct TemporalTimeLikeRecordField {
  58. ValueT StructT::*field_name { nullptr };
  59. PropertyKey property_name;
  60. };
  61. template<typename StructT, typename ValueT>
  62. auto temporal_time_like_record_fields = [](VM& vm) {
  63. using FieldT = TemporalTimeLikeRecordField<StructT, ValueT>;
  64. return AK::Array {
  65. FieldT { &StructT::hour, vm.names.hour },
  66. FieldT { &StructT::microsecond, vm.names.microsecond },
  67. FieldT { &StructT::millisecond, vm.names.millisecond },
  68. FieldT { &StructT::minute, vm.names.minute },
  69. FieldT { &StructT::nanosecond, vm.names.nanosecond },
  70. FieldT { &StructT::second, vm.names.second },
  71. };
  72. };
  73. enum class ToTemporalTimeRecordCompleteness {
  74. Partial,
  75. Complete,
  76. };
  77. TimeDurationRecord difference_time(GlobalObject&, u8 hour1, u8 minute1, u8 second1, u16 millisecond1, u16 microsecond1, u16 nanosecond1, u8 hour2, u8 minute2, u8 second2, u16 millisecond2, u16 microsecond2, u16 nanosecond2);
  78. ThrowCompletionOr<PlainTime*> to_temporal_time(GlobalObject&, Value item, Optional<StringView> overflow = {});
  79. ThrowCompletionOr<TemporalTime> regulate_time(GlobalObject&, double hour, double minute, double second, double millisecond, double microsecond, double nanosecond, StringView overflow);
  80. bool is_valid_time(double hour, double minute, double second, double millisecond, double microsecond, double nanosecond);
  81. DaysAndTime balance_time(double hour, double minute, double second, double millisecond, double microsecond, double nanosecond);
  82. TemporalTime constrain_time(double hour, double minute, double second, double millisecond, double microsecond, double nanosecond);
  83. ThrowCompletionOr<PlainTime*> create_temporal_time(GlobalObject&, u8 hour, u8 minute, u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, FunctionObject const* new_target = nullptr);
  84. ThrowCompletionOr<TemporalTimeLikeRecord> to_temporal_time_record(GlobalObject&, Object const& temporal_time_like, ToTemporalTimeRecordCompleteness = ToTemporalTimeRecordCompleteness::Complete);
  85. String temporal_time_to_string(u8 hour, u8 minute, u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, Variant<StringView, u8> const& precision);
  86. i8 compare_temporal_time(u8 hour1, u8 minute1, u8 second1, u16 millisecond1, u16 microsecond1, u16 nanosecond1, u8 hour2, u8 minute2, u8 second2, u16 millisecond2, u16 microsecond2, u16 nanosecond2);
  87. DaysAndTime add_time(u8 hour, u8 minute, u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds);
  88. DaysAndTime round_time(u8 hour, u8 minute, u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, u64 increment, StringView unit, StringView rounding_mode, Optional<double> day_length_ns = {});
  89. ThrowCompletionOr<Duration*> difference_temporal_plain_time(GlobalObject&, DifferenceOperation, PlainTime const&, Value other, Value options);
  90. ThrowCompletionOr<PlainTime*> add_duration_to_or_subtract_duration_from_plain_time(GlobalObject&, ArithmeticOperation, PlainTime const&, Value temporal_duration_like);
  91. }