Calendar.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/Array.h>
  8. #include <LibJS/Runtime/GlobalObject.h>
  9. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  10. #include <LibJS/Runtime/Temporal/Calendar.h>
  11. #include <LibJS/Runtime/Temporal/CalendarConstructor.h>
  12. #include <LibJS/Runtime/Temporal/PlainDate.h>
  13. #include <LibJS/Runtime/Temporal/PlainDateTime.h>
  14. #include <LibJS/Runtime/Temporal/PlainMonthDay.h>
  15. #include <LibJS/Runtime/Temporal/PlainTime.h>
  16. #include <LibJS/Runtime/Temporal/PlainYearMonth.h>
  17. #include <LibJS/Runtime/Temporal/ZonedDateTime.h>
  18. #include <LibJS/Runtime/Value.h>
  19. namespace JS::Temporal {
  20. // 12 Temporal.Calendar Objects, https://tc39.es/proposal-temporal/#sec-temporal-calendar-objects
  21. Calendar::Calendar(String identifier, Object& prototype)
  22. : Object(prototype)
  23. , m_identifier(move(identifier))
  24. {
  25. }
  26. // 12.1.1 CreateTemporalCalendar ( identifier [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporalcalendar
  27. Calendar* create_temporal_calendar(GlobalObject& global_object, String const& identifier, FunctionObject* new_target)
  28. {
  29. auto& vm = global_object.vm();
  30. // 1. Assert: ! IsBuiltinCalendar(identifier) is true.
  31. VERIFY(is_builtin_calendar(identifier));
  32. // 2. If newTarget is not provided, set newTarget to %Temporal.Calendar%.
  33. if (!new_target)
  34. new_target = global_object.temporal_calendar_constructor();
  35. // 3. Let object be ? OrdinaryCreateFromConstructor(newTarget, "%Temporal.Calendar.prototype%", « [[InitializedTemporalCalendar]], [[Identifier]] »).
  36. // 4. Set object.[[Identifier]] to identifier.
  37. auto* object = ordinary_create_from_constructor<Calendar>(global_object, *new_target, &GlobalObject::temporal_calendar_prototype, identifier);
  38. if (vm.exception())
  39. return {};
  40. // 5. Return object.
  41. return object;
  42. }
  43. // 12.1.2 IsBuiltinCalendar ( id ), https://tc39.es/proposal-temporal/#sec-temporal-isbuiltincalendar
  44. // NOTE: This is the minimum IsBuiltinCalendar implementation for engines without ECMA-402.
  45. bool is_builtin_calendar(String const& identifier)
  46. {
  47. // 1. If id is not "iso8601", return false.
  48. if (identifier != "iso8601"sv)
  49. return false;
  50. // 2. Return true.
  51. return true;
  52. }
  53. // 12.1.3 GetBuiltinCalendar ( id ), https://tc39.es/proposal-temporal/#sec-temporal-getbuiltincalendar
  54. Calendar* get_builtin_calendar(GlobalObject& global_object, String const& identifier)
  55. {
  56. auto& vm = global_object.vm();
  57. // 1. If ! IsBuiltinCalendar(id) is false, throw a RangeError exception.
  58. if (!is_builtin_calendar(identifier)) {
  59. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidCalendarIdentifier, identifier);
  60. return {};
  61. }
  62. // 2. Return ? Construct(%Temporal.Calendar%, « id »).
  63. MarkedValueList arguments(vm.heap());
  64. arguments.append(js_string(vm, identifier));
  65. auto calendar = vm.construct(*global_object.temporal_calendar_constructor(), *global_object.temporal_calendar_constructor(), move(arguments));
  66. if (vm.exception())
  67. return {};
  68. return static_cast<Calendar*>(&calendar.as_object());
  69. }
  70. // 12.1.4 GetISO8601Calendar ( )
  71. Calendar* get_iso8601_calendar(GlobalObject& global_object)
  72. {
  73. // 1. Return ! GetBuiltinCalendar("iso8601").
  74. return get_builtin_calendar(global_object, "iso8601");
  75. }
  76. // 12.1.5 CalendarFields ( calendar, fieldNames ), https://tc39.es/proposal-temporal/#sec-temporal-calendarfields
  77. Vector<String> calendar_fields(GlobalObject& global_object, Object& calendar, Vector<StringView> const& field_names)
  78. {
  79. auto& vm = global_object.vm();
  80. // 1. Let fields be ? GetMethod(calendar, "fields").
  81. auto fields = Value(&calendar).get_method(global_object, vm.names.fields);
  82. if (vm.exception())
  83. return {};
  84. // 2. Let fieldsArray be ! CreateArrayFromList(fieldNames).
  85. auto field_names_values = MarkedValueList { vm.heap() };
  86. for (auto& field_name : field_names)
  87. field_names_values.append(js_string(vm, field_name));
  88. Value fields_array = Array::create_from(global_object, field_names_values);
  89. // 3. If fields is not undefined, then
  90. if (fields) {
  91. // a. Set fieldsArray to ? Call(fields, calendar, « fieldsArray »).
  92. fields_array = vm.call(*fields, &calendar, fields_array);
  93. if (vm.exception())
  94. return {};
  95. }
  96. // 4. Return ? IterableToListOfType(fieldsArray, « String »).
  97. auto list = iterable_to_list_of_type(global_object, fields_array, { OptionType::String });
  98. if (vm.exception())
  99. return {};
  100. Vector<String> result;
  101. for (auto& value : list)
  102. result.append(value.as_string().string());
  103. return result;
  104. }
  105. // 12.1.9 CalendarYear ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendaryear
  106. double calendar_year(GlobalObject& global_object, Object& calendar, Object& date_like)
  107. {
  108. auto& vm = global_object.vm();
  109. // 1. Assert: Type(calendar) is Object.
  110. // 2. Let result be ? Invoke(calendar, "year", « dateLike »).
  111. auto result = Value(&calendar).invoke(global_object, vm.names.year, &date_like);
  112. if (vm.exception())
  113. return {};
  114. // 3. If result is undefined, throw a RangeError exception.
  115. if (result.is_undefined()) {
  116. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidCalendarFunctionResult, vm.names.year.as_string());
  117. return {};
  118. }
  119. // 4. Return ? ToIntegerOrInfinity(result).
  120. return result.to_integer_or_infinity(global_object);
  121. }
  122. // 12.1.10 CalendarMonth ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarmonth
  123. double calendar_month(GlobalObject& global_object, Object& calendar, Object& date_like)
  124. {
  125. auto& vm = global_object.vm();
  126. // 1. Assert: Type(calendar) is Object.
  127. // 2. Let result be ? Invoke(calendar, "month", « dateLike »).
  128. auto result = Value(&calendar).invoke(global_object, vm.names.month, &date_like);
  129. if (vm.exception())
  130. return {};
  131. // 3. If result is undefined, throw a RangeError exception.
  132. if (result.is_undefined()) {
  133. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidCalendarFunctionResult, vm.names.month.as_string());
  134. return {};
  135. }
  136. // 4. Return ? ToPositiveIntegerOrInfinity(result).
  137. return to_positive_integer_or_infinity(global_object, result);
  138. }
  139. // 12.1.11 CalendarMonthCode ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarmonthcode
  140. String calendar_month_code(GlobalObject& global_object, Object& calendar, Object& date_like)
  141. {
  142. auto& vm = global_object.vm();
  143. // 1. Assert: Type(calendar) is Object.
  144. // 2. Let result be ? Invoke(calendar, "monthCode", « dateLike »).
  145. auto result = Value(&calendar).invoke(global_object, vm.names.monthCode, &date_like);
  146. if (vm.exception())
  147. return {};
  148. // 3. If result is undefined, throw a RangeError exception.
  149. if (result.is_undefined()) {
  150. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidCalendarFunctionResult, vm.names.monthCode.as_string());
  151. return {};
  152. }
  153. // 4. Return ? ToString(result).
  154. return result.to_string(global_object);
  155. }
  156. // 12.1.12 CalendarDay ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarday
  157. double calendar_day(GlobalObject& global_object, Object& calendar, Object& date_like)
  158. {
  159. auto& vm = global_object.vm();
  160. // 1. Assert: Type(calendar) is Object.
  161. // 2. Let result be ? Invoke(calendar, "day", « dateLike »).
  162. auto result = Value(&calendar).invoke(global_object, vm.names.day, &date_like);
  163. if (vm.exception())
  164. return {};
  165. // 3. If result is undefined, throw a RangeError exception.
  166. if (result.is_undefined()) {
  167. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidCalendarFunctionResult, vm.names.day.as_string());
  168. return {};
  169. }
  170. // 4. Return ? ToPositiveIntegerOrInfinity(result).
  171. return to_positive_integer_or_infinity(global_object, result);
  172. }
  173. // 12.1.13 CalendarDayOfWeek ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendardayofweek
  174. Value calendar_day_of_week(GlobalObject& global_object, Object& calendar, Object& date_like)
  175. {
  176. auto& vm = global_object.vm();
  177. // 1. Assert: Type(calendar) is Object.
  178. // 2. Return ? Invoke(calendar, "dayOfWeek", « dateLike »).
  179. return Value(&calendar).invoke(global_object, vm.names.dayOfWeek, &date_like);
  180. }
  181. // 12.1.14 CalendarDayOfYear ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendardayofyear
  182. Value calendar_day_of_year(GlobalObject& global_object, Object& calendar, Object& date_like)
  183. {
  184. auto& vm = global_object.vm();
  185. // 1. Assert: Type(calendar) is Object.
  186. // 2. Return ? Invoke(calendar, "dayOfYear", « dateLike »).
  187. return Value(&calendar).invoke(global_object, vm.names.dayOfYear, &date_like);
  188. }
  189. // 12.1.15 CalendarWeekOfYear ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarweekofyear
  190. Value calendar_week_of_year(GlobalObject& global_object, Object& calendar, Object& date_like)
  191. {
  192. auto& vm = global_object.vm();
  193. // 1. Assert: Type(calendar) is Object.
  194. // 2. Return ? Invoke(calendar, "weekOfYear", « dateLike »).
  195. return Value(&calendar).invoke(global_object, vm.names.weekOfYear, &date_like);
  196. }
  197. // 12.1.16 CalendarDaysInWeek ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendardaysinweek
  198. Value calendar_days_in_week(GlobalObject& global_object, Object& calendar, Object& date_like)
  199. {
  200. auto& vm = global_object.vm();
  201. // 1. Assert: Type(calendar) is Object.
  202. // 2. Return ? Invoke(calendar, "daysInWeek", « dateLike »).
  203. return Value(&calendar).invoke(global_object, vm.names.daysInWeek, &date_like);
  204. }
  205. // 12.1.17 CalendarDaysInMonth ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendardaysinmonth
  206. Value calendar_days_in_month(GlobalObject& global_object, Object& calendar, Object& date_like)
  207. {
  208. auto& vm = global_object.vm();
  209. // 1. Assert: Type(calendar) is Object.
  210. // 2. Return ? Invoke(calendar, "daysInMonth", « dateLike »).
  211. return Value(&calendar).invoke(global_object, vm.names.daysInMonth, &date_like);
  212. }
  213. // 12.1.18 CalendarDaysInYear ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendardaysinyear
  214. Value calendar_days_in_year(GlobalObject& global_object, Object& calendar, Object& date_like)
  215. {
  216. auto& vm = global_object.vm();
  217. // 1. Assert: Type(calendar) is Object.
  218. // 2. Return ? Invoke(calendar, "daysInYear", « dateLike »).
  219. return Value(&calendar).invoke(global_object, vm.names.daysInYear, &date_like);
  220. }
  221. // 12.1.19 CalendarMonthsInYear ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarmonthsinyear
  222. Value calendar_months_in_year(GlobalObject& global_object, Object& calendar, Object& date_like)
  223. {
  224. auto& vm = global_object.vm();
  225. // 1. Assert: Type(calendar) is Object.
  226. // 2. Return ? Invoke(calendar, "monthsInYear", « dateLike »).
  227. return Value(&calendar).invoke(global_object, vm.names.monthsInYear, &date_like);
  228. }
  229. // 12.1.20 CalendarInLeapYear ( calendar, dateLike ), https://tc39.es/proposal-temporal/#sec-temporal-calendarinleapyear
  230. Value calendar_in_leap_year(GlobalObject& global_object, Object& calendar, Object& date_like)
  231. {
  232. auto& vm = global_object.vm();
  233. // 1. Assert: Type(calendar) is Object.
  234. // 2. Return ? Invoke(calendar, "inLeapYear", « dateLike »).
  235. return Value(&calendar).invoke(global_object, vm.names.inLeapYear, &date_like);
  236. }
  237. // 12.1.21 ToTemporalCalendar ( temporalCalendarLike ), https://tc39.es/proposal-temporal/#sec-temporal-totemporalcalendar
  238. Object* to_temporal_calendar(GlobalObject& global_object, Value temporal_calendar_like)
  239. {
  240. auto& vm = global_object.vm();
  241. // 1. If Type(temporalCalendarLike) is Object, then
  242. if (temporal_calendar_like.is_object()) {
  243. auto& temporal_calendar_like_object = temporal_calendar_like.as_object();
  244. // a. If temporalCalendarLike has an [[InitializedTemporalDate]], [[InitializedTemporalDateTime]], [[InitializedTemporalMonthDay]], [[InitializedTemporalTime]], [[InitializedTemporalYearMonth]], or [[InitializedTemporalZonedDateTime]] internal slot, then
  245. // i. Return temporalCalendarLike.[[Calendar]].
  246. if (is<PlainDate>(temporal_calendar_like_object))
  247. return &static_cast<PlainDate&>(temporal_calendar_like_object).calendar();
  248. if (is<PlainDateTime>(temporal_calendar_like_object))
  249. return &static_cast<PlainDateTime&>(temporal_calendar_like_object).calendar();
  250. if (is<PlainMonthDay>(temporal_calendar_like_object))
  251. return &static_cast<PlainMonthDay&>(temporal_calendar_like_object).calendar();
  252. if (is<PlainTime>(temporal_calendar_like_object))
  253. return &static_cast<PlainTime&>(temporal_calendar_like_object).calendar();
  254. if (is<PlainYearMonth>(temporal_calendar_like_object))
  255. return &static_cast<PlainYearMonth&>(temporal_calendar_like_object).calendar();
  256. if (is<ZonedDateTime>(temporal_calendar_like_object))
  257. return &static_cast<ZonedDateTime&>(temporal_calendar_like_object).calendar();
  258. // b. If ? HasProperty(temporalCalendarLike, "calendar") is false, return temporalCalendarLike.
  259. auto has_property = temporal_calendar_like_object.has_property(vm.names.calendar);
  260. if (vm.exception())
  261. return {};
  262. if (!has_property)
  263. return &temporal_calendar_like_object;
  264. // c. Set temporalCalendarLike to ? Get(temporalCalendarLike, "calendar").
  265. temporal_calendar_like = temporal_calendar_like_object.get(vm.names.calendar);
  266. if (vm.exception())
  267. return {};
  268. // d. If Type(temporalCalendarLike) is Object and ? HasProperty(temporalCalendarLike, "calendar") is false, return temporalCalendarLike.
  269. if (temporal_calendar_like.is_object()) {
  270. has_property = temporal_calendar_like.as_object().has_property(vm.names.calendar);
  271. if (vm.exception())
  272. return {};
  273. if (!has_property)
  274. return &temporal_calendar_like.as_object();
  275. }
  276. }
  277. // 2. Let identifier be ? ToString(temporalCalendarLike).
  278. auto identifier = temporal_calendar_like.to_string(global_object);
  279. if (vm.exception())
  280. return {};
  281. // 3. If ! IsBuiltinCalendar(identifier) is false, then
  282. if (!is_builtin_calendar(identifier)) {
  283. // a. Let identifier be ? ParseTemporalCalendarString(identifier).
  284. auto parsed_identifier = parse_temporal_calendar_string(global_object, identifier);
  285. if (vm.exception())
  286. return {};
  287. identifier = move(*parsed_identifier);
  288. }
  289. // 4. Return ! CreateTemporalCalendar(identifier).
  290. return create_temporal_calendar(global_object, identifier);
  291. }
  292. // 12.1.22 ToTemporalCalendarWithISODefault ( temporalCalendarLike ), https://tc39.es/proposal-temporal/#sec-temporal-totemporalcalendarwithisodefault
  293. Object* to_temporal_calendar_with_iso_default(GlobalObject& global_object, Value temporal_calendar_like)
  294. {
  295. // 1. If temporalCalendarLike is undefined, then
  296. if (temporal_calendar_like.is_undefined()) {
  297. // a. Return ! GetISO8601Calendar().
  298. return get_iso8601_calendar(global_object);
  299. }
  300. // 2. Return ? ToTemporalCalendar(temporalCalendarLike).
  301. return to_temporal_calendar(global_object, temporal_calendar_like);
  302. }
  303. // 12.1.23 GetTemporalCalendarWithISODefault ( item ), https://tc39.es/proposal-temporal/#sec-temporal-gettemporalcalendarwithisodefault
  304. Object* get_temporal_calendar_with_iso_default(GlobalObject& global_object, Object& item)
  305. {
  306. auto& vm = global_object.vm();
  307. // 1. If item has an [[InitializedTemporalDate]], [[InitializedTemporalDateTime]], [[InitializedTemporalMonthDay]], [[InitializedTemporalTime]], [[InitializedTemporalYearMonth]], or [[InitializedTemporalZonedDateTime]] internal slot, then
  308. // a. Return item.[[Calendar]].
  309. if (is<PlainDate>(item))
  310. return &static_cast<PlainDate&>(item).calendar();
  311. if (is<PlainDateTime>(item))
  312. return &static_cast<PlainDateTime&>(item).calendar();
  313. if (is<PlainMonthDay>(item))
  314. return &static_cast<PlainMonthDay&>(item).calendar();
  315. if (is<PlainTime>(item))
  316. return &static_cast<PlainTime&>(item).calendar();
  317. if (is<PlainYearMonth>(item))
  318. return &static_cast<PlainYearMonth&>(item).calendar();
  319. if (is<ZonedDateTime>(item))
  320. return &static_cast<ZonedDateTime&>(item).calendar();
  321. // 2. Let calendar be ? Get(item, "calendar").
  322. auto calendar = item.get(vm.names.calendar);
  323. if (vm.exception())
  324. return {};
  325. // 3. Return ? ToTemporalCalendarWithISODefault(calendar).
  326. return to_temporal_calendar_with_iso_default(global_object, calendar);
  327. }
  328. // 12.1.24 DateFromFields ( calendar, fields, options ), https://tc39.es/proposal-temporal/#sec-temporal-datefromfields
  329. PlainDate* date_from_fields(GlobalObject& global_object, Object& calendar, Object& fields, Object& options)
  330. {
  331. auto& vm = global_object.vm();
  332. // 1. Assert: Type(calendar) is Object.
  333. // 2. Assert: Type(fields) is Object.
  334. // 3. Let date be ? Invoke(calendar, "dateFromFields", « fields, options »).
  335. auto date = Value(&calendar).invoke(global_object, vm.names.dateFromFields, &fields, &options);
  336. if (vm.exception())
  337. return {};
  338. // 4. Perform ? RequireInternalSlot(date, [[InitializedTemporalDate]]).
  339. auto* date_object = date.to_object(global_object);
  340. if (!date_object)
  341. return {};
  342. if (!is<PlainDate>(date_object)) {
  343. vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "Temporal.PlainDate");
  344. return {};
  345. }
  346. // 5. Return date.
  347. return static_cast<PlainDate*>(date_object);
  348. }
  349. // 12.1.28 CalendarEquals ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal-calendarequals
  350. bool calendar_equals(GlobalObject& global_object, Object& one, Object& two)
  351. {
  352. auto& vm = global_object.vm();
  353. // 1. If one and two are the same Object value, return true.
  354. if (&one == &two)
  355. return true;
  356. // 2. Let calendarOne be ? ToString(one).
  357. auto calendar_one = Value(&one).to_string(global_object);
  358. if (vm.exception())
  359. return {};
  360. // 3. Let calendarTwo be ? ToString(two).
  361. auto calendar_two = Value(&two).to_string(global_object);
  362. if (vm.exception())
  363. return {};
  364. // 4. If calendarOne is calendarTwo, return true.
  365. if (calendar_one == calendar_two)
  366. return true;
  367. // 5. Return false.
  368. return false;
  369. }
  370. // 12.1.29 ConsolidateCalendars ( one, two ), https://tc39.es/proposal-temporal/#sec-temporal-consolidatecalendars
  371. Object* consolidate_calendars(GlobalObject& global_object, Object& one, Object& two)
  372. {
  373. auto& vm = global_object.vm();
  374. // 1. If one and two are the same Object value, return two.
  375. if (&one == &two)
  376. return &two;
  377. // 2. Let calendarOne be ? ToString(one).
  378. auto calendar_one = Value(&one).to_string(global_object);
  379. if (vm.exception())
  380. return {};
  381. // 3. Let calendarTwo be ? ToString(two).
  382. auto calendar_two = Value(&two).to_string(global_object);
  383. if (vm.exception())
  384. return {};
  385. // 4. If calendarOne is calendarTwo, return two.
  386. if (calendar_one == calendar_two)
  387. return &two;
  388. // 5. If calendarOne is "iso8601", return two.
  389. if (calendar_one == "iso8601"sv)
  390. return &two;
  391. // 6. If calendarTwo is "iso8601", return one.
  392. if (calendar_two == "iso8601"sv)
  393. return &one;
  394. // 7. Throw a RangeError exception.
  395. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidCalendar);
  396. return {};
  397. }
  398. // 12.1.30 IsISOLeapYear ( year ), https://tc39.es/proposal-temporal/#sec-temporal-isisoleapyear
  399. bool is_iso_leap_year(i32 year)
  400. {
  401. // 1. Assert: year is an integer.
  402. // 2. If year modulo 4 ≠ 0, return false.
  403. if (year % 4 != 0)
  404. return false;
  405. // 3. If year modulo 400 = 0, return true.
  406. if (year % 400 == 0)
  407. return true;
  408. // 4. If year modulo 100 = 0, return false.
  409. if (year % 100 == 0)
  410. return false;
  411. // 5. Return true.
  412. return true;
  413. }
  414. // 12.1.31 ISODaysInYear ( year ), https://tc39.es/proposal-temporal/#sec-temporal-isodaysinyear
  415. u16 iso_days_in_year(i32 year)
  416. {
  417. // 1. Assert: year is an integer.
  418. // 2. If ! IsISOLeapYear(year) is true, then
  419. if (is_iso_leap_year(year)) {
  420. // a. Return 366.
  421. return 366;
  422. }
  423. // 3. Return 365.
  424. return 365;
  425. }
  426. // 12.1.32 ISODaysInMonth ( year, month ), https://tc39.es/proposal-temporal/#sec-temporal-isodaysinmonth
  427. u8 iso_days_in_month(i32 year, u8 month)
  428. {
  429. // 1. Assert: year is an integer.
  430. // 2. Assert: month is an integer, month ≥ 1, and month ≤ 12.
  431. VERIFY(month >= 1 && month <= 12);
  432. // 3. If month is 1, 3, 5, 7, 8, 10, or 12, return 31.
  433. if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
  434. return 31;
  435. // 4. If month is 4, 6, 9, or 11, return 30.
  436. if (month == 4 || month == 6 || month == 9 || month == 11)
  437. return 30;
  438. // 5. If ! IsISOLeapYear(year) is true, return 29.
  439. if (is_iso_leap_year(year))
  440. return 29;
  441. // 6. Return 28.
  442. return 28;
  443. }
  444. // 12.1.33 ToISODayOfWeek ( year, month, day ), https://tc39.es/proposal-temporal/#sec-temporal-toisodayofweek
  445. u8 to_iso_day_of_week(i32 year, u8 month, u8 day)
  446. {
  447. // 1. Assert: year is an integer.
  448. // 2. Assert: month is an integer.
  449. // 3. Assert: day is an integer.
  450. // 4. Let date be the date given by year, month, and day.
  451. // 5. Return date's day of the week according to ISO-8601.
  452. // NOTE: Implemented based on https://cs.uwaterloo.ca/~alopez-o/math-faq/node73.html
  453. auto normalized_month = month + (month < 3 ? 10 : -2);
  454. auto normalized_year = year - (month < 3 ? 1 : 0);
  455. auto century = normalized_year / 100;
  456. auto truncated_year = normalized_year - (century * 100);
  457. auto result = (day + static_cast<u8>((2.6 * normalized_month) - 0.2) - (2 * century) + truncated_year + (truncated_year / 4) + (century / 4)) % 7;
  458. if (result <= 0) // Mathematical modulo
  459. result += 7;
  460. return result;
  461. }
  462. // 12.1.34 ToISODayOfYear ( year, month, day ), https://tc39.es/proposal-temporal/#sec-temporal-toisodayofyear
  463. u16 to_iso_day_of_year(i32 year, u8 month, u8 day)
  464. {
  465. // 1. Assert: year is an integer.
  466. // 2. Assert: month is an integer.
  467. // 3. Assert: day is an integer.
  468. // 4. Let date be the date given by year, month, and day.
  469. // 5. Return date's ordinal date in the year according to ISO-8601.
  470. u16 days = day;
  471. for (u8 i = month - 1; i > 0; --i)
  472. days += iso_days_in_month(year, i);
  473. return days;
  474. }
  475. // 12.1.35 ToISOWeekOfYear ( year, month, day ), https://tc39.es/proposal-temporal/#sec-temporal-toisoweekofyear
  476. u8 to_iso_week_of_year(i32 year, u8 month, u8 day)
  477. {
  478. // 1. Assert: year is an integer.
  479. // 2. Assert: month is an integer.
  480. // 3. Assert: day is an integer.
  481. // 4. Let date be the date given by year, month, and day.
  482. // 5. Return date's week number according to ISO-8601.
  483. auto day_of_year = to_iso_day_of_year(year, month, day);
  484. auto day_of_week = to_iso_day_of_week(year, month, day);
  485. auto week = (day_of_year - day_of_week + 10) / 7;
  486. if (week < 1) {
  487. auto day_of_jump = to_iso_day_of_week(year, 1, 1);
  488. if (day_of_jump == 5 || (is_iso_leap_year(year) && day_of_jump == 6))
  489. return 53;
  490. else
  491. return 52;
  492. } else if (week == 53) {
  493. auto days_in_year = iso_days_in_year(year);
  494. if (days_in_year - day_of_year < 4 - day_of_week)
  495. return 1;
  496. }
  497. return week;
  498. }
  499. // 12.1.36 BuildISOMonthCode ( month ), https://tc39.es/proposal-temporal/#sec-buildisomonthcode
  500. String build_iso_month_code(u8 month)
  501. {
  502. return String::formatted("M{:02}", month);
  503. }
  504. // 12.1.37 ResolveISOMonth ( fields ), https://tc39.es/proposal-temporal/#sec-temporal-resolveisomonth
  505. double resolve_iso_month(GlobalObject& global_object, Object& fields)
  506. {
  507. auto& vm = global_object.vm();
  508. // 1. Let month be ? Get(fields, "month").
  509. auto month = fields.get(vm.names.month);
  510. if (vm.exception())
  511. return {};
  512. // 2. Let monthCode be ? Get(fields, "monthCode").
  513. auto month_code = fields.get(vm.names.monthCode);
  514. if (vm.exception())
  515. return {};
  516. // 3. If monthCode is undefined, then
  517. if (month_code.is_undefined()) {
  518. // a. If month is undefined, throw a TypeError exception.
  519. if (month.is_undefined()) {
  520. vm.throw_exception<TypeError>(global_object, ErrorType::TemporalMissingRequiredProperty, vm.names.month.as_string());
  521. return {};
  522. }
  523. // b. Return month.
  524. return month.as_double();
  525. }
  526. // 4. Assert: Type(monthCode) is String.
  527. VERIFY(month_code.is_string());
  528. auto& month_code_string = month_code.as_string().string();
  529. // 5. Let monthLength be the length of monthCode.
  530. auto month_length = month_code_string.length();
  531. // 6. If monthLength is not 3, throw a RangeError exception.
  532. if (month_length != 3) {
  533. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidMonthCode);
  534. return {};
  535. }
  536. // 7. Let numberPart be the substring of monthCode from 1.
  537. auto number_part = month_code_string.substring(1);
  538. // 8. Set numberPart to ! ToIntegerOrInfinity(numberPart).
  539. auto number_part_integer = Value(js_string(vm, move(number_part))).to_integer_or_infinity(global_object);
  540. // 9. If numberPart < 1 or numberPart > 12, throw a RangeError exception.
  541. if (number_part_integer < 1 || number_part_integer > 12) {
  542. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidMonthCode);
  543. return {};
  544. }
  545. // 10. If month is not undefined, and month ≠ numberPart, then
  546. if (!month.is_undefined() && month.as_double() != number_part_integer) {
  547. // a. Throw a RangeError exception.
  548. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidMonthCode);
  549. return {};
  550. }
  551. // 11. If ! SameValueNonNumeric(monthCode, ! BuildISOMonthCode(numberPart)) is false, then
  552. if (month_code_string != build_iso_month_code(number_part_integer)) {
  553. // a. Throw a RangeError exception.
  554. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidMonthCode);
  555. return {};
  556. }
  557. // 12. Return numberPart.
  558. return number_part_integer;
  559. }
  560. // 12.1.38 ISODateFromFields ( fields, options ), https://tc39.es/proposal-temporal/#sec-temporal-isodatefromfields
  561. Optional<ISODate> iso_date_from_fields(GlobalObject& global_object, Object& fields, Object& options)
  562. {
  563. auto& vm = global_object.vm();
  564. // 1. Assert: Type(fields) is Object.
  565. // 2. Let overflow be ? ToTemporalOverflow(options).
  566. auto overflow = to_temporal_overflow(global_object, options);
  567. if (vm.exception())
  568. return {};
  569. // 3. Set fields to ? PrepareTemporalFields(fields, « "day", "month", "monthCode", "year" », «»).
  570. auto* prepared_fields = prepare_temporal_fields(global_object, fields, { "day", "month", "monthCode", "year" }, {});
  571. if (vm.exception())
  572. return {};
  573. // 4. Let year be ? Get(fields, "year").
  574. auto year = prepared_fields->get(vm.names.year);
  575. if (vm.exception())
  576. return {};
  577. // 5. If year is undefined, throw a TypeError exception.
  578. if (year.is_undefined()) {
  579. vm.throw_exception<TypeError>(global_object, ErrorType::TemporalMissingRequiredProperty, vm.names.year.as_string());
  580. return {};
  581. }
  582. // 6. Let month be ? ResolveISOMonth(fields).
  583. auto month = resolve_iso_month(global_object, *prepared_fields);
  584. if (vm.exception())
  585. return {};
  586. // 7. Let day be ? Get(fields, "day").
  587. auto day = prepared_fields->get(vm.names.day);
  588. if (vm.exception())
  589. return {};
  590. // 8. If day is undefined, throw a TypeError exception.
  591. if (day.is_undefined()) {
  592. vm.throw_exception<TypeError>(global_object, ErrorType::TemporalMissingRequiredProperty, vm.names.day.as_string());
  593. return {};
  594. }
  595. // 9. Return ? RegulateISODate(year, month, day, overflow).
  596. return regulate_iso_date(global_object, year.as_double(), month, day.as_double(), *overflow);
  597. }
  598. // 12.1.41 ISOYear ( temporalObject ), https://tc39.es/proposal-temporal/#sec-temporal-isoyear
  599. i32 iso_year(Object& temporal_object)
  600. {
  601. // 1. Assert: temporalObject has an [[ISOYear]] internal slot.
  602. // NOTE: Asserted by the VERIFY_NOT_REACHED at the end
  603. // 2. Return 𝔽(temporalObject.[[ISOYear]]).
  604. if (is<PlainDate>(temporal_object))
  605. return static_cast<PlainDate&>(temporal_object).iso_year();
  606. if (is<PlainDateTime>(temporal_object))
  607. return static_cast<PlainDateTime&>(temporal_object).iso_year();
  608. if (is<PlainYearMonth>(temporal_object))
  609. return static_cast<PlainYearMonth&>(temporal_object).iso_year();
  610. if (is<PlainMonthDay>(temporal_object))
  611. return static_cast<PlainMonthDay&>(temporal_object).iso_year();
  612. VERIFY_NOT_REACHED();
  613. }
  614. // 12.1.42 ISOMonth ( temporalObject ), https://tc39.es/proposal-temporal/#sec-temporal-isomonth
  615. u8 iso_month(Object& temporal_object)
  616. {
  617. // 1. Assert: temporalObject has an [[ISOMonth]] internal slot.
  618. // NOTE: Asserted by the VERIFY_NOT_REACHED at the end
  619. // 2. Return 𝔽(temporalObject.[[ISOMonth]]).
  620. if (is<PlainDate>(temporal_object))
  621. return static_cast<PlainDate&>(temporal_object).iso_month();
  622. if (is<PlainDateTime>(temporal_object))
  623. return static_cast<PlainDateTime&>(temporal_object).iso_month();
  624. if (is<PlainYearMonth>(temporal_object))
  625. return static_cast<PlainYearMonth&>(temporal_object).iso_month();
  626. if (is<PlainMonthDay>(temporal_object))
  627. return static_cast<PlainMonthDay&>(temporal_object).iso_month();
  628. VERIFY_NOT_REACHED();
  629. }
  630. // 12.1.43 ISOMonthCode ( temporalObject ), https://tc39.es/proposal-temporal/#sec-temporal-isomonthcode
  631. String iso_month_code(Object& temporal_object)
  632. {
  633. // 1. Assert: temporalObject has an [[ISOMonth]] internal slot.
  634. // NOTE: Asserted by the VERIFY_NOT_REACHED at the end
  635. // 2. Return ! BuildISOMonthCode(temporalObject.[[ISOMonth]]).
  636. if (is<PlainDate>(temporal_object))
  637. return build_iso_month_code(static_cast<PlainDate&>(temporal_object).iso_month());
  638. if (is<PlainDateTime>(temporal_object))
  639. return build_iso_month_code(static_cast<PlainDateTime&>(temporal_object).iso_month());
  640. if (is<PlainYearMonth>(temporal_object))
  641. return build_iso_month_code(static_cast<PlainYearMonth&>(temporal_object).iso_month());
  642. if (is<PlainMonthDay>(temporal_object))
  643. return build_iso_month_code(static_cast<PlainMonthDay&>(temporal_object).iso_month());
  644. VERIFY_NOT_REACHED();
  645. }
  646. // 12.1.44 ISODay ( temporalObject ), https://tc39.es/proposal-temporal/#sec-temporal-isomonthcode
  647. u8 iso_day(Object& temporal_object)
  648. {
  649. // 1. Assert: temporalObject has an [[ISODay]] internal slot.
  650. // NOTE: Asserted by the VERIFY_NOT_REACHED at the end
  651. // 2. Return 𝔽(temporalObject.[[ISODay]]).
  652. if (is<PlainDate>(temporal_object))
  653. return static_cast<PlainDate&>(temporal_object).iso_day();
  654. if (is<PlainDateTime>(temporal_object))
  655. return static_cast<PlainDateTime&>(temporal_object).iso_day();
  656. if (is<PlainYearMonth>(temporal_object))
  657. return static_cast<PlainYearMonth&>(temporal_object).iso_day();
  658. if (is<PlainMonthDay>(temporal_object))
  659. return static_cast<PlainMonthDay&>(temporal_object).iso_day();
  660. VERIFY_NOT_REACHED();
  661. }
  662. }