mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 22:02:44 +00:00 
			
		
		
		
	LibJS: Implement Temporal.ZonedDateTime.prototype.withTimeZone
This commit is contained in:
		
							parent
							
								
									d5f2745a19
								
							
						
					
					
						commit
						58bb73b60f
					
				
					 4 changed files with 61 additions and 0 deletions
				
			
		|  | @ -487,6 +487,7 @@ namespace JS { | ||||||
|     P(withCalendar)                          \ |     P(withCalendar)                          \ | ||||||
|     P(withPlainDate)                         \ |     P(withPlainDate)                         \ | ||||||
|     P(withPlainTime)                         \ |     P(withPlainTime)                         \ | ||||||
|  |     P(withTimeZone)                          \ | ||||||
|     P(writable)                              \ |     P(writable)                              \ | ||||||
|     P(year)                                  \ |     P(year)                                  \ | ||||||
|     P(yearMonthFromFields)                   \ |     P(yearMonthFromFields)                   \ | ||||||
|  |  | ||||||
|  | @ -66,6 +66,7 @@ void ZonedDateTimePrototype::initialize(GlobalObject& global_object) | ||||||
|     u8 attr = Attribute::Writable | Attribute::Configurable; |     u8 attr = Attribute::Writable | Attribute::Configurable; | ||||||
|     define_native_function(vm.names.withPlainTime, with_plain_time, 0, attr); |     define_native_function(vm.names.withPlainTime, with_plain_time, 0, attr); | ||||||
|     define_native_function(vm.names.withPlainDate, with_plain_date, 1, attr); |     define_native_function(vm.names.withPlainDate, with_plain_date, 1, attr); | ||||||
|  |     define_native_function(vm.names.withTimeZone, with_time_zone, 1, attr); | ||||||
|     define_native_function(vm.names.valueOf, value_of, 0, attr); |     define_native_function(vm.names.valueOf, value_of, 0, attr); | ||||||
|     define_native_function(vm.names.startOfDay, start_of_day, 0, attr); |     define_native_function(vm.names.startOfDay, start_of_day, 0, attr); | ||||||
|     define_native_function(vm.names.toInstant, to_instant, 0, attr); |     define_native_function(vm.names.toInstant, to_instant, 0, attr); | ||||||
|  | @ -778,6 +779,20 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::with_plain_date) | ||||||
|     return MUST(create_temporal_zoned_date_time(global_object, instant->nanoseconds(), time_zone, *calendar)); |     return MUST(create_temporal_zoned_date_time(global_object, instant->nanoseconds(), time_zone, *calendar)); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | // 6.3.33 Temporal.ZonedDateTime.prototype.withTimeZone ( timeZoneLike ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.withtimezone
 | ||||||
