Calendar.cpp 28 KB

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