TimeZone.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/DateTimeLexer.h>
  7. #include <LibCrypto/BigInt/UnsignedBigInteger.h>
  8. #include <LibJS/Runtime/AbstractOperations.h>
  9. #include <LibJS/Runtime/Date.h>
  10. #include <LibJS/Runtime/GlobalObject.h>
  11. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  12. #include <LibJS/Runtime/Temporal/Instant.h>
  13. #include <LibJS/Runtime/Temporal/PlainDateTime.h>
  14. #include <LibJS/Runtime/Temporal/TimeZone.h>
  15. #include <LibJS/Runtime/Temporal/TimeZoneConstructor.h>
  16. #include <LibJS/Runtime/Temporal/ZonedDateTime.h>
  17. namespace JS::Temporal {
  18. // 11 Temporal.TimeZone Objects, https://tc39.es/proposal-temporal/#sec-temporal-timezone-objects
  19. TimeZone::TimeZone(String identifier, Object& prototype)
  20. : Object(prototype)
  21. , m_identifier(move(identifier))
  22. {
  23. }
  24. // 11.1.1 IsValidTimeZoneName ( timeZone ), https://tc39.es/proposal-temporal/#sec-isvalidtimezonename
  25. // NOTE: This is the minimum implementation of IsValidTimeZoneName, supporting only the "UTC" time zone.
  26. bool is_valid_time_zone_name(String const& time_zone)
  27. {
  28. // 1. Assert: Type(timeZone) is String.
  29. // 2. Let tzText be ! StringToCodePoints(timeZone).
  30. // 3. Let tzUpperText be the result of toUppercase(tzText), according to the Unicode Default Case Conversion algorithm.
  31. // 4. Let tzUpper be ! CodePointsToString(tzUpperText).
  32. auto tz_upper = time_zone.to_uppercase();
  33. // 5. If tzUpper and "UTC" are the same sequence of code points, return true.
  34. if (tz_upper == "UTC")
  35. return true;
  36. // 6. Return false.
  37. return false;
  38. }
  39. // 11.1.2 CanonicalizeTimeZoneName ( timeZone ), https://tc39.es/proposal-temporal/#sec-canonicalizetimezonename
  40. // NOTE: This is the minimum implementation of CanonicalizeTimeZoneName, supporting only the "UTC" time zone.
  41. String canonicalize_time_zone_name(String const& time_zone)
  42. {
  43. // 1. Assert: Type(timeZone) is String.
  44. // 2. Assert: ! IsValidTimeZoneName(timeZone) is true.
  45. VERIFY(is_valid_time_zone_name(time_zone));
  46. // 3. Return "UTC".
  47. return "UTC";
  48. }
  49. // 11.1.3 DefaultTimeZone ( ), https://tc39.es/proposal-temporal/#sec-defaulttimezone
  50. // NOTE: This is the minimum implementation of DefaultTimeZone, supporting only the "UTC" time zone.
  51. String default_time_zone()
  52. {
  53. // 1. Return "UTC".
  54. return "UTC";
  55. }
  56. // 11.6.1 ParseTemporalTimeZone ( string ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaltimezone
  57. ThrowCompletionOr<String> parse_temporal_time_zone(GlobalObject& global_object, String const& string)
  58. {
  59. // 1. Assert: Type(string) is String.
  60. // 2. Let result be ? ParseTemporalTimeZoneString(string).
  61. auto result = TRY(parse_temporal_time_zone_string(global_object, string));
  62. // 3. If result.[[Z]] is not undefined, return "UTC".
  63. if (result.z)
  64. return String { "UTC" };
  65. // 4. Return result.[[Name]].
  66. return *result.name;
  67. }
  68. // 11.6.2 CreateTemporalTimeZone ( identifier [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporaltimezone
  69. ThrowCompletionOr<TimeZone*> create_temporal_time_zone(GlobalObject& global_object, String const& identifier, FunctionObject const* new_target)
  70. {
  71. // 1. If newTarget is not present, set it to %Temporal.TimeZone%.
  72. if (!new_target)
  73. new_target = global_object.temporal_time_zone_constructor();
  74. // 2. Let object be ? OrdinaryCreateFromConstructor(newTarget, "%Temporal.TimeZone.prototype%", « [[InitializedTemporalTimeZone]], [[Identifier]], [[OffsetNanoseconds]] »).
  75. // 3. Set object.[[Identifier]] to identifier.
  76. auto* object = TRY(ordinary_create_from_constructor<TimeZone>(global_object, *new_target, &GlobalObject::temporal_time_zone_prototype, identifier));
  77. // 4. If identifier satisfies the syntax of a TimeZoneNumericUTCOffset (see 13.33), then
  78. if (is_valid_time_zone_numeric_utc_offset_syntax(identifier)) {
  79. // a. Set object.[[OffsetNanoseconds]] to ! ParseTimeZoneOffsetString(identifier).
  80. object->set_offset_nanoseconds(TRY(parse_time_zone_offset_string(global_object, identifier)));
  81. }
  82. // 5. Else,
  83. else {
  84. // a. Assert: ! CanonicalizeTimeZoneName(identifier) is identifier.
  85. VERIFY(canonicalize_time_zone_name(identifier) == identifier);
  86. // b. Set object.[[OffsetNanoseconds]] to undefined.
  87. // NOTE: No-op.
  88. }
  89. // 6. Return object.
  90. return object;
  91. }
  92. // 11.6.3 GetISOPartsFromEpoch ( epochNanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal-getisopartsfromepoch
  93. ISODateTime get_iso_parts_from_epoch(BigInt const& epoch_nanoseconds)
  94. {
  95. // 1. Assert: epochNanoseconds is an integer.
  96. // 2. Let remainderNs be remainder(epochNanoseconds, 10^6).
  97. auto remainder_ns_bigint = epoch_nanoseconds.big_integer().divided_by(Crypto::UnsignedBigInteger { 1'000'000 }).remainder;
  98. auto remainder_ns = remainder_ns_bigint.to_base(10).to_int<i64>().value();
  99. // 3. Let epochMilliseconds be (epochNanoseconds − remainderNs) / 10^6.
  100. auto epoch_milliseconds_bigint = epoch_nanoseconds.big_integer().minus(remainder_ns_bigint).divided_by(Crypto::UnsignedBigInteger { 1'000'000 }).quotient;
  101. auto epoch_milliseconds = (double)epoch_milliseconds_bigint.to_base(10).to_int<i64>().value();
  102. // 4. Let year be ! YearFromTime(epochMilliseconds).
  103. auto year = year_from_time(epoch_milliseconds);
  104. // 5. Let month be ! MonthFromTime(epochMilliseconds) + 1.
  105. auto month = static_cast<u8>(month_from_time(epoch_milliseconds) + 1);
  106. // 6. Let day be ! DateFromTime(epochMilliseconds).
  107. auto day = date_from_time(epoch_milliseconds);
  108. // 7. Let hour be ! HourFromTime(epochMilliseconds).
  109. auto hour = hour_from_time(epoch_milliseconds);
  110. // 8. Let minute be ! MinFromTime(epochMilliseconds).
  111. auto minute = min_from_time(epoch_milliseconds);
  112. // 9. Let second be ! SecFromTime(epochMilliseconds).
  113. auto second = sec_from_time(epoch_milliseconds);
  114. // 10. Let millisecond be ! msFromTime(epochMilliseconds).
  115. auto millisecond = ms_from_time(epoch_milliseconds);
  116. // 11. Let microsecond be floor(remainderNs / 1000) modulo 1000.
  117. auto microsecond = static_cast<u16>((remainder_ns / 1000) % 1000);
  118. // 12. Let nanosecond be remainderNs modulo 1000.
  119. auto nanosecond = static_cast<u16>(remainder_ns % 1000);
  120. // 13. Return the Record { [[Year]]: year, [[Month]]: month, [[Day]]: day, [[Hour]]: hour, [[Minute]]: minute, [[Second]]: second, [[Millisecond]]: millisecond, [[Microsecond]]: microsecond, [[Nanosecond]]: nanosecond }.
  121. return { .year = year, .month = month, .day = day, .hour = hour, .minute = minute, .second = second, .millisecond = millisecond, .microsecond = microsecond, .nanosecond = nanosecond };
  122. }
  123. // 11.6.4 GetIANATimeZoneEpochValue ( timeZoneIdentifier, year, month, day, hour, minute, second, millisecond, microsecond, nanosecond ), https://tc39.es/proposal-temporal/#sec-temporal-getianatimezoneepochvalue
  124. MarkedValueList get_iana_time_zone_epoch_value(GlobalObject& global_object, StringView time_zone_identifier, i32 year, u8 month, u8 day, u8 hour, u8 minute, u8 second, u16 millisecond, u16 microsecond, u16 nanosecond)
  125. {
  126. // The abstract operation GetIANATimeZoneEpochValue is an implementation-defined algorithm that returns a List of integers. Each integer in the List represents a number of nanoseconds since the Unix epoch in UTC that may correspond to the given calendar date and wall-clock time in the IANA time zone identified by timeZoneIdentifier.
  127. // When the input represents a local time repeating multiple times at a negative time zone transition (e.g. when the daylight saving time ends or the time zone offset is decreased due to a time zone rule change), the returned List will have more than one element. When the input represents a skipped local time at a positive time zone transition (e.g. when the daylight saving time starts or the time zone offset is increased due to a time zone rule change), the returned List will be empty. Otherwise, the returned List will have one element.
  128. VERIFY(time_zone_identifier == "UTC"sv);
  129. // FIXME: MarkedValueList<T> for T != Value would still be nice.
  130. auto& vm = global_object.vm();
  131. auto list = MarkedValueList { vm.heap() };
  132. list.append(get_epoch_from_iso_parts(global_object, year, month, day, hour, minute, second, millisecond, microsecond, nanosecond));
  133. return list;
  134. }
  135. // 11.6.5 GetIANATimeZoneOffsetNanoseconds ( epochNanoseconds, timeZoneIdentifier ), https://tc39.es/proposal-temporal/#sec-temporal-getianatimezoneoffsetnanoseconds
  136. i64 get_iana_time_zone_offset_nanoseconds([[maybe_unused]] BigInt const& epoch_nanoseconds, [[maybe_unused]] String const& time_zone_identifier)
  137. {
  138. // The abstract operation GetIANATimeZoneOffsetNanoseconds is an implementation-defined algorithm that returns an integer representing the offset of the IANA time zone identified by timeZoneIdentifier from UTC, at the instant corresponding to epochNanoseconds.
  139. // Given the same values of epochNanoseconds and timeZoneIdentifier, the result must be the same for the lifetime of the surrounding agent.
  140. // TODO: Implement this
  141. return 0;
  142. }
  143. // 11.6.6 GetIANATimeZoneNextTransition ( epochNanoseconds, timeZoneIdentifier ), https://tc39.es/proposal-temporal/#sec-temporal-getianatimezonenexttransition
  144. BigInt* get_iana_time_zone_next_transition(GlobalObject&, [[maybe_unused]] BigInt const& epoch_nanoseconds, [[maybe_unused]] StringView time_zone_identifier)
  145. {
  146. // The abstract operation GetIANATimeZoneNextTransition is an implementation-defined algorithm that returns an integer representing the number of nanoseconds since the Unix epoch in UTC that corresponds to the first time zone transition after epochNanoseconds in the IANA time zone identified by timeZoneIdentifier or null if no such transition exists.
  147. // TODO: Implement this
  148. return nullptr;
  149. }
  150. // https://tc39.es/proposal-temporal/#prod-TimeZoneNumericUTCOffset
  151. static bool parse_time_zone_numeric_utc_offset_syntax(String const& offset_string, StringView& sign, StringView& hours, Optional<StringView>& minutes, Optional<StringView>& seconds, Optional<StringView>& fraction)
  152. {
  153. DateTimeLexer lexer(offset_string);
  154. auto sign_part = lexer.consume_sign();
  155. if (!sign_part.has_value())
  156. return false;
  157. sign = *sign_part;
  158. auto hours_part = lexer.consume_hours();
  159. if (!hours_part.has_value())
  160. return false;
  161. hours = *hours_part;
  162. if (!lexer.tell_remaining())
  163. return true;
  164. auto uses_separator = lexer.consume_specific(':');
  165. minutes = lexer.consume_minutes_or_seconds();
  166. if (!minutes.has_value())
  167. return false;
  168. if (!lexer.tell_remaining())
  169. return true;
  170. if (lexer.consume_specific(':') != uses_separator)
  171. return false;
  172. seconds = lexer.consume_minutes_or_seconds();
  173. if (!seconds.has_value())
  174. return false;
  175. if (!lexer.tell_remaining())
  176. return true;
  177. if (!lexer.consume_specific('.') && !lexer.consume_specific(','))
  178. return false;
  179. fraction = lexer.consume_fractional_seconds();
  180. return fraction.has_value();
  181. }
  182. bool is_valid_time_zone_numeric_utc_offset_syntax(String const& offset_string)
  183. {
  184. StringView discarded;
  185. Optional<StringView> optionally_discarded;
  186. // FIXME: This is very wasteful
  187. return parse_time_zone_numeric_utc_offset_syntax(offset_string, discarded, discarded, optionally_discarded, optionally_discarded, optionally_discarded);
  188. }
  189. // 11.6.8 ParseTimeZoneOffsetString ( offsetString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetimezoneoffsetstring
  190. ThrowCompletionOr<double> parse_time_zone_offset_string(GlobalObject& global_object, String const& offset_string)
  191. {
  192. auto& vm = global_object.vm();
  193. // 1. Assert: Type(offsetString) is String.
  194. // 2. If offsetString does not satisfy the syntax of a TimeZoneNumericUTCOffset (see 13.33), then
  195. // a. Throw a RangeError exception.
  196. // 3. Let sign, hours, minutes, seconds, and fraction be the parts of offsetString produced respectively by the TimeZoneUTCOffsetSign, TimeZoneUTCOffsetHour, TimeZoneUTCOffsetMinute, TimeZoneUTCOffsetSecond, and TimeZoneUTCOffsetFraction productions, or undefined if not present.
  197. StringView sign_part;
  198. StringView hours_part;
  199. Optional<StringView> minutes_part;
  200. Optional<StringView> seconds_part;
  201. Optional<StringView> fraction_part;
  202. auto success = parse_time_zone_numeric_utc_offset_syntax(offset_string, sign_part, hours_part, minutes_part, seconds_part, fraction_part);
  203. if (!success)
  204. return vm.throw_completion<RangeError>(global_object, ErrorType::InvalidFormat, "TimeZone offset");
  205. // 4. If either hours or sign are undefined, throw a RangeError exception.
  206. // NOTE: Both of these checks are always false, due to the handling of Step 2
  207. double sign;
  208. // 5. If sign is the code unit 0x002D (HYPHEN-MINUS) or 0x2212 (MINUS SIGN), then
  209. if (sign_part.is_one_of("-", "\xE2\x88\x92")) {
  210. // a. Set sign to −1.
  211. sign = -1;
  212. }
  213. // 6. Else,
  214. else {
  215. // a. Set sign to 1.
  216. sign = 1;
  217. }
  218. // 7. Set hours to ! ToIntegerOrInfinity(hours).
  219. auto hours = MUST(Value(js_string(vm, hours_part)).to_integer_or_infinity(global_object));
  220. // 8. Set minutes to ! ToIntegerOrInfinity(minutes).
  221. auto minutes = MUST(Value(js_string(vm, minutes_part.value_or(""sv))).to_integer_or_infinity(global_object));
  222. // 9. Set seconds to ! ToIntegerOrInfinity(seconds).
  223. auto seconds = MUST(Value(js_string(vm, seconds_part.value_or(""sv))).to_integer_or_infinity(global_object));
  224. double nanoseconds;
  225. // 10. If fraction is not undefined, then
  226. if (fraction_part.has_value()) {
  227. // a. Set fraction to the string-concatenation of the previous value of fraction and the string "000000000".
  228. auto fraction = String::formatted("{}000000000", *fraction_part);
  229. // b. Let nanoseconds be the String value equal to the substring of fraction consisting of the code units with indices 0 (inclusive) through 9 (exclusive).
  230. // c. Set nanoseconds to ! ToIntegerOrInfinity(nanoseconds).
  231. nanoseconds = MUST(Value(js_string(vm, fraction_part->substring_view(0, 9))).to_integer_or_infinity(global_object));
  232. }
  233. // 11. Else,
  234. else {
  235. // a. Let nanoseconds be 0.
  236. nanoseconds = 0;
  237. }
  238. // 12. Return sign × (((hours × 60 + minutes) × 60 + seconds) × 10^9 + nanoseconds).
  239. return sign * (((hours * 60 + minutes) * 60 + seconds) * 1000000000 + nanoseconds);
  240. }
  241. // 11.6.9 FormatTimeZoneOffsetString ( offsetNanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal-formattimezoneoffsetstring
  242. String format_time_zone_offset_string(double offset_nanoseconds)
  243. {
  244. auto offset = static_cast<i64>(offset_nanoseconds);
  245. // 1. Assert: offsetNanoseconds is an integer.
  246. VERIFY(offset == offset_nanoseconds);
  247. StringBuilder builder;
  248. // 2. If offsetNanoseconds ≥ 0, let sign be "+"; otherwise, let sign be "-".
  249. if (offset >= 0)
  250. builder.append('+');
  251. else
  252. builder.append('-');
  253. // 3. Let nanoseconds be abs(offsetNanoseconds) modulo 10^9.
  254. auto nanoseconds = AK::abs(offset) % 1000000000;
  255. // 4. Let seconds be floor(offsetNanoseconds / 10^9) modulo 60.
  256. auto seconds = (offset / 1000000000) % 60;
  257. // 5. Let minutes be floor(offsetNanoseconds / (6 × 10^10)) modulo 60.
  258. auto minutes = (offset / 60000000000) % 60;
  259. // 6. Let hours be floor(offsetNanoseconds / (3.6 × 10^12)).
  260. auto hours = offset / 3600000000000;
  261. // 7. Let h be hours, formatted as a two-digit decimal number, padded to the left with a zero if necessary.
  262. builder.appendff("{:02}", hours);
  263. // 8. Let m be minutes, formatted as a two-digit decimal number, padded to the left with a zero if necessary.
  264. builder.appendff(":{:02}", minutes);
  265. // 9. Let s be seconds, formatted as a two-digit decimal number, padded to the left with a zero if necessary.
  266. // Handled by steps 10 & 11
  267. // 10. If nanoseconds ≠ 0, then
  268. if (nanoseconds != 0) {
  269. // a. Let fraction be nanoseconds, formatted as a nine-digit decimal number, padded to the left with zeroes if necessary.
  270. // b. Set fraction to the longest possible substring of fraction starting at position 0 and not ending with the code unit 0x0030 (DIGIT ZERO).
  271. // c. Let post be the string-concatenation of the code unit 0x003A (COLON), s, the code unit 0x002E (FULL STOP), and fraction.
  272. builder.appendff(":{:02}.{:9}", seconds, nanoseconds);
  273. }
  274. // 11. Else if seconds ≠ 0, then
  275. else if (seconds != 0) {
  276. // a. Let post be the string-concatenation of the code unit 0x003A (COLON) and s.
  277. builder.appendff(":{:02}", seconds);
  278. }
  279. // 12. Return the string-concatenation of sign, h, the code unit 0x003A (COLON), m, and post.
  280. return builder.to_string();
  281. }
  282. // 11.6.10 ToTemporalTimeZone ( temporalTimeZoneLike ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaltimezone
  283. ThrowCompletionOr<Object*> to_temporal_time_zone(GlobalObject& global_object, Value temporal_time_zone_like)
  284. {
  285. auto& vm = global_object.vm();
  286. // 1. If Type(temporalTimeZoneLike) is Object, then
  287. if (temporal_time_zone_like.is_object()) {
  288. // a. If temporalTimeZoneLike has an [[InitializedTemporalZonedDateTime]] internal slot, then
  289. if (is<ZonedDateTime>(temporal_time_zone_like.as_object())) {
  290. auto& zoned_date_time = static_cast<ZonedDateTime&>(temporal_time_zone_like.as_object());
  291. // i. Return temporalTimeZoneLike.[[TimeZone]].
  292. return &zoned_date_time.time_zone();
  293. }
  294. // b. If ? HasProperty(temporalTimeZoneLike, "timeZone") is false, return temporalTimeZoneLike.
  295. if (!TRY(temporal_time_zone_like.as_object().has_property(vm.names.timeZone)))
  296. return &temporal_time_zone_like.as_object();
  297. // c. Set temporalTimeZoneLike to ? Get(temporalTimeZoneLike, "timeZone").
  298. temporal_time_zone_like = TRY(temporal_time_zone_like.as_object().get(vm.names.timeZone));
  299. // d. If Type(temporalTimeZoneLike) is Object and ? HasProperty(temporalTimeZoneLike, "timeZone") is false, return temporalTimeZoneLike.
  300. if (temporal_time_zone_like.is_object() && !TRY(temporal_time_zone_like.as_object().has_property(vm.names.timeZone)))
  301. return &temporal_time_zone_like.as_object();
  302. }
  303. // 2. Let identifier be ? ToString(temporalTimeZoneLike).
  304. auto identifier = TRY(temporal_time_zone_like.to_string(global_object));
  305. // 3. Let result be ? ParseTemporalTimeZone(identifier).
  306. auto result = TRY(parse_temporal_time_zone(global_object, identifier));
  307. // 4. Return ? CreateTemporalTimeZone(result).
  308. return TRY(create_temporal_time_zone(global_object, result));
  309. }
  310. // 11.6.11 GetOffsetNanosecondsFor ( timeZone, instant ), https://tc39.es/proposal-temporal/#sec-temporal-getoffsetnanosecondsfor
  311. ThrowCompletionOr<double> get_offset_nanoseconds_for(GlobalObject& global_object, Value time_zone, Instant& instant)
  312. {
  313. auto& vm = global_object.vm();
  314. // 1. Let getOffsetNanosecondsFor be ? GetMethod(timeZone, "getOffsetNanosecondsFor").
  315. auto* get_offset_nanoseconds_for = TRY(time_zone.get_method(global_object, vm.names.getOffsetNanosecondsFor));
  316. // 2. If getOffsetNanosecondsFor is undefined, set getOffsetNanosecondsFor to %Temporal.TimeZone.prototype.getOffsetNanosecondsFor%.
  317. if (!get_offset_nanoseconds_for)
  318. get_offset_nanoseconds_for = global_object.temporal_time_zone_prototype_get_offset_nanoseconds_for_function();
  319. // 3. Let offsetNanoseconds be ? Call(getOffsetNanosecondsFor, timeZone, « instant »).
  320. auto offset_nanoseconds_value = TRY(vm.call(*get_offset_nanoseconds_for, time_zone, &instant));
  321. // 4. If Type(offsetNanoseconds) is not Number, throw a TypeError exception.
  322. if (!offset_nanoseconds_value.is_number())
  323. return vm.throw_completion<TypeError>(global_object, ErrorType::IsNotA, "Offset nanoseconds value", "number");
  324. // 5. If ! IsIntegralNumber(offsetNanoseconds) is false, throw a RangeError exception.
  325. if (!offset_nanoseconds_value.is_integral_number())
  326. return vm.throw_completion<RangeError>(global_object, ErrorType::IsNotAn, "Offset nanoseconds value", "integral number");
  327. // 6. Set offsetNanoseconds to ℝ(offsetNanoseconds).
  328. auto offset_nanoseconds = offset_nanoseconds_value.as_double();
  329. // 7. If abs(offsetNanoseconds) > 86400 × 10^9, throw a RangeError exception.
  330. if (fabs(offset_nanoseconds) > 86400000000000.0)
  331. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidOffsetNanosecondsValue);
  332. // 8. Return offsetNanoseconds.
  333. return offset_nanoseconds;
  334. }
  335. // 11.6.12 BuiltinTimeZoneGetOffsetStringFor ( timeZone, instant ), https://tc39.es/proposal-temporal/#sec-temporal-builtintimezonegetoffsetstringfor
  336. ThrowCompletionOr<String> builtin_time_zone_get_offset_string_for(GlobalObject& global_object, Value time_zone, Instant& instant)
  337. {
  338. // 1. Let offsetNanoseconds be ? GetOffsetNanosecondsFor(timeZone, instant).
  339. auto offset_nanoseconds = TRY(get_offset_nanoseconds_for(global_object, time_zone, instant));
  340. // 2. Return ! FormatTimeZoneOffsetString(offsetNanoseconds).
  341. return format_time_zone_offset_string(offset_nanoseconds);
  342. }
  343. // 11.6.13 BuiltinTimeZoneGetPlainDateTimeFor ( timeZone, instant, calendar ), https://tc39.es/proposal-temporal/#sec-temporal-builtintimezonegetplaindatetimefor
  344. ThrowCompletionOr<PlainDateTime*> builtin_time_zone_get_plain_date_time_for(GlobalObject& global_object, Value time_zone, Instant& instant, Object& calendar)
  345. {
  346. // 1. Assert: instant has an [[InitializedTemporalInstant]] internal slot.
  347. // 2. Let offsetNanoseconds be ? GetOffsetNanosecondsFor(timeZone, instant).
  348. auto offset_nanoseconds = TRY(get_offset_nanoseconds_for(global_object, time_zone, instant));
  349. // 3. Let result be ! GetISOPartsFromEpoch(instant.[[Nanoseconds]]).
  350. auto result = get_iso_parts_from_epoch(instant.nanoseconds());
  351. // 4. Set result to ! BalanceISODateTime(result.[[Year]], result.[[Month]], result.[[Day]], result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]] + offsetNanoseconds).
  352. result = balance_iso_date_time(result.year, result.month, result.day, result.hour, result.minute, result.second, result.millisecond, result.microsecond, result.nanosecond + offset_nanoseconds);
  353. // 5. Return ? CreateTemporalDateTime(result.[[Year]], result.[[Month]], result.[[Day]], result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]], calendar).
  354. return create_temporal_date_time(global_object, result.year, result.month, result.day, result.hour, result.minute, result.second, result.millisecond, result.microsecond, result.nanosecond, calendar);
  355. }
  356. }