PlainTime.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Runtime/AbstractOperations.h>
  8. #include <LibJS/Runtime/GlobalObject.h>
  9. #include <LibJS/Runtime/Object.h>
  10. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  11. #include <LibJS/Runtime/Temporal/Calendar.h>
  12. #include <LibJS/Runtime/Temporal/Instant.h>
  13. #include <LibJS/Runtime/Temporal/PlainDateTime.h>
  14. #include <LibJS/Runtime/Temporal/PlainTime.h>
  15. #include <LibJS/Runtime/Temporal/PlainTimeConstructor.h>
  16. #include <LibJS/Runtime/Temporal/TimeZone.h>
  17. #include <LibJS/Runtime/Temporal/ZonedDateTime.h>
  18. namespace JS::Temporal {
  19. // 4 Temporal.PlainTime Objects, https://tc39.es/proposal-temporal/#sec-temporal-plaintime-objects
  20. PlainTime::PlainTime(u8 iso_hour, u8 iso_minute, u8 iso_second, u16 iso_millisecond, u16 iso_microsecond, u16 iso_nanosecond, Calendar& calendar, Object& prototype)
  21. : Object(prototype)
  22. , m_iso_hour(iso_hour)
  23. , m_iso_minute(iso_minute)
  24. , m_iso_second(iso_second)
  25. , m_iso_millisecond(iso_millisecond)
  26. , m_iso_microsecond(iso_microsecond)
  27. , m_iso_nanosecond(iso_nanosecond)
  28. , m_calendar(calendar)
  29. {
  30. }
  31. void PlainTime::visit_edges(Visitor& visitor)
  32. {
  33. Base::visit_edges(visitor);
  34. visitor.visit(&m_calendar);
  35. }
  36. // 4.5.2 ToTemporalTime ( item [ , overflow ] ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaltime
  37. PlainTime* to_temporal_time(GlobalObject& global_object, Value item, Optional<StringView> overflow)
  38. {
  39. auto& vm = global_object.vm();
  40. // 1. If overflow is not present, set it to "constrain".
  41. if (!overflow.has_value())
  42. overflow = "constrain"sv;
  43. // 2. Assert: overflow is either "constrain" or "reject".
  44. VERIFY(overflow == "constrain"sv || overflow == "reject"sv);
  45. Optional<TemporalTime> result;
  46. // 3. If Type(item) is Object, then
  47. if (item.is_object()) {
  48. auto& item_object = item.as_object();
  49. // a. If item has an [[InitializedTemporalTime]] internal slot, then
  50. if (is<PlainTime>(item_object)) {
  51. // i. Return item.
  52. return &static_cast<PlainTime&>(item_object);
  53. }
  54. // b. If item has an [[InitializedTemporalZonedDateTime]] internal slot, then
  55. if (is<ZonedDateTime>(item_object)) {
  56. auto& zoned_date_time = static_cast<ZonedDateTime&>(item_object);
  57. // i. Let instant be ! CreateTemporalInstant(item.[[Nanoseconds]]).
  58. auto* instant = create_temporal_instant(global_object, zoned_date_time.nanoseconds());
  59. // ii. Set plainDateTime to ? BuiltinTimeZoneGetPlainDateTimeFor(item.[[TimeZone]], instant, item.[[Calendar]]).
  60. auto* plain_date_time = builtin_time_zone_get_plain_date_time_for(global_object, &zoned_date_time.time_zone(), *instant, zoned_date_time.calendar());
  61. if (vm.exception())
  62. return {};
  63. // iii. Return ! CreateTemporalTime(plainDateTime.[[ISOHour]], plainDateTime.[[ISOMinute]], plainDateTime.[[ISOSecond]], plainDateTime.[[ISOMillisecond]], plainDateTime.[[ISOMicrosecond]], plainDateTime.[[ISONanosecond]]).
  64. return create_temporal_time(global_object, plain_date_time->iso_hour(), plain_date_time->iso_minute(), plain_date_time->iso_second(), plain_date_time->iso_millisecond(), plain_date_time->iso_microsecond(), plain_date_time->iso_nanosecond());
  65. }
  66. // c. If item has an [[InitializedTemporalDateTime]] internal slot, then
  67. if (is<PlainDateTime>(item_object)) {
  68. auto& plain_date_time = static_cast<PlainDateTime&>(item_object);
  69. // i. Return ! CreateTemporalTime(item.[[ISOHour]], item.[[ISOMinute]], item.[[ISOSecond]], item.[[ISOMillisecond]], item.[[ISOMicrosecond]], item.[[ISONanosecond]]).
  70. return create_temporal_time(global_object, plain_date_time.iso_hour(), plain_date_time.iso_minute(), plain_date_time.iso_second(), plain_date_time.iso_millisecond(), plain_date_time.iso_microsecond(), plain_date_time.iso_nanosecond());
  71. }
  72. // d. Let calendar be ? GetTemporalCalendarWithISODefault(item).
  73. auto* calendar = get_temporal_calendar_with_iso_default(global_object, item_object);
  74. if (vm.exception())
  75. return {};
  76. // e. If ? ToString(calendar) is not "iso8601", then
  77. auto calendar_identifier = Value(calendar).to_string(global_object);
  78. if (vm.exception())
  79. return {};
  80. if (calendar_identifier != "iso8601"sv) {
  81. // i. Throw a RangeError exception.
  82. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidCalendarIdentifier, calendar_identifier);
  83. return {};
  84. }
  85. // f. Let result be ? ToTemporalTimeRecord(item).
  86. auto unregulated_result = to_temporal_time_record(global_object, item_object);
  87. if (vm.exception())
  88. return {};
  89. // g. Set result to ? RegulateTime(result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]], overflow).
  90. result = regulate_time(global_object, unregulated_result->hour, unregulated_result->minute, unregulated_result->second, unregulated_result->millisecond, unregulated_result->microsecond, unregulated_result->nanosecond, *overflow);
  91. if (vm.exception())
  92. return {};
  93. }
  94. // 4. Else,
  95. else {
  96. // a. Let string be ? ToString(item).
  97. auto string = item.to_string(global_object);
  98. if (vm.exception())
  99. return {};
  100. // b. Let result be ? ParseTemporalTimeString(string).
  101. result = parse_temporal_time_string(global_object, string);
  102. if (vm.exception())
  103. return {};
  104. // c. Assert: ! IsValidTime(result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]]) is true.
  105. VERIFY(is_valid_time(result->hour, result->minute, result->second, result->millisecond, result->microsecond, result->nanosecond));
  106. // d. If result.[[Calendar]] is not one of undefined or "iso8601", then
  107. if (result->calendar.has_value() && *result->calendar != "iso8601"sv) {
  108. // i. Throw a RangeError exception.
  109. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidCalendarIdentifier, *result->calendar);
  110. return {};
  111. }
  112. }
  113. // 5. Return ? CreateTemporalTime(result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]]).
  114. return create_temporal_time(global_object, result->hour, result->minute, result->second, result->millisecond, result->microsecond, result->nanosecond);
  115. }
  116. // 4.5.4 RegulateTime ( hour, minute, second, millisecond, microsecond, nanosecond, overflow ), https://tc39.es/proposal-temporal/#sec-temporal-regulatetime
  117. Optional<TemporalTime> regulate_time(GlobalObject& global_object, double hour, double minute, double second, double millisecond, double microsecond, double nanosecond, StringView overflow)
  118. {
  119. auto& vm = global_object.vm();
  120. // 1. Assert: hour, minute, second, millisecond, microsecond and nanosecond are integers.
  121. // NOTE: As the spec is currently written this assertion can fail, these are either integers _or_ infinity.
  122. // See https://github.com/tc39/proposal-temporal/issues/1672.
  123. // 2. Assert: overflow is either "constrain" or "reject".
  124. // NOTE: Asserted by the VERIFY_NOT_REACHED at the end
  125. // 3. If overflow is "constrain", then
  126. if (overflow == "constrain"sv) {
  127. // a. Return ! ConstrainTime(hour, minute, second, millisecond, microsecond, nanosecond).
  128. return constrain_time(hour, minute, second, millisecond, microsecond, nanosecond);
  129. }
  130. // 4. If overflow is "reject", then
  131. if (overflow == "reject"sv) {
  132. // a. If ! IsValidTime(hour, minute, second, millisecond, microsecond, nanosecond) is false, throw a RangeError exception.
  133. if (!is_valid_time(hour, minute, second, millisecond, microsecond, nanosecond)) {
  134. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainTime);
  135. return {};
  136. }
  137. // b. Return the Record { [[Hour]]: hour, [[Minute]]: minute, [[Second]]: second, [[Millisecond]]: millisecond, [[Microsecond]]: microsecond, [[Nanosecond]]: nanosecond }.
  138. return TemporalTime { .hour = static_cast<u8>(hour), .minute = static_cast<u8>(minute), .second = static_cast<u8>(second), .millisecond = static_cast<u16>(millisecond), .microsecond = static_cast<u16>(microsecond), .nanosecond = static_cast<u16>(nanosecond) };
  139. }
  140. VERIFY_NOT_REACHED();
  141. }
  142. // 4.5.5 IsValidTime ( hour, minute, second, millisecond, microsecond, nanosecond ), https://tc39.es/proposal-temporal/#sec-temporal-isvalidtime
  143. bool is_valid_time(double hour, double minute, double second, double millisecond, double microsecond, double nanosecond)
  144. {
  145. // 1. Assert: hour, minute, second, millisecond, microsecond, and nanosecond are integers.
  146. // 2. If hour < 0 or hour > 23, then
  147. if (hour > 23) {
  148. // a. Return false.
  149. return false;
  150. }
  151. // 3. If minute < 0 or minute > 59, then
  152. if (minute > 59) {
  153. // a. Return false.
  154. return false;
  155. }
  156. // 4. If second < 0 or second > 59, then
  157. if (second > 59) {
  158. // a. Return false.
  159. return false;
  160. }
  161. // 5. If millisecond < 0 or millisecond > 999, then
  162. if (millisecond > 999) {
  163. // a. Return false.
  164. return false;
  165. }
  166. // 6. If microsecond < 0 or microsecond > 999, then
  167. if (microsecond > 999) {
  168. // a. Return false.
  169. return false;
  170. }
  171. // 7. If nanosecond < 0 or nanosecond > 999, then
  172. if (nanosecond > 999) {
  173. // a. Return false.
  174. return false;
  175. }
  176. // 8. Return true.
  177. return true;
  178. }
  179. // 4.5.6 BalanceTime ( hour, minute, second, millisecond, microsecond, nanosecond ), https://tc39.es/proposal-temporal/#sec-temporal-balancetime
  180. DaysAndTime balance_time(i64 hour, i64 minute, i64 second, i64 millisecond, i64 microsecond, i64 nanosecond)
  181. {
  182. // 1. Assert: hour, minute, second, millisecond, microsecond, and nanosecond are integers.
  183. // 2. Set microsecond to microsecond + floor(nanosecond / 1000).
  184. microsecond += nanosecond / 1000;
  185. // 3. Set nanosecond to nanosecond modulo 1000.
  186. nanosecond %= 1000;
  187. // 4. Set millisecond to millisecond + floor(microsecond / 1000).
  188. millisecond += microsecond / 1000;
  189. // 5. Set microsecond to microsecond modulo 1000.
  190. microsecond %= 1000;
  191. // 6. Set second to second + floor(millisecond / 1000).
  192. second += millisecond / 1000;
  193. // 7. Set millisecond to millisecond modulo 1000.
  194. millisecond %= 1000;
  195. // 8. Set minute to minute + floor(second / 60).
  196. minute += second / 60;
  197. // 9. Set second to second modulo 60.
  198. second %= 60;
  199. // 10. Set hour to hour + floor(minute / 60).
  200. hour += minute / 60;
  201. // 11. Set minute to minute modulo 60.
  202. minute %= 60;
  203. // 12. Let days be floor(hour / 24).
  204. u8 days = hour / 24;
  205. // 13. Set hour to hour modulo 24.
  206. hour %= 24;
  207. // 14. Return the Record { [[Days]]: days, [[Hour]]: hour, [[Minute]]: minute, [[Second]]: second, [[Millisecond]]: millisecond, [[Microsecond]]: microsecond, [[Nanosecond]]: nanosecond }.
  208. return DaysAndTime {
  209. .days = static_cast<i32>(days),
  210. .hour = static_cast<u8>(hour),
  211. .minute = static_cast<u8>(minute),
  212. .second = static_cast<u8>(second),
  213. .millisecond = static_cast<u16>(millisecond),
  214. .microsecond = static_cast<u16>(microsecond),
  215. .nanosecond = static_cast<u16>(nanosecond),
  216. };
  217. }
  218. // 4.5.7 ConstrainTime ( hour, minute, second, millisecond, microsecond, nanosecond ), https://tc39.es/proposal-temporal/#sec-temporal-constraintime
  219. TemporalTime constrain_time(double hour, double minute, double second, double millisecond, double microsecond, double nanosecond)
  220. {
  221. // 1. Assert: hour, minute, second, millisecond, microsecond, and nanosecond are integers.
  222. // 2. Set hour to ! ConstrainToRange(hour, 0, 23).
  223. hour = constrain_to_range(hour, 0, 23);
  224. // 3. Set minute to ! ConstrainToRange(minute, 0, 59).
  225. minute = constrain_to_range(minute, 0, 59);
  226. // 4. Set second to ! ConstrainToRange(second, 0, 59).
  227. second = constrain_to_range(second, 0, 59);
  228. // 5. Set millisecond to ! ConstrainToRange(millisecond, 0, 999).
  229. millisecond = constrain_to_range(millisecond, 0, 999);
  230. // 6. Set microsecond to ! ConstrainToRange(microsecond, 0, 999).
  231. microsecond = constrain_to_range(microsecond, 0, 999);
  232. // 7. Set nanosecond to ! ConstrainToRange(nanosecond, 0, 999).
  233. nanosecond = constrain_to_range(nanosecond, 0, 999);
  234. // 8. Return the Record { [[Hour]]: hour, [[Minute]]: minute, [[Second]]: second, [[Millisecond]]: millisecond, [[Microsecond]]: microsecond, [[Nanosecond]]: nanosecond }.
  235. return TemporalTime { .hour = static_cast<u8>(hour), .minute = static_cast<u8>(minute), .second = static_cast<u8>(second), .millisecond = static_cast<u16>(millisecond), .microsecond = static_cast<u16>(microsecond), .nanosecond = static_cast<u16>(nanosecond) };
  236. }
  237. // 4.5.8 CreateTemporalTime ( hour, minute, second, millisecond, microsecond, nanosecond [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporaltime
  238. PlainTime* create_temporal_time(GlobalObject& global_object, u8 hour, u8 minute, u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, FunctionObject* new_target)
  239. {
  240. auto& vm = global_object.vm();
  241. // 1. Assert: hour, minute, second, millisecond, microsecond and nanosecond are integers.
  242. // 2. If ! IsValidTime(hour, minute, second, millisecond, microsecond, nanosecond) is false, throw a RangeError exception.
  243. if (!is_valid_time(hour, minute, second, millisecond, microsecond, nanosecond)) {
  244. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidPlainTime);
  245. return {};
  246. }
  247. // 3. If newTarget is not present, set it to %Temporal.PlainTime%.
  248. if (!new_target)
  249. new_target = global_object.temporal_plain_time_constructor();
  250. // 4. Let object be ? OrdinaryCreateFromConstructor(newTarget, "%Temporal.PlainTime.prototype%", « [[InitializedTemporalTime]], [[ISOHour]], [[ISOMinute]], [[ISOSecond]], [[ISOMillisecond]], [[ISOMicrosecond]], [[ISONanosecond]], [[Calendar]] »).
  251. // 5. Set object.[[ISOHour]] to hour.
  252. // 6. Set object.[[ISOMinute]] to minute.
  253. // 7. Set object.[[ISOSecond]] to second.
  254. // 8. Set object.[[ISOMillisecond]] to millisecond.
  255. // 9. Set object.[[ISOMicrosecond]] to microsecond.
  256. // 10. Set object.[[ISONanosecond]] to nanosecond.
  257. // 11. Set object.[[Calendar]] to ! GetISO8601Calendar().
  258. auto* object = ordinary_create_from_constructor<PlainTime>(global_object, *new_target, &GlobalObject::temporal_plain_time_prototype, hour, minute, second, millisecond, microsecond, nanosecond, *get_iso8601_calendar(global_object));
  259. if (vm.exception())
  260. return {};
  261. // 12. Return object.
  262. return object;
  263. }
  264. // 4.5.9 ToTemporalTimeRecord ( temporalTimeLike ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaltimerecord
  265. Optional<UnregulatedTemporalTime> to_temporal_time_record(GlobalObject& global_object, Object& temporal_time_like)
  266. {
  267. auto& vm = global_object.vm();
  268. // 1. Assert: Type(temporalTimeLike) is Object.
  269. // 2. Let result be the Record { [[Hour]]: undefined, [[Minute]]: undefined, [[Second]]: undefined, [[Millisecond]]: undefined, [[Microsecond]]: undefined, [[Nanosecond]]: undefined }.
  270. auto result = UnregulatedTemporalTime {};
  271. // 3. For each row of Table 3, except the header row, in table order, do
  272. for (auto& [internal_slot, property] : temporal_time_like_properties<UnregulatedTemporalTime, double>(vm)) {
  273. // a. Let property be the Property value of the current row.
  274. // b. Let value be ? Get(temporalTimeLike, property).
  275. auto value = temporal_time_like.get(property);
  276. if (vm.exception())
  277. return {};
  278. // c. If value is undefined, then
  279. if (value.is_undefined()) {
  280. // i. Throw a TypeError exception.
  281. vm.throw_exception<TypeError>(global_object, ErrorType::TemporalMissingRequiredProperty, property);
  282. return {};
  283. }
  284. // d. Set value to ? ToIntegerThrowOnInfinity(value).
  285. auto value_number = to_integer_throw_on_infinity(global_object, value, ErrorType::TemporalPropertyMustBeFinite);
  286. if (vm.exception())
  287. return {};
  288. // e. Set result's internal slot whose name is the Internal Slot value of the current row to value.
  289. result.*internal_slot = value_number;
  290. }
  291. // 4. Return result.
  292. return result;
  293. }
  294. // 4.5.11 CompareTemporalTime ( h1, min1, s1, ms1, mus1, ns1, h2, min2, s2, ms2, mus2, ns2 ), https://tc39.es/proposal-temporal/#sec-temporal-comparetemporaltime
  295. i8 compare_temporal_time(u8 hour1, u8 minute1, u8 second1, u16 millisecond1, u16 microsecond1, u16 nanosecond1, u8 hour2, u8 minute2, u8 second2, u16 millisecond2, u16 microsecond2, u16 nanosecond2)
  296. {
  297. // 1. Assert: h1, min1, s1, ms1, mus1, ns1, h2, min2, s2, ms2, mus2, and ns2 are integers.
  298. // 2. If h1 > h2, return 1.
  299. if (hour1 > hour2)
  300. return 1;
  301. // 3. If h1 < h2, return -1.
  302. if (hour1 < hour2)
  303. return -1;
  304. // 4. If min1 > min2, return 1.
  305. if (minute1 > minute2)
  306. return 1;
  307. // 5. If min1 < min2, return -1.
  308. if (minute1 < minute2)
  309. return -1;
  310. // 6. If s1 > s2, return 1.
  311. if (second1 > second2)
  312. return 1;
  313. // 7. If s1 < s2, return -1.
  314. if (second1 < second2)
  315. return -1;
  316. // 8. If ms1 > ms2, return 1.
  317. if (millisecond1 > millisecond2)
  318. return 1;
  319. // 9. If ms1 < ms2, return -1.
  320. if (millisecond1 < millisecond2)
  321. return -1;
  322. // 10. If mus1 > mus2, return 1.
  323. if (microsecond1 > microsecond2)
  324. return 1;
  325. // 11. If mus1 < mus2, return -1.
  326. if (microsecond1 < microsecond2)
  327. return -1;
  328. // 12. If ns1 > ns2, return 1.
  329. if (nanosecond1 > nanosecond2)
  330. return 1;
  331. // 13. If ns1 < ns2, return -1.
  332. if (nanosecond1 < nanosecond2)
  333. return -1;
  334. // 14. Return 0.
  335. return 0;
  336. }
  337. }