TimeZoneConstructor.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/GlobalObject.h>
  7. #include <LibJS/Runtime/Temporal/ISO8601.h>
  8. #include <LibJS/Runtime/Temporal/TimeZone.h>
  9. #include <LibJS/Runtime/Temporal/TimeZoneConstructor.h>
  10. namespace JS::Temporal {
  11. // 11.2 The Temporal.TimeZone Constructor, https://tc39.es/proposal-temporal/#sec-temporal-timezone-constructor
  12. TimeZoneConstructor::TimeZoneConstructor(GlobalObject& global_object)
  13. : NativeFunction(vm().names.TimeZone.as_string(), *global_object.function_prototype())
  14. {
  15. }
  16. void TimeZoneConstructor::initialize(GlobalObject& global_object)
  17. {
  18. NativeFunction::initialize(global_object);
  19. auto& vm = this->vm();
  20. // 11.3.1 Temporal.TimeZone.prototype, https://tc39.es/proposal-temporal/#sec-temporal-timezone-prototype
  21. define_direct_property(vm.names.prototype, global_object.temporal_time_zone_prototype(), 0);
  22. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  23. }
  24. // 11.2.1 Temporal.TimeZone ( identifier ), https://tc39.es/proposal-temporal/#sec-temporal.timezone
  25. Value TimeZoneConstructor::call()
  26. {
  27. auto& vm = this->vm();
  28. // 1. If NewTarget is undefined, then
  29. // a. Throw a TypeError exception.
  30. vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Temporal.TimeZone");
  31. return {};
  32. }
  33. // 11.2.1 Temporal.TimeZone ( identifier ), https://tc39.es/proposal-temporal/#sec-temporal.timezone
  34. Value TimeZoneConstructor::construct(FunctionObject& new_target)
  35. {
  36. auto& vm = this->vm();
  37. auto& global_object = this->global_object();
  38. // 2. Set identifier to ? ToString(identifier).
  39. auto identifier = vm.argument(0).to_string(global_object);
  40. if (vm.exception())
  41. return {};
  42. String canonical;
  43. // 3. If identifier satisfies the syntax of a TimeZoneNumericUTCOffset (see 13.33), then
  44. if (is_valid_time_zone_numeric_utc_offset(identifier)) {
  45. // TODO:
  46. // a. Let offsetNanoseconds be ? ParseTimeZoneOffsetString(identifier).
  47. // b. Let canonical be ! FormatTimeZoneOffsetString(offsetNanoseconds).
  48. }
  49. // 4. Else,
  50. else {
  51. // a. If ! IsValidTimeZoneName(identifier) is false, then
  52. if (!is_valid_time_zone_name(identifier)) {
  53. // i. Throw a RangeError exception.
  54. vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidTimeZoneName);
  55. return {};
  56. }
  57. // b. Let canonical be ! CanonicalizeTimeZoneName(identifier).
  58. canonical = canonicalize_time_zone_name(identifier);
  59. }
  60. // 5. Return ? CreateTemporalTimeZone(canonical, NewTarget).
  61. return create_temporal_time_zone(global_object, canonical, &new_target);
  62. }
  63. }