Duration.cpp 27 KB

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