PlainTime.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Runtime/AbstractOperations.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/Calendar.h>
  12. #include <LibJS/Runtime/Temporal/Duration.h>
  13. #include <LibJS/Runtime/Temporal/Instant.h>
  14. #include <LibJS/Runtime/Temporal/PlainDateTime.h>
  15. #include <LibJS/Runtime/Temporal/PlainTime.h>
  16. #include <LibJS/Runtime/Temporal/PlainTimeConstructor.h>
  17. #include <LibJS/Runtime/Temporal/TimeZone.h>
  18. #include <LibJS/Runtime/Temporal/ZonedDateTime.h>
  19. namespace JS::Temporal {
  20. // 4 Temporal.PlainTime Objects, https://tc39.es/proposal-temporal/#sec-temporal-plaintime-objects
  21. PlainTime::PlainTime(u8 iso_hour, u8 iso_minute, u8 iso_second, u16 iso_millisecond, u16 iso_microsecond, u16 iso_nanosecond, Calendar& calendar, Object& prototype)
  22. : Object(prototype)
  23. , m_iso_hour(iso_hour)
  24. , m_iso_minute(iso_minute)
  25. , m_iso_second(iso_second)
  26. , m_iso_millisecond(iso_millisecond)
  27. , m_iso_microsecond(iso_microsecond)
  28. , m_iso_nanosecond(iso_nanosecond)
  29. , m_calendar(calendar)
  30. {
  31. }
  32. void PlainTime::visit_edges(Visitor& visitor)
  33. {
  34. Base::visit_edges(visitor);
  35. visitor.visit(&m_calendar);
  36. }
  37. // 4.5.1 DifferenceTime ( h1, min1, s1, ms1, mus1, ns1, h2, min2, s2, ms2, mus2, ns2 ), https://tc39.es/proposal-temporal/#sec-temporal-differencetime
  38. TimeDurationRecord difference_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)
  39. {
  40. // 1. Let hours be h2 − h1.
  41. auto hours = hour2 - hour1;
  42. // 2. Let minutes be min2 − min1.
  43. auto minutes = minute2 - minute1;
  44. // 3. Let seconds be s2 − s1.
  45. auto seconds = second2 - second1;
  46. // 4. Let milliseconds be ms2 − ms1.
  47. auto milliseconds = millisecond2 - millisecond1;
  48. // 5. Let microseconds be mus2 − mus1.
  49. auto microseconds = microsecond2 - microsecond1;
  50. // 6. Let nanoseconds be ns2 − ns1.
  51. auto nanoseconds = nanosecond2 - nanosecond1;
  52. // 7. Let sign be ! DurationSign(0, 0, 0, 0, hours, minutes, seconds, milliseconds, microseconds, nanoseconds).
  53. auto sign = duration_sign(0, 0, 0, 0, hours, minutes, seconds, milliseconds, microseconds, nanoseconds);
  54. // 8. Let bt be ! BalanceTime(hours × sign, minutes × sign, seconds × sign, milliseconds × sign, microseconds × sign, nanoseconds × sign).
  55. auto bt = balance_time(hours * sign, minutes * sign, seconds * sign, milliseconds * sign, microseconds * sign, nanoseconds * sign);
  56. // 9. Return the Record { [[Days]]: bt.[[Days]] × sign, [[Hours]]: bt.[[Hour]] × sign, [[Minutes]]: bt.[[Minute]] × sign, [[Seconds]]: bt.[[Second]] × sign, [[Milliseconds]]: bt.[[Millisecond]] × sign, [[Microseconds]]: bt.[[Microsecond]] × sign, [[Nanoseconds]]: bt.[[Nanosecond]] × sign }.
  57. return TimeDurationRecord { .days = static_cast<double>(bt.days * sign), .hours = static_cast<double>(bt.hour * sign), .minutes = static_cast<double>(bt.minute * sign), .seconds = static_cast<double>(bt.second * sign), .milliseconds = static_cast<double>(bt.millisecond * sign), .microseconds = static_cast<double>(bt.microsecond * sign), .nanoseconds = static_cast<double>(bt.nanosecond * sign) };
  58. }
  59. // 4.5.2 ToTemporalTime ( item [ , overflow ] ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaltime
  60. ThrowCompletionOr<PlainTime*> to_temporal_time(GlobalObject& global_object, Value item, Optional<StringView> overflow)
  61. {
  62. auto& vm = global_object.vm();
  63. // 1. If overflow is not present, set it to "constrain".
  64. if (!overflow.has_value())
  65. overflow = "constrain"sv;
  66. // 2. Assert: overflow is either "constrain" or "reject".
  67. VERIFY(overflow == "constrain"sv || overflow == "reject"sv);
  68. Optional<TemporalTime> result;
  69. // 3. If Type(item) is Object, then
  70. if (item.is_object()) {
  71. auto& item_object = item.as_object();
  72. // a. If item has an [[InitializedTemporalTime]] internal slot, then
  73. if (is<PlainTime>(item_object)) {
  74. // i. Return item.
  75. return &static_cast<PlainTime&>(item_object);
  76. }
  77. // b. If item has an [[InitializedTemporalZonedDateTime]] internal slot, then
  78. if (is<ZonedDateTime>(item_object)) {
  79. auto& zoned_date_time = static_cast<ZonedDateTime&>(item_object);
  80. // i. Let instant be ! CreateTemporalInstant(item.[[Nanoseconds]]).
  81. auto* instant = create_temporal_instant(global_object, zoned_date_time.nanoseconds()).release_value();
  82. // ii. Set plainDateTime to ? BuiltinTimeZoneGetPlainDateTimeFor(item.[[TimeZone]], instant, item.[[Calendar]]).
  83. auto* plain_date_time = TRY(builtin_time_zone_get_plain_date_time_for(global_object, &zoned_date_time.time_zone(), *instant, zoned_date_time.calendar()));
  84. // iii. Return ! CreateTemporalTime(plainDateTime.[[ISOHour]], plainDateTime.[[ISOMinute]], plainDateTime.[[ISOSecond]], plainDateTime.[[ISOMillisecond]], plainDateTime.[[ISOMicrosecond]], plainDateTime.[[ISONanosecond]]).
  85. return TRY(create_temporal_time(global_object, 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()));
  86. }
  87. // c. If item has an [[InitializedTemporalDateTime]] internal slot, then
  88. if (is<PlainDateTime>(item_object)) {
  89. auto& plain_date_time = static_cast<PlainDateTime&>(item_object);
  90. // i. Return ! CreateTemporalTime(item.[[ISOHour]], item.[[ISOMinute]], item.[[ISOSecond]], item.[[ISOMillisecond]], item.[[ISOMicrosecond]], item.[[ISONanosecond]]).
  91. return TRY(create_temporal_time(global_object, 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()));
  92. }
  93. // d. Let calendar be ? GetTemporalCalendarWithISODefault(item).
  94. auto* calendar = TRY(get_temporal_calendar_with_iso_default(global_object, item_object));
  95. // e. If ? ToString(calendar) is not "iso8601", then
  96. auto calendar_identifier = TRY(Value(calendar).to_string(global_object));
  97. if (calendar_identifier != "iso8601"sv) {
  98. // i. Throw a RangeError exception.
  99. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidCalendarIdentifier, calendar_identifier);
  100. }
  101. // f. Let result be ? ToTemporalTimeRecord(item).
  102. auto unregulated_result = TRY(to_temporal_time_record(global_object, item_object));
  103. // g. Set result to ? RegulateTime(result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]], overflow).
  104. result = TRY(regulate_time(global_object, unregulated_result.hour, unregulated_result.minute, unregulated_result.second, unregulated_result.millisecond, unregulated_result.microsecond, unregulated_result.nanosecond, *overflow));
  105. }
  106. // 4. Else,
  107. else {
  108. // a. Let string be ? ToString(item).
  109. auto string = TRY(item.to_string(global_object));
  110. // b. Let result be ? ParseTemporalTimeString(string).
  111. result = TRY(parse_temporal_time_string(global_object, string));
  112. // c. Assert: ! IsValidTime(result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]]) is true.
  113. VERIFY(is_valid_time(result->hour, result->minute, result->second, result->millisecond, result->microsecond, result->nanosecond));
  114. // d. If result.[[Calendar]] is not one of undefined or "iso8601", then
  115. if (result->calendar.has_value() && *result->calendar != "iso8601"sv) {
  116. // i. Throw a RangeError exception.
  117. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidCalendarIdentifier, *result->calendar);
  118. }
  119. }
  120. // 5. Return ? CreateTemporalTime(result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]]).
  121. return TRY(create_temporal_time(global_object, result->hour, result->minute, result->second, result->millisecond, result->microsecond, result->nanosecond));
  122. }
  123. // 4.5.3 ToPartialTime ( temporalTimeLike ), https://tc39.es/proposal-temporal/#sec-temporal-topartialtime
  124. ThrowCompletionOr<PartialUnregulatedTemporalTime> to_partial_time(GlobalObject& global_object, Object& temporal_time_like)
  125. {
  126. auto& vm = global_object.vm();
  127. // 1. Assert: Type(temporalTimeLike) is Object.
  128. // 2. Let result be the Record { [[Hour]]: undefined, [[Minute]]: undefined, [[Second]]: undefined, [[Millisecond]]: undefined, [[Microsecond]]: undefined, [[Nanosecond]]: undefined }.
  129. auto result = PartialUnregulatedTemporalTime {};
  130. // 3. Let any be false.
  131. bool any = false;
  132. // 4. For each row of Table 3, except the header row, in table order, do
  133. for (auto& [internal_slot, property] : temporal_time_like_properties<PartialUnregulatedTemporalTime, Optional<double>>(vm)) {
  134. // a. Let property be the Property value of the current row.
  135. // b. Let value be ? Get(temporalTimeLike, property).
  136. auto value = TRY(temporal_time_like.get(property));
  137. // c. If value is not undefined, then
  138. if (!value.is_undefined()) {
  139. // i. Set any to true.
  140. any = true;
  141. // ii. Set value to ? ToIntegerThrowOnInfinity(value).
  142. auto value_number = TRY(to_integer_throw_on_infinity(global_object, value, ErrorType::TemporalPropertyMustBeFinite));
  143. // iii. Set result's internal slot whose name is the Internal Slot value of the current row to value.
  144. result.*internal_slot = value_number;
  145. }
  146. }
  147. // 5. If any is false, then
  148. if (!any) {
  149. // a. Throw a TypeError exception.
  150. return vm.throw_completion<TypeError>(global_object, ErrorType::TemporalInvalidPlainTimeLikeObject);
  151. }
  152. // 6. Return result.
  153. return result;
  154. }
  155. // 4.5.4 RegulateTime ( hour, minute, second, millisecond, microsecond, nanosecond, overflow ), https://tc39.es/proposal-temporal/#sec-temporal-regulatetime
  156. ThrowCompletionOr<TemporalTime> regulate_time(GlobalObject& global_object, double hour, double minute, double second, double millisecond, double microsecond, double nanosecond, StringView overflow)
  157. {
  158. auto& vm = global_object.vm();
  159. // 1. Assert: hour, minute, second, millisecond, microsecond and nanosecond are integers.
  160. // NOTE: As the spec is currently written this assertion can fail, these are either integers _or_ infinity.
  161. // See https://github.com/tc39/proposal-temporal/issues/1672.
  162. // 2. Assert: overflow is either "constrain" or "reject".
  163. // NOTE: Asserted by the VERIFY_NOT_REACHED at the end
  164. // 3. If overflow is "constrain", then
  165. if (overflow == "constrain"sv) {
  166. // a. Return ! ConstrainTime(hour, minute, second, millisecond, microsecond, nanosecond).
  167. return constrain_time(hour, minute, second, millisecond, microsecond, nanosecond);
  168. }
  169. // 4. If overflow is "reject", then
  170. if (overflow == "reject"sv) {
  171. // a. If ! IsValidTime(hour, minute, second, millisecond, microsecond, nanosecond) is false, throw a RangeError exception.
  172. if (!is_valid_time(hour, minute, second, millisecond, microsecond, nanosecond))
  173. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidPlainTime);
  174. // b. Return the Record { [[Hour]]: hour, [[Minute]]: minute, [[Second]]: second, [[Millisecond]]: millisecond, [[Microsecond]]: microsecond, [[Nanosecond]]: nanosecond }.
  175. 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) };
  176. }
  177. VERIFY_NOT_REACHED();
  178. }
  179. // 4.5.5 IsValidTime ( hour, minute, second, millisecond, microsecond, nanosecond ), https://tc39.es/proposal-temporal/#sec-temporal-isvalidtime
  180. bool is_valid_time(double hour, double minute, double second, double millisecond, double microsecond, double nanosecond)
  181. {
  182. // 1. Assert: hour, minute, second, millisecond, microsecond, and nanosecond are integers.
  183. // 2. If hour < 0 or hour > 23, then
  184. if (hour > 23) {
  185. // a. Return false.
  186. return false;
  187. }
  188. // 3. If minute < 0 or minute > 59, then
  189. if (minute > 59) {
  190. // a. Return false.
  191. return false;
  192. }
  193. // 4. If second < 0 or second > 59, then
  194. if (second > 59) {
  195. // a. Return false.
  196. return false;
  197. }
  198. // 5. If millisecond < 0 or millisecond > 999, then
  199. if (millisecond > 999) {
  200. // a. Return false.
  201. return false;
  202. }
  203. // 6. If microsecond < 0 or microsecond > 999, then
  204. if (microsecond > 999) {
  205. // a. Return false.
  206. return false;
  207. }
  208. // 7. If nanosecond < 0 or nanosecond > 999, then
  209. if (nanosecond > 999) {
  210. // a. Return false.
  211. return false;
  212. }
  213. // 8. Return true.
  214. return true;
  215. }
  216. // 4.5.6 BalanceTime ( hour, minute, second, millisecond, microsecond, nanosecond ), https://tc39.es/proposal-temporal/#sec-temporal-balancetime
  217. DaysAndTime balance_time(double hour, double minute, double second, double millisecond, double microsecond, double nanosecond)
  218. {
  219. // 1. Assert: hour, minute, second, millisecond, microsecond, and nanosecond are integers.
  220. VERIFY(hour == trunc(hour) && minute == trunc(minute) && second == trunc(second) && millisecond == trunc(millisecond) && microsecond == trunc(microsecond) && nanosecond == trunc(nanosecond));
  221. // 2. Set microsecond to microsecond + floor(nanosecond / 1000).
  222. microsecond += floor(nanosecond / 1000);
  223. // 3. Set nanosecond to nanosecond modulo 1000.
  224. nanosecond = modulo(nanosecond, 1000);
  225. // 4. Set millisecond to millisecond + floor(microsecond / 1000).
  226. millisecond += floor(microsecond / 1000);
  227. // 5. Set microsecond to microsecond modulo 1000.
  228. microsecond = modulo(microsecond, 1000);
  229. // 6. Set second to second + floor(millisecond / 1000).
  230. second += floor(millisecond / 1000);
  231. // 7. Set millisecond to millisecond modulo 1000.
  232. millisecond = modulo(millisecond, 1000);
  233. // 8. Set minute to minute + floor(second / 60).
  234. minute += floor(second / 60);
  235. // 9. Set second to second modulo 60.
  236. second = modulo(second, 60);
  237. // 10. Set hour to hour + floor(minute / 60).
  238. hour += floor(minute / 60);
  239. // 11. Set minute to minute modulo 60.
  240. minute = modulo(minute, 60);
  241. // 12. Let days be floor(hour / 24).
  242. auto days = floor(hour / 24);
  243. // 13. Set hour to hour modulo 24.
  244. hour = modulo(hour, 24);
  245. // 14. Return the Record { [[Days]]: days, [[Hour]]: hour, [[Minute]]: minute, [[Second]]: second, [[Millisecond]]: millisecond, [[Microsecond]]: microsecond, [[Nanosecond]]: nanosecond }.
  246. return DaysAndTime {
  247. .days = static_cast<i32>(days),
  248. .hour = static_cast<u8>(hour),
  249. .minute = static_cast<u8>(minute),
  250. .second = static_cast<u8>(second),
  251. .millisecond = static_cast<u16>(millisecond),
  252. .microsecond = static_cast<u16>(microsecond),
  253. .nanosecond = static_cast<u16>(nanosecond),
  254. };
  255. }
  256. // 4.5.7 ConstrainTime ( hour, minute, second, millisecond, microsecond, nanosecond ), https://tc39.es/proposal-temporal/#sec-temporal-constraintime
  257. TemporalTime constrain_time(double hour, double minute, double second, double millisecond, double microsecond, double nanosecond)
  258. {
  259. // 1. Assert: hour, minute, second, millisecond, microsecond, and nanosecond are integers.
  260. // 2. Set hour to ! ConstrainToRange(hour, 0, 23).
  261. hour = constrain_to_range(hour, 0, 23);
  262. // 3. Set minute to ! ConstrainToRange(minute, 0, 59).
  263. minute = constrain_to_range(minute, 0, 59);
  264. // 4. Set second to ! ConstrainToRange(second, 0, 59).
  265. second = constrain_to_range(second, 0, 59);
  266. // 5. Set millisecond to ! ConstrainToRange(millisecond, 0, 999).
  267. millisecond = constrain_to_range(millisecond, 0, 999);
  268. // 6. Set microsecond to ! ConstrainToRange(microsecond, 0, 999).
  269. microsecond = constrain_to_range(microsecond, 0, 999);
  270. // 7. Set nanosecond to ! ConstrainToRange(nanosecond, 0, 999).
  271. nanosecond = constrain_to_range(nanosecond, 0, 999);
  272. // 8. Return the Record { [[Hour]]: hour, [[Minute]]: minute, [[Second]]: second, [[Millisecond]]: millisecond, [[Microsecond]]: microsecond, [[Nanosecond]]: nanosecond }.
  273. 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) };
  274. }
  275. // 4.5.8 CreateTemporalTime ( hour, minute, second, millisecond, microsecond, nanosecond [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporaltime
  276. ThrowCompletionOr<PlainTime*> create_temporal_time(GlobalObject& global_object, u8 hour, u8 minute, u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, FunctionObject const* new_target)
  277. {
  278. auto& vm = global_object.vm();
  279. // 1. Assert: hour, minute, second, millisecond, microsecond and nanosecond are integers.
  280. // 2. If ! IsValidTime(hour, minute, second, millisecond, microsecond, nanosecond) is false, throw a RangeError exception.
  281. if (!is_valid_time(hour, minute, second, millisecond, microsecond, nanosecond))
  282. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidPlainTime);
  283. // 3. If newTarget is not present, set it to %Temporal.PlainTime%.
  284. if (!new_target)
  285. new_target = global_object.temporal_plain_time_constructor();
  286. // 4. Let object be ? OrdinaryCreateFromConstructor(newTarget, "%Temporal.PlainTime.prototype%", « [[InitializedTemporalTime]], [[ISOHour]], [[ISOMinute]], [[ISOSecond]], [[ISOMillisecond]], [[ISOMicrosecond]], [[ISONanosecond]], [[Calendar]] »).
  287. // 5. Set object.[[ISOHour]] to hour.
  288. // 6. Set object.[[ISOMinute]] to minute.
  289. // 7. Set object.[[ISOSecond]] to second.
  290. // 8. Set object.[[ISOMillisecond]] to millisecond.
  291. // 9. Set object.[[ISOMicrosecond]] to microsecond.
  292. // 10. Set object.[[ISONanosecond]] to nanosecond.
  293. // 11. Set object.[[Calendar]] to ! GetISO8601Calendar().
  294. auto* object = TRY(ordinary_create_from_constructor<PlainTime>(global_object, *new_target, &GlobalObject::temporal_plain_time_prototype, hour, minute, second, millisecond, microsecond, nanosecond, *get_iso8601_calendar(global_object)));
  295. // 12. Return object.
  296. return object;
  297. }
  298. // 4.5.9 ToTemporalTimeRecord ( temporalTimeLike ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaltimerecord
  299. ThrowCompletionOr<UnregulatedTemporalTime> to_temporal_time_record(GlobalObject& global_object, Object const& temporal_time_like)
  300. {
  301. auto& vm = global_object.vm();
  302. // 1. Assert: Type(temporalTimeLike) is Object.
  303. // 2. Let result be the Record { [[Hour]]: undefined, [[Minute]]: undefined, [[Second]]: undefined, [[Millisecond]]: undefined, [[Microsecond]]: undefined, [[Nanosecond]]: undefined }.
  304. auto result = UnregulatedTemporalTime {};
  305. // 3. Let any be false.
  306. auto any = false;
  307. // 4. For each row of Table 3, except the header row, in table order, do
  308. for (auto& [internal_slot, property] : temporal_time_like_properties<UnregulatedTemporalTime, double>(vm)) {
  309. // a. Let property be the Property value of the current row.
  310. // b. Let value be ? Get(temporalTimeLike, property).
  311. auto value = TRY(temporal_time_like.get(property));
  312. // c. If value is not undefined, then
  313. if (!value.is_undefined()) {
  314. // i. Set any to true.
  315. any = true;
  316. }
  317. // d. Set value to ? ToIntegerThrowOnInfinity(value).
  318. auto value_number = TRY(to_integer_throw_on_infinity(global_object, value, ErrorType::TemporalPropertyMustBeFinite));
  319. // e. Set result's internal slot whose name is the Internal Slot value of the current row to value.
  320. result.*internal_slot = value_number;
  321. }
  322. // 5. If any is false, then
  323. if (!any) {
  324. // a. Throw a TypeError exception.
  325. return vm.throw_completion<TypeError>(global_object, ErrorType::TemporalInvalidPlainTimeLikeObject);
  326. }
  327. // 6. Return result.
  328. return result;
  329. }
  330. // 4.5.10 TemporalTimeToString ( hour, minute, second, millisecond, microsecond, nanosecond, precision ), https://tc39.es/proposal-temporal/#sec-temporal-temporaltimetostring
  331. String temporal_time_to_string(u8 hour, u8 minute, u8 second, u16 millisecond, u16 microsecond, u16 nanosecond, Variant<StringView, u8> const& precision)
  332. {
  333. // 1. Assert: hour, minute, second, millisecond, microsecond and nanosecond are integers.
  334. // 2. Let hour be hour formatted as a two-digit decimal number, padded to the left with a zero if necessary.
  335. // 3. Let minute be minute formatted as a two-digit decimal number, padded to the left with a zero if necessary.
  336. // 4. Let seconds be ! FormatSecondsStringPart(second, millisecond, microsecond, nanosecond, precision).
  337. auto seconds = format_seconds_string_part(second, millisecond, microsecond, nanosecond, precision);
  338. // 5. Return the string-concatenation of hour, the code unit 0x003A (COLON), minute, and seconds.
  339. return String::formatted("{:02}:{:02}{}", hour, minute, seconds);
  340. }
  341. // 4.5.11 CompareTemporalTime ( h1, min1, s1, ms1, mus1, ns1, h2, min2, s2, ms2, mus2, ns2 ), https://tc39.es/proposal-temporal/#sec-temporal-comparetemporaltime
  342. 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)
  343. {
  344. // 1. Assert: h1, min1, s1, ms1, mus1, ns1, h2, min2, s2, ms2, mus2, and ns2 are integers.
  345. // 2. If h1 > h2, return 1.
  346. if (hour1 > hour2)
  347. return 1;
  348. // 3. If h1 < h2, return -1.
  349. if (hour1 < hour2)
  350. return -1;
  351. // 4. If min1 > min2, return 1.
  352. if (minute1 > minute2)
  353. return 1;
  354. // 5. If min1 < min2, return -1.
  355. if (minute1 < minute2)
  356. return -1;
  357. // 6. If s1 > s2, return 1.
  358. if (second1 > second2)
  359. return 1;
  360. // 7. If s1 < s2, return -1.
  361. if (second1 < second2)
  362. return -1;
  363. // 8. If ms1 > ms2, return 1.
  364. if (millisecond1 > millisecond2)
  365. return 1;
  366. // 9. If ms1 < ms2, return -1.
  367. if (millisecond1 < millisecond2)
  368. return -1;
  369. // 10. If mus1 > mus2, return 1.
  370. if (microsecond1 > microsecond2)
  371. return 1;
  372. // 11. If mus1 < mus2, return -1.
  373. if (microsecond1 < microsecond2)
  374. return -1;
  375. // 12. If ns1 > ns2, return 1.
  376. if (nanosecond1 > nanosecond2)
  377. return 1;
  378. // 13. If ns1 < ns2, return -1.
  379. if (nanosecond1 < nanosecond2)
  380. return -1;
  381. // 14. Return 0.
  382. return 0;
  383. }
  384. // 4.5.12 AddTime ( hour, minute, second, millisecond, microsecond, nanosecond, hours, minutes, seconds, milliseconds, microseconds, nanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal-addtime
  385. 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)
  386. {
  387. // 1. Assert: hour, minute, second, millisecond, microsecond, nanosecond, hours, minutes, seconds, milliseconds, microseconds, and nanoseconds are integers.
  388. VERIFY(hours == trunc(hours) && minutes == trunc(minutes) && seconds == trunc(seconds) && milliseconds == trunc(milliseconds) && microseconds == trunc(microseconds) && nanoseconds == trunc(nanoseconds));
  389. // 2. Let hour be hour + hours.
  390. auto hour_ = hour + hours;
  391. // 3. Let minute be minute + minutes.
  392. auto minute_ = minute + minutes;
  393. // 4. Let second be second + seconds.
  394. auto second_ = second + seconds;
  395. // 5. Let millisecond be millisecond + milliseconds.
  396. auto millisecond_ = millisecond + milliseconds;
  397. // 6. Let microsecond be microsecond + microseconds.
  398. auto microsecond_ = microsecond + microseconds;
  399. // 7. Let nanosecond be nanosecond + nanoseconds.
  400. auto nanosecond_ = nanosecond + nanoseconds;
  401. // 8. Return ! BalanceTime(hour, minute, second, millisecond, microsecond, nanosecond).
  402. return balance_time(hour_, minute_, second_, millisecond_, microsecond_, nanosecond_);
  403. }
  404. // 4.5.13 RoundTime ( hour, minute, second, millisecond, microsecond, nanosecond, increment, unit, roundingMode [ , dayLengthNs ] ), https://tc39.es/proposal-temporal/#sec-temporal-roundtime
  405. 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)
  406. {
  407. // 1. Assert: hour, minute, second, millisecond, microsecond, nanosecond, and increment are integers.
  408. // 2. Let fractionalSecond be nanosecond × 10−9 + microsecond × 10−6 + millisecond × 10−3 + second.
  409. double fractional_second = nanosecond * 0.000000001 + microsecond * 0.000001 + millisecond * 0.001 + second;
  410. double quantity;
  411. // 3. If unit is "day", then
  412. if (unit == "day"sv) {
  413. // a. If dayLengthNs is not present, set it to 8.64 × 10^13.
  414. if (!day_length_ns.has_value())
  415. day_length_ns = 86400000000000;
  416. // b. Let quantity be (((((hour × 60 + minute) × 60 + second) × 1000 + millisecond) × 1000 + microsecond) × 1000 + nanosecond) / dayLengthNs.
  417. quantity = (((((hour * 60 + minute) * 60 + second) * 1000 + millisecond) * 1000 + microsecond) * 1000 + nanosecond) / *day_length_ns;
  418. }
  419. // 4. Else if unit is "hour", then
  420. else if (unit == "hour"sv) {
  421. // a. Let quantity be (fractionalSecond / 60 + minute) / 60 + hour.
  422. quantity = (fractional_second / 60 + minute) / 60 + hour;
  423. }
  424. // 5. Else if unit is "minute", then
  425. else if (unit == "minute"sv) {
  426. // a. Let quantity be fractionalSecond / 60 + minute.
  427. quantity = fractional_second / 60 + minute;
  428. }
  429. // 6. Else if unit is "second", then
  430. else if (unit == "second"sv) {
  431. // a. Let quantity be fractionalSecond.
  432. quantity = fractional_second;
  433. }
  434. // 7. Else if unit is "millisecond", then
  435. else if (unit == "millisecond"sv) {
  436. // a. Let quantity be nanosecond × 10−6 + microsecond × 10−3 + millisecond.
  437. quantity = nanosecond * 0.000001 + 0.001 * microsecond + millisecond;
  438. }
  439. // 8. Else if unit is "microsecond", then
  440. else if (unit == "microsecond"sv) {
  441. // a. Let quantity be nanosecond × 10−3 + microsecond.
  442. quantity = nanosecond * 0.001 + microsecond;
  443. }
  444. // 9. Else,
  445. else {
  446. // a. Assert: unit is "nanosecond".
  447. VERIFY(unit == "nanosecond"sv);
  448. // b. Let quantity be nanosecond.
  449. quantity = nanosecond;
  450. }
  451. // 10. Let result be ! RoundNumberToIncrement(quantity, increment, roundingMode).
  452. auto result = round_number_to_increment(quantity, increment, rounding_mode);
  453. // If unit is "day", then
  454. if (unit == "day"sv) {
  455. // a. Return the Record { [[Days]]: result, [[Hour]]: 0, [[Minute]]: 0, [[Second]]: 0, [[Millisecond]]: 0, [[Microsecond]]: 0, [[Nanosecond]]: 0 }.
  456. return DaysAndTime { .days = (i32)result, .hour = 0, .minute = 0, .second = 0, .millisecond = 0, .microsecond = 0, .nanosecond = 0 };
  457. }
  458. // 12. If unit is "hour", then
  459. if (unit == "hour"sv) {
  460. // a. Return ! BalanceTime(result, 0, 0, 0, 0, 0).
  461. return balance_time(result, 0, 0, 0, 0, 0);
  462. }
  463. // 13. If unit is "minute", then
  464. if (unit == "minute"sv) {
  465. // a. Return ! BalanceTime(hour, result, 0, 0, 0, 0).
  466. return balance_time(hour, result, 0, 0, 0, 0);
  467. }
  468. // 14. If unit is "second", then
  469. if (unit == "second"sv) {
  470. // a. Return ! BalanceTime(hour, minute, result, 0, 0, 0).
  471. return balance_time(hour, minute, result, 0, 0, 0);
  472. }
  473. // 15. If unit is "millisecond", then
  474. if (unit == "millisecond"sv) {
  475. // a. Return ! BalanceTime(hour, minute, second, result, 0, 0).
  476. return balance_time(hour, minute, second, result, 0, 0);
  477. }
  478. // 16. If unit is "microsecond", then
  479. if (unit == "microsecond"sv) {
  480. // a. Return ! BalanceTime(hour, minute, second, millisecond, result, 0).
  481. return balance_time(hour, minute, second, millisecond, result, 0);
  482. }
  483. // 17. Assert: unit is "nanosecond".
  484. VERIFY(unit == "nanosecond"sv);
  485. // 18. Return ! BalanceTime(hour, minute, second, millisecond, microsecond, result).
  486. return balance_time(hour, minute, second, millisecond, microsecond, result);
  487. }
  488. }