Calendar.h 5.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. * Copyright (c) 2021-2023, 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. #include <LibJS/Runtime/Temporal/PlainDate.h>
  11. #include <LibJS/Runtime/Temporal/PlainMonthDay.h>
  12. #include <LibJS/Runtime/Temporal/PlainYearMonth.h>
  13. #include <LibJS/Runtime/Value.h>
  14. namespace JS::Temporal {
  15. class Calendar final : public Object {
  16. JS_OBJECT(Calendar, Object);
  17. public:
  18. virtual ~Calendar() override = default;
  19. [[nodiscard]] String const& identifier() const { return m_identifier; }
  20. private:
  21. Calendar(String identifier, Object& prototype);
  22. // 12.5 Properties of Temporal.Calendar Instances, https://tc39.es/proposal-temporal/#sec-properties-of-temporal-calendar-instances
  23. String m_identifier; // [[Identifier]]
  24. };
  25. // 14.2 The Year-Week Record Specification Type, https://tc39.es/proposal-temporal/#sec-year-week-record-specification-type
  26. struct YearWeekRecord {
  27. u8 week { 0 };
  28. i32 year { 0 };
  29. };
  30. bool is_builtin_calendar(StringView identifier);
  31. ReadonlySpan<StringView> available_calendars();
  32. ThrowCompletionOr<Calendar*> create_temporal_calendar(VM&, String const& identifier, FunctionObject const* new_target = nullptr);
  33. ThrowCompletionOr<Calendar*> get_builtin_calendar(VM&, String const& identifier);
  34. Calendar* get_iso8601_calendar(VM&);
  35. ThrowCompletionOr<Vector<String>> calendar_fields(VM&, Object& calendar, Vector<StringView> const& field_names);
  36. ThrowCompletionOr<Object*> calendar_merge_fields(VM&, Object& calendar, Object& fields, Object& additional_fields);
  37. ThrowCompletionOr<PlainDate*> calendar_date_add(VM&, Object& calendar, Value date, Duration&, Object* options = nullptr, FunctionObject* date_add = nullptr);
  38. ThrowCompletionOr<Duration*> calendar_date_until(VM&, Object& calendar, Value one, Value two, Object& options, FunctionObject* date_until = nullptr);
  39. ThrowCompletionOr<double> calendar_year(VM&, Object& calendar, Object& date_like);
  40. ThrowCompletionOr<double> calendar_month(VM&, Object& calendar, Object& date_like);
  41. ThrowCompletionOr<String> calendar_month_code(VM&, Object& calendar, Object& date_like);
  42. ThrowCompletionOr<double> calendar_day(VM&, Object& calendar, Object& date_like);
  43. ThrowCompletionOr<double> calendar_day_of_week(VM&, Object& calendar, Object& date_like);
  44. ThrowCompletionOr<double> calendar_day_of_year(VM&, Object& calendar, Object& date_like);
  45. ThrowCompletionOr<double> calendar_week_of_year(VM&, Object& calendar, Object& date_like);
  46. ThrowCompletionOr<double> calendar_year_of_week(VM&, Object& calendar, Object& date_like);
  47. ThrowCompletionOr<double> calendar_days_in_week(VM&, Object& calendar, Object& date_like);
  48. ThrowCompletionOr<double> calendar_days_in_month(VM&, Object& calendar, Object& date_like);
  49. ThrowCompletionOr<double> calendar_days_in_year(VM&, Object& calendar, Object& date_like);
  50. ThrowCompletionOr<double> calendar_months_in_year(VM&, Object& calendar, Object& date_like);
  51. ThrowCompletionOr<Value> calendar_in_leap_year(VM&, Object& calendar, Object& date_like);
  52. ThrowCompletionOr<Value> calendar_era(VM&, Object& calendar, Object& date_like);
  53. ThrowCompletionOr<Value> calendar_era_year(VM&, Object& calendar, Object& date_like);
  54. ThrowCompletionOr<Object*> to_temporal_calendar(VM&, Value);
  55. ThrowCompletionOr<Object*> to_temporal_calendar_with_iso_default(VM&, Value);
  56. ThrowCompletionOr<Object*> get_temporal_calendar_with_iso_default(VM&, Object&);
  57. ThrowCompletionOr<PlainDate*> calendar_date_from_fields(VM&, Object& calendar, Object const& fields, Object const* options = nullptr);
  58. ThrowCompletionOr<PlainYearMonth*> calendar_year_month_from_fields(VM&, Object& calendar, Object const& fields, Object const* options = nullptr);
  59. ThrowCompletionOr<PlainMonthDay*> calendar_month_day_from_fields(VM&, Object& calendar, Object const& fields, Object const* options = nullptr);
  60. ThrowCompletionOr<String> maybe_format_calendar_annotation(VM&, Object const* calendar_object, StringView show_calendar);
  61. ThrowCompletionOr<String> format_calendar_annotation(VM&, StringView id, StringView show_calendar);
  62. ThrowCompletionOr<bool> calendar_equals(VM&, Object& one, Object& two);
  63. ThrowCompletionOr<Object*> consolidate_calendars(VM&, Object& one, Object& two);
  64. u8 iso_days_in_month(i32 year, u8 month);
  65. YearWeekRecord to_iso_week_of_year(i32 year, u8 month, u8 day);
  66. ThrowCompletionOr<String> iso_month_code(VM&, u8 month);
  67. ThrowCompletionOr<double> resolve_iso_month(VM&, Object const& fields);
  68. ThrowCompletionOr<ISODateRecord> iso_date_from_fields(VM&, Object const& fields, Object const& options);
  69. ThrowCompletionOr<ISOYearMonth> iso_year_month_from_fields(VM&, Object const& fields, Object const& options);
  70. ThrowCompletionOr<ISOMonthDay> iso_month_day_from_fields(VM&, Object const& fields, Object const& options);
  71. ThrowCompletionOr<Object*> default_merge_calendar_fields(VM&, Object const& fields, Object const& additional_fields);
  72. u16 to_iso_day_of_year(i32 year, u8 month, u8 day);
  73. u8 to_iso_day_of_week(i32 year, u8 month, u8 day);
  74. }