TimeZone.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. auto& vm = global_object.vm();
  60. // 1. Assert: Type(string) is String.
  61. // 2. Let result be ? ParseTemporalTimeZoneString(string).
  62. auto result = parse_temporal_time_zone_string(global_object, string);
  63. if (auto* exception = vm.exception())
  64. return throw_completion(exception->value());
  65. // 3. If result.[[Z]] is not undefined, return "UTC".
  66. if (result->z)
  67. return String { "UTC" };
  68. // 4. Return result.[[Name]].
  69. return *result->name;
  70. }
  71. // 11.6.2 CreateTemporalTimeZone ( identifier [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporaltimezone
  72. ThrowCompletionOr<TimeZone*> create_temporal_time_zone(GlobalObject& global_object, String const& identifier, FunctionObject const* new_target)
  73. {
  74. // 1. If newTarget is not present, set it to %Temporal.TimeZone%.
  75. if (!new_target)
  76. new_target = global_object.temporal_time_zone_constructor();
  77. // 2. Let object be ? OrdinaryCreateFromConstructor(newTarget, "%Temporal.TimeZone.prototype%", « [[InitializedTemporalTimeZone]], [[Identifier]], [[OffsetNanoseconds]] »).
  78. // 3. Set object.[[Identifier]] to identifier.
  79. auto* object = TRY(ordinary_create_from_constructor<TimeZone>(global_object, *new_target, &GlobalObject::temporal_time_zone_prototype, identifier));
  80. // 4. If identifier satisfies the syntax of a TimeZoneNumericUTCOffset (see 13.33), then
  81. if (is_valid_time_zone_numeric_utc_offset_syntax(identifier)) {
  82. // a. Set object.[[OffsetNanoseconds]] to ! ParseTimeZoneOffsetString(identifier).
  83. object->set_offset_nanoseconds(TRY(parse_time_zone_offset_string(global_object, identifier)));
  84. }
  85. // 5. Else,
  86. else {
  87. // a. Assert: ! CanonicalizeTimeZoneName(identifier) is identifier.
  88. VERIFY(canonicalize_time_zone_name(identifier) == identifier);
  89. // b. Set object.[[OffsetNanoseconds]] to undefined.
  90. // NOTE: No-op.
  91. }
  92. // 6. Return object.
  93. return object;
  94. }
  95. // 11.6.3 GetISOPartsFromEpoch ( epochNanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal-getisopartsfromepoch
  96. ISODateTime get_iso_parts_from_epoch(BigInt const& epoch_nanoseconds)
  97. {
  98. // 1. Let remainderNs be remainder(epochNanoseconds, 10^6).
  99. auto remainder_ns_bigint = epoch_nanoseconds.big_integer().divided_by(Crypto::UnsignedBigInteger { 1'000'000 }).remainder;
  100. auto remainder_ns = remainder_ns_bigint.to_base(10).to_int<i64>().value();
  101. // 2. Let epochMilliseconds be (epochNanoseconds − remainderNs) / 10^6.
  102. auto epoch_milliseconds_bigint = epoch_nanoseconds.big_integer().minus(remainder_ns_bigint).divided_by(Crypto::UnsignedBigInteger { 1'000'000 }).quotient;
  103. auto epoch_milliseconds = (double)epoch_milliseconds_bigint.to_base(10).to_int<i64>().value();
  104. // 3. Let year be ! YearFromTime(epochMilliseconds).
  105. auto year = year_from_time(epoch_milliseconds);
  106. // 4. Let month be ! MonthFromTime(epochMilliseconds) + 1.
  107. auto month = static_cast<u8>(month_from_time(epoch_milliseconds) + 1);
  108. // 5. Let day be ! DateFromTime(epochMilliseconds).
  109. auto day = date_from_time(epoch_milliseconds);
  110. // 6. Let hour be ! HourFromTime(epochMilliseconds).
  111. auto hour = hour_from_time(epoch_milliseconds);
  112. // 7. Let minute be ! MinFromTime(epochMilliseconds).
  113. auto minute = min_from_time(epoch_milliseconds);
  114. // 8. Let second be ! SecFromTime(epochMilliseconds).
  115. auto second = sec_from_time(epoch_milliseconds);
  116. // 9. Let millisecond be ! msFromTime(epochMilliseconds).
  117. auto millisecond = ms_from_time(epoch_milliseconds);
  118. // 10. Let microsecond be floor(remainderNs / 1000) modulo 1000.
  119. auto microsecond = static_cast<u16>((remainder_ns / 1000) % 1000);
  120. // 11. Let nanosecond be remainderNs modulo 1000.
  121. auto nanosecond = static_cast<u16>(remainder_ns % 1000);
  122. // 12. Return the Record { [[Year]]: year, [[Month]]: month, [[Day]]: day, [[Hour]]: hour, [[Minute]]: minute, [[Second]]: second, [[Millisecond]]: millisecond, [[Microsecond]]: microsecond, [[Nanosecond]]: nanosecond }.
  123. return { .year = year, .month = month, .day = day, .hour = hour, .minute = minute, .second = second, .millisecond = millisecond, .microsecond = microsecond, .nanosecond = nanosecond };
  124. }
  125. // 11.6.5 GetIANATimeZoneOffsetNanoseconds ( epochNanoseconds, timeZoneIdentifier ), https://tc39.es/proposal-temporal/#sec-temporal-getianatimezoneoffsetnanoseconds
  126. i64 get_iana_time_zone_offset_nanoseconds([[maybe_unused]] BigInt const& epoch_nanoseconds, [[maybe_unused]] String const& time_zone_identifier)
  127. {
  128. // 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.
  129. // Given the same values of epochNanoseconds and timeZoneIdentifier, the result must be the same for the lifetime of the surrounding agent.
  130. // TODO: Implement this
  131. return 0;
  132. }
  133. // https://tc39.es/proposal-temporal/#prod-TimeZoneNumericUTCOffset
  134. 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)
  135. {
  136. DateTimeLexer lexer(offset_string);
  137. auto sign_part = lexer.consume_sign();
  138. if (!sign_part.has_value())
  139. return false;
  140. sign = *sign_part;
  141. auto hours_part = lexer.consume_hours();
  142. if (!hours_part.has_value())
  143. return false;
  144. hours = *hours_part;
  145. if (!lexer.tell_remaining())
  146. return true;
  147. auto uses_separator = lexer.consume_specific(':');
  148. minutes = lexer.consume_minutes_or_seconds();
  149. if (!minutes.has_value())
  150. return false;
  151. if (!lexer.tell_remaining())
  152. return true;
  153. if (lexer.consume_specific(':') != uses_separator)
  154. return false;
  155. seconds = lexer.consume_minutes_or_seconds();
  156. if (!seconds.has_value())
  157. return false;
  158. if (!lexer.tell_remaining())
  159. return true;
  160. if (!lexer.consume_specific('.') && !lexer.consume_specific(','))
  161. return false;
  162. fraction = lexer.consume_fractional_seconds();
  163. return fraction.has_value();
  164. }
  165. bool is_valid_time_zone_numeric_utc_offset_syntax(String const& offset_string)
  166. {
  167. StringView discarded;
  168. Optional<StringView> optionally_discarded;
  169. // FIXME: This is very wasteful
  170. return parse_time_zone_numeric_utc_offset_syntax(offset_string, discarded, discarded, optionally_discarded, optionally_discarded, optionally_discarded);
  171. }
  172. // 11.6.8 ParseTimeZoneOffsetString ( offsetString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetimezoneoffsetstring
  173. ThrowCompletionOr<double> parse_time_zone_offset_string(GlobalObject& global_object, String const& offset_string)
  174. {
  175. auto& vm = global_object.vm();
  176. // 1. Assert: Type(offsetString) is String.
  177. // 2. If offsetString does not satisfy the syntax of a TimeZoneNumericUTCOffset (see 13.33), then
  178. // a. Throw a RangeError exception.
  179. // 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.
  180. StringView sign_part;
  181. StringView hours_part;
  182. Optional<StringView> minutes_part;
  183. Optional<StringView> seconds_part;
  184. Optional<StringView> fraction_part;
  185. auto success = parse_time_zone_numeric_utc_offset_syntax(offset_string, sign_part, hours_part, minutes_part, seconds_part, fraction_part);
  186. if (!success)
  187. return vm.throw_completion<RangeError>(global_object, ErrorType::InvalidFormat, "TimeZone offset");
  188. // 4. If either hours or sign are undefined, throw a RangeError exception.
  189. // NOTE: Both of these checks are always false, due to the handling of Step 2
  190. double sign;
  191. // 5. If sign is the code unit 0x002D (HYPHEN-MINUS) or 0x2212 (MINUS SIGN), then
  192. if (sign_part.is_one_of("-", "\xE2\x88\x92")) {
  193. // a. Set sign to −1.
  194. sign = -1;
  195. }
  196. // 6. Else,
  197. else {
  198. // a. Set sign to 1.
  199. sign = 1;
  200. }
  201. // 7. Set hours to ! ToIntegerOrInfinity(hours).
  202. auto hours = Value(js_string(vm, hours_part)).to_integer_or_infinity(global_object);
  203. // 8. Set minutes to ! ToIntegerOrInfinity(minutes).
  204. auto minutes = Value(js_string(vm, minutes_part.value_or(""sv))).to_integer_or_infinity(global_object);
  205. // 9. Set seconds to ! ToIntegerOrInfinity(seconds).
  206. auto seconds = Value(js_string(vm, seconds_part.value_or(""sv))).to_integer_or_infinity(global_object);
  207. double nanoseconds;
  208. // 10. If fraction is not undefined, then
  209. if (fraction_part.has_value()) {
  210. // a. Set fraction to the string-concatenation of the previous value of fraction and the string "000000000".
  211. auto fraction = String::formatted("{}000000000", *fraction_part);
  212. // 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).
  213. // c. Set nanoseconds to ! ToIntegerOrInfinity(nanoseconds).
  214. nanoseconds = Value(js_string(vm, fraction_part->substring_view(0, 9))).to_integer_or_infinity(global_object);
  215. }
  216. // 11. Else,
  217. else {
  218. // a. Let nanoseconds be 0.
  219. nanoseconds = 0;
  220. }
  221. // 12. Return sign × (((hours × 60 + minutes) × 60 + seconds) × 10^9 + nanoseconds).
  222. return sign * (((hours * 60 + minutes) * 60 + seconds) * 1000000000 + nanoseconds);
  223. }
  224. // 11.6.9 FormatTimeZoneOffsetString ( offsetNanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal-formattimezoneoffsetstring
  225. String format_time_zone_offset_string(double offset_nanoseconds)
  226. {
  227. auto offset = static_cast<i64>(offset_nanoseconds);
  228. // 1. Assert: offsetNanoseconds is an integer.
  229. VERIFY(offset == offset_nanoseconds);
  230. StringBuilder builder;
  231. // 2. If offsetNanoseconds ≥ 0, let sign be "+"; otherwise, let sign be "-".
  232. if (offset >= 0)
  233. builder.append('+');
  234. else
  235. builder.append('-');
  236. // 3. Let nanoseconds be abs(offsetNanoseconds) modulo 10^9.
  237. auto nanoseconds = AK::abs(offset) % 1000000000;
  238. // 4. Let seconds be floor(offsetNanoseconds / 10^9) modulo 60.
  239. auto seconds = (offset / 1000000000) % 60;
  240. // 5. Let minutes be floor(offsetNanoseconds / (6 × 10^10)) modulo 60.
  241. auto minutes = (offset / 60000000000) % 60;
  242. // 6. Let hours be floor(offsetNanoseconds / (3.6 × 10^12)).
  243. auto hours = offset / 3600000000000;
  244. // 7. Let h be hours, formatted as a two-digit decimal number, padded to the left with a zero if necessary.
  245. builder.appendff("{:02}", hours);
  246. // 8. Let m be minutes, formatted as a two-digit decimal number, padded to the left with a zero if necessary.
  247. builder.appendff(":{:02}", minutes);
  248. // 9. Let s be seconds, formatted as a two-digit decimal number, padded to the left with a zero if necessary.
  249. // Handled by steps 10 & 11
  250. // 10. If nanoseconds ≠ 0, then
  251. if (nanoseconds != 0) {
  252. // a. Let fraction be nanoseconds, formatted as a nine-digit decimal number, padded to the left with zeroes if necessary.
  253. // b. Set fraction to the longest possible substring of fraction starting at position 0 and not ending with the code unit 0x0030 (DIGIT ZERO).
  254. // c. Let post be the string-concatenation of the code unit 0x003A (COLON), s, the code unit 0x002E (FULL STOP), and fraction.
  255. builder.appendff(":{:02}.{:9}", seconds, nanoseconds);
  256. }
  257. // 11. Else if seconds ≠ 0, then
  258. else if (seconds != 0) {
  259. // a. Let post be the string-concatenation of the code unit 0x003A (COLON) and s.
  260. builder.appendff(":{:02}", seconds);
  261. }
  262. // 12. Return the string-concatenation of sign, h, the code unit 0x003A (COLON), m, and post.
  263. return builder.to_string();
  264. }
  265. // 11.6.10 ToTemporalTimeZone ( temporalTimeZoneLike ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaltimezone
  266. ThrowCompletionOr<Object*> to_temporal_time_zone(GlobalObject& global_object, Value temporal_time_zone_like)
  267. {
  268. auto& vm = global_object.vm();
  269. // 1. If Type(temporalTimeZoneLike) is Object, then
  270. if (temporal_time_zone_like.is_object()) {
  271. // a. If temporalTimeZoneLike has an [[InitializedTemporalZonedDateTime]] internal slot, then
  272. if (is<ZonedDateTime>(temporal_time_zone_like.as_object())) {
  273. auto& zoned_date_time = static_cast<ZonedDateTime&>(temporal_time_zone_like.as_object());
  274. // i. Return temporalTimeZoneLike.[[TimeZone]].
  275. return &zoned_date_time.time_zone();
  276. }
  277. // b. If ? HasProperty(temporalTimeZoneLike, "timeZone") is false, return temporalTimeZoneLike.
  278. auto has_property = temporal_time_zone_like.as_object().has_property(vm.names.timeZone);
  279. if (auto* exception = vm.exception())
  280. return throw_completion(exception->value());
  281. if (!has_property)
  282. return &temporal_time_zone_like.as_object();
  283. // c. Set temporalTimeZoneLike to ? Get(temporalTimeZoneLike, "timeZone").
  284. temporal_time_zone_like = temporal_time_zone_like.as_object().get(vm.names.timeZone);
  285. if (auto* exception = vm.exception())
  286. return throw_completion(exception->value());
  287. // d. If Type(temporalTimeZoneLike) is Object and ? HasProperty(temporalTimeZoneLike, "timeZone") is false, return temporalTimeZoneLike.
  288. if (temporal_time_zone_like.is_object()) {
  289. has_property = temporal_time_zone_like.as_object().has_property(vm.names.timeZone);
  290. if (auto* exception = vm.exception())
  291. return throw_completion(exception->value());
  292. if (!has_property)
  293. return &temporal_time_zone_like.as_object();
  294. }
  295. }
  296. // 2. Let identifier be ? ToString(temporalTimeZoneLike).
  297. auto identifier = temporal_time_zone_like.to_string(global_object);
  298. if (auto* exception = vm.exception())
  299. return throw_completion(exception->value());
  300. // 3. Let result be ? ParseTemporalTimeZone(identifier).
  301. auto result = TRY(parse_temporal_time_zone(global_object, identifier));
  302. // 4. Return ? CreateTemporalTimeZone(result).
  303. return TRY(create_temporal_time_zone(global_object, result));
  304. }
  305. // 11.6.11 GetOffsetNanosecondsFor ( timeZone, instant ), https://tc39.es/proposal-temporal/#sec-temporal-getoffsetnanosecondsfor
  306. ThrowCompletionOr<double> get_offset_nanoseconds_for(GlobalObject& global_object, Value time_zone, Instant& instant)
  307. {
  308. auto& vm = global_object.vm();
  309. // 1. Let getOffsetNanosecondsFor be ? GetMethod(timeZone, "getOffsetNanosecondsFor").
  310. auto* get_offset_nanoseconds_for = time_zone.get_method(global_object, vm.names.getOffsetNanosecondsFor);
  311. if (auto* exception = vm.exception())
  312. return throw_completion(exception->value());
  313. // 2. If getOffsetNanosecondsFor is undefined, set getOffsetNanosecondsFor to %Temporal.TimeZone.prototype.getOffsetNanosecondsFor%.
  314. if (!get_offset_nanoseconds_for)
  315. get_offset_nanoseconds_for = global_object.temporal_time_zone_prototype_get_offset_nanoseconds_for_function();
  316. // 3. Let offsetNanoseconds be ? Call(getOffsetNanosecondsFor, timeZone, « instant »).
  317. auto offset_nanoseconds_value = vm.call(*get_offset_nanoseconds_for, time_zone, &instant);
  318. if (auto* exception = vm.exception())
  319. return throw_completion(exception->value());
  320. // 4. If Type(offsetNanoseconds) is not Number, throw a TypeError exception.
  321. if (!offset_nanoseconds_value.is_number())
  322. return vm.throw_completion<TypeError>(global_object, ErrorType::IsNotA, "Offset nanoseconds value", "number");
  323. // 5. If ! IsIntegralNumber(offsetNanoseconds) is false, throw a RangeError exception.
  324. if (!offset_nanoseconds_value.is_integral_number())
  325. return vm.throw_completion<RangeError>(global_object, ErrorType::IsNotAn, "Offset nanoseconds value", "integral number");
  326. // 6. Set offsetNanoseconds to ℝ(offsetNanoseconds).
  327. auto offset_nanoseconds = offset_nanoseconds_value.as_double();
  328. // 7. If abs(offsetNanoseconds) > 86400 × 10^9, throw a RangeError exception.
  329. if (fabs(offset_nanoseconds) > 86400000000000.0)
  330. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidOffsetNanosecondsValue);
  331. // 8. Return offsetNanoseconds.
  332. return offset_nanoseconds;
  333. }
  334. // 11.6.12 BuiltinTimeZoneGetOffsetStringFor ( timeZone, instant ), https://tc39.es/proposal-temporal/#sec-temporal-builtintimezonegetoffsetstringfor
  335. ThrowCompletionOr<String> builtin_time_zone_get_offset_string_for(GlobalObject& global_object, Value time_zone, Instant& instant)
  336. {
  337. // 1. Let offsetNanoseconds be ? GetOffsetNanosecondsFor(timeZone, instant).
  338. auto offset_nanoseconds = TRY(get_offset_nanoseconds_for(global_object, time_zone, instant));
  339. // 2. Return ! FormatTimeZoneOffsetString(offsetNanoseconds).
  340. return format_time_zone_offset_string(offset_nanoseconds);
  341. }
  342. // 11.6.13 BuiltinTimeZoneGetPlainDateTimeFor ( timeZone, instant, calendar ), https://tc39.es/proposal-temporal/#sec-temporal-builtintimezonegetplaindatetimefor
  343. ThrowCompletionOr<PlainDateTime*> builtin_time_zone_get_plain_date_time_for(GlobalObject& global_object, Value time_zone, Instant& instant, Object& calendar)
  344. {
  345. auto& vm = global_object.vm();
  346. // 1. Let offsetNanoseconds be ? GetOffsetNanosecondsFor(timeZone, instant).
  347. auto offset_nanoseconds = TRY(get_offset_nanoseconds_for(global_object, time_zone, instant));
  348. // 2. Let result be ! GetISOPartsFromEpoch(instant.[[Nanoseconds]]).
  349. auto result = get_iso_parts_from_epoch(instant.nanoseconds());
  350. // 3. Set result to ! BalanceISODateTime(result.[[Year]], result.[[Month]], result.[[Day]], result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]] + offsetNanoseconds).
  351. 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);
  352. // 4. Return ? CreateTemporalDateTime(result.[[Year]], result.[[Month]], result.[[Day]], result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]], calendar).
  353. auto* date_time = 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);
  354. if (auto* exception = vm.exception())
  355. return throw_completion(exception->value());
  356. return date_time;
  357. }
  358. }