Duration.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/Object.h>
  9. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  10. #include <LibJS/Runtime/Temporal/Duration.h>
  11. #include <LibJS/Runtime/Temporal/DurationConstructor.h>
  12. #include <LibJS/Runtime/Temporal/ZonedDateTime.h>
  13. namespace JS::Temporal {
  14. // 7 Temporal.Duration Objects, https://tc39.es/proposal-temporal/#sec-temporal-duration-objects
  15. Duration::Duration(double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds, Object& prototype)
  16. : Object(prototype)
  17. , m_years(years)
  18. , m_months(months)
  19. , m_weeks(weeks)
  20. , m_days(days)
  21. , m_hours(hours)
  22. , m_minutes(minutes)
  23. , m_seconds(seconds)
  24. , m_milliseconds(milliseconds)
  25. , m_microseconds(microseconds)
  26. , m_nanoseconds(nanoseconds)
  27. {
  28. }
  29. // 7.5.1 ToTemporalDuration ( item ), https://tc39.es/proposal-temporal/#sec-temporal-totemporalduration
  30. Duration* to_temporal_duration(GlobalObject& global_object, Value item)
  31. {
  32. auto& vm = global_object.vm();
  33. Optional<TemporalDuration> result;
  34. // 1. If Type(item) is Object, then
  35. if (item.is_object()) {
  36. // a. If item has an [[InitializedTemporalDuration]] internal slot, then
  37. if (is<Duration>(item.as_object())) {
  38. // i. Return item.
  39. return &static_cast<Duration&>(item.as_object());
  40. }
  41. // b. Let result be ? ToTemporalDurationRecord(item).
  42. result = to_temporal_duration_record(global_object, item.as_object());
  43. if (vm.exception())
  44. return {};
  45. }
  46. // 2. Else,
  47. else {
  48. // a. Let string be ? ToString(item).
  49. auto string = item.to_string(global_object);
  50. if (vm.exception())
  51. return {};
  52. // b. Let result be ? ParseTemporalDurationString(string).
  53. result = TRY_OR_DISCARD(parse_temporal_duration_string(global_object, string));
  54. }
  55. // 3. Return ? CreateTemporalDuration(result.[[Years]], result.[[Months]], result.[[Weeks]], result.[[Days]], result.[[Hours]], result.[[Minutes]], result.[[Seconds]], result.[[Milliseconds]], result.[[Microseconds]], result.[[Nanoseconds]]).
  56. return create_temporal_duration(global_object, result->years, result->months, result->weeks, result->days, result->hours, result->minutes, result->seconds, result->milliseconds, result->microseconds, result->nanoseconds);
  57. }
  58. // 7.5.2 ToTemporalDurationRecord ( temporalDurationLike ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaldurationrecord
  59. TemporalDuration to_temporal_duration_record(GlobalObject& global_object, Object const& temporal_duration_like)
  60. {
  61. auto& vm = global_object.vm();
  62. // 1. Assert: Type(temporalDurationLike) is Object.
  63. // 2. If temporalDurationLike has an [[InitializedTemporalDuration]] internal slot, then
  64. if (is<Duration>(temporal_duration_like)) {
  65. auto& duration = static_cast<Duration const&>(temporal_duration_like);
  66. // a. Return the Record { [[Years]]: temporalDurationLike.[[Years]], [[Months]]: temporalDurationLike.[[Months]], [[Weeks]]: temporalDurationLike.[[Weeks]], [[Days]]: temporalDurationLike.[[Days]], [[Hours]]: temporalDurationLike.[[Hours]], [[Minutes]]: temporalDurationLike.[[Minutes]], [[Seconds]]: temporalDurationLike.[[Seconds]], [[Milliseconds]]: temporalDurationLike.[[Milliseconds]], [[Microseconds]]: temporalDurationLike.[[Microseconds]], [[Nanoseconds]]: temporalDurationLike.[[Nanoseconds]] }.
  67. return TemporalDuration { .years = duration.years(), .months = duration.months(), .weeks = duration.weeks(), .days = duration.days(), .hours = duration.hours(), .minutes = duration.minutes(), .seconds = duration.seconds(), .milliseconds = duration.milliseconds(), .microseconds = duration.microseconds(), .nanoseconds = duration.nanoseconds() };
  68. }
  69. // 3. Let result be a new Record with all the internal slots given in the Internal Slot column in Table 7.
  70. auto result = TemporalDuration {};
  71. // 4. Let any be false.
  72. auto any = false;
  73. // 5. For each row of Table 7, except the header row, in table order, do
  74. for (auto& [internal_slot, property] : temporal_duration_like_properties<TemporalDuration, double>(vm)) {
  75. // a. Let prop be the Property value of the current row.
  76. // b. Let val be ? Get(temporalDurationLike, prop).
  77. auto value = temporal_duration_like.get(property);
  78. if (vm.exception())
  79. return {};
  80. // c. If val is undefined, then
  81. if (value.is_undefined()) {
  82. // i. Set result's internal slot whose name is the Internal Slot value of the current row to 0.
  83. result.*internal_slot = 0;
  84. }
  85. // d. Else,
  86. else {
  87. // i. Set any to true.
  88. any = true;
  89. // ii. Let val be ? ToNumber(val).
  90. value = value.to_number(global_object);
  91. if (vm.exception())
  92. return {};
  93. // iii. If ! IsIntegralNumber(val) is false, then
  94. if (!value.is_integral_number()) {
  95. // 1. Throw a RangeError exception.
  96. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidDurationPropertyValueNonIntegral, property.as_string(), value.to_string_without_side_effects());
  97. return {};
  98. }
  99. // iv. Set result's internal slot whose name is the Internal Slot value of the current row to val.
  100. result.*internal_slot = value.as_double();
  101. }
  102. }
  103. // 6. If any is false, then
  104. if (!any) {
  105. // a. Throw a TypeError exception.
  106. vm.throw_exception<TypeError>(global_object, ErrorType::TemporalInvalidDurationLikeObject);
  107. return {};
  108. }
  109. // 7. Return result.
  110. return result;
  111. }
  112. // 7.5.3 DurationSign ( years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal-durationsign
  113. i8 duration_sign(double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds)
  114. {
  115. // 1. For each value v of « years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds », do
  116. for (auto& v : { years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds }) {
  117. // a. If v < 0, return −1.
  118. if (v < 0)
  119. return -1;
  120. // b. If v > 0, return 1.
  121. if (v > 0)
  122. return 1;
  123. }
  124. // 2. Return 0.
  125. return 0;
  126. }
  127. // 7.5.4 IsValidDuration ( years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal-isvalidduration
  128. bool is_valid_duration(double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds)
  129. {
  130. // 1. Let sign be ! DurationSign(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds).
  131. auto sign = duration_sign(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds);
  132. // 2. For each value v of « years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds », do
  133. for (auto& v : { years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds }) {
  134. // a. If v is not finite, return false.
  135. if (!isfinite(v))
  136. return false;
  137. // b. If v < 0 and sign > 0, return false.
  138. if (v < 0 && sign > 0)
  139. return false;
  140. // c. If v > 0 and sign < 0, return false.
  141. if (v > 0 && sign < 0)
  142. return false;
  143. }
  144. // 3. Return true.
  145. return true;
  146. }
  147. // 7.5.6 ToPartialDuration ( temporalDurationLike ), https://tc39.es/proposal-temporal/#sec-temporal-topartialduration
  148. PartialDuration to_partial_duration(GlobalObject& global_object, Value temporal_duration_like)
  149. {
  150. auto& vm = global_object.vm();
  151. // 1. If Type(temporalDurationLike) is not Object, then
  152. if (!temporal_duration_like.is_object()) {
  153. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, temporal_duration_like.to_string_without_side_effects());
  154. return {};
  155. }
  156. // 2. Let result be the Record { [[Years]]: undefined, [[Months]]: undefined, [[Weeks]]: undefined, [[Days]]: undefined, [[Hours]]: undefined, [[Minutes]]: undefined, [[Seconds]]: undefined, [[Milliseconds]]: undefined, [[Microseconds]]: undefined, [[Nanoseconds]]: undefined }.
  157. auto result = PartialDuration {};
  158. // 3. Let any be false.
  159. auto any = false;
  160. // 4. For each row of Table 7, except the header row, in table order, do
  161. for (auto& [internal_slot, property] : temporal_duration_like_properties<PartialDuration, Optional<double>>(vm)) {
  162. // a. Let property be the Property value of the current row.
  163. // b. Let value be ? Get(temporalDurationLike, property).
  164. auto value = temporal_duration_like.as_object().get(property);
  165. if (vm.exception())
  166. return {};
  167. // c. If value is not undefined, then
  168. if (!value.is_undefined()) {
  169. // i. Set any to true.
  170. any = true;
  171. // ii. Set value to ? ToNumber(value).
  172. value = value.to_number(global_object);
  173. if (vm.exception())
  174. return {};
  175. // iii. If ! IsIntegralNumber(value) is false, then
  176. if (!value.is_integral_number()) {
  177. // 1. Throw a RangeError exception.
  178. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidDurationPropertyValueNonIntegral, property.as_string(), value.to_string_without_side_effects());
  179. return {};
  180. }
  181. // iv. Set result's internal slot whose name is the Internal Slot value of the current row to value.
  182. result.*internal_slot = value.as_double();
  183. }
  184. }
  185. // 5. If any is false, then
  186. if (!any) {
  187. // a. Throw a TypeError exception.
  188. vm.throw_exception<TypeError>(global_object, ErrorType::TemporalInvalidDurationLikeObject);
  189. return {};
  190. }
  191. // 6. Return result.
  192. return result;
  193. }
  194. // 7.5.7 CreateTemporalDuration ( years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporalduration
  195. Duration* create_temporal_duration(GlobalObject& global_object, double years, double months, double weeks, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds, FunctionObject const* new_target)
  196. {
  197. auto& vm = global_object.vm();
  198. // 1. If ! IsValidDuration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds) is false, throw a RangeError exception.
  199. if (!is_valid_duration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds)) {
  200. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidDuration);
  201. return {};
  202. }
  203. // 2. If newTarget is not present, set it to %Temporal.Duration%.
  204. if (!new_target)
  205. new_target = global_object.temporal_duration_constructor();
  206. // 3. Let object be ? OrdinaryCreateFromConstructor(newTarget, "%Temporal.Duration.prototype%", « [[InitializedTemporalDuration]], [[Years]], [[Months]], [[Weeks]], [[Days]], [[Hours]], [[Minutes]], [[Seconds]], [[Milliseconds]], [[Microseconds]], [[Nanoseconds]] »).
  207. // 4. Set object.[[Years]] to years.
  208. // 5. Set object.[[Months]] to months.
  209. // 6. Set object.[[Weeks]] to weeks.
  210. // 7. Set object.[[Days]] to days.
  211. // 8. Set object.[[Hours]] to hours.
  212. // 9. Set object.[[Minutes]] to minutes.
  213. // 10. Set object.[[Seconds]] to seconds.
  214. // 11. Set object.[[Milliseconds]] to milliseconds.
  215. // 12. Set object.[[Microseconds]] to microseconds.
  216. // 13. Set object.[[Nanoseconds]] to nanoseconds.
  217. auto* object = TRY_OR_DISCARD(ordinary_create_from_constructor<Duration>(global_object, *new_target, &GlobalObject::temporal_duration_prototype, years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds));
  218. // 14. Return object.
  219. return object;
  220. }
  221. // 7.5.8 CreateNegatedTemporalDuration ( duration ), https://tc39.es/proposal-temporal/#sec-temporal-createnegatedtemporalduration
  222. Duration* create_negated_temporal_duration(GlobalObject& global_object, Duration const& duration)
  223. {
  224. // 1. Assert: Type(duration) is Object.
  225. // 2. Assert: duration has an [[InitializedTemporalDuration]] internal slot.
  226. // 3. Return ! CreateTemporalDuration(−duration.[[Years]], −duration.[[Months]], −duration.[[Weeks]], −duration.[[Days]], −duration.[[Hours]], −duration.[[Minutes]], −duration.[[Seconds]], −duration.[[Milliseconds]], −duration.[[Microseconds]], −duration.[[Nanoseconds]]).
  227. return create_temporal_duration(global_object, -duration.years(), -duration.months(), -duration.weeks(), -duration.days(), -duration.hours(), -duration.minutes(), -duration.seconds(), -duration.milliseconds(), -duration.microseconds(), -duration.nanoseconds());
  228. }
  229. // 7.5.10 TotalDurationNanoseconds ( days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds, offsetShift ), https://tc39.es/proposal-temporal/#sec-temporal-totaldurationnanoseconds
  230. BigInt* total_duration_nanoseconds(GlobalObject& global_object, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, BigInt const& nanoseconds, double offset_shift)
  231. {
  232. auto& vm = global_object.vm();
  233. // 1. Assert: offsetShift is an integer.
  234. VERIFY(offset_shift == trunc(offset_shift));
  235. // 2. Set nanoseconds to ℝ(nanoseconds).
  236. auto result_nanoseconds = nanoseconds.big_integer();
  237. // TODO: Add a way to create SignedBigIntegers from doubles with full precision and remove this restriction
  238. VERIFY(AK::is_within_range<i64>(days) && AK::is_within_range<i64>(hours) && AK::is_within_range<i64>(minutes) && AK::is_within_range<i64>(seconds) && AK::is_within_range<i64>(milliseconds) && AK::is_within_range<i64>(microseconds));
  239. // 3. If days ≠ 0, then
  240. if (days != 0) {
  241. // a. Set nanoseconds to nanoseconds − offsetShift.
  242. result_nanoseconds = result_nanoseconds.minus(Crypto::SignedBigInteger::create_from(offset_shift));
  243. }
  244. // 4. Set hours to ℝ(hours) + ℝ(days) × 24.
  245. auto total_hours = Crypto::SignedBigInteger::create_from(hours).plus(Crypto::SignedBigInteger::create_from(days).multiplied_by(Crypto::UnsignedBigInteger(24)));
  246. // 5. Set minutes to ℝ(minutes) + hours × 60.
  247. auto total_minutes = Crypto::SignedBigInteger::create_from(minutes).plus(total_hours.multiplied_by(Crypto::UnsignedBigInteger(60)));
  248. // 6. Set seconds to ℝ(seconds) + minutes × 60.
  249. auto total_seconds = Crypto::SignedBigInteger::create_from(seconds).plus(total_minutes.multiplied_by(Crypto::UnsignedBigInteger(60)));
  250. // 7. Set milliseconds to ℝ(milliseconds) + seconds × 1000.
  251. auto total_milliseconds = Crypto::SignedBigInteger::create_from(milliseconds).plus(total_seconds.multiplied_by(Crypto::UnsignedBigInteger(1000)));
  252. // 8. Set microseconds to ℝ(microseconds) + milliseconds × 1000.
  253. auto total_microseconds = Crypto::SignedBigInteger::create_from(microseconds).plus(total_milliseconds.multiplied_by(Crypto::UnsignedBigInteger(1000)));
  254. // 9. Return nanoseconds + microseconds × 1000.
  255. return js_bigint(vm, result_nanoseconds.plus(total_microseconds.multiplied_by(Crypto::UnsignedBigInteger(1000))));
  256. }
  257. // 7.5.11 BalanceDuration ( days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds, largestUnit [ , relativeTo ] ), https://tc39.es/proposal-temporal/#sec-temporal-balanceduration
  258. Optional<BalancedDuration> balance_duration(GlobalObject& global_object, double days, double hours, double minutes, double seconds, double milliseconds, double microseconds, BigInt const& nanoseconds, String const& largest_unit, Object* relative_to)
  259. {
  260. auto& vm = global_object.vm();
  261. // 1. If relativeTo is not present, set relativeTo to undefined.
  262. Crypto::SignedBigInteger total_nanoseconds;
  263. // 2. If Type(relativeTo) is Object and relativeTo has an [[InitializedTemporalZonedDateTime]] internal slot, then
  264. if (relative_to && is<ZonedDateTime>(*relative_to)) {
  265. // a. Let endNs be ? AddZonedDateTime(relativeTo.[[Nanoseconds]], relativeTo.[[TimeZone]], relativeTo.[[Calendar]], 0, 0, 0, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds).
  266. TODO();
  267. if (vm.exception())
  268. return {};
  269. // b. Set nanoseconds to endNs − relativeTo.[[Nanoseconds]].
  270. }
  271. // 3. Else,
  272. else {
  273. // a. Set nanoseconds to ℤ(! TotalDurationNanoseconds(days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds, 0)).
  274. total_nanoseconds = total_duration_nanoseconds(global_object, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds, 0)->big_integer();
  275. }
  276. // 4. If largestUnit is one of "year", "month", "week", or "day", then
  277. if (largest_unit.is_one_of("year"sv, "month"sv, "week"sv, "day"sv)) {
  278. // a. Let result be ? NanosecondsToDays(nanoseconds, relativeTo).
  279. TODO();
  280. if (vm.exception())
  281. return {};
  282. // b. Set days to result.[[Days]].
  283. // c. Set nanoseconds to result.[[Nanoseconds]].
  284. }
  285. // 5. Else,
  286. else {
  287. // a. Set days to 0.
  288. days = 0;
  289. }
  290. // 6. Set hours, minutes, seconds, milliseconds, and microseconds to 0.
  291. hours = 0;
  292. minutes = 0;
  293. seconds = 0;
  294. milliseconds = 0;
  295. microseconds = 0;
  296. // 7. Set nanoseconds to ℝ(nanoseconds).
  297. double result_nanoseconds = total_nanoseconds.to_double();
  298. // 8. If nanoseconds < 0, let sign be −1; else, let sign be 1.
  299. i8 sign = total_nanoseconds.is_negative() ? -1 : 1;
  300. // 9. Set nanoseconds to abs(nanoseconds).
  301. total_nanoseconds = Crypto::SignedBigInteger(total_nanoseconds.unsigned_value());
  302. result_nanoseconds = fabs(result_nanoseconds);
  303. // 10. If largestUnit is "year", "month", "week", "day", or "hour", then
  304. if (largest_unit.is_one_of("year"sv, "month"sv, "day"sv, "hour"sv)) {
  305. // a. Set microseconds to floor(nanoseconds / 1000).
  306. auto nanoseconds_division_result = total_nanoseconds.divided_by(Crypto::UnsignedBigInteger(1000));
  307. // b. Set nanoseconds to nanoseconds modulo 1000.
  308. result_nanoseconds = nanoseconds_division_result.remainder.to_double();
  309. // c. Set milliseconds to floor(microseconds / 1000).
  310. auto microseconds_division_result = nanoseconds_division_result.quotient.divided_by(Crypto::UnsignedBigInteger(1000));
  311. // d. Set microseconds to microseconds modulo 1000.
  312. microseconds = microseconds_division_result.remainder.to_double();
  313. // e. Set seconds to floor(milliseconds / 1000).
  314. auto milliseconds_division_result = microseconds_division_result.quotient.divided_by(Crypto::UnsignedBigInteger(1000));
  315. // f. Set milliseconds to milliseconds modulo 1000.
  316. milliseconds = milliseconds_division_result.remainder.to_double();
  317. // g. Set minutes to floor(seconds / 60).
  318. auto seconds_division_result = milliseconds_division_result.quotient.divided_by(Crypto::UnsignedBigInteger(60));
  319. // h. Set seconds to seconds modulo 60.
  320. seconds = seconds_division_result.remainder.to_double();
  321. // i. Set hours to floor(minutes / 60).
  322. auto minutes_division_result = milliseconds_division_result.quotient.divided_by(Crypto::UnsignedBigInteger(60));
  323. hours = minutes_division_result.quotient.to_double();
  324. // j. Set minutes to minutes modulo 60.
  325. minutes = minutes_division_result.remainder.to_double();
  326. }
  327. // 11. Else if largestUnit is "minute", then
  328. else if (largest_unit == "minute"sv) {
  329. // a. Set microseconds to floor(nanoseconds / 1000).
  330. auto nanoseconds_division_result = total_nanoseconds.divided_by(Crypto::UnsignedBigInteger(1000));
  331. // b. Set nanoseconds to nanoseconds modulo 1000.
  332. result_nanoseconds = nanoseconds_division_result.remainder.to_double();
  333. // c. Set milliseconds to floor(microseconds / 1000).
  334. auto microseconds_division_result = nanoseconds_division_result.quotient.divided_by(Crypto::UnsignedBigInteger(1000));
  335. // d. Set microseconds to microseconds modulo 1000.
  336. microseconds = microseconds_division_result.remainder.to_double();
  337. // e. Set seconds to floor(milliseconds / 1000).
  338. auto milliseconds_division_result = microseconds_division_result.quotient.divided_by(Crypto::UnsignedBigInteger(1000));
  339. // f. Set milliseconds to milliseconds modulo 1000.
  340. milliseconds = milliseconds_division_result.remainder.to_double();
  341. // g. Set minutes to floor(seconds / 60).
  342. auto seconds_division_result = milliseconds_division_result.quotient.divided_by(Crypto::UnsignedBigInteger(60));
  343. minutes = seconds_division_result.quotient.to_double();
  344. // h. Set seconds to seconds modulo 60.
  345. seconds = seconds_division_result.remainder.to_double();
  346. }
  347. // 12. Else if largestUnit is "second", then
  348. else if (largest_unit == "second"sv) {
  349. // a. Set microseconds to floor(nanoseconds / 1000).
  350. auto nanoseconds_division_result = total_nanoseconds.divided_by(Crypto::UnsignedBigInteger(1000));
  351. // b. Set nanoseconds to nanoseconds modulo 1000.
  352. result_nanoseconds = nanoseconds_division_result.remainder.to_double();
  353. // c. Set milliseconds to floor(microseconds / 1000).
  354. auto microseconds_division_result = nanoseconds_division_result.quotient.divided_by(Crypto::UnsignedBigInteger(1000));
  355. // d. Set microseconds to microseconds modulo 1000.
  356. microseconds = microseconds_division_result.remainder.to_double();
  357. // e. Set seconds to floor(milliseconds / 1000).
  358. auto milliseconds_division_result = microseconds_division_result.quotient.divided_by(Crypto::UnsignedBigInteger(1000));
  359. seconds = milliseconds_division_result.quotient.to_double();
  360. // f. Set milliseconds to milliseconds modulo 1000.
  361. milliseconds = milliseconds_division_result.remainder.to_double();
  362. }
  363. // 13. Else if largestUnit is "millisecond", then
  364. else if (largest_unit == "millisecond"sv) {
  365. // a. Set microseconds to floor(nanoseconds / 1000).
  366. auto nanoseconds_division_result = total_nanoseconds.divided_by(Crypto::UnsignedBigInteger(1000));
  367. // b. Set nanoseconds to nanoseconds modulo 1000.
  368. result_nanoseconds = nanoseconds_division_result.remainder.to_double();
  369. // c. Set milliseconds to floor(microseconds / 1000).
  370. auto microseconds_division_result = nanoseconds_division_result.quotient.divided_by(Crypto::UnsignedBigInteger(1000));
  371. milliseconds = microseconds_division_result.quotient.to_double();
  372. // d. Set microseconds to microseconds modulo 1000.
  373. microseconds = microseconds_division_result.remainder.to_double();
  374. }
  375. // 14. Else if largestUnit is "microsecond", then
  376. else if (largest_unit == "microsecond"sv) {
  377. // a. Set microseconds to floor(nanoseconds / 1000).
  378. auto nanoseconds_division_result = total_nanoseconds.divided_by(Crypto::UnsignedBigInteger(1000));
  379. microseconds = nanoseconds_division_result.quotient.to_double();
  380. // b. Set nanoseconds to nanoseconds modulo 1000.
  381. result_nanoseconds = nanoseconds_division_result.remainder.to_double();
  382. }
  383. // 15. Else,
  384. else {
  385. // a. Assert: largestUnit is "nanosecond".
  386. VERIFY(largest_unit == "nanosecond"sv);
  387. }
  388. // 16. Return the Record { [[Days]]: 𝔽(days), [[Hours]]: 𝔽(hours × sign), [[Minutes]]: 𝔽(minutes × sign), [[Seconds]]: 𝔽(seconds × sign), [[Milliseconds]]: 𝔽(milliseconds × sign), [[Microseconds]]: 𝔽(microseconds × sign), [[Nanoseconds]]: 𝔽(nanoseconds × sign) }.
  389. return BalancedDuration { .days = days, .hours = hours * sign, .minutes = minutes * sign, .seconds = seconds * sign, .milliseconds = milliseconds * sign, .microseconds = microseconds * sign, .nanoseconds = result_nanoseconds * sign };
  390. }
  391. // 7.5.20 ToLimitedTemporalDuration ( temporalDurationLike, disallowedFields ),https://tc39.es/proposal-temporal/#sec-temporal-tolimitedtemporalduration
  392. Optional<TemporalDuration> to_limited_temporal_duration(GlobalObject& global_object, Value temporal_duration_like, Vector<StringView> const& disallowed_fields)
  393. {
  394. auto& vm = global_object.vm();
  395. Optional<TemporalDuration> duration;
  396. // 1. If Type(temporalDurationLike) is not Object, then
  397. if (!temporal_duration_like.is_object()) {
  398. // a. Let str be ? ToString(temporalDurationLike).
  399. auto str = temporal_duration_like.to_string(global_object);
  400. if (vm.exception())
  401. return {};
  402. // b. Let duration be ? ParseTemporalDurationString(str).
  403. duration = TRY_OR_DISCARD(parse_temporal_duration_string(global_object, str));
  404. }
  405. // 2. Else,
  406. else {
  407. // a. Let duration be ? ToTemporalDurationRecord(temporalDurationLike).
  408. duration = to_temporal_duration_record(global_object, temporal_duration_like.as_object());
  409. if (vm.exception())
  410. return {};
  411. }
  412. // 3. If ! IsValidDuration(duration.[[Years]], duration.[[Months]], duration.[[Weeks]], duration.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]]) is false, throw a RangeError exception.
  413. if (!is_valid_duration(duration->years, duration->months, duration->weeks, duration->days, duration->hours, duration->minutes, duration->seconds, duration->milliseconds, duration->microseconds, duration->nanoseconds)) {
  414. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidDuration);
  415. return {};
  416. }
  417. // 4. For each row of Table 7, except the header row, in table order, do
  418. for (auto& [internal_slot, property] : temporal_duration_like_properties<TemporalDuration, double>(vm)) {
  419. // a. Let prop be the Property value of the current row.
  420. // b. Let value be duration's internal slot whose name is the Internal Slot value of the current row.
  421. auto value = (*duration).*internal_slot;
  422. // If value is not 0 and disallowedFields contains prop, then
  423. if (value != 0 && disallowed_fields.contains_slow(property.as_string())) {
  424. // i. Throw a RangeError exception.
  425. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidDurationPropertyValueNonZero, property.as_string(), value);
  426. return {};
  427. }
  428. }
  429. // 5. Return duration.
  430. return duration;
  431. }
  432. }