PlainTime.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/TypeCasts.h>
  8. #include <LibJS/Runtime/AbstractOperations.h>
  9. #include <LibJS/Runtime/Date.h>
  10. #include <LibJS/Runtime/GlobalObject.h>
  11. #include <LibJS/Runtime/Object.h>
  12. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  13. #include <LibJS/Runtime/Temporal/Calendar.h>
  14. #include <LibJS/Runtime/Temporal/Duration.h>
  15. #include <LibJS/Runtime/Temporal/Instant.h>
  16. #include <LibJS/Runtime/Temporal/PlainDateTime.h>
  17. #include <LibJS/Runtime/Temporal/PlainTime.h>
  18. #include <LibJS/Runtime/Temporal/PlainTimeConstructor.h>
  19. #include <LibJS/Runtime/Temporal/TimeZone.h>
  20. #include <LibJS/Runtime/Temporal/ZonedDateTime.h>
  21. namespace JS::Temporal {
  22. JS_DEFINE_ALLOCATOR(PlainTime);
  23. // 4 Temporal.PlainTime Objects, https://tc39.es/proposal-temporal/#sec-temporal-plaintime-objects
  24. PlainTime::PlainTime(u8 iso_hour, u8 iso_minute, u8 iso_second, u16 iso_millisecond, u16 iso_microsecond, u16 iso_nanosecond, Calendar& calendar, Object& prototype)
  25. : Object(ConstructWithPrototypeTag::Tag, prototype)
  26. , m_iso_hour(iso_hour)
  27. , m_iso_minute(iso_minute)
  28. , m_iso_second(iso_second)
  29. , m_iso_millisecond(iso_millisecond)
  30. , m_iso_microsecond(iso_microsecond)
  31. , m_iso_nanosecond(iso_nanosecond)
  32. , m_calendar(calendar)
  33. {
  34. }
  35. void PlainTime::visit_edges(Visitor& visitor)
  36. {
  37. Base::visit_edges(visitor);
  38. visitor.visit(m_calendar);
  39. }
  40. // 4.5.1 DifferenceTime ( h1, min1, s1, ms1, mus1, ns1, h2, min2, s2, ms2, mus2, ns2 ), https://tc39.es/proposal-temporal/#sec-temporal-differencetime
  41. TimeDurationRecord difference_time(VM& vm, u8 hour1, u8 minute1, u8 second1, u16 millisecond1, u16 microsecond1, u16 nanosecond1, u8 hour2, u8 minute2, u8 second2, u16 millisecond2, u16 microsecond2, u16 nanosecond2)
  42. {
  43. // 1. Let hours be h2 - h1.
  44. auto hours = hour2 - hour1;
  45. // 2. Let minutes be min2 - min1.
  46. auto minutes = minute2 - minute1;
  47. // 3. Let seconds be s2 - s1.
  48. auto seconds = second2 - second1;
  49. // 4. Let milliseconds be ms2 - ms1.
  50. auto milliseconds = millisecond2 - millisecond1;
  51. // 5. Let microseconds be mus2 - mus1.
  52. auto microseconds = microsecond2 - microsecond1;
  53. // 6. Let nanoseconds be ns2 - ns1.
  54. auto nanoseconds = nanosecond2 - nanosecond1;
  55. // 7. Let sign be ! DurationSign(0, 0, 0, 0, hours, minutes, seconds, milliseconds, microseconds, nanoseconds).
  56. auto sign = duration_sign(0, 0, 0, 0, hours, minutes, seconds, milliseconds, microseconds, nanoseconds);
  57. // 8. Let bt be ! BalanceTime(hours × sign, minutes × sign, seconds × sign, milliseconds × sign, microseconds × sign, nanoseconds × sign).
  58. auto bt = balance_time(hours * sign, minutes * sign, seconds * sign, milliseconds * sign, microseconds * sign, nanoseconds * sign);
  59. // 9. Assert: bt.[[Days]] is 0.
  60. VERIFY(bt.days == 0);
  61. // 10. Return ! CreateTimeDurationRecord(0, bt.[[Hour]] × sign, bt.[[Minute]] × sign, bt.[[Second]] × sign, bt.[[Millisecond]] × sign, bt.[[Microsecond]] × sign, bt.[[Nanosecond]] × sign).
  62. return MUST(create_time_duration_record(vm, 0, static_cast<double>(bt.hour * sign), static_cast<double>(bt.minute * sign), static_cast<double>(bt.second * sign), static_cast<double>(bt.millisecond * sign), static_cast<double>(bt.microsecond * sign), static_cast<double>(bt.nanosecond * sign)));
  63. }
  64. // 4.5.2 ToTemporalTime ( item [ , overflow ] ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaltime
  65. ThrowCompletionOr<PlainTime*> to_temporal_time(VM& vm, Value item, Optional<StringView> overflow)
  66. {
  67. // 1. If overflow is not present, set overflow to "constrain".
  68. if (!overflow.has_value())
  69. overflow = "constrain"sv;
  70. // 2. Assert: overflow is either "constrain" or "reject".
  71. VERIFY(overflow == "constrain"sv || overflow == "reject"sv);
  72. Optional<TemporalTime> result;
  73. // 3. If Type(item) is Object, then
  74. if (item.is_object()) {
  75. auto& item_object = item.as_object();
  76. // a. If item has an [[InitializedTemporalTime]] internal slot, then
  77. if (is<PlainTime>(item_object)) {
  78. // i. Return item.
  79. return &static_cast<PlainTime&>(item_object);
  80. }
  81. // b. If item has an [[InitializedTemporalZonedDateTime]] internal slot, then
  82. if (is<ZonedDateTime>(item_object)) {
  83. auto& zoned_date_time = static_cast<ZonedDateTime&>(item_object);
  84. // i. Let instant be ! CreateTemporalInstant(item.[[Nanoseconds]]).
  85. auto* instant = create_temporal_instant(vm, zoned_date_time.nanoseconds()).release_value();
  86. // ii. Set plainDateTime to ? BuiltinTimeZoneGetPlainDateTimeFor(item.[[TimeZone]], instant, item.[[Calendar]]).
  87. auto* plain_date_time = TRY(builtin_time_zone_get_plain_date_time_for(vm, &zoned_date_time.time_zone(), *instant, zoned_date_time.calendar()));
  88. // iii. Return ! CreateTemporalTime(plainDateTime.[[ISOHour]], plainDateTime.[[ISOMinute]], plainDateTime.[[ISOSecond]], plainDateTime.[[ISOMillisecond]], plainDateTime.[[ISOMicrosecond]], plainDateTime.[[ISONanosecond]]).
  89. return TRY(create_temporal_time(vm, plain_date_time->iso_hour(), plain_date_time->iso_minute(), plain_date_time->iso_second(), plain_date_time->iso_millisecond(), plain_date_time->iso_microsecond(), plain_date_time->iso_nanosecond()));
  90. }
  91. // c. If item has an [[InitializedTemporalDateTime]] internal slot, then
  92. if (is<PlainDateTime>(item_object)) {
  93. auto& plain_date_time = static_cast<PlainDateTime&>(item_object);
  94. // i. Return ! CreateTemporalTime(item.[[ISOHour]], item.[[ISOMinute]], item.[[ISOSecond]], item.[[ISOMillisecond]], item.[[ISOMicrosecond]], item.[[ISONanosecond]]).
  95. return TRY(create_temporal_time(vm, plain_date_time.iso_hour(), plain_date_time.iso_minute(), plain_date_time.iso_second(), plain_date_time.iso_millisecond(), plain_date_time.iso_microsecond(), plain_date_time.iso_nanosecond()));
  96. }
  97. // d. Let calendar be ? GetTemporalCalendarWithISODefault(item).
  98. auto* calendar = TRY(get_temporal_calendar_with_iso_default(vm, item_object));
  99. // e. If ? ToString(calendar) is not "iso8601", then
  100. auto calendar_identifier = TRY(Value(calendar).to_string(vm));
  101. if (calendar_identifier != "iso8601"sv) {
  102. // i. Throw a RangeError exception.
  103. return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidCalendarIdentifier, calendar_identifier);
  104. }
  105. // f. Let result be ? ToTemporalTimeRecord(item).
  106. auto unregulated_result = TRY(to_temporal_time_record(vm, item_object));
  107. // g. Set result to ? RegulateTime(result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]], overflow).
  108. result = TRY(regulate_time(vm, *unregulated_result.hour, *unregulated_result.minute, *unregulated_result.second, *unregulated_result.millisecond, *unregulated_result.microsecond, *unregulated_result.nanosecond, *overflow));
  109. }
  110. // 4. Else,
  111. else {
  112. // a. Let string be ? ToString(item).
  113. auto string = TRY(item.to_string(vm));
  114. // b. Let result be ? ParseTemporalTimeString(string).
  115. result = TRY(parse_temporal_time_string(vm, string));
  116. // c. Assert: IsValidTime(result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]]) is true.
  117. VERIFY(is_valid_time(result->hour, result->minute, result->second, result->millisecond, result->microsecond, result->nanosecond));
  118. // d. If result.[[Calendar]] is not one of undefined or "iso8601", then
  119. if (result->calendar.has_value() && *result->calendar != "iso8601"sv) {
  120. // i. Throw a RangeError exception.
  121. return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidCalendarIdentifier, *result->calendar);
  122. }
  123. }
  124. // 5. Return ! CreateTemporalTime(result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]]).
  125. return MUST(create_temporal_time(vm, result->hour, result->minute, result->second, result->millisecond, result->microsecond, result->nanosecond));
  126. }
  127. // 4.5.3 RegulateTime ( hour, minute, second, millisecond, microsecond, nanosecond, overflow ), https://tc39.es/proposal-temporal/#sec-temporal-regulatetime
  128. ThrowCompletionOr<TemporalTime> regulate_time(VM& vm, double hour, double minute, double second, double millisecond, double microsecond, double nanosecond, StringView overflow)
  129. {
  130. // 1. Assert: hour, minute, second, millisecond, microsecond and nanosecond are integers.
  131. VERIFY(trunc(hour) == hour && trunc(minute) == minute && trunc(second) == second && trunc(millisecond) == millisecond && trunc(microsecond) == microsecond && trunc(nanosecond) == nanosecond);
  132. // 2. Assert: overflow is either "constrain" or "reject".
  133. // NOTE: Asserted by the VERIFY_NOT_REACHED at the end
  134. // 3. If overflow is "constrain", then
  135. if (overflow == "constrain"sv) {
  136. // a. Return ! ConstrainTime(hour, minute, second, millisecond, microsecond, nanosecond).
  137. return constrain_time(hour, minute, second, millisecond, microsecond, nanosecond);
  138. }
  139. // 4. Else,
  140. else {
  141. // a. Assert: overflow is "reject".
  142. VERIFY(overflow == "reject"sv);
  143. // b. If IsValidTime(hour, minute, second, millisecond, microsecond, nanosecond) is false, throw a RangeError exception.
  144. if (!is_valid_time(hour, minute, second, millisecond, microsecond, nanosecond))
  145. return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidPlainTime);
  146. // c. Return the Record { [[Hour]]: hour, [[Minute]]: minute, [[Second]]: second, [[Millisecond]]: millisecond, [[Microsecond]]: microsecond, [[Nanosecond]]: nanosecond }.
  147. return TemporalTime { .hour = static_cast<u8>(hour), .minute = static_cast<u8>(minute), .second = static_cast<u8>(second), .millisecond = static_cast<u16>(millisecond), .microsecond = static_cast<u16>(microsecond), .nanosecond = static_cast<u16>(nanosecond) };
  148. }
  149. }
  150. // 4.5.4 IsValidTime ( hour, minute, second, millisecond, microsecond, nanosecond ), https://tc39.es/proposal-temporal/#sec-temporal-isvalidtime
  151. bool is_valid_time(double hour, double minute, double second, double millisecond, double microsecond, double nanosecond)
  152. {
  153. // 1. If hour < 0 or hour > 23, then
  154. if (hour > 23) {
  155. // a. Return false.
  156. return false;
  157. }
  158. // 2. If minute < 0 or minute > 59, then
  159. if (minute > 59) {
  160. // a. Return false.
  161. return false;
  162. }
  163. // 3. If second < 0 or second > 59, then
  164. if (second > 59) {
  165. // a. Return false.
  166. return false;
  167. }
  168. // 4. If millisecond < 0 or millisecond > 999, then
  169. if (millisecond > 999) {
  170. // a. Return false.
  171. return false;
  172. }
  173. // 5. If microsecond < 0 or microsecond > 999, then
  174. if (microsecond > 999) {
  175. // a. Return false.
  176. return false;
  177. }
  178. // 6. If nanosecond < 0 or nanosecond > 999, then
  179. if (nanosecond > 999) {
  180. // a. Return false.
  181. return false;
  182. }
  183. // 7. Return true.
  184. return true;
  185. }
  186. // 4.5.5 BalanceTime ( hour, minute, second, millisecond, microsecond, nanosecond ), https://tc39.es/proposal-temporal/#sec-temporal-balancetime
  187. DaysAndTime balance_time(double hour, double minute, double second, double millisecond, double microsecond, double nanosecond)
  188. {
  189. // 1. Assert: hour, minute, second, millisecond, microsecond, and nanosecond are integers.
  190. VERIFY(hour == trunc(hour) && minute == trunc(minute) && second == trunc(second) && millisecond == trunc(millisecond) && microsecond == trunc(microsecond) && nanosecond == trunc(nanosecond));
  191. // 2. Set microsecond to microsecond + floor(nanosecond / 1000).
  192. microsecond += floor(nanosecond / 1000);
  193. // 3. Set nanosecond to nanosecond modulo 1000.
  194. nanosecond = modulo(nanosecond, 1000);
  195. // 4. Set millisecond to millisecond + floor(microsecond / 1000).
  196. millisecond += floor(microsecond / 1000);
  197. // 5. Set microsecond to microsecond modulo 1000.
  198. microsecond = modulo(microsecond, 1000);
  199. // 6. Set second to second + floor(millisecond / 1000).
  200. second += floor(millisecond / 1000);
  201. // 7. Set millisecond to millisecond modulo 1000.
  202. millisecond = modulo(millisecond, 1000);
  203. // 8. Set minute to minute + floor(second / 60).
  204. minute += floor(second / 60);
  205. // 9. Set second to second modulo 60.
  206. second = modulo(second, 60);
  207. // 10. Set hour to hour + floor(minute / 60).
  208. hour += floor(minute / 60);
  209. // 11. Set minute to minute modulo 60.
  210. minute = modulo(minute, 60);
  211. // 12. Let days be floor(hour / 24).
  212. auto days = floor(hour / 24);
  213. // 13. Set hour to hour modulo 24.
  214. hour = modulo(hour, 24);
  215. // 14. Return the Record { [[Days]]: days, [[Hour]]: hour, [[Minute]]: minute, [[Second]]: second, [[Millisecond]]: millisecond, [[Microsecond]]: microsecond, [[Nanosecond]]: nanosecond }.
  216. return DaysAndTime {
  217. .days = static_cast<i32>(days),
  218. .hour = static_cast<u8>(hour),
  219. .minute = static_cast<u8>(minute),
  220. .second = static_cast<u8>(second),
  221. .millisecond = static_cast<u16>(millisecond),
  222. .microsecond = static_cast<u16>(microsecond),
  223. .nanosecond = static_cast<u16>(nanosecond),
  224. };
  225. }
  226. // 4.5.6 ConstrainTime ( hour, minute, second, millisecond, microsecond, nanosecond ), https://tc39.es/proposal-temporal/#sec-temporal-constraintime
  227. TemporalTime constrain_time(double hour, double minute, double second, double millisecond, double microsecond, double nanosecond)
  228. {
  229. // 1. Assert: hour, minute, second, millisecond, microsecond, and nanosecond are integers.
  230. // 2. Set hour to the result of clamping hour between 0 and 23.
  231. hour = clamp(hour, 0, 23);
  232. // 3. Set minute to the result of clamping minute between 0 and 59.
  233. minute = clamp(minute, 0, 59);
  234. // 4. Set second to the result of clamping second between 0 and 59.
  235. second = clamp(second, 0, 59);
  236. // 5. Set millisecond to the result of clamping millisecond between 0 and 999.
  237. millisecond = clamp(millisecond, 0, 999);
  238. // 6. Set microsecond to the result of clamping microsecond between 0 and 999.
  239. microsecond = clamp(microsecond, 0, 999);
  240. // 7. Set nanosecond to the result of clamping nanosecond between 0 and 999.
  241. nanosecond = clamp(nanosecond, 0, 999);
  242. // 8. Return the Record { [[Hour]]: hour, [[Minute]]: minute, [[Second]]: second, [[Millisecond]]: millisecond, [[Microsecond]]: microsecond, [[Nanosecond]]: nanosecond }.
  243. return TemporalTime { .hour = static_cast<u8>(hour), .minute = static_cast<u8>(minute), .second = static_cast<u8>(second), .millisecond = static_cast<u16>(millisecond), .microsecond = static_cast<u16>(microsecond), .nanosecond = static_cast<u16>(nanosecond) };
  244. }
  245. // 4.5.7 CreateTemporalTime ( hour, minute, second, millisecond, microsecond, nanosecond [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporaltime
  246. ThrowCompletionOr<PlainTime*> create_temporal_time(VM& vm, u8 hour, u8 minute, u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, FunctionObject const* new_target)
  247. {
  248. auto& realm = *vm.current_realm();
  249. // 1. Assert: hour, minute, second, millisecond, microsecond and nanosecond are integers.
  250. // 2. If IsValidTime(hour, minute, second, millisecond, microsecond, nanosecond) is false, throw a RangeError exception.
  251. if (!is_valid_time(hour, minute, second, millisecond, microsecond, nanosecond))
  252. return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidPlainTime);
  253. // 3. If newTarget is not present, set newTarget to %Temporal.PlainTime%.
  254. if (!new_target)
  255. new_target = realm.intrinsics().temporal_plain_time_constructor();
  256. // 4. Let object be ? OrdinaryCreateFromConstructor(newTarget, "%Temporal.PlainTime.prototype%", « [[InitializedTemporalTime]], [[ISOHour]], [[ISOMinute]], [[ISOSecond]], [[ISOMillisecond]], [[ISOMicrosecond]], [[ISONanosecond]], [[Calendar]] »).
  257. // 5. Set object.[[ISOHour]] to hour.
  258. // 6. Set object.[[ISOMinute]] to minute.
  259. // 7. Set object.[[ISOSecond]] to second.
  260. // 8. Set object.[[ISOMillisecond]] to millisecond.
  261. // 9. Set object.[[ISOMicrosecond]] to microsecond.
  262. // 10. Set object.[[ISONanosecond]] to nanosecond.
  263. // 11. Set object.[[Calendar]] to ! GetISO8601Calendar().
  264. auto object = TRY(ordinary_create_from_constructor<PlainTime>(vm, *new_target, &Intrinsics::temporal_plain_time_prototype, hour, minute, second, millisecond, microsecond, nanosecond, *get_iso8601_calendar(vm)));
  265. // 12. Return object.
  266. return object.ptr();
  267. }
  268. // 4.5.8 ToTemporalTimeRecord ( temporalTimeLike [ , completeness ] ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaltimerecord
  269. ThrowCompletionOr<TemporalTimeLikeRecord> to_temporal_time_record(VM& vm, Object const& temporal_time_like, ToTemporalTimeRecordCompleteness completeness)
  270. {
  271. // 1. If completeness is not present, set completeness to complete.
  272. // 2. Let partial be ? PrepareTemporalFields(temporalTimeLike, « "hour", "microsecond", "millisecond", "minute", "nanosecond", "second" », partial).
  273. auto* partial = TRY(prepare_temporal_fields(vm, temporal_time_like,
  274. { "hour"_string,
  275. "microsecond"_string,
  276. "millisecond"_string,
  277. "minute"_string,
  278. "nanosecond"_string,
  279. "second"_string },
  280. PrepareTemporalFieldsPartial {}));
  281. TemporalTimeLikeRecord result;
  282. // 3. If completeness is complete, then
  283. if (completeness == ToTemporalTimeRecordCompleteness::Complete) {
  284. // a. Let result be a new TemporalTimeLike Record with each field set to 0.
  285. result = TemporalTimeLikeRecord { 0, 0, 0, 0, 0, 0 };
  286. }
  287. // 4. Else,
  288. else {
  289. // a. Let result be a new TemporalTimeLike Record with each field set to undefined.
  290. result = TemporalTimeLikeRecord {};
  291. }
  292. // 5. Let hourDesc be OrdinaryGetOwnProperty(partial, "hour").
  293. auto hour_desc = MUST(partial->Object::internal_get_own_property(vm.names.hour));
  294. // 6. If hourDesc is not undefined, then
  295. if (hour_desc.has_value()) {
  296. // a. Assert: hourDesc is a data Property Descriptor.
  297. VERIFY(hour_desc->is_data_descriptor());
  298. // b. Set result.[[Hour]] to ℝ(hourDesc.[[Value]]).
  299. result.hour = hour_desc->value->as_double();
  300. }
  301. // 7. Let minuteDesc be OrdinaryGetOwnProperty(partial, "minute").
  302. auto minute_desc = MUST(partial->Object::internal_get_own_property(vm.names.minute));
  303. // 8. If minuteDesc is not undefined, then
  304. if (minute_desc.has_value()) {
  305. // a. Assert: minuteDesc is a data Property Descriptor.
  306. VERIFY(minute_desc->is_data_descriptor());
  307. // b. Set result.[[Minute]] to ℝ(minuteDesc.[[Value]]).
  308. result.minute = minute_desc->value->as_double();
  309. }
  310. // 9. Let secondDesc be OrdinaryGetOwnProperty(partial, "second").
  311. auto second_desc = MUST(partial->Object::internal_get_own_property(vm.names.second));
  312. // 10. If secondDesc is not undefined, then
  313. if (second_desc.has_value()) {
  314. // a. Assert: secondDesc is a data Property Descriptor.
  315. VERIFY(second_desc->is_data_descriptor());
  316. // b. Set result.[[Second]] to ℝ(secondDesc.[[Value]]).
  317. result.second = second_desc->value->as_double();
  318. }
  319. // 11. Let millisecondDesc be OrdinaryGetOwnProperty(partial, "millisecond").
  320. auto millisecond_desc = MUST(partial->Object::internal_get_own_property(vm.names.millisecond));
  321. // 12. If millisecondDesc is not undefined, then
  322. if (millisecond_desc.has_value()) {
  323. // a. Assert: millisecondDesc is a data Property Descriptor.
  324. VERIFY(millisecond_desc->is_data_descriptor());
  325. // b. Set result.[[Millisecond]] to ℝ(millisecondDesc.[[Value]]).
  326. result.millisecond = millisecond_desc->value->as_double();
  327. }
  328. // 13. Let microsecondDesc be OrdinaryGetOwnProperty(partial, "microsecond").
  329. auto microsecond_desc = MUST(partial->Object::internal_get_own_property(vm.names.microsecond));
  330. // 14. If microsecondDesc is not undefined, then
  331. if (microsecond_desc.has_value()) {
  332. // a. Assert: microsecondDesc is a data Property Descriptor.
  333. VERIFY(microsecond_desc->is_data_descriptor());
  334. // b. Set result.[[Microsecond]] to ℝ(microsecondDesc.[[Value]]).
  335. result.microsecond = microsecond_desc->value->as_double();
  336. }
  337. // 15. Let nanosecondDesc be OrdinaryGetOwnProperty(partial, "nanosecond").
  338. auto nanosecond_desc = MUST(partial->Object::internal_get_own_property(vm.names.nanosecond));
  339. // 16. If nanosecondDesc is not undefined, then
  340. if (nanosecond_desc.has_value()) {
  341. // a. Assert: nanosecondDesc is a data Property Descriptor.
  342. VERIFY(nanosecond_desc->is_data_descriptor());
  343. // b. Set result.[[Nanosecond]] to ℝ(nanosecondDesc.[[Value]]).
  344. result.nanosecond = nanosecond_desc->value->as_double();
  345. }
  346. // 17. Return result.
  347. return result;
  348. }
  349. // 4.5.9 TemporalTimeToString ( hour, minute, second, millisecond, microsecond, nanosecond, precision ), https://tc39.es/proposal-temporal/#sec-temporal-temporaltimetostring
  350. ThrowCompletionOr<String> temporal_time_to_string(VM& vm, u8 hour, u8 minute, u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, Variant<StringView, u8> const& precision)
  351. {
  352. // 1. Assert: hour, minute, second, millisecond, microsecond and nanosecond are integers.
  353. // 2. Let hour be ToZeroPaddedDecimalString(hour, 2).
  354. // 3. Let minute be ToZeroPaddedDecimalString(minute, 2).
  355. // 4. Let seconds be ! FormatSecondsStringPart(second, millisecond, microsecond, nanosecond, precision).
  356. auto seconds = MUST_OR_THROW_OOM(format_seconds_string_part(vm, second, millisecond, microsecond, nanosecond, precision));
  357. // 5. Return the string-concatenation of hour, the code unit 0x003A (COLON), minute, and seconds.
  358. return TRY_OR_THROW_OOM(vm, String::formatted("{:02}:{:02}{}", hour, minute, seconds));
  359. }
  360. // 4.5.10 CompareTemporalTime ( h1, min1, s1, ms1, mus1, ns1, h2, min2, s2, ms2, mus2, ns2 ), https://tc39.es/proposal-temporal/#sec-temporal-comparetemporaltime
  361. i8 compare_temporal_time(u8 hour1, u8 minute1, u8 second1, u16 millisecond1, u16 microsecond1, u16 nanosecond1, u8 hour2, u8 minute2, u8 second2, u16 millisecond2, u16 microsecond2, u16 nanosecond2)
  362. {
  363. // 1. Assert: h1, min1, s1, ms1, mus1, ns1, h2, min2, s2, ms2, mus2, and ns2 are integers.
  364. // 2. If h1 > h2, return 1.
  365. if (hour1 > hour2)
  366. return 1;
  367. // 3. If h1 < h2, return -1.
  368. if (hour1 < hour2)
  369. return -1;
  370. // 4. If min1 > min2, return 1.
  371. if (minute1 > minute2)
  372. return 1;
  373. // 5. If min1 < min2, return -1.
  374. if (minute1 < minute2)
  375. return -1;
  376. // 6. If s1 > s2, return 1.
  377. if (second1 > second2)
  378. return 1;
  379. // 7. If s1 < s2, return -1.
  380. if (second1 < second2)
  381. return -1;
  382. // 8. If ms1 > ms2, return 1.
  383. if (millisecond1 > millisecond2)
  384. return 1;
  385. // 9. If ms1 < ms2, return -1.
  386. if (millisecond1 < millisecond2)
  387. return -1;
  388. // 10. If mus1 > mus2, return 1.
  389. if (microsecond1 > microsecond2)
  390. return 1;
  391. // 11. If mus1 < mus2, return -1.
  392. if (microsecond1 < microsecond2)
  393. return -1;
  394. // 12. If ns1 > ns2, return 1.
  395. if (nanosecond1 > nanosecond2)
  396. return 1;
  397. // 13. If ns1 < ns2, return -1.
  398. if (nanosecond1 < nanosecond2)
  399. return -1;
  400. // 14. Return 0.
  401. return 0;
  402. }
  403. // 4.5.11 AddTime ( hour, minute, second, millisecond, microsecond, nanosecond, hours, minutes, seconds, milliseconds, microseconds, nanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal-addtime
  404. DaysAndTime add_time(u8 hour, u8 minute, u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds)
  405. {
  406. // 1. Assert: hour, minute, second, millisecond, microsecond, nanosecond, hours, minutes, seconds, milliseconds, microseconds, and nanoseconds are integers.
  407. VERIFY(hours == trunc(hours) && minutes == trunc(minutes) && seconds == trunc(seconds) && milliseconds == trunc(milliseconds) && microseconds == trunc(microseconds) && nanoseconds == trunc(nanoseconds));
  408. // 2. Assert: IsValidTime(hour, minute, second, millisecond, microsecond, nanosecond) is true.
  409. VERIFY(is_valid_time(hour, minute, second, millisecond, microsecond, nanosecond));
  410. // 3. Let hour be hour + hours.
  411. auto hour_ = hour + hours;
  412. // 4. Let minute be minute + minutes.
  413. auto minute_ = minute + minutes;
  414. // 5. Let second be second + seconds.
  415. auto second_ = second + seconds;
  416. // 6. Let millisecond be millisecond + milliseconds.
  417. auto millisecond_ = millisecond + milliseconds;
  418. // 7. Let microsecond be microsecond + microseconds.
  419. auto microsecond_ = microsecond + microseconds;
  420. // 8. Let nanosecond be nanosecond + nanoseconds.
  421. auto nanosecond_ = nanosecond + nanoseconds;
  422. // 9. Return ! BalanceTime(hour, minute, second, millisecond, microsecond, nanosecond).
  423. return balance_time(hour_, minute_, second_, millisecond_, microsecond_, nanosecond_);
  424. }
  425. // 4.5.12 RoundTime ( hour, minute, second, millisecond, microsecond, nanosecond, increment, unit, roundingMode [ , dayLengthNs ] ), https://tc39.es/proposal-temporal/#sec-temporal-roundtime
  426. DaysAndTime round_time(u8 hour, u8 minute, u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, u64 increment, StringView unit, StringView rounding_mode, Optional<double> day_length_ns)
  427. {
  428. // 1. Assert: hour, minute, second, millisecond, microsecond, nanosecond, and increment are integers.
  429. // 2. Let fractionalSecond be nanosecond × 10-9 + microsecond × 10-6 + millisecond × 10-3 + second.
  430. double fractional_second = nanosecond * 0.000000001 + microsecond * 0.000001 + millisecond * 0.001 + second;
  431. double quantity;
  432. // 3. If unit is "day", then
  433. if (unit == "day"sv) {
  434. // a. If dayLengthNs is not present, set dayLengthNs to nsPerDay.
  435. if (!day_length_ns.has_value())
  436. day_length_ns = ns_per_day;
  437. // b. Let quantity be (((((hour × 60 + minute) × 60 + second) × 1000 + millisecond) × 1000 + microsecond) × 1000 + nanosecond) / dayLengthNs.
  438. quantity = (((((hour * 60.0 + minute) * 60.0 + second) * 1000.0 + millisecond) * 1000.0 + microsecond) * 1000.0 + nanosecond) / *day_length_ns;
  439. }
  440. // 4. Else if unit is "hour", then
  441. else if (unit == "hour"sv) {
  442. // a. Let quantity be (fractionalSecond / 60 + minute) / 60 + hour.
  443. quantity = (fractional_second / 60.0 + minute) / 60.0 + hour;
  444. }
  445. // 5. Else if unit is "minute", then
  446. else if (unit == "minute"sv) {
  447. // a. Let quantity be fractionalSecond / 60 + minute.
  448. quantity = fractional_second / 60.0 + minute;
  449. }
  450. // 6. Else if unit is "second", then
  451. else if (unit == "second"sv) {
  452. // a. Let quantity be fractionalSecond.
  453. quantity = fractional_second;
  454. }
  455. // 7. Else if unit is "millisecond", then
  456. else if (unit == "millisecond"sv) {
  457. // a. Let quantity be nanosecond × 10-6 + microsecond × 10-3 + millisecond.
  458. quantity = nanosecond * 0.000001 + 0.001 * microsecond + millisecond;
  459. }
  460. // 8. Else if unit is "microsecond", then
  461. else if (unit == "microsecond"sv) {
  462. // a. Let quantity be nanosecond × 10-3 + microsecond.
  463. quantity = nanosecond * 0.001 + microsecond;
  464. }
  465. // 9. Else,
  466. else {
  467. // a. Assert: unit is "nanosecond".
  468. VERIFY(unit == "nanosecond"sv);
  469. // b. Let quantity be nanosecond.
  470. quantity = nanosecond;
  471. }
  472. // 10. Let result be RoundNumberToIncrement(quantity, increment, roundingMode).
  473. auto result = round_number_to_increment(quantity, increment, rounding_mode);
  474. // If unit is "day", then
  475. if (unit == "day"sv) {
  476. // a. Return the Record { [[Days]]: result, [[Hour]]: 0, [[Minute]]: 0, [[Second]]: 0, [[Millisecond]]: 0, [[Microsecond]]: 0, [[Nanosecond]]: 0 }.
  477. return DaysAndTime { .days = (i32)result, .hour = 0, .minute = 0, .second = 0, .millisecond = 0, .microsecond = 0, .nanosecond = 0 };
  478. }
  479. // 12. If unit is "hour", then
  480. if (unit == "hour"sv) {
  481. // a. Return ! BalanceTime(result, 0, 0, 0, 0, 0).
  482. return balance_time(result, 0, 0, 0, 0, 0);
  483. }
  484. // 13. If unit is "minute", then
  485. if (unit == "minute"sv) {
  486. // a. Return ! BalanceTime(hour, result, 0, 0, 0, 0).
  487. return balance_time(hour, result, 0, 0, 0, 0);
  488. }
  489. // 14. If unit is "second", then
  490. if (unit == "second"sv) {
  491. // a. Return ! BalanceTime(hour, minute, result, 0, 0, 0).
  492. return balance_time(hour, minute, result, 0, 0, 0);
  493. }
  494. // 15. If unit is "millisecond", then
  495. if (unit == "millisecond"sv) {
  496. // a. Return ! BalanceTime(hour, minute, second, result, 0, 0).
  497. return balance_time(hour, minute, second, result, 0, 0);
  498. }
  499. // 16. If unit is "microsecond", then
  500. if (unit == "microsecond"sv) {
  501. // a. Return ! BalanceTime(hour, minute, second, millisecond, result, 0).
  502. return balance_time(hour, minute, second, millisecond, result, 0);
  503. }
  504. // 17. Assert: unit is "nanosecond".
  505. VERIFY(unit == "nanosecond"sv);
  506. // 18. Return ! BalanceTime(hour, minute, second, millisecond, microsecond, result).
  507. return balance_time(hour, minute, second, millisecond, microsecond, result);
  508. }
  509. // 4.5.13 DifferenceTemporalPlainTime ( operation, temporalTime, other, options ), https://tc39.es/proposal-temporal/#sec-temporal-differencetemporalplaintime
  510. ThrowCompletionOr<NonnullGCPtr<Duration>> difference_temporal_plain_time(VM& vm, DifferenceOperation operation, PlainTime const& temporal_time, Value other_value, Value options_value)
  511. {
  512. // 1. If operation is since, let sign be -1. Otherwise, let sign be 1.
  513. i8 sign = operation == DifferenceOperation::Since ? -1 : 1;
  514. // 2. Set other to ? ToTemporalTime(other).
  515. auto* other = TRY(to_temporal_time(vm, other_value));
  516. // 3. Let settings be ? GetDifferenceSettings(operation, options, time, « », "nanosecond", "hour").
  517. auto settings = TRY(get_difference_settings(vm, operation, options_value, UnitGroup::Time, {}, { "nanosecond"sv }, "hour"sv));
  518. // 4. Let result be ! DifferenceTime(temporalTime.[[ISOHour]], temporalTime.[[ISOMinute]], temporalTime.[[ISOSecond]], temporalTime.[[ISOMillisecond]], temporalTime.[[ISOMicrosecond]], temporalTime.[[ISONanosecond]], other.[[ISOHour]], other.[[ISOMinute]], other.[[ISOSecond]], other.[[ISOMillisecond]], other.[[ISOMicrosecond]], other.[[ISONanosecond]]).
  519. auto result = difference_time(vm, temporal_time.iso_hour(), temporal_time.iso_minute(), temporal_time.iso_second(), temporal_time.iso_millisecond(), temporal_time.iso_microsecond(), temporal_time.iso_nanosecond(), other->iso_hour(), other->iso_minute(), other->iso_second(), other->iso_millisecond(), other->iso_microsecond(), other->iso_nanosecond());
  520. // 5. Set result to (! RoundDuration(0, 0, 0, 0, result.[[Hours]], result.[[Minutes]], result.[[Seconds]], result.[[Milliseconds]], result.[[Microseconds]], result.[[Nanoseconds]], settings.[[RoundingIncrement]], settings.[[SmallestUnit]], settings.[[RoundingMode]])).[[DurationRecord]].
  521. auto rounded_result = MUST(round_duration(vm, 0, 0, 0, 0, result.hours, result.minutes, result.seconds, result.milliseconds, result.microseconds, result.nanoseconds, settings.rounding_increment, settings.smallest_unit, settings.rounding_mode)).duration_record;
  522. // 6. Set result to ! BalanceDuration(0, result.[[Hours]], result.[[Minutes]], result.[[Seconds]], result.[[Milliseconds]], result.[[Microseconds]], result.[[Nanoseconds]], settings.[[LargestUnit]]).
  523. result = MUST(balance_duration(vm, 0, rounded_result.hours, rounded_result.minutes, rounded_result.seconds, rounded_result.milliseconds, rounded_result.microseconds, Crypto::SignedBigInteger { rounded_result.nanoseconds }, settings.largest_unit));
  524. // 7. Return ! CreateTemporalDuration(0, 0, 0, 0, sign × result.[[Hours]], sign × result.[[Minutes]], sign × result.[[Seconds]], sign × result.[[Milliseconds]], sign × result.[[Microseconds]], sign × result.[[Nanoseconds]]).
  525. return MUST(create_temporal_duration(vm, 0, 0, 0, 0, sign * result.hours, sign * result.minutes, sign * result.seconds, sign * result.milliseconds, sign * result.microseconds, sign * result.nanoseconds));
  526. }
  527. // 4.5.14 AddDurationToOrSubtractDurationFromPlainTime ( operation, temporalTime, temporalDurationLike ), https://tc39.es/proposal-temporal/#sec-temporal-adddurationtoorsubtractdurationfromplaintime
  528. ThrowCompletionOr<PlainTime*> add_duration_to_or_subtract_duration_from_plain_time(VM& vm, ArithmeticOperation operation, PlainTime const& temporal_time, Value temporal_duration_like)
  529. {
  530. // 1. If operation is subtract, let sign be -1. Otherwise, let sign be 1.
  531. i8 sign = operation == ArithmeticOperation::Subtract ? -1 : 1;
  532. // 2. Let duration be ? ToTemporalDurationRecord(temporalDurationLike).
  533. auto duration = TRY(to_temporal_duration_record(vm, temporal_duration_like));
  534. // 3. Let result be ! AddTime(temporalTime.[[ISOHour]], temporalTime.[[ISOMinute]], temporalTime.[[ISOSecond]], temporalTime.[[ISOMillisecond]], temporalTime.[[ISOMicrosecond]], temporalTime.[[ISONanosecond]], sign × duration.[[Hours]], sign × duration.[[Minutes]], sign × duration.[[Seconds]], sign × duration.[[Milliseconds]], sign × duration.[[Microseconds]], sign × duration.[[Nanoseconds]]).
  535. auto result = add_time(temporal_time.iso_hour(), temporal_time.iso_minute(), temporal_time.iso_second(), temporal_time.iso_millisecond(), temporal_time.iso_microsecond(), temporal_time.iso_nanosecond(), sign * duration.hours, sign * duration.minutes, sign * duration.seconds, sign * duration.milliseconds, sign * duration.microseconds, sign * duration.nanoseconds);
  536. // 4. Assert: IsValidTime(result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]]) is true.
  537. VERIFY(is_valid_time(result.hour, result.minute, result.second, result.millisecond, result.microsecond, result.nanosecond));
  538. // 5. Return ! CreateTemporalTime(result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]]).
  539. return MUST(create_temporal_time(vm, result.hour, result.minute, result.second, result.millisecond, result.microsecond, result.nanosecond));
  540. }
  541. }