| 
									
										
										
										
											2020-03-30 00:21:56 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2022-05-06 19:10:34 +02:00
										 |  |  |  * Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org> | 
					
						
							| 
									
										
										
										
											2024-11-20 20:06:42 -05:00
										 |  |  |  * Copyright (c) 2022-2024, Tim Flynn <trflynn89@ladybird.org> | 
					
						
							| 
									
										
										
										
											2020-03-30 00:21:56 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-03-30 00:21:56 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-28 20:40:53 +02:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-28 11:26:03 -05:00
										 |  |  | #include <LibCrypto/BigInt/SignedBigInteger.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-30 00:21:56 +01:00
										 |  |  | #include <LibJS/Runtime/Object.h>
 | 
					
						
							| 
									
										
										
										
											2024-06-25 15:27:20 -04:00
										 |  |  | #include <LibUnicode/TimeZone.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-30 00:21:56 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace JS { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Date final : public Object { | 
					
						
							| 
									
										
										
										
											2020-06-21 15:14:02 +02:00
										 |  |  |     JS_OBJECT(Date, Object); | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     GC_DECLARE_ALLOCATOR(Date); | 
					
						
							| 
									
										
										
										
											2020-06-21 15:14:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 00:21:56 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     static GC::Ref<Date> create(Realm&, double date_value); | 
					
						
							| 
									
										
										
										
											2020-04-17 19:07:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-09 16:01:28 -07:00
										 |  |  |     // Out of line to ensure we have a key function
 | 
					
						
							|  |  |  |     virtual ~Date() override; | 
					
						
							| 
									
										
										
										
											2020-03-30 00:21:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-14 17:42:42 -05:00
										 |  |  |     double date_value() const { return m_date_value; } | 
					
						
							|  |  |  |     void set_date_value(double value) { m_date_value = value; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 17:46:01 -07:00
										 |  |  |     ErrorOr<String> iso_date_string() const; | 
					
						
							| 
									
										
										
										
											2020-08-20 16:29:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 00:21:56 +01:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2022-08-28 23:51:28 +02:00
										 |  |  |     Date(double date_value, Object& prototype); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-14 17:42:42 -05:00
										 |  |  |     double m_date_value { 0 }; // [[DateValue]]
 | 
					
						
							| 
									
										
										
										
											2020-03-30 00:21:56 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-03 12:14:59 -04:00
										 |  |  | // 21.4.1.22 Time Zone Identifier Record, https://tc39.es/ecma262/#sec-time-zone-identifier-record
 | 
					
						
							|  |  |  | struct TimeZoneIdentifier { | 
					
						
							| 
									
										
										
										
											2024-06-25 11:06:08 -04:00
										 |  |  |     String identifier;         // [[Identifier]]
 | 
					
						
							|  |  |  |     String primary_identifier; // [[PrimaryIdentifier]]
 | 
					
						
							| 
									
										
										
										
											2023-10-03 12:14:59 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-06 19:10:34 +02:00
										 |  |  | // https://tc39.es/ecma262/#eqn-HoursPerDay
 | 
					
						
							| 
									
										
										
										
											2023-01-28 11:12:54 -05:00
										 |  |  | constexpr inline double hours_per_day = 24; | 
					
						
							| 
									
										
										
										
											2022-05-06 19:10:34 +02:00
										 |  |  | // https://tc39.es/ecma262/#eqn-MinutesPerHour
 | 
					
						
							| 
									
										
										
										
											2023-01-28 11:12:54 -05:00
										 |  |  | constexpr inline double minutes_per_hour = 60; | 
					
						
							| 
									
										
										
										
											2022-05-06 19:10:34 +02:00
										 |  |  | // https://tc39.es/ecma262/#eqn-SecondsPerMinute
 | 
					
						
							| 
									
										
										
										
											2023-01-28 11:12:54 -05:00
										 |  |  | constexpr inline double seconds_per_minute = 60; | 
					
						
							| 
									
										
										
										
											2022-05-06 19:10:34 +02:00
										 |  |  | // https://tc39.es/ecma262/#eqn-msPerSecond
 | 
					
						
							| 
									
										
										
										
											2023-01-28 11:12:54 -05:00
										 |  |  | constexpr inline double ms_per_second = 1'000; | 
					
						
							| 
									
										
										
										
											2022-05-06 19:10:34 +02:00
										 |  |  | // https://tc39.es/ecma262/#eqn-msPerMinute
 | 
					
						
							| 
									
										
										
										
											2023-01-28 11:12:54 -05:00
										 |  |  | constexpr inline double ms_per_minute = 60'000; | 
					
						
							| 
									
										
										
										
											2022-05-06 19:10:34 +02:00
										 |  |  | // https://tc39.es/ecma262/#eqn-msPerHour
 | 
					
						
							| 
									
										
										
										
											2023-01-28 11:12:54 -05:00
										 |  |  | constexpr inline double ms_per_hour = 3'600'000; | 
					
						
							| 
									
										
										
										
											2022-05-06 19:10:34 +02:00
										 |  |  | // https://tc39.es/ecma262/#eqn-msPerDay
 | 
					
						
							| 
									
										
										
										
											2023-01-28 11:12:54 -05:00
										 |  |  | constexpr inline double ms_per_day = 86'400'000; | 
					
						
							| 
									
										
										
										
											2022-05-06 19:28:56 +02:00
										 |  |  | // https://tc39.es/proposal-temporal/#eqn-nsPerDay
 | 
					
						
							| 
									
										
										
										
											2023-01-28 11:12:54 -05:00
										 |  |  | constexpr inline double ns_per_day = 86'400'000'000'000; | 
					
						
							|  |  |  | extern Crypto::SignedBigInteger const ns_per_day_bigint; | 
					
						
							| 
									
										
										
										
											2022-05-06 19:10:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-03 11:01:19 -04:00
										 |  |  | double day(double); | 
					
						
							|  |  |  | double time_within_day(double); | 
					
						
							| 
									
										
										
										
											2021-07-25 20:47:40 +01:00
										 |  |  | u16 days_in_year(i32); | 
					
						
							|  |  |  | double day_from_year(i32); | 
					
						
							| 
									
										
										
										
											2021-11-05 01:18:27 +01:00
										 |  |  | double time_from_year(i32); | 
					
						
							| 
									
										
										
										
											2021-07-25 20:47:40 +01:00
										 |  |  | i32 year_from_time(double); | 
					
						
							| 
									
										
										
										
											2023-10-03 11:01:19 -04:00
										 |  |  | u16 day_within_year(double); | 
					
						
							| 
									
										
										
										
											2021-07-25 20:47:40 +01:00
										 |  |  | bool in_leap_year(double); | 
					
						
							|  |  |  | u8 month_from_time(double); | 
					
						
							| 
									
										
										
										
											2023-10-03 11:01:19 -04:00
										 |  |  | u8 date_from_time(double); | 
					
						
							|  |  |  | u8 week_day(double); | 
					
						
							| 
									
										
										
										
											2021-07-25 20:47:40 +01:00
										 |  |  | u8 hour_from_time(double); | 
					
						
							|  |  |  | u8 min_from_time(double); | 
					
						
							|  |  |  | u8 sec_from_time(double); | 
					
						
							|  |  |  | u16 ms_from_time(double); | 
					
						
							| 
									
										
										
										
											2025-02-28 11:54:54 -05:00
										 |  |  | Crypto::SignedBigInteger get_utc_epoch_nanoseconds(Temporal::ISODateTime const&); | 
					
						
							|  |  |  | Vector<Crypto::SignedBigInteger> get_named_time_zone_epoch_nanoseconds(StringView time_zone_identifier, Temporal::ISODateTime const&); | 
					
						
							| 
									
										
										
										
											2024-06-25 15:27:20 -04:00
										 |  |  | Unicode::TimeZoneOffset get_named_time_zone_offset_nanoseconds(StringView time_zone_identifier, Crypto::SignedBigInteger const& epoch_nanoseconds); | 
					
						
							| 
									
										
										
										
											2024-09-03 10:26:03 -04:00
										 |  |  | Unicode::TimeZoneOffset get_named_time_zone_offset_milliseconds(StringView time_zone_identifier, double epoch_milliseconds); | 
					
						
							| 
									
										
										
										
											2024-06-24 10:17:32 -04:00
										 |  |  | String system_time_zone_identifier(); | 
					
						
							| 
									
										
										
										
											2024-09-03 10:18:43 -04:00
										 |  |  | void clear_system_time_zone_cache(); | 
					
						
							| 
									
										
										
										
											2022-01-14 13:10:07 -05:00
										 |  |  | double local_time(double time); | 
					
						
							|  |  |  | double utc_time(double time); | 
					
						
							| 
									
										
										
										
											2022-05-06 20:45:25 +02:00
										 |  |  | double make_time(double hour, double min, double sec, double ms); | 
					
						
							|  |  |  | double make_day(double year, double month, double date); | 
					
						
							|  |  |  | double make_date(double day, double time); | 
					
						
							|  |  |  | double time_clip(double time); | 
					
						
							| 
									
										
										
										
											2024-11-20 20:06:42 -05:00
										 |  |  | bool is_offset_time_zone_identifier(StringView offset_string); | 
					
						
							|  |  |  | ThrowCompletionOr<double> parse_date_time_utc_offset(VM&, StringView offset_string); | 
					
						
							|  |  |  | double parse_date_time_utc_offset(StringView offset_string); | 
					
						
							|  |  |  | double parse_date_time_utc_offset(Temporal::TimeZoneOffset const&); | 
					
						
							| 
									
										
										
										
											2021-07-11 21:04:11 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 00:21:56 +01:00
										 |  |  | } |