PlainDate.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <LibJS/Runtime/Completion.h>
  9. #include <LibJS/Runtime/Object.h>
  10. namespace JS::Temporal {
  11. class PlainDate final : public Object {
  12. JS_OBJECT(PlainDate, Object);
  13. public:
  14. PlainDate(i32 iso_year, u8 iso_month, u8 iso_day, Object& calendar, Object& prototype);
  15. virtual ~PlainDate() override = default;
  16. [[nodiscard]] i32 iso_year() const { return m_iso_year; }
  17. [[nodiscard]] u8 iso_month() const { return m_iso_month; }
  18. [[nodiscard]] u8 iso_day() const { return m_iso_day; }
  19. [[nodiscard]] Object const& calendar() const { return m_calendar; }
  20. [[nodiscard]] Object& calendar() { return m_calendar; }
  21. private:
  22. virtual void visit_edges(Visitor&) override;
  23. // 3.4 Properties of Temporal.PlainDate Instances, https://tc39.es/proposal-temporal/#sec-properties-of-temporal-plaindate-instances
  24. i32 m_iso_year { 0 }; // [[ISOYear]]
  25. u8 m_iso_month { 1 }; // [[ISOMonth]]
  26. u8 m_iso_day { 1 }; // [[ISODay]]
  27. Object& m_calendar; // [[Calendar]]
  28. };
  29. struct ISODate {
  30. i32 year;
  31. u8 month;
  32. u8 day;
  33. };
  34. struct DifferenceISODateResult {
  35. double years;
  36. double months;
  37. double weeks;
  38. double days;
  39. };
  40. ThrowCompletionOr<PlainDate*> create_temporal_date(GlobalObject&, i32 iso_year, u8 iso_month, u8 iso_day, Object& calendar, FunctionObject const* new_target = nullptr);
  41. ThrowCompletionOr<PlainDate*> to_temporal_date(GlobalObject&, Value item, Object* options = nullptr);
  42. DifferenceISODateResult difference_iso_date(GlobalObject&, i32 year1, u8 month1, u8 day1, i32 year2, u8 month2, u8 day2, StringView largest_unit);
  43. ThrowCompletionOr<ISODate> regulate_iso_date(GlobalObject&, double year, double month, double day, StringView overflow);
  44. bool is_valid_iso_date(i32 year, u8 month, u8 day);
  45. ISODate balance_iso_date(double year, double month, double day);
  46. String pad_iso_year(i32 y);
  47. ThrowCompletionOr<String> temporal_date_to_string(GlobalObject&, PlainDate&, StringView show_calendar);
  48. ThrowCompletionOr<ISODate> add_iso_date(GlobalObject&, i32 year, u8 month, u8 day, double years, double months, double weeks, double days, StringView overflow);
  49. i8 compare_iso_date(i32 year1, u8 month1, u8 day1, i32 year2, u8 month2, u8 day2);
  50. }