Calendar.cpp 30 KB

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