PlainTime.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2024, Tim Flynn <trflynn89@ladybird.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/Assertions.h>
  9. #include <LibJS/Runtime/AbstractOperations.h>
  10. #include <LibJS/Runtime/Temporal/Duration.h>
  11. #include <LibJS/Runtime/Temporal/Instant.h>
  12. #include <LibJS/Runtime/Temporal/PlainTime.h>
  13. #include <LibJS/Runtime/Temporal/PlainTimeConstructor.h>
  14. #include <math.h>
  15. namespace JS::Temporal {
  16. GC_DEFINE_ALLOCATOR(PlainTime);
  17. // 4 Temporal.PlainTime Objects, https://tc39.es/proposal-temporal/#sec-temporal-plaintime-objects
  18. PlainTime::PlainTime(Time const& time, Object& prototype)
  19. : Object(ConstructWithPrototypeTag::Tag, prototype)
  20. , m_time(time)
  21. {
  22. }
  23. // FIXME: We should add a generic floor() method to our BigInt classes. But for now, since we know we are only dividing
  24. // by powers of 10, we can implement a very situationally specific method to compute the floor of a division.
  25. static TimeDuration big_floor(TimeDuration const& numerator, Crypto::UnsignedBigInteger const& denominator)
  26. {
  27. auto result = numerator.divided_by(denominator);
  28. if (result.remainder.is_zero())
  29. return result.quotient;
  30. if (!result.quotient.is_negative() && result.remainder.is_positive())
  31. return result.quotient;
  32. return result.quotient.minus(TimeDuration { 1 });
  33. }
  34. // 4.5.2 CreateTimeRecord ( hour, minute, second, millisecond, microsecond, nanosecond [ , deltaDays ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtimerecord
  35. Time create_time_record(double hour, double minute, double second, double millisecond, double microsecond, double nanosecond, double delta_days)
  36. {
  37. // 1. If deltaDays is not present, set deltaDays to 0.
  38. // 2. Assert: IsValidTime(hour, minute, second, millisecond, microsecond, nanosecond).
  39. VERIFY(is_valid_time(hour, minute, second, millisecond, microsecond, nanosecond));
  40. // 3. Return Time Record { [[Days]]: deltaDays, [[Hour]]: hour, [[Minute]]: minute, [[Second]]: second, [[Millisecond]]: millisecond, [[Microsecond]]: microsecond, [[Nanosecond]]: nanosecond }.
  41. return {
  42. .days = delta_days,
  43. .hour = static_cast<u8>(hour),
  44. .minute = static_cast<u8>(minute),
  45. .second = static_cast<u8>(second),
  46. .millisecond = static_cast<u16>(millisecond),
  47. .microsecond = static_cast<u16>(microsecond),
  48. .nanosecond = static_cast<u16>(nanosecond),
  49. };
  50. }
  51. // 4.5.3 MidnightTimeRecord ( ), https://tc39.es/proposal-temporal/#sec-temporal-midnighttimerecord
  52. Time midnight_time_record()
  53. {
  54. // 1. Return Time Record { [[Days]]: 0, [[Hour]]: 0, [[Minute]]: 0, [[Second]]: 0, [[Millisecond]]: 0, [[Microsecond]]: 0, [[Nanosecond]]: 0 }.
  55. return { .days = 0, .hour = 0, .minute = 0, .second = 0, .millisecond = 0, .microsecond = 0, .nanosecond = 0 };
  56. }
  57. // 4.5.4 NoonTimeRecord ( ), https://tc39.es/proposal-temporal/#sec-temporal-noontimerecord
  58. Time noon_time_record()
  59. {
  60. // 1. Return Time Record { [[Days]]: 0, [[Hour]]: 12, [[Minute]]: 0, [[Second]]: 0, [[Millisecond]]: 0, [[Microsecond]]: 0, [[Nanosecond]]: 0 }.
  61. return { .days = 0, .hour = 12, .minute = 0, .second = 0, .millisecond = 0, .microsecond = 0, .nanosecond = 0 };
  62. }
  63. // 4.5.5 DifferenceTime ( time1, time2 ), https://tc39.es/proposal-temporal/#sec-temporal-differencetime
  64. TimeDuration difference_time(Time const& time1, Time const& time2)
  65. {
  66. // 1. Let hours be time2.[[Hour]] - time1.[[Hour]].
  67. auto hours = static_cast<double>(time2.hour) - static_cast<double>(time1.hour);
  68. // 2. Let minutes be time2.[[Minute]] - time1.[[Minute]].
  69. auto minutes = static_cast<double>(time2.minute) - static_cast<double>(time1.minute);
  70. // 3. Let seconds be time2.[[Second]] - time1.[[Second]].
  71. auto seconds = static_cast<double>(time2.second) - static_cast<double>(time1.second);
  72. // 4. Let milliseconds be time2.[[Millisecond]] - time1.[[Millisecond]].
  73. auto milliseconds = static_cast<double>(time2.millisecond) - static_cast<double>(time1.millisecond);
  74. // 5. Let microseconds be time2.[[Microsecond]] - time1.[[Microsecond]].
  75. auto microseconds = static_cast<double>(time2.microsecond) - static_cast<double>(time1.microsecond);
  76. // 6. Let nanoseconds be time2.[[Nanosecond]] - time1.[[Nanosecond]].
  77. auto nanoseconds = static_cast<double>(time2.nanosecond) - static_cast<double>(time1.nanosecond);
  78. // 7. Let timeDuration be TimeDurationFromComponents(hours, minutes, seconds, milliseconds, microseconds, nanoseconds).
  79. auto time_duration = time_duration_from_components(hours, minutes, seconds, milliseconds, microseconds, nanoseconds);
  80. // 8. Assert: abs(timeDuration) < nsPerDay.
  81. VERIFY(time_duration.unsigned_value() < NANOSECONDS_PER_DAY);
  82. // 9. Return timeDuration.
  83. return time_duration;
  84. }
  85. // 4.5.6 ToTemporalTime ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaltime
  86. ThrowCompletionOr<GC::Ref<PlainTime>> to_temporal_time(VM& vm, Value item, Value options)
  87. {
  88. // 1. If options is not present, set options to undefined.
  89. Time time;
  90. // 2. If item is an Object, then
  91. if (item.is_object()) {
  92. auto const& object = item.as_object();
  93. // a. If item has an [[InitializedTemporalTime]] internal slot, then
  94. if (is<PlainTime>(object)) {
  95. auto const& plain_time = static_cast<PlainTime const&>(object);
  96. // i. Let resolvedOptions be ? GetOptionsObject(options).
  97. auto resolved_options = TRY(get_options_object(vm, options));
  98. // ii. Perform ? GetTemporalOverflowOption(resolvedOptions).
  99. TRY(get_temporal_overflow_option(vm, resolved_options));
  100. // iii. Return ! CreateTemporalTime(item.[[Time]]).
  101. return MUST(create_temporal_time(vm, plain_time.time()));
  102. }
  103. // FIXME: b. If item has an [[InitializedTemporalDateTime]] internal slot, then
  104. // FIXME: i. Let resolvedOptions be ? GetOptionsObject(options).
  105. // FIXME: ii. Perform ? GetTemporalOverflowOption(resolvedOptions).
  106. // FIXME: iii. Return ! CreateTemporalTime(item.[[ISODateTime]].[[Time]]).
  107. // FIXME: c. If item has an [[InitializedTemporalZonedDateTime]] internal slot, then
  108. // FIXME: i. Let isoDateTime be GetISODateTimeFor(item.[[TimeZone]], item.[[EpochNanoseconds]]).
  109. // FIXME: ii. Let resolvedOptions be ? GetOptionsObject(options).
  110. // FIXME: iii. Perform ? GetTemporalOverflowOption(resolvedOptions).
  111. // FIXME: iv. Return ! CreateTemporalTime(isoDateTime.[[Time]]).
  112. // d. Let result be ? ToTemporalTimeRecord(item).
  113. auto result = TRY(to_temporal_time_record(vm, object));
  114. // e. Let resolvedOptions be ? GetOptionsObject(options).
  115. auto resolved_options = TRY(get_options_object(vm, options));
  116. // f. Let overflow be ? GetTemporalOverflowOption(resolvedOptions).
  117. auto overflow = TRY(get_temporal_overflow_option(vm, resolved_options));
  118. // g. Set result to ? RegulateTime(result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]], overflow).
  119. time = TRY(regulate_time(vm, *result.hour, *result.minute, *result.second, *result.millisecond, *result.microsecond, *result.nanosecond, overflow));
  120. }
  121. // 3. Else,
  122. else {
  123. // a. If item is not a String, throw a TypeError exception.
  124. if (!item.is_string())
  125. return vm.throw_completion<TypeError>(ErrorType::TemporalInvalidPlainTime);
  126. // b. Let parseResult be ? ParseISODateTime(item, « TemporalTimeString »).
  127. auto parse_result = TRY(parse_iso_date_time(vm, item.as_string().utf8_string_view(), { { Production::TemporalTimeString } }));
  128. // c. Assert: parseResult.[[Time]] is not START-OF-DAY.
  129. VERIFY(!parse_result.time.has<ParsedISODateTime::StartOfDay>());
  130. // d. Set result to parseResult.[[Time]].
  131. time = parse_result.time.get<Time>();
  132. // e. NOTE: A successful parse using TemporalTimeString guarantees absence of ambiguity with respect to any
  133. // ISO 8601 date-only, year-month, or month-day representation.
  134. // f. Let resolvedOptions be ? GetOptionsObject(options).
  135. auto resolved_options = TRY(get_options_object(vm, options));
  136. // g. Perform ? GetTemporalOverflowOption(resolvedOptions).
  137. TRY(get_temporal_overflow_option(vm, resolved_options));
  138. }
  139. // 4. Return ! CreateTemporalTime(result).
  140. return MUST(create_temporal_time(vm, time));
  141. }
  142. // 4.5.8 RegulateTime ( hour, minute, second, millisecond, microsecond, nanosecond, overflow ), https://tc39.es/proposal-temporal/#sec-temporal-regulatetime
  143. ThrowCompletionOr<Time> regulate_time(VM& vm, double hour, double minute, double second, double millisecond, double microsecond, double nanosecond, Overflow overflow)
  144. {
  145. switch (overflow) {
  146. // 1. If overflow is CONSTRAIN, then
  147. case Overflow::Constrain:
  148. // a. Set hour to the result of clamping hour between 0 and 23.
  149. hour = clamp(hour, 0, 23);
  150. // b. Set minute to the result of clamping minute between 0 and 59.
  151. minute = clamp(minute, 0, 59);
  152. // c. Set second to the result of clamping second between 0 and 59.
  153. second = clamp(second, 0, 59);
  154. // d. Set millisecond to the result of clamping millisecond between 0 and 999.
  155. millisecond = clamp(millisecond, 0, 999);
  156. // e. Set microsecond to the result of clamping microsecond between 0 and 999.
  157. microsecond = clamp(microsecond, 0, 999);
  158. // f. Set nanosecond to the result of clamping nanosecond between 0 and 999.
  159. nanosecond = clamp(nanosecond, 0, 999);
  160. break;
  161. // 2. Else,
  162. case Overflow::Reject:
  163. // a. Assert: overflow is REJECT.
  164. // b. If IsValidTime(hour, minute, second, millisecond, microsecond, nanosecond) is false, throw a RangeError exception.
  165. if (!is_valid_time(hour, minute, second, millisecond, microsecond, nanosecond))
  166. return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidPlainTime);
  167. break;
  168. }
  169. // 3. Return CreateTimeRecord(hour, minute, second, millisecond, microsecond,nanosecond).
  170. return create_time_record(hour, minute, second, millisecond, microsecond, nanosecond);
  171. }
  172. // 4.5.9 IsValidTime ( hour, minute, second, millisecond, microsecond, nanosecond ), https://tc39.es/proposal-temporal/#sec-temporal-isvalidtime
  173. bool is_valid_time(double hour, double minute, double second, double millisecond, double microsecond, double nanosecond)
  174. {
  175. // 1. If hour < 0 or hour > 23, then
  176. if (hour < 0 || hour > 23) {
  177. // a. Return false.
  178. return false;
  179. }
  180. // 2. If minute < 0 or minute > 59, then
  181. if (minute < 0 || minute > 59) {
  182. // a. Return false.
  183. return false;
  184. }
  185. // 3. If second < 0 or second > 59, then
  186. if (second < 0 || second > 59) {
  187. // a. Return false.
  188. return false;
  189. }
  190. // 4. If millisecond < 0 or millisecond > 999, then
  191. if (millisecond < 0 || millisecond > 999) {
  192. // a. Return false.
  193. return false;
  194. }
  195. // 5. If microsecond < 0 or microsecond > 999, then
  196. if (microsecond < 0 || microsecond > 999) {
  197. // a. Return false.
  198. return false;
  199. }
  200. // 6. If nanosecond < 0 or nanosecond > 999, then
  201. if (nanosecond < 0 || nanosecond > 999) {
  202. // a. Return false.
  203. return false;
  204. }
  205. // 7. Return true.
  206. return true;
  207. }
  208. // 4.5.10 BalanceTime ( hour, minute, second, millisecond, microsecond, nanosecond ), https://tc39.es/proposal-temporal/#sec-temporal-balancetime
  209. Time balance_time(double hour, double minute, double second, double millisecond, double microsecond, double nanosecond)
  210. {
  211. // 1. Set microsecond to microsecond + floor(nanosecond / 1000).
  212. microsecond += floor(nanosecond / 1000.0);
  213. // 2. Set nanosecond to nanosecond modulo 1000.
  214. nanosecond = modulo(nanosecond, 1000.0);
  215. // 3. Set millisecond to millisecond + floor(microsecond / 1000).
  216. millisecond += floor(microsecond / 1000.0);
  217. // 4. Set microsecond to microsecond modulo 1000.
  218. microsecond = modulo(microsecond, 1000.0);
  219. // 5. Set second to second + floor(millisecond / 1000).
  220. second += floor(millisecond / 1000.0);
  221. // 6. Set millisecond to millisecond modulo 1000.
  222. millisecond = modulo(millisecond, 1000.0);
  223. // 7. Set minute to minute + floor(second / 60).
  224. minute += floor(second / 60.0);
  225. // 8. Set second to second modulo 60.
  226. second = modulo(second, 60.0);
  227. // 9. Set hour to hour + floor(minute / 60).
  228. hour += floor(minute / 60.0);
  229. // 10. Set minute to minute modulo 60.
  230. minute = modulo(minute, 60.0);
  231. // 11. Let deltaDays be floor(hour / 24).
  232. auto delta_days = floor(hour / 24.0);
  233. // 12. Set hour to hour modulo 24.
  234. hour = modulo(hour, 24.0);
  235. // 13. Return CreateTimeRecord(hour, minute, second, millisecond, microsecond, nanosecond, deltaDays).
  236. return create_time_record(hour, minute, second, millisecond, microsecond, nanosecond, delta_days);
  237. }
  238. // 4.5.10 BalanceTime ( hour, minute, second, millisecond, microsecond, nanosecond ), https://tc39.es/proposal-temporal/#sec-temporal-balancetime
  239. Time balance_time(double hour, double minute, double second, double millisecond, double microsecond, TimeDuration const& nanosecond_value)
  240. {
  241. // 1. Set microsecond to microsecond + floor(nanosecond / 1000).
  242. auto microsecond_value = TimeDuration { microsecond }.plus(big_floor(nanosecond_value, NANOSECONDS_PER_MICROSECOND));
  243. // 2. Set nanosecond to nanosecond modulo 1000.
  244. auto nanosecond = modulo(nanosecond_value, NANOSECONDS_PER_MICROSECOND).to_double();
  245. // 3. Set millisecond to millisecond + floor(microsecond / 1000).
  246. auto millisecond_value = TimeDuration { millisecond }.plus(big_floor(microsecond_value, MICROSECONDS_PER_MILLISECOND));
  247. // 4. Set microsecond to microsecond modulo 1000.
  248. microsecond = modulo(microsecond_value, MICROSECONDS_PER_MILLISECOND).to_double();
  249. // 5. Set second to second + floor(millisecond / 1000).
  250. auto second_value = TimeDuration { second }.plus(big_floor(millisecond_value, MILLISECONDS_PER_SECOND));
  251. // 6. Set millisecond to millisecond modulo 1000.
  252. millisecond = modulo(millisecond_value, MILLISECONDS_PER_SECOND).to_double();
  253. // 7. Set minute to minute + floor(second / 60).
  254. auto minute_value = TimeDuration { minute }.plus(big_floor(second_value, SECONDS_PER_MINUTE));
  255. // 8. Set second to second modulo 60.
  256. second = modulo(second_value, SECONDS_PER_MINUTE).to_double();
  257. // 9. Set hour to hour + floor(minute / 60).
  258. auto hour_value = TimeDuration { hour }.plus(big_floor(minute_value, MINUTES_PER_HOUR));
  259. // 10. Set minute to minute modulo 60.
  260. minute = modulo(minute_value, MINUTES_PER_HOUR).to_double();
  261. // 11. Let deltaDays be floor(hour / 24).
  262. auto delta_days = big_floor(hour_value, HOURS_PER_DAY).to_double();
  263. // 12. Set hour to hour modulo 24.
  264. hour = modulo(hour_value, HOURS_PER_DAY).to_double();
  265. // 13. Return CreateTimeRecord(hour, minute, second, millisecond, microsecond, nanosecond, deltaDays).
  266. return create_time_record(hour, minute, second, millisecond, microsecond, nanosecond, delta_days);
  267. }
  268. // 4.5.11 CreateTemporalTime ( time [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporaltime
  269. ThrowCompletionOr<GC::Ref<PlainTime>> create_temporal_time(VM& vm, Time const& time, GC::Ptr<FunctionObject> new_target)
  270. {
  271. auto& realm = *vm.current_realm();
  272. // 1. If newTarget is not present, set newTarget to %Temporal.PlainTime%.
  273. if (!new_target)
  274. new_target = realm.intrinsics().temporal_plain_time_constructor();
  275. // 2. Let object be ? OrdinaryCreateFromConstructor(newTarget, "%Temporal.PlainTime.prototype%", « [[InitializedTemporalTime]], [[Time]] »).
  276. // 3. Set object.[[Time]] to time.
  277. auto object = TRY(ordinary_create_from_constructor<PlainTime>(vm, *new_target, &Intrinsics::temporal_plain_time_prototype, time));
  278. // 4. Return object.
  279. return object;
  280. }
  281. // 4.5.12 ToTemporalTimeRecord ( temporalTimeLike [ , completeness ] ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaltimerecord
  282. ThrowCompletionOr<TemporalTimeLike> to_temporal_time_record(VM& vm, Object const& temporal_time_like, Completeness completeness)
  283. {
  284. // 1. If completeness is not present, set completeness to COMPLETE.
  285. TemporalTimeLike result;
  286. // 2. If completeness is COMPLETE, then
  287. if (completeness == Completeness::Complete) {
  288. // a. Let result be a new TemporalTimeLike Record with each field set to 0.
  289. result = TemporalTimeLike::zero();
  290. }
  291. // 3. Else,
  292. else {
  293. // a. Let result be a new TemporalTimeLike Record with each field set to UNSET.
  294. }
  295. // 4. Let any be false.
  296. auto any = false;
  297. auto apply_field = [&](auto const& key, auto& result_field) -> ThrowCompletionOr<void> {
  298. auto field = TRY(temporal_time_like.get(key));
  299. if (field.is_undefined())
  300. return {};
  301. result_field = TRY(to_integer_with_truncation(vm, field, ErrorType::TemporalInvalidTimeLikeField, field, key));
  302. any = true;
  303. return {};
  304. };
  305. // 5. Let hour be ? Get(temporalTimeLike, "hour").
  306. // 6. If hour is not undefined, then
  307. // a. Set result.[[Hour]] to ? ToIntegerWithTruncation(hour).
  308. // b. Set any to true.
  309. TRY(apply_field(vm.names.hour, result.hour));
  310. // 7. Let microsecond be ? Get(temporalTimeLike, "microsecond").
  311. // 8. If microsecond is not undefined, then
  312. // a. Set result.[[Microsecond]] to ? ToIntegerWithTruncation(microsecond).
  313. // b. Set any to true.
  314. TRY(apply_field(vm.names.microsecond, result.microsecond));
  315. // 9. Let millisecond be ? Get(temporalTimeLike, "millisecond").
  316. // 10. If millisecond is not undefined, then
  317. // a. Set result.[[Millisecond]] to ? ToIntegerWithTruncation(millisecond).
  318. // b. Set any to true.
  319. TRY(apply_field(vm.names.millisecond, result.millisecond));
  320. // 11. Let minute be ? Get(temporalTimeLike, "minute").
  321. // 12. If minute is not undefined, then
  322. // a. Set result.[[Minute]] to ? ToIntegerWithTruncation(minute).
  323. // b. Set any to true.
  324. TRY(apply_field(vm.names.minute, result.minute));
  325. // 13. Let nanosecond be ? Get(temporalTimeLike, "nanosecond").
  326. // 14. If nanosecond is not undefined, then
  327. // a. Set result.[[Nanosecond]] to ? ToIntegerWithTruncation(nanosecond).
  328. // b. Set any to true.
  329. TRY(apply_field(vm.names.nanosecond, result.nanosecond));
  330. // 15. Let second be ? Get(temporalTimeLike, "second").
  331. // 16. If second is not undefined, then
  332. // a. Set result.[[Second]] to ? ToIntegerWithTruncation(second).
  333. // b. Set any to true.
  334. TRY(apply_field(vm.names.second, result.second));
  335. // 17. If any is false, throw a TypeError exception.
  336. if (!any)
  337. return vm.throw_completion<TypeError>(ErrorType::TemporalInvalidTime);
  338. // 18. Return result.
  339. return result;
  340. }
  341. // 4.5.13 TimeRecordToString ( time, precision ), https://tc39.es/proposal-temporal/#sec-temporal-timerecordtostring
  342. String time_record_to_string(Time const& time, SecondsStringPrecision::Precision precision)
  343. {
  344. // 1. Let subSecondNanoseconds be time.[[Millisecond]] × 10**6 + time.[[Microsecond]] × 10**3 + time.[[Nanosecond]].
  345. auto sub_second_nanoseconds = (static_cast<u64>(time.millisecond) * 1'000'000) + (static_cast<u64>(time.microsecond) * 1000) + static_cast<u64>(time.nanosecond);
  346. // 2. Return FormatTimeString(time.[[Hour]], time.[[Minute]], time.[[Second]], subSecondNanoseconds, precision).
  347. return format_time_string(time.hour, time.minute, time.second, sub_second_nanoseconds, precision);
  348. }
  349. // 4.5.14 CompareTimeRecord ( time1, time2 ), https://tc39.es/proposal-temporal/#sec-temporal-comparetimerecord
  350. i8 compare_time_record(Time const& time1, Time const& time2)
  351. {
  352. // 1. If time1.[[Hour]] > time2.[[Hour]], return 1.
  353. if (time1.hour > time2.hour)
  354. return 1;
  355. // 2. If time1.[[Hour]] < time2.[[Hour]], return -1.
  356. if (time1.hour < time2.hour)
  357. return -1;
  358. // 3. If time1.[[Minute]] > time2.[[Minute]], return 1.
  359. if (time1.minute > time2.minute)
  360. return 1;
  361. // 4. If time1.[[Minute]] < time2.[[Minute]], return -1.
  362. if (time1.minute < time2.minute)
  363. return -1;
  364. // 5. If time1.[[Second]] > time2.[[Second]], return 1.
  365. if (time1.second > time2.second)
  366. return 1;
  367. // 6. If time1.[[Second]] < time2.[[Second]], return -1.
  368. if (time1.second < time2.second)
  369. return -1;
  370. // 7. If time1.[[Millisecond]] > time2.[[Millisecond]], return 1.
  371. if (time1.millisecond > time2.millisecond)
  372. return 1;
  373. // 8. If time1.[[Millisecond]] < time2.[[Millisecond]], return -1.
  374. if (time1.millisecond < time2.millisecond)
  375. return -1;
  376. // 9. If time1.[[Microsecond]] > time2.[[Microsecond]], return 1.
  377. if (time1.microsecond > time2.microsecond)
  378. return 1;
  379. // 10. If time1.[[Microsecond]] < time2.[[Microsecond]], return -1.
  380. if (time1.microsecond < time2.microsecond)
  381. return -1;
  382. // 11. If time1.[[Nanosecond]] > time2.[[Nanosecond]], return 1.
  383. if (time1.nanosecond > time2.nanosecond)
  384. return 1;
  385. // 12. If time1.[[Nanosecond]] < time2.[[Nanosecond]], return -1.
  386. if (time1.nanosecond < time2.nanosecond)
  387. return -1;
  388. // 13. Return 0.
  389. return 0;
  390. }
  391. // 4.5.15 AddTime ( time, timeDuration ), https://tc39.es/proposal-temporal/#sec-temporal-addtime
  392. Time add_time(Time const& time, TimeDuration const& time_duration)
  393. {
  394. auto nanoseconds = time_duration.plus(TimeDuration { static_cast<i64>(time.nanosecond) });
  395. // 1. Return BalanceTime(time.[[Hour]], time.[[Minute]], time.[[Second]], time.[[Millisecond]], time.[[Microsecond]], time.[[Nanosecond]] + timeDuration).
  396. return balance_time(time.hour, time.minute, time.second, time.millisecond, time.microsecond, nanoseconds);
  397. }
  398. // 4.5.16 RoundTime ( time, increment, unit, roundingMode ), https://tc39.es/proposal-temporal/#sec-temporal-roundtime
  399. Time round_time(Time const& time, u64 increment, Unit unit, RoundingMode rounding_mode)
  400. {
  401. double quantity = 0;
  402. switch (unit) {
  403. // 1. If unit is DAY or HOUR, then
  404. case Unit::Day:
  405. case Unit::Hour:
  406. // a. Let quantity be ((((time.[[Hour]] × 60 + time.[[Minute]]) × 60 + time.[[Second]]) × 1000 + time.[[Millisecond]]) × 1000 + time.[[Microsecond]]) × 1000 + time.[[Nanosecond]].
  407. quantity = ((((time.hour * 60.0 + time.minute) * 60.0 + time.second) * 1000.0 + time.millisecond) * 1000.0 + time.microsecond) * 1000.0 + time.nanosecond;
  408. break;
  409. // 2. Else if unit is MINUTE, then
  410. case Unit::Minute:
  411. // a. Let quantity be (((time.[[Minute]] × 60 + time.[[Second]]) × 1000 + time.[[Millisecond]]) × 1000 + time.[[Microsecond]]) × 1000 + time.[[Nanosecond]].
  412. quantity = (((time.minute * 60.0 + time.second) * 1000.0 + time.millisecond) * 1000.0 + time.microsecond) * 1000.0 + time.nanosecond;
  413. break;
  414. // 3. Else if unit is SECOND, then
  415. case Unit::Second:
  416. // a. Let quantity be ((time.[[Second]] × 1000 + time.[[Millisecond]]) × 1000 + time.[[Microsecond]]) × 1000 + time.[[Nanosecond]].
  417. quantity = ((time.second * 1000.0 + time.millisecond) * 1000.0 + time.microsecond) * 1000.0 + time.nanosecond;
  418. break;
  419. // 4. Else if unit is MILLISECOND, then
  420. case Unit::Millisecond:
  421. // a. Let quantity be (time.[[Millisecond]] × 1000 + time.[[Microsecond]]) × 1000 + time.[[Nanosecond]].
  422. quantity = (time.millisecond * 1000.0 + time.microsecond) * 1000.0 + time.nanosecond;
  423. break;
  424. // 5. Else if unit is MICROSECOND, then
  425. case Unit::Microsecond:
  426. // a. Let quantity be time.[[Microsecond]] × 1000 + time.[[Nanosecond]].
  427. quantity = time.microsecond * 1000.0 + time.nanosecond;
  428. break;
  429. // 6. Else,
  430. case Unit::Nanosecond:
  431. // a. Assert: unit is NANOSECOND.
  432. // b. Let quantity be time.[[Nanosecond]].
  433. quantity = time.nanosecond;
  434. break;
  435. default:
  436. VERIFY_NOT_REACHED();
  437. }
  438. // 7. Let unitLength be the value in the "Length in Nanoseconds" column of the row of Table 21 whose "Value" column contains unit.
  439. auto unit_length = temporal_unit_length_in_nanoseconds(unit).to_u64();
  440. // 8. Let result be RoundNumberToIncrement(quantity, increment × unitLength, roundingMode) / unitLength.
  441. auto result = round_number_to_increment(quantity, increment * unit_length, rounding_mode) / static_cast<double>(unit_length);
  442. switch (unit) {
  443. // 9. If unit is DAY, then
  444. case Unit::Day:
  445. // a. Return CreateTimeRecord(0, 0, 0, 0, 0, 0, result).
  446. return create_time_record(0, 0, 0, 0, 0, 0, result);
  447. // 10. If unit is HOUR, then
  448. case Unit::Hour:
  449. // a. Return BalanceTime(result, 0, 0, 0, 0, 0).
  450. return balance_time(result, 0, 0, 0, 0, 0);
  451. // 11. If unit is MINUTE, then
  452. case Unit::Minute:
  453. // a. Return BalanceTime(time.[[Hour]], result, 0, 0, 0, 0).
  454. return balance_time(time.hour, result, 0, 0, 0, 0);
  455. // 12. If unit is SECOND, then
  456. case Unit::Second:
  457. // a. Return BalanceTime(time.[[Hour]], time.[[Minute]], result, 0, 0, 0).
  458. return balance_time(time.hour, time.minute, result, 0, 0, 0);
  459. // 13. If unit is MILLISECOND, then
  460. case Unit::Millisecond:
  461. // a. Return BalanceTime(time.[[Hour]], time.[[Minute]], time.[[Second]], result, 0, 0).
  462. return balance_time(time.hour, time.minute, time.second, result, 0, 0);
  463. // 14. If unit is MICROSECOND, then
  464. case Unit::Microsecond:
  465. // a. Return BalanceTime(time.[[Hour]], time.[[Minute]], time.[[Second]], time.[[Millisecond]], result, 0).
  466. return balance_time(time.hour, time.minute, time.second, time.millisecond, result, 0);
  467. // 15. Assert: unit is NANOSECOND.
  468. case Unit::Nanosecond:
  469. // 16. Return BalanceTime(time.[[Hour]], time.[[Minute]], time.[[Second]], time.[[Millisecond]], time.[[Microsecond]], result).
  470. return balance_time(time.hour, time.minute, time.second, time.millisecond, time.microsecond, result);
  471. default:
  472. break;
  473. }
  474. VERIFY_NOT_REACHED();
  475. }
  476. // 4.5.17 DifferenceTemporalPlainTime ( operation, temporalTime, other, options ), https://tc39.es/proposal-temporal/#sec-temporal-differencetemporalplaintime
  477. ThrowCompletionOr<GC::Ref<Duration>> difference_temporal_plain_time(VM& vm, DurationOperation operation, PlainTime const& temporal_time, Value other_value, Value options)
  478. {
  479. // 1. Set other to ? ToTemporalTime(other).
  480. auto other = TRY(to_temporal_time(vm, other_value));
  481. // 2. Let resolvedOptions be ? GetOptionsObject(options).
  482. auto resolved_options = TRY(get_options_object(vm, options));
  483. // 3. Let settings be ? GetDifferenceSettings(operation, resolvedOptions, TIME, « », NANOSECOND, HOUR).
  484. auto settings = TRY(get_difference_settings(vm, operation, resolved_options, UnitGroup::Time, {}, Unit::Nanosecond, Unit::Hour));
  485. // 4. Let timeDuration be DifferenceTime(temporalTime.[[Time]], other.[[Time]]).
  486. auto time_duration = difference_time(temporal_time.time(), other->time());
  487. // 5. Set timeDuration to ! RoundTimeDuration(timeDuration, settings.[[RoundingIncrement]], settings.[[SmallestUnit]], settings.[[RoundingMode]]).
  488. time_duration = MUST(round_time_duration(vm, time_duration, Crypto::UnsignedBigInteger { settings.rounding_increment }, settings.smallest_unit, settings.rounding_mode));
  489. // 6. Let duration be ! CombineDateAndTimeDuration(ZeroDateDuration(), timeDuration).
  490. auto duration = MUST(combine_date_and_time_duration(vm, zero_date_duration(vm), move(time_duration)));
  491. // 7. Let result be ! TemporalDurationFromInternal(duration, settings.[[LargestUnit]]).
  492. auto result = MUST(temporal_duration_from_internal(vm, duration, settings.largest_unit));
  493. // 8. If operation is SINCE, set result to CreateNegatedTemporalDuration(result).
  494. if (operation == DurationOperation::Since)
  495. result = create_negated_temporal_duration(vm, result);
  496. // 9. Return result.
  497. return result;
  498. }
  499. // 4.5.18 AddDurationToTime ( operation, temporalTime, temporalDurationLike ), https://tc39.es/proposal-temporal/#sec-temporal-adddurationtotime
  500. ThrowCompletionOr<GC::Ref<PlainTime>> add_duration_to_time(VM& vm, ArithmeticOperation operation, PlainTime const& temporal_time, Value temporal_duration_like)
  501. {
  502. // 1. Let duration be ? ToTemporalDuration(temporalDurationLike).
  503. auto duration = TRY(to_temporal_duration(vm, temporal_duration_like));
  504. // 2. If operation is SUBTRACT, set duration to CreateNegatedTemporalDuration(duration).
  505. if (operation == ArithmeticOperation::Subtract)
  506. duration = create_negated_temporal_duration(vm, duration);
  507. // 3. Let internalDuration be ToInternalDurationRecord(duration).
  508. auto internal_duration = to_internal_duration_record(vm, duration);
  509. // 4. Let result be AddTime(temporalTime.[[Time]], internalDuration.[[Time]]).
  510. auto result = add_time(temporal_time.time(), internal_duration.time);
  511. // 5. Return ! CreateTemporalTime(result).
  512. return MUST(create_temporal_time(vm, result));
  513. }
  514. }