TimeZoneConstructor.cpp 2.9 KB

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