mirror of
				https://github.com/LadybirdBrowser/ladybird.git
				synced 2025-10-31 05:10:57 +00:00 
			
		
		
		
	
		
			
	
	
		
			80 lines
		
	
	
	
		
			2.7 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
		
		
			
		
	
	
			80 lines
		
	
	
	
		
			2.7 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
|   | /*
 | ||
|  |  * Copyright (c) 2021, Linus Groh <linusg@serenityos.org> | ||
|  |  * | ||
|  |  * SPDX-License-Identifier: BSD-2-Clause | ||
|  |  */ | ||
|  | 
 | ||
|  | #include <LibJS/Runtime/GlobalObject.h>
 | ||
|  | #include <LibJS/Runtime/Temporal/ISO8601.h>
 | ||
|  | #include <LibJS/Runtime/Temporal/TimeZone.h>
 | ||
|  | #include <LibJS/Runtime/Temporal/TimeZoneConstructor.h>
 | ||
|  | 
 | ||
|  | namespace JS::Temporal { | ||
|  | 
 | ||
|  | // 11.2 The Temporal.TimeZone Constructor, https://tc39.es/proposal-temporal/#sec-temporal-timezone-constructor
 | ||
|  | TimeZoneConstructor::TimeZoneConstructor(GlobalObject& global_object) | ||
|  |     : NativeFunction(vm().names.TimeZone.as_string(), *global_object.function_prototype()) | ||
|  | { | ||
|  | } | ||
|  | 
 | ||
|  | void TimeZoneConstructor::initialize(GlobalObject& global_object) | ||
|  | { | ||
|  |     NativeFunction::initialize(global_object); | ||
|  | 
 | ||
|  |     auto& vm = this->vm(); | ||
|  | 
 | ||
|  |     // 11.3.1 Temporal.TimeZone.prototype, https://tc39.es/proposal-temporal/#sec-temporal-timezone-prototype
 | ||
|  |     define_direct_property(vm.names.prototype, global_object.temporal_time_zone_prototype(), 0); | ||
|  | 
 | ||
|  |     define_direct_property(vm.names.length, Value(1), Attribute::Configurable); | ||
|  | } | ||
|  | 
 | ||
|  | // 11.2.1 Temporal.TimeZone ( identifier ), https://tc39.es/proposal-temporal/#sec-temporal.timezone
 | ||
|  | Value TimeZoneConstructor::call() | ||
|  | { | ||
|  |     auto& vm = this->vm(); | ||
|  | 
 | ||
|  |     // 1. If NewTarget is undefined, then
 | ||
|  |     // a. Throw a TypeError exception.
 | ||
|  |     vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Temporal.TimeZone"); | ||
|  |     return {}; | ||
|  | } | ||
|  | 
 | ||
|  | // 11.2.1 Temporal.TimeZone ( identifier ), https://tc39.es/proposal-temporal/#sec-temporal.timezone
 | ||
|  | Value TimeZoneConstructor::construct(FunctionObject& new_target) | ||
|  | { | ||
|  |     auto& vm = this->vm(); | ||
|  |     auto& global_object = this->global_object(); | ||
|  | 
 | ||
|  |     // 2. Set identifier to ? ToString(identifier).
 | ||
|  |     auto identifier = vm.argument(0).to_string(global_object); | ||
|  |     if (vm.exception()) | ||
|  |         return {}; | ||
|  | 
 | ||
|  |     String canonical; | ||
|  | 
 | ||
|  |     // 3. If identifier satisfies the syntax of a TimeZoneNumericUTCOffset (see 13.33), then
 | ||
|  |     if (is_valid_time_zone_numeric_utc_offset(identifier)) { | ||
|  |         // TODO:
 | ||
|  |         // a. Let offsetNanoseconds be ? ParseTimeZoneOffsetString(identifier).
 | ||
|  |         // b. Let canonical be ! FormatTimeZoneOffsetString(offsetNanoseconds).
 | ||
|  |     } | ||
|  |     // 4. Else,
 | ||
|  |     else { | ||
|  |         // a. If ! IsValidTimeZoneName(identifier) is false, then
 | ||
|  |         if (!is_valid_time_zone_name(identifier)) { | ||
|  |             // i. Throw a RangeError exception.
 | ||
|  |             vm.throw_exception<RangeError>(global_object); | ||
|  |             return {}; | ||
|  |         } | ||
|  | 
 | ||
|  |         // b. Let canonical be ! CanonicalizeTimeZoneName(identifier).
 | ||
|  |         canonical = canonicalize_time_zone_name(identifier); | ||
|  |     } | ||
|  | 
 | ||
|  |     // 5. Return ? CreateTemporalTimeZone(canonical, NewTarget).
 | ||
|  |     return create_temporal_time_zone(global_object, canonical, &new_target); | ||
|  | } | ||
|  | 
 | ||
|  | } |