PlainMonthDay.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/Temporal/Calendar.h>
  9. #include <LibJS/Runtime/Temporal/PlainDate.h>
  10. #include <LibJS/Runtime/Temporal/PlainMonthDay.h>
  11. #include <LibJS/Runtime/Temporal/PlainMonthDayConstructor.h>
  12. namespace JS::Temporal {
  13. // 10 Temporal.PlainMonthDay Objects, https://tc39.es/proposal-temporal/#sec-temporal-plainmonthday-objects
  14. PlainMonthDay::PlainMonthDay(u8 iso_month, u8 iso_day, i32 iso_year, Object& calendar, Object& prototype)
  15. : Object(prototype)
  16. , m_iso_year(iso_year)
  17. , m_iso_month(iso_month)
  18. , m_iso_day(iso_day)
  19. , m_calendar(calendar)
  20. {
  21. }
  22. void PlainMonthDay::visit_edges(Visitor& visitor)
  23. {
  24. Base::visit_edges(visitor);
  25. visitor.visit(&m_calendar);
  26. }
  27. // 10.5.2 CreateTemporalMonthDay ( isoMonth, isoDay, calendar, referenceISOYear [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporalmonthday
  28. PlainMonthDay* create_temporal_month_day(GlobalObject& global_object, u8 iso_month, u8 iso_day, Object& calendar, i32 reference_iso_year, FunctionObject const* new_target)
  29. {
  30. auto& vm = global_object.vm();
  31. // 1. Assert: isoMonth, isoDay, and referenceISOYear are integers.
  32. // 2. Assert: Type(calendar) is Object.
  33. // 3. If ! IsValidISODate(referenceISOYear, isoMonth, isoDay) is false, throw a RangeError exception.
  34. if (!is_valid_iso_date(reference_iso_year, iso_month, iso_day)) {
  35. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainMonthDay);
  36. return {};
  37. }
  38. // 4. If newTarget is not present, set it to %Temporal.PlainMonthDay%.
  39. if (!new_target)
  40. new_target = global_object.temporal_plain_month_day_constructor();
  41. // 5. Let object be ? OrdinaryCreateFromConstructor(newTarget, "%Temporal.PlainMonthDay.prototype%", « [[InitializedTemporalMonthDay]], [[ISOMonth]], [[ISODay]], [[ISOYear]], [[Calendar]] »).
  42. // 6. Set object.[[ISOMonth]] to isoMonth.
  43. // 7. Set object.[[ISODay]] to isoDay.
  44. // 8. Set object.[[Calendar]] to calendar.
  45. // 9. Set object.[[ISOYear]] to referenceISOYear.
  46. auto* object = ordinary_create_from_constructor<PlainMonthDay>(global_object, *new_target, &GlobalObject::temporal_plain_month_day_prototype, iso_month, iso_day, reference_iso_year, calendar);
  47. if (vm.exception())
  48. return {};
  49. // 10. Return object.
  50. return object;
  51. }
  52. // 10.5.3 TemporalMonthDayToString ( monthDay, showCalendar ), https://tc39.es/proposal-temporal/#sec-temporal-temporalmonthdaytostring
  53. Optional<String> temporal_month_day_to_string(GlobalObject& global_object, PlainMonthDay& month_day, StringView show_calendar)
  54. {
  55. auto& vm = global_object.vm();
  56. // 1. Assert: Type(monthDay) is Object.
  57. // 2. Assert: monthDay has an [[InitializedTemporalMonthDay]] internal slot.
  58. // 3. Let month be monthDay.[[ISOMonth]] formatted as a two-digit decimal number, padded to the left with a zero if necessary.
  59. // 4. Let day be monthDay.[[ISODay]] formatted as a two-digit decimal number, padded to the left with a zero if necessary.
  60. // 5. Let result be the string-concatenation of month, the code unit 0x002D (HYPHEN-MINUS), and day.
  61. auto result = String::formatted("{:02}-{:02}", month_day.iso_month(), month_day.iso_day());
  62. // 6. Let calendarID be ? ToString(monthDay.[[Calendar]]).
  63. auto calendar_id = Value(&month_day.calendar()).to_string(global_object);
  64. if (vm.exception())
  65. return {};
  66. // 7. If calendarID is not "iso8601", then
  67. if (calendar_id != "iso8601"sv) {
  68. // a. Let year be ! PadISOYear(monthDay.[[ISOYear]]).
  69. // b. Set result to the string-concatenation of year, the code unit 0x002D (HYPHEN-MINUS), and result.
  70. result = String::formatted("{}-{}", pad_iso_year(month_day.iso_year()), result);
  71. }
  72. // 8. Let calendarString be ! FormatCalendarAnnotation(calendarID, showCalendar).
  73. auto calendar_string = format_calendar_annotation(calendar_id, show_calendar);
  74. // 9. Set result to the string-concatenation of result and calendarString.
  75. // 10. Return result.
  76. return String::formatted("{}{}", result, calendar_string);
  77. }
  78. }