Instant.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 <LibCrypto/BigInt/SignedBigInteger.h>
  8. #include <LibJS/Runtime/AbstractOperations.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  11. #include <LibJS/Runtime/Temporal/Instant.h>
  12. #include <LibJS/Runtime/Temporal/InstantConstructor.h>
  13. #include <LibJS/Runtime/Temporal/PlainDateTime.h>
  14. #include <LibJS/Runtime/Temporal/TimeZone.h>
  15. namespace JS::Temporal {
  16. // 8 Temporal.Instant Objects, https://tc39.es/proposal-temporal/#sec-temporal-instant-objects
  17. Instant::Instant(BigInt& nanoseconds, Object& prototype)
  18. : Object(prototype)
  19. , m_nanoseconds(nanoseconds)
  20. {
  21. }
  22. void Instant::visit_edges(Cell::Visitor& visitor)
  23. {
  24. Base::visit_edges(visitor);
  25. visitor.visit(&m_nanoseconds);
  26. }
  27. // 8.5.1 IsValidEpochNanoseconds ( epochNanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal-isvalidepochnanoseconds
  28. bool is_valid_epoch_nanoseconds(BigInt const& epoch_nanoseconds)
  29. {
  30. // 1. Assert: Type(epochNanoseconds) is BigInt.
  31. // 2. If epochNanoseconds < −86400ℤ × 10^17ℤ or epochNanoseconds > 86400ℤ × 10^17ℤ, then
  32. if (epoch_nanoseconds.big_integer() < INSTANT_NANOSECONDS_MIN || epoch_nanoseconds.big_integer() > INSTANT_NANOSECONDS_MAX) {
  33. // a. Return false.
  34. return false;
  35. }
  36. // 3. Return true.
  37. return true;
  38. }
  39. // 8.5.2 CreateTemporalInstant ( epochNanoseconds [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporalinstant
  40. Instant* create_temporal_instant(GlobalObject& global_object, BigInt& epoch_nanoseconds, FunctionObject* new_target)
  41. {
  42. auto& vm = global_object.vm();
  43. // 1. Assert: Type(epochNanoseconds) is BigInt.
  44. // 2. Assert: ! IsValidEpochNanoseconds(epochNanoseconds) is true.
  45. VERIFY(is_valid_epoch_nanoseconds(epoch_nanoseconds));
  46. // 3. If newTarget is not present, set it to %Temporal.Instant%.
  47. if (!new_target)
  48. new_target = global_object.temporal_instant_constructor();
  49. // 4. Let object be ? OrdinaryCreateFromConstructor(newTarget, "%Temporal.Instant.prototype%", « [[InitializedTemporalInstant]], [[Nanoseconds]] »).
  50. // 5. Set object.[[Nanoseconds]] to epochNanoseconds.
  51. auto* object = ordinary_create_from_constructor<Instant>(global_object, *new_target, &GlobalObject::temporal_instant_prototype, epoch_nanoseconds);
  52. if (vm.exception())
  53. return {};
  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. // TODO:
  69. // b. If item has an [[InitializedTemporalZonedDateTime]] internal slot, then
  70. // i. Return ! CreateTemporalInstant(item.[[Nanoseconds]]).
  71. }
  72. // 2. Let string be ? ToString(item).
  73. auto string = item.to_string(global_object);
  74. if (vm.exception())
  75. return {};
  76. // 3. Let epochNanoseconds be ? ParseTemporalInstant(string).
  77. auto* epoch_nanoseconds = parse_temporal_instant(global_object, string);
  78. if (vm.exception())
  79. return {};
  80. // 4. Return ! CreateTemporalInstant(ℤ(epochNanoseconds)).
  81. return create_temporal_instant(global_object, *epoch_nanoseconds);
  82. }
  83. // 8.5.4 ParseTemporalInstant ( isoString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporalinstant
  84. BigInt* parse_temporal_instant(GlobalObject& global_object, String const& iso_string)
  85. {
  86. auto& vm = global_object.vm();
  87. // 1. Assert: Type(isoString) is String.
  88. // 2. Let result be ? ParseTemporalInstantString(isoString).
  89. auto result = parse_temporal_instant_string(global_object, iso_string);
  90. if (vm.exception())
  91. return {};
  92. // 3. Let offsetString be result.[[TimeZoneOffsetString]].
  93. auto& offset_string = result->time_zone_offset;
  94. // 4. Assert: offsetString is not undefined.
  95. VERIFY(offset_string.has_value());
  96. // 5. Let utc be ? GetEpochFromISOParts(result.[[Year]], result.[[Month]], result.[[Day]], result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]]).
  97. 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);
  98. if (vm.exception())
  99. return {};
  100. // 6. If utc < −8.64 × 10^21 or utc > 8.64 × 10^21, then
  101. if (utc->big_integer() < INSTANT_NANOSECONDS_MIN || utc->big_integer() > INSTANT_NANOSECONDS_MAX) {
  102. // a. Throw a RangeError exception.
  103. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidEpochNanoseconds);
  104. return {};
  105. }
  106. // 7. Let offsetNanoseconds be ? ParseTimeZoneOffsetString(offsetString).
  107. auto offset_nanoseconds = parse_time_zone_offset_string(global_object, *offset_string);
  108. if (vm.exception())
  109. return {};
  110. // 8. Return utc − offsetNanoseconds.
  111. return js_bigint(vm.heap(), utc->big_integer().minus(Crypto::SignedBigInteger::create_from(offset_nanoseconds)));
  112. }
  113. // 8.5.5 CompareEpochNanoseconds ( epochNanosecondsOne, epochNanosecondsTwo )
  114. i32 compare_epoch_nanoseconds(BigInt const& epoch_nanoseconds_one, BigInt const& epoch_nanoseconds_two)
  115. {
  116. // 1. If epochNanosecondsOne > epochNanosecondsTwo, return 1.
  117. if (epoch_nanoseconds_one.big_integer() > epoch_nanoseconds_two.big_integer())
  118. return 1;
  119. // 2. If epochNanosecondsOne < epochNanosecondsTwo, return -1.
  120. if (epoch_nanoseconds_one.big_integer() < epoch_nanoseconds_two.big_integer())
  121. return -1;
  122. // 3. Return 0.
  123. return 0;
  124. }
  125. // 8.5.8 RoundTemporalInstant ( ns, increment, unit, roundingMode ), https://tc39.es/proposal-temporal/#sec-temporal-roundtemporalinstant
  126. BigInt* round_temporal_instant(GlobalObject& global_object, BigInt const& nanoseconds, u64 increment, String const& unit, String const& rounding_mode)
  127. {
  128. // 1. Assert: Type(ns) is BigInt.
  129. u64 increment_nanoseconds;
  130. // 2. If unit is "hour", then
  131. if (unit == "hour") {
  132. // a. Let incrementNs be increment × 3.6 × 10^12.
  133. increment_nanoseconds = increment * 3600000000000;
  134. }
  135. // 3. Else if unit is "minute", then
  136. else if (unit == "minute") {
  137. // a. Let incrementNs be increment × 6 × 10^10.
  138. increment_nanoseconds = increment * 60000000000;
  139. }
  140. // 4. Else if unit is "second", then
  141. else if (unit == "second") {
  142. // a. Let incrementNs be increment × 10^9.
  143. increment_nanoseconds = increment * 1000000000;
  144. }
  145. // 5. Else if unit is "millisecond", then
  146. else if (unit == "millisecond") {
  147. // a. Let incrementNs be increment × 10^6.
  148. increment_nanoseconds = increment * 1000000;
  149. }
  150. // 6. Else if unit is "microsecond", then
  151. else if (unit == "microsecond") {
  152. // a. Let incrementNs be increment × 10^3.
  153. increment_nanoseconds = increment * 1000;
  154. }
  155. // 7. Else,
  156. else {
  157. // a. Assert: unit is "nanosecond".
  158. VERIFY(unit == "nanosecond");
  159. // b. Let incrementNs be increment.
  160. increment_nanoseconds = increment;
  161. }
  162. // 8. Return ! RoundNumberToIncrement(ℝ(ns), incrementNs, roundingMode).
  163. return round_number_to_increment(global_object, nanoseconds, increment_nanoseconds, rounding_mode);
  164. }
  165. }