Duration.cpp 26 KB

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