TimeZoneConstructor.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. u8 attr = Attribute::Writable | Attribute::Configurable;
  22. define_native_function(vm.names.from, from, 1, attr);
  23. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  24. }
  25. // 11.2.1 Temporal.TimeZone ( identifier ), https://tc39.es/proposal-temporal/#sec-temporal.timezone
  26. Value TimeZoneConstructor::call()
  27. {
  28. auto& vm = this->vm();
  29. // 1. If NewTarget is undefined, then
  30. // a. Throw a TypeError exception.
  31. vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Temporal.TimeZone");
  32. return {};
  33. }
  34. // 11.2.1 Temporal.TimeZone ( identifier ), https://tc39.es/proposal-temporal/#sec-temporal.timezone
  35. Value TimeZoneConstructor::construct(FunctionObject& new_target)
  36. {
  37. auto& vm = this->vm();
  38. auto& global_object = this->global_object();
  39. // 2. Set identifier to ? ToString(identifier).
  40. auto identifier = vm.argument(0).to_string(global_object);
  41. if (vm.exception())
  42. return {};
  43. String canonical;
  44. // 3. If identifier satisfies the syntax of a TimeZoneNumericUTCOffset (see 13.33), then
  45. if (is_valid_time_zone_numeric_utc_offset_syntax(identifier)) {
  46. // a. Let offsetNanoseconds be ? ParseTimeZoneOffsetString(identifier).
  47. auto offset_nanoseconds = TRY_OR_DISCARD(parse_time_zone_offset_string(global_object, identifier));
  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 TRY_OR_DISCARD(create_temporal_time_zone(global_object, canonical, &new_target));
  64. }
  65. // 11.3.2 Temporal.TimeZone.from ( item ), https://tc39.es/proposal-temporal/#sec-temporal.timezone.from
  66. JS_DEFINE_NATIVE_FUNCTION(TimeZoneConstructor::from)
  67. {
  68. auto item = vm.argument(0);
  69. // 1. Return ? ToTemporalTimeZone(item).
  70. return TRY_OR_DISCARD(to_temporal_time_zone(global_object, item));
  71. }
  72. }