|  | JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::with_time_zone) | ||||||
|  | { | ||||||
|  |     // 1. Let zonedDateTime be the this value.
 | ||||||
|  |     // 2. Perform ? RequireInternalSlot(zonedDateTime, [[InitializedTemporalZonedDateTime]]).
 | ||||||
|  |     auto* zoned_date_time = TRY(typed_this_object(global_object)); | ||||||
|  | 
 | ||||||
|  |     // 3. Let timeZone be ? ToTemporalTimeZone(timeZoneLike).
 | ||||||
|  |     auto* time_zone = TRY(to_temporal_time_zone(global_object, vm.argument(0))); | ||||||
|  | 
 | ||||||
|  |     // 4. Return ! CreateTemporalZonedDateTime(zonedDateTime.[[Nanoseconds]], timeZone, zonedDateTime.[[Calendar]]).
 | ||||||
|  |     return MUST(create_temporal_zoned_date_time(global_object, zoned_date_time->nanoseconds(), *time_zone, zoned_date_time->calendar())); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| // 6.3.44 Temporal.ZonedDateTime.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.valueof
 | // 6.3.44 Temporal.ZonedDateTime.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.valueof
 | ||||||
| JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::value_of) | JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::value_of) | ||||||
| { | { | ||||||
|  |  | ||||||
|  | @ -51,6 +51,7 @@ private: | ||||||
|     JS_DECLARE_NATIVE_FUNCTION(era_year_getter); |     JS_DECLARE_NATIVE_FUNCTION(era_year_getter); | ||||||
|     JS_DECLARE_NATIVE_FUNCTION(with_plain_time); |     JS_DECLARE_NATIVE_FUNCTION(with_plain_time); | ||||||
|     JS_DECLARE_NATIVE_FUNCTION(with_plain_date); |     JS_DECLARE_NATIVE_FUNCTION(with_plain_date); | ||||||
|  |     JS_DECLARE_NATIVE_FUNCTION(with_time_zone); | ||||||
|     JS_DECLARE_NATIVE_FUNCTION(value_of); |     JS_DECLARE_NATIVE_FUNCTION(value_of); | ||||||
|     JS_DECLARE_NATIVE_FUNCTION(start_of_day); |     JS_DECLARE_NATIVE_FUNCTION(start_of_day); | ||||||
|     JS_DECLARE_NATIVE_FUNCTION(to_instant); |     JS_DECLARE_NATIVE_FUNCTION(to_instant); | ||||||
|  |  | ||||||
|  | @ -0,0 +1,44 @@ | ||||||
|  | describe("correct behavior", () => { | ||||||
|  |     test("length is 1", () => { | ||||||
|  |         expect(Temporal.ZonedDateTime.prototype.withTimeZone).toHaveLength(1); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test("basic functionality", () => { | ||||||
|  |         const object = {}; | ||||||
|  |         const zonedDateTime = new Temporal.ZonedDateTime(1n, object); | ||||||
|  |         expect(zonedDateTime.timeZone).toBe(object); | ||||||
|  | 
 | ||||||
|  |         const timeZone = new Temporal.TimeZone("UTC"); | ||||||
|  |         const withTimeZoneZonedDateTime = zonedDateTime.withTimeZone(timeZone); | ||||||
|  |         expect(withTimeZoneZonedDateTime.timeZone).toBe(timeZone); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     // FIXME: Enable this when time zone string parsing is implemented.
 | ||||||
|  |     test.skip("from time zone string", () => { | ||||||
|  |         const object = {}; | ||||||
|  |         const zonedDateTime = new Temporal.ZonedDateTime(1n, object); | ||||||
|  |         expect(zonedDateTime.timeZone).toBe(object); | ||||||
|  | 
 | ||||||
|  |         const withTimeZoneZonedDateTime = zonedDateTime.withTimeZone("UTC"); | ||||||
|  |         expect(withTimeZoneZonedDateTime.timeZone).toBeInstanceOf(Temporal.TimeZone); | ||||||
|  |         expect(withTimeZoneZonedDateTime.timeZone.id).toBe("UTC"); | ||||||
|  |     }); | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | describe("errors", () => { | ||||||
|  |     test("this value must be a Temporal.ZonedDateTime object", () => { | ||||||
|  |         expect(() => { | ||||||
|  |             Temporal.ZonedDateTime.prototype.withTimeZone.call("foo"); | ||||||
|  |         }).toThrowWithMessage(TypeError, "Not an object of type Temporal.ZonedDateTime"); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     // FIXME: Enable this when time zone string parsing is implemented.
 | ||||||
|  |     test.skip("from invalid time zone string", () => { | ||||||
|  |         const zonedDateTime = new Temporal.ZonedDateTime(1n, {}); | ||||||
|  | 
 | ||||||
|  |         // FIXME: Use "toThrowWithMessage" once this has an error message.
 | ||||||
|  |         expect(() => { | ||||||
|  |             zonedDateTime.withTimeZone("UTCfoobar"); | ||||||
|  |         }).toThrow(RangeError); | ||||||
|  |     }); | ||||||
|  | }); | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Luke Wilde
						Luke Wilde