Calendar.cpp 29 KB

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