Calendar.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2023-2024, Shannon Booth <shannon@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <LibJS/Runtime/Completion.h>
  10. #include <LibJS/Runtime/Object.h>
  11. #include <LibJS/Runtime/Temporal/PlainDate.h>
  12. #include <LibJS/Runtime/Temporal/PlainMonthDay.h>
  13. #include <LibJS/Runtime/Temporal/PlainYearMonth.h>
  14. #include <LibJS/Runtime/Value.h>
  15. namespace JS::Temporal {
  16. class Calendar final : public Object {
  17. JS_OBJECT(Calendar, Object);
  18. JS_DECLARE_ALLOCATOR(Calendar);
  19. public:
  20. virtual ~Calendar() override = default;
  21. [[nodiscard]] String const& identifier() const { return m_identifier; }
  22. private:
  23. Calendar(String identifier, Object& prototype);
  24. // 12.5 Properties of Temporal.Calendar Instances, https://tc39.es/proposal-temporal/#sec-properties-of-temporal-calendar-instances
  25. String m_identifier; // [[Identifier]]
  26. };
  27. // 14.2 The Year-Week Record Specification Type, https://tc39.es/proposal-temporal/#sec-year-week-record-specification-type
  28. struct YearWeekRecord {
  29. u8 week { 0 };
  30. i32 year { 0 };
  31. };
  32. bool is_builtin_calendar(StringView identifier);
  33. ReadonlySpan<StringView> available_calendars();
  34. ThrowCompletionOr<Calendar*> create_temporal_calendar(VM&, String const& identifier, FunctionObject const* new_target = nullptr);
  35. ThrowCompletionOr<Calendar*> get_builtin_calendar(VM&, String const& identifier);
  36. Calendar* get_iso8601_calendar(VM&);
  37. ThrowCompletionOr<Vector<String>> calendar_fields(VM&, Object& calendar, Vector<StringView> const& field_names);
  38. ThrowCompletionOr<Object*> calendar_merge_fields(VM&, Object& calendar, Object& fields, Object& additional_fields);
  39. ThrowCompletionOr<PlainDate*> calendar_date_add(VM&, Object& calendar, Value date, Duration&, Object* options = nullptr, FunctionObject* date_add = nullptr);
  40. ThrowCompletionOr<NonnullGCPtr<Duration>> calendar_date_until(VM&, CalendarMethods const&, Value one, Value two, Object const& options);
  41. ThrowCompletionOr<double> calendar_year(VM&, Object& calendar, Object& date_like);
  42. ThrowCompletionOr<double> calendar_month(VM&, Object& calendar, Object& date_like);
  43. ThrowCompletionOr<String> calendar_month_code(VM&, Object& calendar, Object& date_like);
  44. ThrowCompletionOr<double> calendar_day(VM&, Object& calendar, Object& date_like);
  45. ThrowCompletionOr<double> calendar_day_of_week(VM&, Object& calendar, Object& date_like);
  46. ThrowCompletionOr<double> calendar_day_of_year(VM&, Object& calendar, Object& date_like);
  47. ThrowCompletionOr<double> calendar_week_of_year(VM&, Object& calendar, Object& date_like);
  48. ThrowCompletionOr<double> calendar_year_of_week(VM&, Object& calendar, Object& date_like);
  49. ThrowCompletionOr<double> calendar_days_in_week(VM&, Object& calendar, Object& date_like);
  50. ThrowCompletionOr<double> calendar_days_in_month(VM&, Object& calendar, Object& date_like);
  51. ThrowCompletionOr<double> calendar_days_in_year(VM&, Object& calendar, Object& date_like);
  52. ThrowCompletionOr<double> calendar_months_in_year(VM&, Object& calendar, Object& date_like);
  53. ThrowCompletionOr<Value> calendar_in_leap_year(VM&, Object& calendar, Object& date_like);
  54. ThrowCompletionOr<Value> calendar_era(VM&, Object& calendar, Object& date_like);
  55. ThrowCompletionOr<Value> calendar_era_year(VM&, Object& calendar, Object& date_like);
  56. ThrowCompletionOr<Object*> to_temporal_calendar(VM&, Value);
  57. ThrowCompletionOr<Object*> to_temporal_calendar_with_iso_default(VM&, Value);
  58. ThrowCompletionOr<Object*> get_temporal_calendar_with_iso_default(VM&, Object&);
  59. ThrowCompletionOr<PlainDate*> calendar_date_from_fields(VM&, Object& calendar, Object const& fields, Object const* options = nullptr);
  60. ThrowCompletionOr<PlainYearMonth*> calendar_year_month_from_fields(VM&, Object& calendar, Object const& fields, Object const* options = nullptr);
  61. ThrowCompletionOr<PlainMonthDay*> calendar_month_day_from_fields(VM&, Object& calendar, Object const& fields, Object const* options = nullptr);
  62. ThrowCompletionOr<String> maybe_format_calendar_annotation(VM&, Object const* calendar_object, StringView show_calendar);
  63. ThrowCompletionOr<String> format_calendar_annotation(VM&, StringView id, StringView show_calendar);
  64. ThrowCompletionOr<bool> calendar_equals(VM&, Object& one, Object& two);
  65. ThrowCompletionOr<Object*> consolidate_calendars(VM&, Object& one, Object& two);
  66. u8 iso_days_in_month(i32 year, u8 month);
  67. YearWeekRecord to_iso_week_of_year(i32 year, u8 month, u8 day);
  68. ThrowCompletionOr<String> iso_month_code(VM&, u8 month);
  69. ThrowCompletionOr<double> resolve_iso_month(VM&, Object const& fields);
  70. ThrowCompletionOr<ISODateRecord> iso_date_from_fields(VM&, Object const& fields, Object const& options);
  71. ThrowCompletionOr<ISOYearMonth> iso_year_month_from_fields(VM&, Object const& fields, Object const& options);
  72. ThrowCompletionOr<ISOMonthDay> iso_month_day_from_fields(VM&, Object const& fields, Object const& options);
  73. ThrowCompletionOr<Object*> default_merge_calendar_fields(VM&, Object const& fields, Object const& additional_fields);
  74. u16 to_iso_day_of_year(i32 year, u8 month, u8 day);
  75. u8 to_iso_day_of_week(i32 year, u8 month, u8 day);
  76. // https://tc39.es/proposal-temporal/#table-temporal-calendar-methods-record-fields
  77. struct CalendarMethods {
  78. // The calendar object, or a string indicating a built-in time zone.
  79. Variant<String, NonnullGCPtr<Object>> receiver; // [[Reciever]]
  80. // The calendar's dateAdd method. For a built-in calendar this is always %Temporal.Calendar.prototype.dateAdd%.
  81. GCPtr<FunctionObject> date_add; // [[DateAdd]]
  82. // The calendar's dateFromFields method. For a built-in calendar this is always %Temporal.Calendar.prototype.dateFromFields%.
  83. GCPtr<FunctionObject> date_from_fields; // [[DateFromFields]]
  84. // The calendar's dateUntil method. For a built-in calendar this is always %Temporal.Calendar.prototype.dateUntil%.
  85. GCPtr<FunctionObject> date_until; // [[DateUntil]]
  86. // The calendar's day method. For a built-in calendar this is always %Temporal.Calendar.prototype.day%.
  87. GCPtr<FunctionObject> day; // [[Day]]
  88. // The calendar's fields method. For a built-in calendar this is always %Temporal.Calendar.prototype.fields%.
  89. GCPtr<FunctionObject> fields; // [[Fields]]
  90. // The calendar's mergeFields method. For a built-in calendar this is always %Temporal.Calendar.prototype.mergeFields%.
  91. GCPtr<FunctionObject> merge_fields; // [[MergeFields]]
  92. // The calendar's monthDayFromFields method. For a built-in calendar this is always %Temporal.Calendar.prototype.monthDayFromFields%.
  93. GCPtr<FunctionObject> month_day_from_fields; // [[MonthDayFromFields]]
  94. // The calendar's yearMonthFromFields method. For a built-in calendar this is always %Temporal.Calendar.prototype.yearMonthFromFields%.
  95. GCPtr<FunctionObject> year_month_from_fields; // [[YearMonthFromFields]]
  96. };
  97. #define JS_ENUMERATE_CALENDAR_METHODS \
  98. __JS_ENUMERATE(DateAdd, dateAdd, date_add) \
  99. __JS_ENUMERATE(DateFromFields, dateFromFields, date_from_fields) \
  100. __JS_ENUMERATE(DateUntil, dateUntil, date_until) \
  101. __JS_ENUMERATE(Day, day, day) \
  102. __JS_ENUMERATE(Fields, fields, fields) \
  103. __JS_ENUMERATE(MergeFields, mergeFields, merge_fields) \
  104. __JS_ENUMERATE(MonthDayFromFields, monthDayFromFields, month_day_from_fields) \
  105. __JS_ENUMERATE(YearMonthFromFields, yearMonthFromFields, year_month_from_fields)
  106. enum class CalendarMethod {
  107. #define __JS_ENUMERATE(PascalName, camelName, snake_name) \
  108. PascalName,
  109. JS_ENUMERATE_CALENDAR_METHODS
  110. #undef __JS_ENUMERATE
  111. };
  112. ThrowCompletionOr<void> calendar_methods_record_lookup(VM&, CalendarMethods&, CalendarMethod);
  113. ThrowCompletionOr<CalendarMethods> create_calendar_methods_record(VM&, Variant<String, NonnullGCPtr<Object>> calendar, ReadonlySpan<CalendarMethod>);
  114. ThrowCompletionOr<Optional<CalendarMethods>> create_calendar_methods_record_from_relative_to(VM&, GCPtr<PlainDate>, GCPtr<ZonedDateTime>, ReadonlySpan<CalendarMethod>);
  115. bool calendar_methods_record_has_looked_up(CalendarMethods const&, CalendarMethod);
  116. bool calendar_methods_record_is_builtin(CalendarMethods const&);
  117. ThrowCompletionOr<Value> calendar_methods_record_call(VM&, CalendarMethods const&, CalendarMethod, ReadonlySpan<Value> arguments);
  118. }