PlainDate.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. #include <LibJS/Runtime/AbstractOperations.h>
  8. #include <LibJS/Runtime/GlobalObject.h>
  9. #include <LibJS/Runtime/Temporal/Calendar.h>
  10. #include <LibJS/Runtime/Temporal/PlainDate.h>
  11. #include <LibJS/Runtime/Temporal/PlainDateConstructor.h>
  12. #include <LibJS/Runtime/Temporal/PlainDateTime.h>
  13. #include <LibJS/Runtime/Temporal/PlainYearMonth.h>
  14. namespace JS::Temporal {
  15. // 3 Temporal.PlainDate Objects, https://tc39.es/proposal-temporal/#sec-temporal-plaindate-objects
  16. PlainDate::PlainDate(i32 year, u8 month, u8 day, Object& calendar, Object& prototype)
  17. : Object(prototype)
  18. , m_iso_year(year)
  19. , m_iso_month(month)
  20. , m_iso_day(day)
  21. , m_calendar(calendar)
  22. {
  23. }
  24. void PlainDate::visit_edges(Visitor& visitor)
  25. {
  26. visitor.visit(&m_calendar);
  27. }
  28. // 3.5.1 CreateTemporalDate ( isoYear, isoMonth, isoDay, calendar [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporaldate
  29. PlainDate* create_temporal_date(GlobalObject& global_object, i32 iso_year, u8 iso_month, u8 iso_day, Object& calendar, FunctionObject* new_target)
  30. {
  31. auto& vm = global_object.vm();
  32. // 1. Assert: isoYear is an integer.
  33. // 2. Assert: isoMonth is an integer.
  34. // 3. Assert: isoDay is an integer.
  35. // 4. Assert: Type(calendar) is Object.
  36. // 5. If ! IsValidISODate(isoYear, isoMonth, isoDay) is false, throw a RangeError exception.
  37. if (!is_valid_iso_date(iso_year, iso_month, iso_day)) {
  38. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
  39. return {};
  40. }
  41. // 6. If ! ISODateTimeWithinLimits(isoYear, isoMonth, isoDay, 12, 0, 0, 0, 0, 0) is false, throw a RangeError exception.
  42. if (!iso_date_time_within_limits(global_object, iso_year, iso_month, iso_day, 12, 0, 0, 0, 0, 0)) {
  43. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
  44. return {};
  45. }
  46. // 7. If newTarget is not present, set it to %Temporal.PlainDate%.
  47. if (!new_target)
  48. new_target = global_object.temporal_plain_date_constructor();
  49. // 8. Let object be ? OrdinaryCreateFromConstructor(newTarget, "%Temporal.PlainDate.prototype%", « [[InitializedTemporalDate]], [[ISOYear]], [[ISOMonth]], [[ISODay]], [[Calendar]] »).
  50. // 9. Set object.[[ISOYear]] to isoYear.
  51. // 10. Set object.[[ISOMonth]] to isoMonth.
  52. // 11. Set object.[[ISODay]] to isoDay.
  53. // 12. Set object.[[Calendar]] to calendar.
  54. auto* object = ordinary_create_from_constructor<PlainDate>(global_object, *new_target, &GlobalObject::temporal_plain_date_prototype, iso_year, iso_month, iso_day, calendar);
  55. if (vm.exception())
  56. return {};
  57. return object;
  58. }
  59. // 3.5.2 ToTemporalDate ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaldate
  60. PlainDate* to_temporal_date(GlobalObject& global_object, Value item, Object* options)
  61. {
  62. auto& vm = global_object.vm();
  63. // 1. If options is not present, set options to ! OrdinaryObjectCreate(null).
  64. if (!options)
  65. options = Object::create(global_object, nullptr);
  66. // 2. Assert: Type(options) is Object.
  67. // 3. If Type(item) is Object, then
  68. if (item.is_object()) {
  69. auto& item_object = item.as_object();
  70. // a. If item has an [[InitializedTemporalDate]] internal slot, then
  71. if (is<PlainDate>(item_object)) {
  72. // i. Return item.
  73. return static_cast<PlainDate*>(&item_object);
  74. }
  75. // b. If item has an [[InitializedTemporalZonedDateTime]] internal slot, then
  76. // i. Let instant be ! CreateTemporalInstant(item.[[Nanoseconds]]).
  77. // ii. Let plainDateTime be ? BuiltinTimeZoneGetPlainDateTimeFor(item.[[TimeZone]], instant, item.[[Calendar]]).
  78. // iii. Return ! CreateTemporalDate(plainDateTime.[[ISOYear]], plainDateTime.[[ISOMonth]], plainDateTime.[[ISODay]], plainDateTime.[[Calendar]]).
  79. // TODO
  80. // c. If item has an [[InitializedTemporalDateTime]] internal slot, then
  81. if (is<PlainDateTime>(item_object)) {
  82. auto& date_time_item = static_cast<PlainDateTime&>(item_object);
  83. // i. Return ! CreateTemporalDate(item.[[ISOYear]], item.[[ISOMonth]], item.[[ISODay]], item.[[Calendar]]).
  84. return create_temporal_date(global_object, date_time_item.iso_year(), date_time_item.iso_month(), date_time_item.iso_day(), date_time_item.calendar());
  85. }
  86. // d. Let calendar be ? GetTemporalCalendarWithISODefault(item).
  87. auto* calendar = get_temporal_calendar_with_iso_default(global_object, item_object);
  88. if (vm.exception())
  89. return {};
  90. // e. Let fieldNames be ? CalendarFields(calendar, « "day", "month", "monthCode", "year" »).
  91. auto field_names = calendar_fields(global_object, *calendar, { "day"sv, "month"sv, "monthCode"sv, "year"sv });
  92. if (vm.exception())
  93. return {};
  94. // f. Let fields be ? PrepareTemporalFields(item, fieldNames, «»).
  95. auto* fields = prepare_temporal_fields(global_object, item_object, field_names, {});
  96. if (vm.exception())
  97. return {};
  98. // g. Return ? DateFromFields(calendar, fields, options).
  99. return date_from_fields(global_object, *calendar, *fields, *options);
  100. }
  101. // 4. Perform ? ToTemporalOverflow(options).
  102. (void)to_temporal_overflow(global_object, *options);
  103. if (vm.exception())
  104. return {};
  105. // 5. Let string be ? ToString(item).
  106. auto string = item.to_string(global_object);
  107. if (vm.exception())
  108. return {};
  109. // 6. Let result be ? ParseTemporalDateString(string).
  110. auto result = parse_temporal_date_string(global_object, string);
  111. if (vm.exception())
  112. return {};
  113. // 7. Assert: ! IsValidISODate(result.[[Year]], result.[[Month]], result.[[Day]]) is true.
  114. VERIFY(is_valid_iso_date(result->year, result->month, result->day));
  115. // 8. Let calendar be ? ToTemporalCalendarWithISODefault(result.[[Calendar]]).
  116. auto calendar = to_temporal_calendar_with_iso_default(global_object, result->calendar.has_value() ? js_string(vm, *result->calendar) : js_undefined());
  117. if (vm.exception())
  118. return {};
  119. // 9. Return ? CreateTemporalDate(result.[[Year]], result.[[Month]], result.[[Day]], calendar).
  120. return create_temporal_date(global_object, result->year, result->month, result->day, *calendar);
  121. }
  122. // 3.5.4 RegulateISODate ( year, month, day, overflow ), https://tc39.es/proposal-temporal/#sec-temporal-regulateisodate
  123. Optional<TemporalDate> regulate_iso_date(GlobalObject& global_object, double year, double month, double day, String const& overflow)
  124. {
  125. auto& vm = global_object.vm();
  126. // 1. Assert: year, month, and day are integers.
  127. VERIFY(year == trunc(year) && month == trunc(month) && day == trunc(day));
  128. // 2. Assert: overflow is either "constrain" or "reject".
  129. // NOTE: Asserted by the VERIFY_NOT_REACHED at the end
  130. // 3. If overflow is "reject", then
  131. if (overflow == "reject"sv) {
  132. // IMPLEMENTATION DEFINED: This is an optimization that allows us to treat these doubles as normal integers from this point onwards.
  133. // This does not change the exposed behaviour as the call to IsValidISODate will immediately check that these values are valid ISO
  134. // values (for years: -273975 - 273975, for months: 1 - 12, for days: 1 - 31) all of which are subsets of this check.
  135. if (!AK::is_within_range<i32>(year) || !AK::is_within_range<u8>(month) || !AK::is_within_range<u8>(day)) {
  136. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
  137. return {};
  138. }
  139. auto y = static_cast<i32>(year);
  140. auto m = static_cast<u8>(month);
  141. auto d = static_cast<u8>(day);
  142. // a. If ! IsValidISODate(year, month, day) is false, throw a RangeError exception.
  143. if (is_valid_iso_date(y, m, d)) {
  144. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
  145. return {};
  146. }
  147. // b. Return the Record { [[Year]]: year, [[Month]]: month, [[Day]]: day }.
  148. return TemporalDate { .year = y, .month = m, .day = d, .calendar = {} };
  149. }
  150. // 4. If overflow is "constrain", then
  151. else if (overflow == "constrain"sv) {
  152. // IMPLEMENTATION DEFINED: This is an optimization that allows us to treat this double as normal integer from this point onwards. This
  153. // does not change the exposed behaviour as the parent's call to CreateTemporalDate will immediately check that this value is a valid
  154. // ISO value for years: -273975 - 273975, which is a subset of this check.
  155. if (!AK::is_within_range<i32>(year)) {
  156. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
  157. return {};
  158. }
  159. auto y = static_cast<i32>(year);
  160. // a. Set month to ! ConstrainToRange(month, 1, 12).
  161. month = constrain_to_range(month, 1, 12);
  162. // b. Set day to ! ConstrainToRange(day, 1, ! ISODaysInMonth(year, month)).
  163. day = constrain_to_range(day, 1, iso_days_in_month(y, month));
  164. // c. Return the Record { [[Year]]: year, [[Month]]: month, [[Day]]: day }.
  165. return TemporalDate { .year = y, .month = static_cast<u8>(month), .day = static_cast<u8>(day), .calendar = {} };
  166. }
  167. VERIFY_NOT_REACHED();
  168. }
  169. // 3.5.5 IsValidISODate ( year, month, day ), https://tc39.es/proposal-temporal/#sec-temporal-isvalidisodate
  170. bool is_valid_iso_date(i32 year, u8 month, u8 day)
  171. {
  172. // 1. Assert: year, month, and day are integers.
  173. // 2. If month < 1 or month > 12, then
  174. if (month < 1 || month > 12) {
  175. // a. Return false.
  176. return false;
  177. }
  178. // 3. Let daysInMonth be ! ISODaysInMonth(year, month).
  179. auto days_in_month = iso_days_in_month(year, month);
  180. // 4. If day < 1 or day > daysInMonth, then
  181. if (day < 1 || day > days_in_month) {
  182. // a. Return false.
  183. return false;
  184. }
  185. // 5. Return true.
  186. return true;
  187. }
  188. // 3.5.6 BalanceISODate ( year, month, day ), https://tc39.es/proposal-temporal/#sec-temporal-balanceisodate
  189. ISODate balance_iso_date(i32 year, i32 month, i32 day)
  190. {
  191. // 1. Assert: year, month, and day are integers.
  192. // 2. Let balancedYearMonth be ! BalanceISOYearMonth(year, month).
  193. auto balanced_year_month = balance_iso_year_month(year, month);
  194. // 3. Set month to balancedYearMonth.[[Month]].
  195. month = balanced_year_month.month;
  196. // 4. Set year to balancedYearMonth.[[Year]].
  197. year = balanced_year_month.year;
  198. // 5. NOTE: To deal with negative numbers of days whose absolute value is greater than the number of days in a year, the following section subtracts years and adds days until the number of days is greater than −366 or −365.
  199. i32 test_year;
  200. // 6. If month > 2, then
  201. if (month > 2) {
  202. // a. Let testYear be year.
  203. test_year = year;
  204. }
  205. // 7. Else,
  206. else {
  207. // a. Let testYear be year − 1.
  208. test_year = year - 1;
  209. }
  210. // 8. Repeat, while day < −1 × ! ISODaysInYear(testYear),
  211. while (day < -1 * iso_days_in_year(test_year)) {
  212. // a.Set day to day + !ISODaysInYear(testYear).
  213. day += iso_days_in_year(test_year);
  214. // b.Set year to year − 1.
  215. year--;
  216. // c.Set testYear to testYear − 1.
  217. test_year--;
  218. }
  219. // 9. NOTE: To deal with numbers of days greater than the number of days in a year, the following section adds years and subtracts days until the number of days is less than 366 or 365.
  220. // 10. Let testYear be year + 1.
  221. test_year = year + 1;
  222. // 11. Repeat, while day > ! ISODaysInYear(testYear),
  223. while (day > iso_days_in_year(test_year)) {
  224. // a. Set day to day − ! ISODaysInYear(testYear).
  225. day -= iso_days_in_year(test_year);
  226. // b. Set year to year + 1.
  227. year++;
  228. // c. Set testYear to testYear + 1.
  229. test_year++;
  230. }
  231. // 12. NOTE: To deal with negative numbers of days whose absolute value is greater than the number of days in the current month, the following section subtracts months and adds days until the number of days is greater than 0.
  232. // 13. Repeat, while day < 1,
  233. while (day < 1) {
  234. // a. Set balancedYearMonth to ! BalanceISOYearMonth(year, month − 1).
  235. balanced_year_month = balance_iso_year_month(year, month - 1);
  236. // b. Set year to balancedYearMonth.[[Year]].
  237. year = balanced_year_month.year;
  238. // c. Set month to balancedYearMonth.[[Month]].
  239. month = balanced_year_month.month;
  240. // d. Set day to day + ! ISODaysInMonth(year, month).
  241. day += iso_days_in_month(year, month);
  242. }
  243. // 14. NOTE: To deal with numbers of days greater than the number of days in the current month, the following section adds months and subtracts days until the number of days is less than the number of days in the month.
  244. // 15. Repeat, while day > ! ISODaysInMonth(year, month),
  245. while (day > iso_days_in_month(year, month)) {
  246. // a. Set day to day − ! ISODaysInMonth(year, month).
  247. day -= iso_days_in_month(year, month);
  248. // b. Set balancedYearMonth to ! BalanceISOYearMonth(year, month + 1).
  249. balanced_year_month = balance_iso_year_month(year, month + 1);
  250. // c. Set year to balancedYearMonth.[[Year]].
  251. year = balanced_year_month.year;
  252. // d. Set month to balancedYearMonth.[[Month]].
  253. month = balanced_year_month.month;
  254. }
  255. // 16. Return the new Record { [[Year]]: year, [[Month]]: month, [[Day]]: day }.
  256. return ISODate { .year = year, .month = static_cast<u8>(month), .day = static_cast<u8>(day) };
  257. }
  258. // 3.5.10 CompareISODate ( y1, m1, d1, y2, m2, d2 ), https://tc39.es/proposal-temporal/#sec-temporal-compareisodate
  259. i8 compare_iso_date(i32 year1, u8 month1, u8 day1, i32 year2, u8 month2, u8 day2)
  260. {
  261. // 1. Assert: y1, m1, d1, y2, m2, and d2 are integers.
  262. // 2. If y1 > y2, return 1.
  263. if (year1 > year2)
  264. return 1;
  265. // 3. If y1 < y2, return -1.
  266. if (year1 < year2)
  267. return -1;
  268. // 4. If m1 > m2, return 1.
  269. if (month1 > month2)
  270. return 1;
  271. // 5. If m1 < m2, return -1.
  272. if (month1 < month2)
  273. return -1;
  274. // 6. If d1 > d2, return 1.
  275. if (day1 > day2)
  276. return 1;
  277. // 7. If d1 < d2, return -1.
  278. if (day1 < day2)
  279. return -1;
  280. // 8. Return 0.
  281. return 0;
  282. }
  283. }