TimeZone.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
  4. * Copyright (c) 2024, Tim Flynn <trflynn89@ladybird.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibJS/Runtime/AbstractOperations.h>
  9. #include <LibJS/Runtime/Date.h>
  10. #include <LibJS/Runtime/Intl/AbstractOperations.h>
  11. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  12. #include <LibJS/Runtime/Temporal/ISO8601.h>
  13. #include <LibJS/Runtime/Temporal/Instant.h>
  14. #include <LibJS/Runtime/Temporal/PlainDateTime.h>
  15. #include <LibJS/Runtime/Temporal/TimeZone.h>
  16. #include <LibJS/Runtime/VM.h>
  17. namespace JS::Temporal {
  18. // 11.1.5 FormatOffsetTimeZoneIdentifier ( offsetMinutes [ , style ] ), https://tc39.es/proposal-temporal/#sec-temporal-formatoffsettimezoneidentifier
  19. String format_offset_time_zone_identifier(i64 offset_minutes, Optional<TimeStyle> style)
  20. {
  21. // 1. If offsetMinutes ≥ 0, let sign be the code unit 0x002B (PLUS SIGN); otherwise, let sign be the code unit 0x002D (HYPHEN-MINUS).
  22. auto sign = offset_minutes >= 0 ? '+' : '-';
  23. // 2. Let absoluteMinutes be abs(offsetMinutes).
  24. auto absolute_minutes = abs(offset_minutes);
  25. // 3. Let hour be floor(absoluteMinutes / 60).
  26. auto hour = static_cast<u8>(floor(static_cast<double>(absolute_minutes) / 60.0));
  27. // 4. Let minute be absoluteMinutes modulo 60.
  28. auto minute = static_cast<u8>(modulo(static_cast<double>(absolute_minutes), 60.0));
  29. // 5. Let timeString be FormatTimeString(hour, minute, 0, 0, MINUTE, style).
  30. auto time_string = format_time_string(hour, minute, 0, 0, SecondsStringPrecision::Minute {}, style);
  31. // 6. Return the string-concatenation of sign and timeString.
  32. return MUST(String::formatted("{}{}", sign, time_string));
  33. }
  34. // 11.1.8 ToTemporalTimeZoneIdentifier ( temporalTimeZoneLike ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaltimezoneidentifier
  35. ThrowCompletionOr<String> to_temporal_time_zone_identifier(VM& vm, Value temporal_time_zone_like)
  36. {
  37. // 1. If temporalTimeZoneLike is an Object, then
  38. if (temporal_time_zone_like.is_object()) {
  39. // FIXME: a. If temporalTimeZoneLike has an [[InitializedTemporalZonedDateTime]] internal slot, then
  40. // FIXME: i. Return temporalTimeZoneLike.[[TimeZone]].
  41. }
  42. // 2. If temporalTimeZoneLike is not a String, throw a TypeError exception.
  43. if (!temporal_time_zone_like.is_string())
  44. return vm.throw_completion<TypeError>(ErrorType::TemporalInvalidTimeZoneName, temporal_time_zone_like);
  45. // 3. Let parseResult be ? ParseTemporalTimeZoneString(temporalTimeZoneLike).
  46. auto parse_result = TRY(parse_temporal_time_zone_string(vm, temporal_time_zone_like.as_string().utf8_string_view()));
  47. // 4. Let offsetMinutes be parseResult.[[OffsetMinutes]].
  48. // 5. If offsetMinutes is not empty, return FormatOffsetTimeZoneIdentifier(offsetMinutes).
  49. if (parse_result.offset_minutes.has_value())
  50. return format_offset_time_zone_identifier(*parse_result.offset_minutes);
  51. // 6. Let name be parseResult.[[Name]].
  52. // 7. Let timeZoneIdentifierRecord be GetAvailableNamedTimeZoneIdentifier(name).
  53. auto time_zone_identifier_record = Intl::get_available_named_time_zone_identifier(*parse_result.name);
  54. // 8. If timeZoneIdentifierRecord is empty, throw a RangeError exception.
  55. if (!time_zone_identifier_record.has_value())
  56. return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidTimeZoneName, temporal_time_zone_like);
  57. // 9. Return timeZoneIdentifierRecord.[[Identifier]].
  58. return time_zone_identifier_record->identifier;
  59. }
  60. // 11.1.11 GetEpochNanosecondsFor ( timeZone, isoDateTime, disambiguation ), https://tc39.es/proposal-temporal/#sec-temporal-getepochnanosecondsfor
  61. ThrowCompletionOr<Crypto::SignedBigInteger> get_epoch_nanoseconds_for(VM& vm, StringView time_zone, ISODateTime const& iso_date_time, Disambiguation disambiguation)
  62. {
  63. // 1. Let possibleEpochNs be ? GetPossibleEpochNanoseconds(timeZone, isoDateTime).
  64. auto possible_epoch_ns = TRY(get_possible_epoch_nanoseconds(vm, time_zone, iso_date_time));
  65. // 2. Return ? DisambiguatePossibleEpochNanoseconds(possibleEpochNs, timeZone, isoDateTime, disambiguation).
  66. return TRY(disambiguate_possible_epoch_nanoseconds(vm, move(possible_epoch_ns), time_zone, iso_date_time, disambiguation));
  67. }
  68. // 11.1.12 DisambiguatePossibleEpochNanoseconds ( possibleEpochNs, timeZone, isoDateTime, disambiguation ), https://tc39.es/proposal-temporal/#sec-temporal-disambiguatepossibleepochnanoseconds
  69. ThrowCompletionOr<Crypto::SignedBigInteger> disambiguate_possible_epoch_nanoseconds(VM& vm, Vector<Crypto::SignedBigInteger> possible_epoch_ns, StringView time_zone, ISODateTime const& iso_date_time, Disambiguation disambiguation)
  70. {
  71. // 1. Let n be possibleEpochNs's length.
  72. auto n = possible_epoch_ns.size();
  73. // 2. If n = 1, then
  74. if (n == 1) {
  75. // a. Return possibleEpochNs[0].
  76. return move(possible_epoch_ns[0]);
  77. }
  78. // 3. If n ≠ 0, then
  79. if (n != 0) {
  80. // a. If disambiguation is EARLIER or COMPATIBLE, then
  81. if (disambiguation == Disambiguation::Earlier || disambiguation == Disambiguation::Compatible) {
  82. // i. Return possibleEpochNs[0].
  83. return move(possible_epoch_ns[0]);
  84. }
  85. // b. If disambiguation is LATER, then
  86. if (disambiguation == Disambiguation::Later) {
  87. // i. Return possibleEpochNs[n - 1].
  88. return move(possible_epoch_ns[n - 1]);
  89. }
  90. // c. Assert: disambiguation is REJECT.
  91. VERIFY(disambiguation == Disambiguation::Reject);
  92. // d. Throw a RangeError exception.
  93. return vm.throw_completion<RangeError>(ErrorType::TemporalDisambiguatePossibleEpochNSRejectMoreThanOne);
  94. }
  95. // 4. Assert: n = 0.
  96. VERIFY(n == 0);
  97. // 5. If disambiguation is REJECT, then
  98. if (disambiguation == Disambiguation::Reject) {
  99. // a. Throw a RangeError exception.
  100. return vm.throw_completion<RangeError>(ErrorType::TemporalDisambiguatePossibleEpochNSRejectZero);
  101. }
  102. // FIXME: GetNamedTimeZoneEpochNanoseconds currently does not produce zero instants.
  103. (void)time_zone;
  104. (void)iso_date_time;
  105. TODO();
  106. }
  107. // 11.1.13 GetPossibleEpochNanoseconds ( timeZone, isoDateTime ), https://tc39.es/proposal-temporal/#sec-temporal-getpossibleepochnanoseconds
  108. ThrowCompletionOr<Vector<Crypto::SignedBigInteger>> get_possible_epoch_nanoseconds(VM& vm, StringView time_zone, ISODateTime const& iso_date_time)
  109. {
  110. Vector<Crypto::SignedBigInteger> possible_epoch_nanoseconds;
  111. // 1. Let parseResult be ! ParseTimeZoneIdentifier(timeZone).
  112. auto parse_result = parse_time_zone_identifier(time_zone);
  113. // 2. If parseResult.[[OffsetMinutes]] is not empty, then
  114. if (parse_result.offset_minutes.has_value()) {
  115. // a. Let balanced be BalanceISODateTime(isoDateTime.[[ISODate]].[[Year]], isoDateTime.[[ISODate]].[[Month]], isoDateTime.[[ISODate]].[[Day]], isoDateTime.[[Time]].[[Hour]], isoDateTime.[[Time]].[[Minute]] - parseResult.[[OffsetMinutes]], isoDateTime.[[Time]].[[Second]], isoDateTime.[[Time]].[[Millisecond]], isoDateTime.[[Time]].[[Microsecond]], isoDateTime.[[Time]].[[Nanosecond]]).
  116. auto balanced = balance_iso_date_time(
  117. iso_date_time.iso_date.year,
  118. iso_date_time.iso_date.month,
  119. iso_date_time.iso_date.day,
  120. iso_date_time.time.hour,
  121. static_cast<double>(iso_date_time.time.minute) - static_cast<double>(*parse_result.offset_minutes),
  122. iso_date_time.time.second,
  123. iso_date_time.time.millisecond,
  124. iso_date_time.time.microsecond,
  125. iso_date_time.time.nanosecond);
  126. // b. Perform ? CheckISODaysRange(balanced.[[ISODate]]).
  127. TRY(check_iso_days_range(vm, balanced.iso_date));
  128. // c. Let epochNanoseconds be GetUTCEpochNanoseconds(balanced).
  129. auto epoch_nanoseconds = get_utc_epoch_nanoseconds(balanced);
  130. // d. Let possibleEpochNanoseconds be « epochNanoseconds ».
  131. possible_epoch_nanoseconds.append(move(epoch_nanoseconds));
  132. }
  133. // 3. Else,
  134. else {
  135. // a. Perform ? CheckISODaysRange(isoDateTime.[[ISODate]]).
  136. TRY(check_iso_days_range(vm, iso_date_time.iso_date));
  137. // b. Let possibleEpochNanoseconds be GetNamedTimeZoneEpochNanoseconds(parseResult.[[Name]], isoDateTime).
  138. possible_epoch_nanoseconds = get_named_time_zone_epoch_nanoseconds(
  139. *parse_result.name,
  140. iso_date_time.iso_date.year,
  141. iso_date_time.iso_date.month,
  142. iso_date_time.iso_date.day,
  143. iso_date_time.time.hour,
  144. iso_date_time.time.minute,
  145. iso_date_time.time.second,
  146. iso_date_time.time.millisecond,
  147. iso_date_time.time.microsecond,
  148. iso_date_time.time.nanosecond);
  149. }
  150. // 4. For each value epochNanoseconds in possibleEpochNanoseconds, do
  151. for (auto const& epoch_nanoseconds : possible_epoch_nanoseconds) {
  152. // a. If IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
  153. if (!is_valid_epoch_nanoseconds(epoch_nanoseconds))
  154. return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidEpochNanoseconds);
  155. }
  156. // 5. Return possibleEpochNanoseconds.
  157. return possible_epoch_nanoseconds;
  158. }
  159. // 11.1.16 ParseTimeZoneIdentifier ( identifier ), https://tc39.es/proposal-temporal/#sec-parsetimezoneidentifier
  160. ThrowCompletionOr<TimeZone> parse_time_zone_identifier(VM& vm, StringView identifier)
  161. {
  162. // 1. Let parseResult be ParseText(StringToCodePoints(identifier), TimeZoneIdentifier).
  163. auto parse_result = parse_iso8601(Production::TimeZoneIdentifier, identifier);
  164. // 2. If parseResult is a List of errors, throw a RangeError exception.
  165. if (!parse_result.has_value())
  166. return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidTimeZoneString, identifier);
  167. return parse_time_zone_identifier(*parse_result);
  168. }
  169. // 11.1.16 ParseTimeZoneIdentifier ( identifier ), https://tc39.es/proposal-temporal/#sec-parsetimezoneidentifier
  170. TimeZone parse_time_zone_identifier(StringView identifier)
  171. {
  172. // OPTIMIZATION: Some callers can assume that parsing will succeed.
  173. // 1. Let parseResult be ParseText(StringToCodePoints(identifier), TimeZoneIdentifier).
  174. auto parse_result = parse_iso8601(Production::TimeZoneIdentifier, identifier);
  175. VERIFY(parse_result.has_value());
  176. return parse_time_zone_identifier(*parse_result);
  177. }
  178. // 11.1.16 ParseTimeZoneIdentifier ( identifier ), https://tc39.es/proposal-temporal/#sec-parsetimezoneidentifier
  179. TimeZone parse_time_zone_identifier(ParseResult const& parse_result)
  180. {
  181. // OPTIMIZATION: Some callers will have already parsed and validated the time zone identifier.
  182. // 3. If parseResult contains a TimeZoneIANAName Parse Node, then
  183. if (parse_result.time_zone_iana_name.has_value()) {
  184. // a. Let name be the source text matched by the TimeZoneIANAName Parse Node contained within parseResult.
  185. // b. NOTE: name is syntactically valid, but does not necessarily conform to IANA Time Zone Database naming
  186. // guidelines or correspond with an available named time zone identifier.
  187. // c. Return the Record { [[Name]]: CodePointsToString(name), [[OffsetMinutes]]: empty }.
  188. return TimeZone { .name = String::from_utf8_without_validation(parse_result.time_zone_iana_name->bytes()), .offset_minutes = {} };
  189. }
  190. // 4. Else,
  191. else {
  192. // a. Assert: parseResult contains a UTCOffset[~SubMinutePrecision] Parse Node.
  193. VERIFY(parse_result.time_zone_offset.has_value());
  194. // b. Let offsetString be the source text matched by the UTCOffset[~SubMinutePrecision] Parse Node contained within parseResult.
  195. // c. Let offsetNanoseconds be ! ParseDateTimeUTCOffset(offsetString).
  196. auto offset_nanoseconds = parse_date_time_utc_offset(parse_result.time_zone_offset->source_text);
  197. // d. Let offsetMinutes be offsetNanoseconds / (60 × 10**9).
  198. auto offset_minutes = offset_nanoseconds / 60'000'000'000;
  199. // e. Assert: offsetMinutes is an integer.
  200. VERIFY(trunc(offset_minutes) == offset_minutes);
  201. // f. Return the Record { [[Name]]: empty, [[OffsetMinutes]]: offsetMinutes }.
  202. return TimeZone { .name = {}, .offset_minutes = static_cast<i64>(offset_minutes) };
  203. }
  204. }
  205. }