PlainTime.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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. ThrowCompletionOr<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 = TRY(builtin_time_zone_get_plain_date_time_for(global_object, &zoned_date_time.time_zone(), *instant, zoned_date_time.calendar()));
  61. // iii. Return ! CreateTemporalTime(plainDateTime.[[ISOHour]], plainDateTime.[[ISOMinute]], plainDateTime.[[ISOSecond]], plainDateTime.[[ISOMillisecond]], plainDateTime.[[ISOMicrosecond]], plainDateTime.[[ISONanosecond]]).
  62. return TRY(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()));
  63. }
  64. // c. If item has an [[InitializedTemporalDateTime]] internal slot, then
  65. if (is<PlainDateTime>(item_object)) {
  66. auto& plain_date_time = static_cast<PlainDateTime&>(item_object);
  67. // i. Return ! CreateTemporalTime(item.[[ISOHour]], item.[[ISOMinute]], item.[[ISOSecond]], item.[[ISOMillisecond]], item.[[ISOMicrosecond]], item.[[ISONanosecond]]).
  68. return TRY(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()));
  69. }
  70. // d. Let calendar be ? GetTemporalCalendarWithISODefault(item).
  71. auto* calendar = get_temporal_calendar_with_iso_default(global_object, item_object);
  72. if (auto* exception = vm.exception())
  73. return throw_completion(exception->value());
  74. // e. If ? ToString(calendar) is not "iso8601", then
  75. auto calendar_identifier = Value(calendar).to_string(global_object);
  76. if (auto* exception = vm.exception())
  77. return throw_completion(exception->value());
  78. if (calendar_identifier != "iso8601"sv) {
  79. // i. Throw a RangeError exception.
  80. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidCalendarIdentifier, calendar_identifier);
  81. }
  82. // f. Let result be ? ToTemporalTimeRecord(item).
  83. auto unregulated_result = TRY(to_temporal_time_record(global_object, item_object));
  84. // g. Set result to ? RegulateTime(result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]], overflow).
  85. result = TRY(regulate_time(global_object, unregulated_result.hour, unregulated_result.minute, unregulated_result.second, unregulated_result.millisecond, unregulated_result.microsecond, unregulated_result.nanosecond, *overflow));
  86. }
  87. // 4. Else,
  88. else {
  89. // a. Let string be ? ToString(item).
  90. auto string = item.to_string(global_object);
  91. if (auto* exception = vm.exception())
  92. return throw_completion(exception->value());
  93. // b. Let result be ? ParseTemporalTimeString(string).
  94. result = parse_temporal_time_string(global_object, string);
  95. if (auto* exception = vm.exception())
  96. return throw_completion(exception->value());
  97. // c. Assert: ! IsValidTime(result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]]) is true.
  98. VERIFY(is_valid_time(result->hour, result->minute, result->second, result->millisecond, result->microsecond, result->nanosecond));
  99. // d. If result.[[Calendar]] is not one of undefined or "iso8601", then
  100. if (result->calendar.has_value() && *result->calendar != "iso8601"sv) {
  101. // i. Throw a RangeError exception.
  102. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidCalendarIdentifier, *result->calendar);
  103. }
  104. }
  105. // 5. Return ? CreateTemporalTime(result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]]).
  106. return TRY(create_temporal_time(global_object, result->hour, result->minute, result->second, result->millisecond, result->microsecond, result->nanosecond));
  107. }
  108. // 4.5.3 ToPartialTime ( temporalTimeLike ), https://tc39.es/proposal-temporal/#sec-temporal-topartialtime
  109. ThrowCompletionOr<PartialUnregulatedTemporalTime> to_partial_time(GlobalObject& global_object, Object& temporal_time_like)
  110. {
  111. auto& vm = global_object.vm();
  112. // 1. Assert: Type(temporalTimeLike) is Object.
  113. // 2. Let result be the Record { [[Hour]]: undefined, [[Minute]]: undefined, [[Second]]: undefined, [[Millisecond]]: undefined, [[Microsecond]]: undefined, [[Nanosecond]]: undefined }.
  114. auto result = PartialUnregulatedTemporalTime {};
  115. // 3. Let any be false.
  116. bool any = false;
  117. // 4. For each row of Table 3, except the header row, in table order, do
  118. for (auto& [internal_slot, property] : temporal_time_like_properties<PartialUnregulatedTemporalTime, Optional<double>>(vm)) {
  119. // a. Let property be the Property value of the current row.
  120. // b. Let value be ? Get(temporalTimeLike, property).
  121. auto value = temporal_time_like.get(property);
  122. if (auto* exception = vm.exception())
  123. return throw_completion(exception->value());
  124. // c. If value is not undefined, then
  125. if (!value.is_undefined()) {
  126. // i. Set any to true.
  127. any = true;
  128. // ii. Set value to ? ToIntegerThrowOnInfinity(value).
  129. auto value_number = to_integer_throw_on_infinity(global_object, value, ErrorType::TemporalPropertyMustBeFinite);
  130. if (auto* exception = vm.exception())
  131. return throw_completion(exception->value());
  132. // iii. Set result's internal slot whose name is the Internal Slot value of the current row to value.
  133. result.*internal_slot = value_number;
  134. }
  135. }
  136. // 5. If any is false, then
  137. if (!any) {
  138. // a. Throw a TypeError exception.
  139. return vm.throw_completion<TypeError>(global_object, ErrorType::TemporalInvalidPlainTimeLikeObject);
  140. }
  141. // 6. Return result.
  142. return result;
  143. }
  144. // 4.5.4 RegulateTime ( hour, minute, second, millisecond, microsecond, nanosecond, overflow ), https://tc39.es/proposal-temporal/#sec-temporal-regulatetime
  145. ThrowCompletionOr<TemporalTime> regulate_time(GlobalObject& global_object, double hour, double minute, double second, double millisecond, double microsecond, double nanosecond, StringView overflow)
  146. {
  147. auto& vm = global_object.vm();
  148. // 1. Assert: hour, minute, second, millisecond, microsecond and nanosecond are integers.
  149. // NOTE: As the spec is currently written this assertion can fail, these are either integers _or_ infinity.
  150. // See https://github.com/tc39/proposal-temporal/issues/1672.
  151. // 2. Assert: overflow is either "constrain" or "reject".
  152. // NOTE: Asserted by the VERIFY_NOT_REACHED at the end
  153. // 3. If overflow is "constrain", then
  154. if (overflow == "constrain"sv) {
  155. // a. Return ! ConstrainTime(hour, minute, second, millisecond, microsecond, nanosecond).
  156. return constrain_time(hour, minute, second, millisecond, microsecond, nanosecond);
  157. }
  158. // 4. If overflow is "reject", then
  159. if (overflow == "reject"sv) {
  160. // a. If ! IsValidTime(hour, minute, second, millisecond, microsecond, nanosecond) is false, throw a RangeError exception.
  161. if (!is_valid_time(hour, minute, second, millisecond, microsecond, nanosecond))
  162. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidPlainTime);
  163. // b. Return the Record { [[Hour]]: hour, [[Minute]]: minute, [[Second]]: second, [[Millisecond]]: millisecond, [[Microsecond]]: microsecond, [[Nanosecond]]: nanosecond }.
  164. 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) };
  165. }
  166. VERIFY_NOT_REACHED();
  167. }
  168. // 4.5.5 IsValidTime ( hour, minute, second, millisecond, microsecond, nanosecond ), https://tc39.es/proposal-temporal/#sec-temporal-isvalidtime
  169. bool is_valid_time(double hour, double minute, double second, double millisecond, double microsecond, double nanosecond)
  170. {
  171. // 1. Assert: hour, minute, second, millisecond, microsecond, and nanosecond are integers.
  172. // 2. If hour < 0 or hour > 23, then
  173. if (hour > 23) {
  174. // a. Return false.
  175. return false;
  176. }
  177. // 3. If minute < 0 or minute > 59, then
  178. if (minute > 59) {
  179. // a. Return false.
  180. return false;
  181. }
  182. // 4. If second < 0 or second > 59, then
  183. if (second > 59) {
  184. // a. Return false.
  185. return false;
  186. }
  187. // 5. If millisecond < 0 or millisecond > 999, then
  188. if (millisecond > 999) {
  189. // a. Return false.
  190. return false;
  191. }
  192. // 6. If microsecond < 0 or microsecond > 999, then
  193. if (microsecond > 999) {
  194. // a. Return false.
  195. return false;
  196. }
  197. // 7. If nanosecond < 0 or nanosecond > 999, then
  198. if (nanosecond > 999) {
  199. // a. Return false.
  200. return false;
  201. }
  202. // 8. Return true.
  203. return true;
  204. }
  205. // 4.5.6 BalanceTime ( hour, minute, second, millisecond, microsecond, nanosecond ), https://tc39.es/proposal-temporal/#sec-temporal-balancetime
  206. DaysAndTime balance_time(i64 hour, i64 minute, i64 second, i64 millisecond, i64 microsecond, i64 nanosecond)
  207. {
  208. // 1. Assert: hour, minute, second, millisecond, microsecond, and nanosecond are integers.
  209. // 2. Set microsecond to microsecond + floor(nanosecond / 1000).
  210. microsecond += nanosecond / 1000;
  211. // 3. Set nanosecond to nanosecond modulo 1000.
  212. nanosecond %= 1000;
  213. // 4. Set millisecond to millisecond + floor(microsecond / 1000).
  214. millisecond += microsecond / 1000;
  215. // 5. Set microsecond to microsecond modulo 1000.
  216. microsecond %= 1000;
  217. // 6. Set second to second + floor(millisecond / 1000).
  218. second += millisecond / 1000;
  219. // 7. Set millisecond to millisecond modulo 1000.
  220. millisecond %= 1000;
  221. // 8. Set minute to minute + floor(second / 60).
  222. minute += second / 60;
  223. // 9. Set second to second modulo 60.
  224. second %= 60;
  225. // 10. Set hour to hour + floor(minute / 60).
  226. hour += minute / 60;
  227. // 11. Set minute to minute modulo 60.
  228. minute %= 60;
  229. // 12. Let days be floor(hour / 24).
  230. u8 days = hour / 24;
  231. // 13. Set hour to hour modulo 24.
  232. hour %= 24;
  233. // 14. Return the Record { [[Days]]: days, [[Hour]]: hour, [[Minute]]: minute, [[Second]]: second, [[Millisecond]]: millisecond, [[Microsecond]]: microsecond, [[Nanosecond]]: nanosecond }.
  234. return DaysAndTime {
  235. .days = static_cast<i32>(days),
  236. .hour = static_cast<u8>(hour),
  237. .minute = static_cast<u8>(minute),
  238. .second = static_cast<u8>(second),
  239. .millisecond = static_cast<u16>(millisecond),
  240. .microsecond = static_cast<u16>(microsecond),
  241. .nanosecond = static_cast<u16>(nanosecond),
  242. };
  243. }
  244. // 4.5.7 ConstrainTime ( hour, minute, second, millisecond, microsecond, nanosecond ), https://tc39.es/proposal-temporal/#sec-temporal-constraintime
  245. TemporalTime constrain_time(double hour, double minute, double second, double millisecond, double microsecond, double nanosecond)
  246. {
  247. // 1. Assert: hour, minute, second, millisecond, microsecond, and nanosecond are integers.
  248. // 2. Set hour to ! ConstrainToRange(hour, 0, 23).
  249. hour = constrain_to_range(hour, 0, 23);
  250. // 3. Set minute to ! ConstrainToRange(minute, 0, 59).
  251. minute = constrain_to_range(minute, 0, 59);
  252. // 4. Set second to ! ConstrainToRange(second, 0, 59).
  253. second = constrain_to_range(second, 0, 59);
  254. // 5. Set millisecond to ! ConstrainToRange(millisecond, 0, 999).
  255. millisecond = constrain_to_range(millisecond, 0, 999);
  256. // 6. Set microsecond to ! ConstrainToRange(microsecond, 0, 999).
  257. microsecond = constrain_to_range(microsecond, 0, 999);
  258. // 7. Set nanosecond to ! ConstrainToRange(nanosecond, 0, 999).
  259. nanosecond = constrain_to_range(nanosecond, 0, 999);
  260. // 8. Return the Record { [[Hour]]: hour, [[Minute]]: minute, [[Second]]: second, [[Millisecond]]: millisecond, [[Microsecond]]: microsecond, [[Nanosecond]]: nanosecond }.
  261. 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) };
  262. }
  263. // 4.5.8 CreateTemporalTime ( hour, minute, second, millisecond, microsecond, nanosecond [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporaltime
  264. ThrowCompletionOr<PlainTime*> create_temporal_time(GlobalObject& global_object, u8 hour, u8 minute, u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, FunctionObject const* new_target)
  265. {
  266. auto& vm = global_object.vm();
  267. // 1. Assert: hour, minute, second, millisecond, microsecond and nanosecond are integers.
  268. // 2. If ! IsValidTime(hour, minute, second, millisecond, microsecond, nanosecond) is false, throw a RangeError exception.
  269. if (!is_valid_time(hour, minute, second, millisecond, microsecond, nanosecond))
  270. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidPlainTime);
  271. // 3. If newTarget is not present, set it to %Temporal.PlainTime%.
  272. if (!new_target)
  273. new_target = global_object.temporal_plain_time_constructor();
  274. // 4. Let object be ? OrdinaryCreateFromConstructor(newTarget, "%Temporal.PlainTime.prototype%", « [[InitializedTemporalTime]], [[ISOHour]], [[ISOMinute]], [[ISOSecond]], [[ISOMillisecond]], [[ISOMicrosecond]], [[ISONanosecond]], [[Calendar]] »).
  275. // 5. Set object.[[ISOHour]] to hour.
  276. // 6. Set object.[[ISOMinute]] to minute.
  277. // 7. Set object.[[ISOSecond]] to second.
  278. // 8. Set object.[[ISOMillisecond]] to millisecond.
  279. // 9. Set object.[[ISOMicrosecond]] to microsecond.
  280. // 10. Set object.[[ISONanosecond]] to nanosecond.
  281. // 11. Set object.[[Calendar]] to ! GetISO8601Calendar().
  282. auto* object = TRY(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)));
  283. // 12. Return object.
  284. return object;
  285. }
  286. // 4.5.9 ToTemporalTimeRecord ( temporalTimeLike ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaltimerecord
  287. ThrowCompletionOr<UnregulatedTemporalTime> to_temporal_time_record(GlobalObject& global_object, Object const& temporal_time_like)
  288. {
  289. auto& vm = global_object.vm();
  290. // 1. Assert: Type(temporalTimeLike) is Object.
  291. // 2. Let result be the Record { [[Hour]]: undefined, [[Minute]]: undefined, [[Second]]: undefined, [[Millisecond]]: undefined, [[Microsecond]]: undefined, [[Nanosecond]]: undefined }.
  292. auto result = UnregulatedTemporalTime {};
  293. // 3. For each row of Table 3, except the header row, in table order, do
  294. for (auto& [internal_slot, property] : temporal_time_like_properties<UnregulatedTemporalTime, double>(vm)) {
  295. // a. Let property be the Property value of the current row.
  296. // b. Let value be ? Get(temporalTimeLike, property).
  297. auto value = temporal_time_like.get(property);
  298. if (auto* exception = vm.exception())
  299. return throw_completion(exception->value());
  300. // c. If value is undefined, then
  301. if (value.is_undefined()) {
  302. // i. Throw a TypeError exception.
  303. return vm.throw_completion<TypeError>(global_object, ErrorType::TemporalMissingRequiredProperty, property);
  304. }
  305. // d. Set value to ? ToIntegerThrowOnInfinity(value).
  306. auto value_number = to_integer_throw_on_infinity(global_object, value, ErrorType::TemporalPropertyMustBeFinite);
  307. if (auto* exception = vm.exception())
  308. return throw_completion(exception->value());
  309. // e. Set result's internal slot whose name is the Internal Slot value of the current row to value.
  310. result.*internal_slot = value_number;
  311. }
  312. // 4. Return result.
  313. return result;
  314. }
  315. // 4.5.10 TemporalTimeToString ( hour, minute, second, millisecond, microsecond, nanosecond, precision ), https://tc39.es/proposal-temporal/#sec-temporal-temporaltimetostring
  316. String temporal_time_to_string(u8 hour, u8 minute, u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, Variant<StringView, u8> const& precision)
  317. {
  318. // 1. Assert: hour, minute, second, millisecond, microsecond and nanosecond are integers.
  319. // 2. Let hour be hour formatted as a two-digit decimal number, padded to the left with a zero if necessary.
  320. // 3. Let minute be minute formatted as a two-digit decimal number, padded to the left with a zero if necessary.
  321. // 4. Let seconds be ! FormatSecondsStringPart(second, millisecond, microsecond, nanosecond, precision).
  322. auto seconds = format_seconds_string_part(second, millisecond, microsecond, nanosecond, precision);
  323. // 5. Return the string-concatenation of hour, the code unit 0x003A (COLON), minute, and seconds.
  324. return String::formatted("{:02}:{:02}{}", hour, minute, seconds);
  325. }
  326. // 4.5.11 CompareTemporalTime ( h1, min1, s1, ms1, mus1, ns1, h2, min2, s2, ms2, mus2, ns2 ), https://tc39.es/proposal-temporal/#sec-temporal-comparetemporaltime
  327. 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)
  328. {
  329. // 1. Assert: h1, min1, s1, ms1, mus1, ns1, h2, min2, s2, ms2, mus2, and ns2 are integers.
  330. // 2. If h1 > h2, return 1.
  331. if (hour1 > hour2)
  332. return 1;
  333. // 3. If h1 < h2, return -1.
  334. if (hour1 < hour2)
  335. return -1;
  336. // 4. If min1 > min2, return 1.
  337. if (minute1 > minute2)
  338. return 1;
  339. // 5. If min1 < min2, return -1.
  340. if (minute1 < minute2)
  341. return -1;
  342. // 6. If s1 > s2, return 1.
  343. if (second1 > second2)
  344. return 1;
  345. // 7. If s1 < s2, return -1.
  346. if (second1 < second2)
  347. return -1;
  348. // 8. If ms1 > ms2, return 1.
  349. if (millisecond1 > millisecond2)
  350. return 1;
  351. // 9. If ms1 < ms2, return -1.
  352. if (millisecond1 < millisecond2)
  353. return -1;
  354. // 10. If mus1 > mus2, return 1.
  355. if (microsecond1 > microsecond2)
  356. return 1;
  357. // 11. If mus1 < mus2, return -1.
  358. if (microsecond1 < microsecond2)
  359. return -1;
  360. // 12. If ns1 > ns2, return 1.
  361. if (nanosecond1 > nanosecond2)
  362. return 1;
  363. // 13. If ns1 < ns2, return -1.
  364. if (nanosecond1 < nanosecond2)
  365. return -1;
  366. // 14. Return 0.
  367. return 0;
  368. }
  369. // 4.5.13 RoundTime ( hour, minute, second, millisecond, microsecond, nanosecond, increment, unit, roundingMode [ , dayLengthNs ] ), https://tc39.es/proposal-temporal/#sec-temporal-roundtime
  370. DaysAndTime round_time(GlobalObject& global_object, u8 hour, u8 minute, u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, u64 increment, StringView unit, StringView rounding_mode, Optional<double> day_length_ns)
  371. {
  372. // 1. Assert: hour, minute, second, millisecond, microsecond, nanosecond, and increment are integers.
  373. // 2. Let fractionalSecond be nanosecond × 10−9 + microsecond × 10−6 + millisecond × 10−3 + second.
  374. double fractional_second = nanosecond * 0.000000001 + microsecond * 0.000001 + millisecond * 0.001 + second;
  375. double quantity;
  376. // 3. If unit is "day", then
  377. if (unit == "day"sv) {
  378. // a. If dayLengthNs is not present, set it to 8.64 × 10^13.
  379. if (!day_length_ns.has_value())
  380. day_length_ns = 86400000000000;
  381. // b. Let quantity be (((((hour × 60 + minute) × 60 + second) × 1000 + millisecond) × 1000 + microsecond) × 1000 + nanosecond) / dayLengthNs.
  382. quantity = (((((hour * 60 + minute) * 60 + second) * 1000 + millisecond) * 1000 + microsecond) * 1000 + nanosecond) / *day_length_ns;
  383. }
  384. // 4. Else if unit is "hour", then
  385. else if (unit == "hour"sv) {
  386. // a. Let quantity be (fractionalSecond / 60 + minute) / 60 + hour.
  387. quantity = (fractional_second / 60 + minute) / 60 + hour;
  388. }
  389. // 5. Else if unit is "minute", then
  390. else if (unit == "minute"sv) {
  391. // a. Let quantity be fractionalSecond / 60 + minute.
  392. quantity = fractional_second / 60 + minute;
  393. }
  394. // 6. Else if unit is "second", then
  395. else if (unit == "second"sv) {
  396. // a. Let quantity be fractionalSecond.
  397. quantity = fractional_second;
  398. }
  399. // 7. Else if unit is "millisecond", then
  400. else if (unit == "millisecond"sv) {
  401. // a. Let quantity be nanosecond × 10−6 + microsecond × 10−3 + millisecond.
  402. quantity = nanosecond * 0.000001 + 0.001 * microsecond + millisecond;
  403. }
  404. // 8. Else if unit is "microsecond", then
  405. else if (unit == "microsecond"sv) {
  406. // a. Let quantity be nanosecond × 10−3 + microsecond.
  407. quantity = nanosecond * 0.001 + microsecond;
  408. }
  409. // 9. Else,
  410. else {
  411. // a. Assert: unit is "nanosecond".
  412. VERIFY(unit == "nanosecond"sv);
  413. // b. Let quantity be nanosecond.
  414. quantity = nanosecond;
  415. }
  416. // FIXME: This doesn't seem right...
  417. auto* quantity_bigint = js_bigint(global_object.vm(), Crypto::SignedBigInteger::create_from((u64)quantity));
  418. // 10. Let result be ! RoundNumberToIncrement(quantity, increment, roundingMode).
  419. auto* result = round_number_to_increment(global_object, *quantity_bigint, increment, rounding_mode);
  420. auto result_i64 = (i64)result->big_integer().to_double();
  421. // If unit is "day", then
  422. if (unit == "day"sv) {
  423. // a. Return the Record { [[Days]]: result, [[Hour]]: 0, [[Minute]]: 0, [[Second]]: 0, [[Millisecond]]: 0, [[Microsecond]]: 0, [[Nanosecond]]: 0 }.
  424. return DaysAndTime { .days = (i32)result_i64, .hour = 0, .minute = 0, .second = 0, .millisecond = 0, .microsecond = 0, .nanosecond = 0 };
  425. }
  426. // 12. If unit is "hour", then
  427. if (unit == "hour"sv) {
  428. // a. Return ! BalanceTime(result, 0, 0, 0, 0, 0).
  429. return balance_time(result_i64, 0, 0, 0, 0, 0);
  430. }
  431. // 13. If unit is "minute", then
  432. if (unit == "minute"sv) {
  433. // a. Return ! BalanceTime(hour, result, 0, 0, 0, 0).
  434. return balance_time(hour, result_i64, 0, 0, 0, 0);
  435. }
  436. // 14. If unit is "second", then
  437. if (unit == "second"sv) {
  438. // a. Return ! BalanceTime(hour, minute, result, 0, 0, 0).
  439. return balance_time(hour, minute, result_i64, 0, 0, 0);
  440. }
  441. // 15. If unit is "millisecond", then
  442. if (unit == "millisecond"sv) {
  443. // a. Return ! BalanceTime(hour, minute, second, result, 0, 0).
  444. return balance_time(hour, minute, second, result_i64, 0, 0);
  445. }
  446. // 16. If unit is "microsecond", then
  447. if (unit == "microsecond"sv) {
  448. // a. Return ! BalanceTime(hour, minute, second, millisecond, result, 0).
  449. return balance_time(hour, minute, second, millisecond, result_i64, 0);
  450. }
  451. // 17. Assert: unit is "nanosecond".
  452. VERIFY(unit == "nanosecond"sv);
  453. // 18. Return ! BalanceTime(hour, minute, second, millisecond, microsecond, result).
  454. return balance_time(hour, minute, second, millisecond, microsecond, result_i64);
  455. }
  456. }