Calendar.h 5.1 KB

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