Instant.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Variant.h>
  8. #include <LibCrypto/BigInt/SignedBigInteger.h>
  9. #include <LibJS/Runtime/AbstractOperations.h>
  10. #include <LibJS/Runtime/GlobalObject.h>
  11. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  12. #include <LibJS/Runtime/Temporal/Calendar.h>
  13. #include <LibJS/Runtime/Temporal/Instant.h>
  14. #include <LibJS/Runtime/Temporal/InstantConstructor.h>
  15. #include <LibJS/Runtime/Temporal/PlainDateTime.h>
  16. #include <LibJS/Runtime/Temporal/TimeZone.h>
  17. #include <LibJS/Runtime/Temporal/ZonedDateTime.h>
  18. namespace JS::Temporal {
  19. // 8 Temporal.Instant Objects, https://tc39.es/proposal-temporal/#sec-temporal-instant-objects
  20. Instant::Instant(BigInt const& nanoseconds, Object& prototype)
  21. : Object(prototype)
  22. , m_nanoseconds(nanoseconds)
  23. {
  24. }
  25. void Instant::visit_edges(Cell::Visitor& visitor)
  26. {
  27. Base::visit_edges(visitor);
  28. visitor.visit(&m_nanoseconds);
  29. }
  30. // 8.5.1 IsValidEpochNanoseconds ( epochNanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal-isvalidepochnanoseconds
  31. bool is_valid_epoch_nanoseconds(BigInt const& epoch_nanoseconds)
  32. {
  33. // 1. Assert: Type(epochNanoseconds) is BigInt.
  34. // 2. If epochNanoseconds < −86400ℤ × 10^17ℤ or epochNanoseconds > 86400ℤ × 10^17ℤ, then
  35. if (epoch_nanoseconds.big_integer() < INSTANT_NANOSECONDS_MIN || epoch_nanoseconds.big_integer() > INSTANT_NANOSECONDS_MAX) {
  36. // a. Return false.
  37. return false;
  38. }
  39. // 3. Return true.
  40. return true;
  41. }
  42. // 8.5.2 CreateTemporalInstant ( epochNanoseconds [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporalinstant
  43. Instant* create_temporal_instant(GlobalObject& global_object, BigInt const& epoch_nanoseconds, FunctionObject const* new_target)
  44. {
  45. // 1. Assert: Type(epochNanoseconds) is BigInt.
  46. // 2. Assert: ! IsValidEpochNanoseconds(epochNanoseconds) is true.
  47. VERIFY(is_valid_epoch_nanoseconds(epoch_nanoseconds));
  48. // 3. If newTarget is not present, set it to %Temporal.Instant%.
  49. if (!new_target)
  50. new_target = global_object.temporal_instant_constructor();
  51. // 4. Let object be ? OrdinaryCreateFromConstructor(newTarget, "%Temporal.Instant.prototype%", « [[InitializedTemporalInstant]], [[Nanoseconds]] »).
  52. // 5. Set object.[[Nanoseconds]] to epochNanoseconds.
  53. auto* object = TRY_OR_DISCARD(ordinary_create_from_constructor<Instant>(global_object, *new_target, &GlobalObject::temporal_instant_prototype, epoch_nanoseconds));
  54. // 6. Return object.
  55. return object;
  56. }
  57. // 8.5.3 ToTemporalInstant ( item ), https://tc39.es/proposal-temporal/#sec-temporal-totemporalinstant
  58. Instant* to_temporal_instant(GlobalObject& global_object, Value item)
  59. {
  60. auto& vm = global_object.vm();
  61. // 1. If Type(item) is Object, then
  62. if (item.is_object()) {
  63. // a. If item has an [[InitializedTemporalInstant]] internal slot, then
  64. if (is<Instant>(item.as_object())) {
  65. // i. Return item.
  66. return &static_cast<Instant&>(item.as_object());
  67. }
  68. // b. If item has an [[InitializedTemporalZonedDateTime]] internal slot, then
  69. if (is<ZonedDateTime>(item.as_object())) {
  70. auto& zoned_date_time = static_cast<ZonedDateTime&>(item.as_object());
  71. // i. Return ! CreateTemporalInstant(item.[[Nanoseconds]]).
  72. return create_temporal_instant(global_object, zoned_date_time.nanoseconds());
  73. }
  74. }
  75. // 2. Let string be ? ToString(item).
  76. auto string = item.to_string(global_object);
  77. if (vm.exception())
  78. return {};
  79. // 3. Let epochNanoseconds be ? ParseTemporalInstant(string).
  80. auto* epoch_nanoseconds = parse_temporal_instant(global_object, string);
  81. if (vm.exception())
  82. return {};
  83. // 4. Return ! CreateTemporalInstant(ℤ(epochNanoseconds)).
  84. return create_temporal_instant(global_object, *epoch_nanoseconds);
  85. }
  86. // 8.5.4 ParseTemporalInstant ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalinstant
  87. BigInt* parse_temporal_instant(GlobalObject& global_object, String const& iso_string)
  88. {
  89. auto& vm = global_object.vm();
  90. // 1. Assert: Type(isoString) is String.
  91. // 2. Let result be ? ParseTemporalInstantString(isoString).
  92. auto result = parse_temporal_instant_string(global_object, iso_string);
  93. if (vm.exception())
  94. return {};
  95. // 3. Let offsetString be result.[[TimeZoneOffsetString]].
  96. auto& offset_string = result->time_zone_offset;
  97. // 4. Assert: offsetString is not undefined.
  98. VERIFY(offset_string.has_value());
  99. // 5. Let utc be ? GetEpochFromISOParts(result.[[Year]], result.[[Month]], result.[[Day]], result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]]).
  100. auto* utc = get_epoch_from_iso_parts(global_object, result->year, result->month, result->day, result->hour, result->minute, result->second, result->millisecond, result->microsecond, result->nanosecond);
  101. if (vm.exception())
  102. return {};
  103. // 6. If utc < −8.64 × 10^21 or utc > 8.64 × 10^21, then
  104. if (utc->big_integer() < INSTANT_NANOSECONDS_MIN || utc->big_integer() > INSTANT_NANOSECONDS_MAX) {
  105. // a. Throw a RangeError exception.
  106. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidEpochNanoseconds);
  107. return {};
  108. }
  109. // 7. Let offsetNanoseconds be ? ParseTimeZoneOffsetString(offsetString).
  110. auto offset_nanoseconds = TRY_OR_DISCARD(parse_time_zone_offset_string(global_object, *offset_string));
  111. // 8. Return utc − offsetNanoseconds.
  112. return js_bigint(vm, utc->big_integer().minus(Crypto::SignedBigInteger::create_from(offset_nanoseconds)));
  113. }
  114. // 8.5.5 CompareEpochNanoseconds ( epochNanosecondsOne, epochNanosecondsTwo ), https://tc39.es/proposal-temporal/#sec-temporal-compareepochnanoseconds
  115. i32 compare_epoch_nanoseconds(BigInt const& epoch_nanoseconds_one, BigInt const& epoch_nanoseconds_two)
  116. {
  117. // 1. If epochNanosecondsOne > epochNanosecondsTwo, return 1.
  118. if (epoch_nanoseconds_one.big_integer() > epoch_nanoseconds_two.big_integer())
  119. return 1;
  120. // 2. If epochNanosecondsOne < epochNanosecondsTwo, return -1.
  121. if (epoch_nanoseconds_one.big_integer() < epoch_nanoseconds_two.big_integer())
  122. return -1;
  123. // 3. Return 0.
  124. return 0;
  125. }
  126. // 8.5.6 AddInstant ( epochNanoseconds, hours, minutes, seconds, milliseconds, microseconds, nanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal-addinstant
  127. BigInt* add_instant(GlobalObject& global_object, BigInt const& epoch_nanoseconds, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds)
  128. {
  129. auto& vm = global_object.vm();
  130. // 1. Assert: hours, minutes, seconds, milliseconds, microseconds, and nanoseconds are integer Number values.
  131. VERIFY(hours == trunc(hours) && minutes == trunc(minutes) && seconds == trunc(seconds) && milliseconds == trunc(milliseconds) && microseconds == trunc(microseconds) && nanoseconds == trunc(nanoseconds));
  132. // 2. Let result be epochNanoseconds + ℤ(nanoseconds) + ℤ(microseconds) × 1000ℤ + ℤ(milliseconds) × 10^6ℤ + ℤ(seconds) × 10^9ℤ + ℤ(minutes) × 60ℤ × 10^9ℤ + ℤ(hours) × 3600ℤ × 10^9ℤ.
  133. // FIXME: Pretty sure i64's are not sufficient for the extreme cases.
  134. auto* result = js_bigint(vm,
  135. epoch_nanoseconds.big_integer()
  136. .plus(Crypto::SignedBigInteger::create_from((i64)nanoseconds))
  137. .plus(Crypto::SignedBigInteger::create_from((i64)microseconds).multiplied_by(Crypto::SignedBigInteger { 1'000 }))
  138. .plus(Crypto::SignedBigInteger::create_from((i64)milliseconds).multiplied_by(Crypto::SignedBigInteger { 1'000'000 }))
  139. .plus(Crypto::SignedBigInteger::create_from((i64)seconds).multiplied_by(Crypto::SignedBigInteger { 1'000'000'000 }))
  140. .plus(Crypto::SignedBigInteger::create_from((i64)minutes).multiplied_by(Crypto::SignedBigInteger { 60 }).multiplied_by(Crypto::SignedBigInteger { 1'000'000'000 }))
  141. .plus(Crypto::SignedBigInteger::create_from((i64)hours).multiplied_by(Crypto::SignedBigInteger { 3600 }).multiplied_by(Crypto::SignedBigInteger { 1'000'000'000 })));
  142. // If ! IsValidEpochNanoseconds(result) is false, throw a RangeError exception.
  143. if (!is_valid_epoch_nanoseconds(*result)) {
  144. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidEpochNanoseconds);
  145. return {};
  146. }
  147. // 4. Return result.
  148. return result;
  149. }
  150. // 8.5.7 DifferenceInstant ( ns1, ns2, roundingIncrement, smallestUnit, roundingMode ), https://tc39.es/proposal-temporal/#sec-temporal-differenceinstant
  151. BigInt* difference_instant(GlobalObject& global_object, BigInt const& nanoseconds1, BigInt const& nanoseconds2, u64 rounding_increment, StringView smallest_unit, StringView rounding_mode)
  152. {
  153. auto& vm = global_object.vm();
  154. // 1. Assert: Type(ns1) is BigInt.
  155. // 2. Assert: Type(ns2) is BigInt.
  156. // 3. Return ! RoundTemporalInstant(ns2 − ns1, roundingIncrement, smallestUnit, roundingMode).
  157. return round_temporal_instant(global_object, *js_bigint(vm, nanoseconds2.big_integer().minus(nanoseconds1.big_integer())), rounding_increment, smallest_unit, rounding_mode);
  158. }
  159. // 8.5.8 RoundTemporalInstant ( ns, increment, unit, roundingMode ), https://tc39.es/proposal-temporal/#sec-temporal-roundtemporalinstant
  160. BigInt* round_temporal_instant(GlobalObject& global_object, BigInt const& nanoseconds, u64 increment, StringView unit, StringView rounding_mode)
  161. {
  162. // 1. Assert: Type(ns) is BigInt.
  163. u64 increment_nanoseconds;
  164. // 2. If unit is "hour", then
  165. if (unit == "hour"sv) {
  166. // a. Let incrementNs be increment × 3.6 × 10^12.
  167. increment_nanoseconds = increment * 3600000000000;
  168. }
  169. // 3. Else if unit is "minute", then
  170. else if (unit == "minute"sv) {
  171. // a. Let incrementNs be increment × 6 × 10^10.
  172. increment_nanoseconds = increment * 60000000000;
  173. }
  174. // 4. Else if unit is "second", then
  175. else if (unit == "second"sv) {
  176. // a. Let incrementNs be increment × 10^9.
  177. increment_nanoseconds = increment * 1000000000;
  178. }
  179. // 5. Else if unit is "millisecond", then
  180. else if (unit == "millisecond"sv) {
  181. // a. Let incrementNs be increment × 10^6.
  182. increment_nanoseconds = increment * 1000000;
  183. }
  184. // 6. Else if unit is "microsecond", then
  185. else if (unit == "microsecond"sv) {
  186. // a. Let incrementNs be increment × 10^3.
  187. increment_nanoseconds = increment * 1000;
  188. }
  189. // 7. Else,
  190. else {
  191. // a. Assert: unit is "nanosecond".
  192. VERIFY(unit == "nanosecond"sv);
  193. // b. Let incrementNs be increment.
  194. increment_nanoseconds = increment;
  195. }
  196. // 8. Return ! RoundNumberToIncrement(ℝ(ns), incrementNs, roundingMode).
  197. return round_number_to_increment(global_object, nanoseconds, increment_nanoseconds, rounding_mode);
  198. }
  199. // 8.5.9 TemporalInstantToString ( instant, timeZone, precision ), https://tc39.es/proposal-temporal/#sec-temporal-temporalinstanttostring
  200. Optional<String> temporal_instant_to_string(GlobalObject& global_object, Instant& instant, Value time_zone, Variant<StringView, u8> const& precision)
  201. {
  202. auto& vm = global_object.vm();
  203. // 1. Assert: Type(instant) is Object.
  204. // 2. Assert: instant has an [[InitializedTemporalInstant]] internal slot.
  205. // 3. Let outputTimeZone be timeZone.
  206. auto output_time_zone = time_zone;
  207. // 4. If outputTimeZone is undefined, then
  208. if (output_time_zone.is_undefined()) {
  209. // TODO: Can this really throw...?
  210. // a. Set outputTimeZone to ? CreateTemporalTimeZone("UTC").
  211. output_time_zone = TRY_OR_DISCARD(create_temporal_time_zone(global_object, "UTC"sv));
  212. }
  213. // 5. Let isoCalendar be ! GetISO8601Calendar().
  214. auto* iso_calendar = get_iso8601_calendar(global_object);
  215. // 6. Let dateTime be ? BuiltinTimeZoneGetPlainDateTimeFor(outputTimeZone, instant, isoCalendar).
  216. auto* date_time = TRY_OR_DISCARD(builtin_time_zone_get_plain_date_time_for(global_object, output_time_zone, instant, *iso_calendar));
  217. // 7. Let dateTimeString be ? TemporalDateTimeToString(dateTime.[[ISOYear]], dateTime.[[ISOMonth]], dateTime.[[ISODay]], dateTime.[[ISOHour]], dateTime.[[ISOMinute]], dateTime.[[ISOSecond]], dateTime.[[ISOMillisecond]], dateTime.[[ISOMicrosecond]], dateTime.[[ISONanosecond]], undefined, precision, "never").
  218. auto date_time_string = temporal_date_time_to_string(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->iso_hour(), date_time->iso_minute(), date_time->iso_second(), date_time->iso_millisecond(), date_time->iso_microsecond(), date_time->iso_nanosecond(), js_undefined(), precision, "never"sv);
  219. if (vm.exception())
  220. return {};
  221. Optional<String> time_zone_string;
  222. // 8. If timeZone is undefined, then
  223. if (time_zone.is_undefined()) {
  224. // a. Let timeZoneString be "Z".
  225. time_zone_string = "Z"sv;
  226. }
  227. // 9. Else,
  228. else {
  229. // a. Let timeZoneString be ? BuiltinTimeZoneGetOffsetStringFor(timeZone, instant).
  230. time_zone_string = TRY_OR_DISCARD(builtin_time_zone_get_offset_string_for(global_object, time_zone, instant));
  231. }
  232. // 10. Return the string-concatenation of dateTimeString and timeZoneString.
  233. return String::formatted("{}{}", *date_time_string, *time_zone_string);
  234. }
  235. }