| 
									
										
										
										
											2021-07-06 23:53:27 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2021, Linus Groh <linusg@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/GlobalObject.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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-31 21:39:36 +01:00
										 |  |  |     u8 attr = Attribute::Writable | Attribute::Configurable; | 
					
						
							| 
									
										
										
										
											2021-10-20 01:40:40 +03:00
										 |  |  |     define_old_native_function(vm.names.from, from, 1, attr); | 
					
						
							| 
									
										
										
										
											2021-07-31 21:39:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-06 23:53:27 +01:00
										 |  |  |     define_direct_property(vm.names.length, Value(1), Attribute::Configurable); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 11.2.1 Temporal.TimeZone ( identifier ), https://tc39.es/proposal-temporal/#sec-temporal.timezone
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  | ThrowCompletionOr<Value> TimeZoneConstructor::call() | 
					
						
							| 
									
										
										
										
											2021-07-06 23:53:27 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     auto& vm = this->vm(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 1. If NewTarget is undefined, then
 | 
					
						
							|  |  |  |     // a. Throw a TypeError exception.
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |     return vm.throw_completion<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, "Temporal.TimeZone"); | 
					
						
							| 
									
										
										
										
											2021-07-06 23:53:27 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 11.2.1 Temporal.TimeZone ( identifier ), https://tc39.es/proposal-temporal/#sec-temporal.timezone
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  | ThrowCompletionOr<Object*> TimeZoneConstructor::construct(FunctionObject& new_target) | 
					
						
							| 
									
										
										
										
											2021-07-06 23:53:27 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     auto& vm = this->vm(); | 
					
						
							|  |  |  |     auto& global_object = this->global_object(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 2. Set identifier to ? ToString(identifier).
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |     auto identifier = TRY(vm.argument(0).to_string(global_object)); | 
					
						
							| 
									
										
										
										
											2021-07-06 23:53:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     String canonical; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 3. If identifier satisfies the syntax of a TimeZoneNumericUTCOffset (see 13.33), then
 | 
					
						
							| 
									
										
										
										
											2021-07-11 21:04:11 +03:00
										 |  |  |     if (is_valid_time_zone_numeric_utc_offset_syntax(identifier)) { | 
					
						
							| 
									
										
										
										
											2021-07-06 23:53:27 +01:00
										 |  |  |         // a. Let offsetNanoseconds be ? ParseTimeZoneOffsetString(identifier).
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |         auto offset_nanoseconds = TRY(parse_time_zone_offset_string(global_object, identifier)); | 
					
						
							| 
									
										
										
										
											2021-07-11 21:04:11 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-06 23:53:27 +01:00
										 |  |  |         // b. Let canonical be ! FormatTimeZoneOffsetString(offsetNanoseconds).
 | 
					
						
							| 
									
										
										
										
											2021-07-11 21:04:11 +03:00
										 |  |  |         canonical = format_time_zone_offset_string(offset_nanoseconds); | 
					
						
							| 
									
										
										
										
											2021-07-06 23:53:27 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     // 4. Else,
 | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         // a. If ! IsValidTimeZoneName(identifier) is false, then
 | 
					
						
							|  |  |  |         if (!is_valid_time_zone_name(identifier)) { | 
					
						
							|  |  |  |             // i. Throw a RangeError exception.
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |             return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidTimeZoneName); | 
					
						
							| 
									
										
										
										
											2021-07-06 23:53:27 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // b. Let canonical be ! CanonicalizeTimeZoneName(identifier).
 | 
					
						
							|  |  |  |         canonical = canonicalize_time_zone_name(identifier); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 5. Return ? CreateTemporalTimeZone(canonical, NewTarget).
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |     return TRY(create_temporal_time_zone(global_object, canonical, &new_target)); | 
					
						
							| 
									
										
										
										
											2021-07-06 23:53:27 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-08 11:04:30 +01:00
										 |  |  | // 11.3.2 Temporal.TimeZone.from ( item ), https://tc39.es/proposal-temporal/#sec-temporal.timezone.from
 | 
					
						
							| 
									
										
										
										
											2021-10-19 20:18:01 +03:00
										 |  |  | JS_DEFINE_OLD_NATIVE_FUNCTION(TimeZoneConstructor::from) | 
					
						
							| 
									
										
										
										
											2021-07-31 21:39:36 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     auto item = vm.argument(0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 1. Return ? ToTemporalTimeZone(item).
 | 
					
						
							| 
									
										
										
										
											2021-09-16 01:49:19 +03:00
										 |  |  |     return TRY_OR_DISCARD(to_temporal_time_zone(global_object, item)); | 
					
						
							| 
									
										
										
										
											2021-07-31 21:39:36 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-06 23:53:27 +01:00
										 |  |  | } |