TimeZone.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/Temporal/ISO8601.h>
  9. #include <LibJS/Runtime/Temporal/TimeZone.h>
  10. #include <LibJS/Runtime/Temporal/TimeZoneConstructor.h>
  11. namespace JS::Temporal {
  12. // 11 Temporal.TimeZone Objects, https://tc39.es/proposal-temporal/#sec-temporal-timezone-objects
  13. TimeZone::TimeZone(String identifier, Object& prototype)
  14. : Object(prototype)
  15. , m_identifier(move(identifier))
  16. {
  17. }
  18. // 11.1.1 IsValidTimeZoneName ( timeZone ), https://tc39.es/proposal-temporal/#sec-isvalidtimezonename
  19. // NOTE: This is the minimum implementation of IsValidTimeZoneName, supporting only the "UTC" time zone.
  20. bool is_valid_time_zone_name(String const& time_zone)
  21. {
  22. // 1. Assert: Type(timeZone) is String.
  23. // 2. Let tzText be ! StringToCodePoints(timeZone).
  24. // 3. Let tzUpperText be the result of toUppercase(tzText), according to the Unicode Default Case Conversion algorithm.
  25. // 4. Let tzUpper be ! CodePointsToString(tzUpperText).
  26. auto tz_upper = time_zone.to_uppercase();
  27. // 5. If tzUpper and "UTC" are the same sequence of code points, return true.
  28. if (tz_upper == "UTC")
  29. return true;
  30. // 6. Return false.
  31. return false;
  32. }
  33. // 11.1.2 CanonicalizeTimeZoneName ( timeZone ), https://tc39.es/proposal-temporal/#sec-canonicalizetimezonename
  34. // NOTE: This is the minimum implementation of CanonicalizeTimeZoneName, supporting only the "UTC" time zone.
  35. String canonicalize_time_zone_name(String const& time_zone)
  36. {
  37. // 1. Assert: Type(timeZone) is String.
  38. // 2. Assert: ! IsValidTimeZoneName(timeZone) is true.
  39. VERIFY(is_valid_time_zone_name(time_zone));
  40. // 3. Return "UTC".
  41. return "UTC";
  42. }
  43. // 11.1.3 DefaultTimeZone ( ), https://tc39.es/proposal-temporal/#sec-defaulttimezone
  44. // NOTE: This is the minimum implementation of DefaultTimeZone, supporting only the "UTC" time zone.
  45. String default_time_zone()
  46. {
  47. // 1. Return "UTC".
  48. return "UTC";
  49. }
  50. // 11.6.2 CreateTemporalTimeZone ( identifier [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporaltimezone
  51. Object* create_temporal_time_zone(GlobalObject& global_object, String const& identifier, FunctionObject* new_target)
  52. {
  53. auto& vm = global_object.vm();
  54. // 1. If newTarget is not present, set it to %Temporal.TimeZone%.
  55. if (!new_target)
  56. new_target = global_object.temporal_time_zone_constructor();
  57. // 2. Let object be ? OrdinaryCreateFromConstructor(newTarget, "%Temporal.TimeZone.prototype%", « [[InitializedTemporalTimeZone]], [[Identifier]], [[OffsetNanoseconds]] »).
  58. // 3. Set object.[[Identifier]] to identifier.
  59. auto* object = ordinary_create_from_constructor<TimeZone>(global_object, *new_target, &GlobalObject::temporal_time_zone_prototype, identifier);
  60. if (vm.exception())
  61. return {};
  62. // 4. If identifier satisfies the syntax of a TimeZoneNumericUTCOffset (see 13.33), then
  63. if (is_valid_time_zone_numeric_utc_offset(identifier)) {
  64. // TODO:
  65. // a. Set object.[[OffsetNanoseconds]] to ! ParseTimeZoneOffsetString(identifier).
  66. }
  67. // 5. Else,
  68. else {
  69. // a. Assert: ! CanonicalizeTimeZoneName(identifier) is identifier.
  70. VERIFY(canonicalize_time_zone_name(identifier) == identifier);
  71. // b. Set object.[[OffsetNanoseconds]] to undefined.
  72. // NOTE: No-op.
  73. }
  74. // 6. Return object.
  75. return object;
  76. }
  77. }