| 
									
										
										
										
											2020-03-30 00:21:56 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-04-22 22:51:19 +02:00
										 |  |  |  * Copyright (c) 2020, Linus Groh <linusg@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-08-21 12:51:00 -04:00
										 |  |  |  * Copyright (c) 2020, Nico Weber <thakis@chromium.org> | 
					
						
							| 
									
										
										
										
											2021-03-16 15:02:16 +01:00
										 |  |  |  * Copyright (c) 2021, Petróczi Zoltán <petroczizoltan@tutanota.com> | 
					
						
							| 
									
										
										
										
											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
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 10:47:09 +02:00
										 |  |  | #include <AK/CharacterTypes.h>
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:51:00 -04:00
										 |  |  | #include <AK/GenericLexer.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-30 00:21:56 +01:00
										 |  |  | #include <LibCore/DateTime.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-20 01:09:39 +01:00
										 |  |  | #include <LibJS/Runtime/AbstractOperations.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-30 00:21:56 +01:00
										 |  |  | #include <LibJS/Runtime/Date.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/DateConstructor.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-18 13:18:06 +02:00
										 |  |  | #include <LibJS/Runtime/GlobalObject.h>
 | 
					
						
							| 
									
										
										
										
											2020-09-27 20:18:30 +02:00
										 |  |  | #include <LibJS/Runtime/VM.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-30 00:21:56 +01:00
										 |  |  | #include <sys/time.h>
 | 
					
						
							|  |  |  | #include <time.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace JS { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 00:22:35 +01:00
										 |  |  | // 21.4.3.2 Date.parse ( string ), https://tc39.es/ecma262/#sec-date.parse
 | 
					
						
							| 
									
										
										
										
											2020-09-18 09:49:51 +02:00
										 |  |  | static Value parse_simplified_iso8601(const String& iso_8601) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-21 12:51:00 -04:00
										 |  |  |     // Date.parse() is allowed to accept many formats. We strictly only accept things matching
 | 
					
						
							| 
									
										
										
										
											2021-06-13 00:22:35 +01:00
										 |  |  |     // 21.4.1.15 Date Time String Format, https://tc39.es/ecma262/#sec-date-time-string-format
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:51:00 -04:00
										 |  |  |     GenericLexer lexer(iso_8601); | 
					
						
							| 
									
										
										
										
											2021-07-10 18:47:16 +01:00
										 |  |  |     auto lex_n_digits = [&](size_t n, Optional<int>& out) { | 
					
						
							| 
									
										
										
										
											2020-08-21 12:51:00 -04:00
										 |  |  |         if (lexer.tell_remaining() < n) | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         int r = 0; | 
					
						
							|  |  |  |         for (size_t i = 0; i < n; ++i) { | 
					
						
							|  |  |  |             char ch = lexer.consume(); | 
					
						
							| 
									
										
										
										
											2021-06-13 10:47:09 +02:00
										 |  |  |             if (!is_ascii_digit(ch)) | 
					
						
							| 
									
										
										
										
											2020-08-21 12:51:00 -04:00
										 |  |  |                 return false; | 
					
						
							|  |  |  |             r = 10 * r + ch - '0'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         out = r; | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-10 18:47:16 +01:00
										 |  |  |     Optional<int> year; | 
					
						
							|  |  |  |     Optional<int> month; | 
					
						
							|  |  |  |     Optional<int> day; | 
					
						
							|  |  |  |     Optional<int> hours; | 
					
						
							|  |  |  |     Optional<int> minutes; | 
					
						
							|  |  |  |     Optional<int> seconds; | 
					
						
							|  |  |  |     Optional<int> milliseconds; | 
					
						
							|  |  |  |     Optional<char> timezone; | 
					
						
							|  |  |  |     Optional<int> timezone_hours; | 
					
						
							|  |  |  |     Optional<int> timezone_minutes; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:51:00 -04:00
										 |  |  |     auto lex_year = [&]() { | 
					
						
							|  |  |  |         if (lexer.consume_specific('+')) | 
					
						
							|  |  |  |             return lex_n_digits(6, year); | 
					
						
							|  |  |  |         if (lexer.consume_specific('-')) { | 
					
						
							| 
									
										
										
										
											2021-07-10 18:47:16 +01:00
										 |  |  |             Optional<int> absolute_year; | 
					
						
							| 
									
										
										
										
											2020-08-21 12:51:00 -04:00
										 |  |  |             if (!lex_n_digits(6, absolute_year)) | 
					
						
							|  |  |  |                 return false; | 
					
						
							| 
									
										
										
										
											2021-07-10 18:47:16 +01:00
										 |  |  |             year = -absolute_year.value(); | 
					
						
							| 
									
										
										
										
											2020-08-21 12:51:00 -04:00
										 |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return lex_n_digits(4, year); | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2021-07-10 18:47:16 +01:00
										 |  |  |     auto lex_month = [&]() { return lex_n_digits(2, month) && *month >= 1 && *month <= 12; }; | 
					
						
							|  |  |  |     auto lex_day = [&]() { return lex_n_digits(2, day) && *day >= 1 && *day <= 31; }; | 
					
						
							| 
									
										
										
										
											2020-08-21 12:51:00 -04:00
										 |  |  |     auto lex_date = [&]() { return lex_year() && (!lexer.consume_specific('-') || (lex_month() && (!lexer.consume_specific('-') || lex_day()))); }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-10 18:47:16 +01:00
										 |  |  |     auto lex_hours_minutes = [&](Optional<int>& out_h, Optional<int>& out_m) { | 
					
						
							|  |  |  |         Optional<int> h; | 
					
						
							|  |  |  |         Optional<int> m; | 
					
						
							|  |  |  |         if (lex_n_digits(2, h) && *h >= 0 && *h <= 24 && lexer.consume_specific(':') && lex_n_digits(2, m) && *m >= 0 && *m <= 59) { | 
					
						
							|  |  |  |             out_h = move(h); | 
					
						
							|  |  |  |             out_m = move(m); | 
					
						
							| 
									
										
										
										
											2020-08-21 12:51:00 -04:00
										 |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2021-07-10 18:47:16 +01:00
										 |  |  |     auto lex_seconds = [&]() { return lex_n_digits(2, seconds) && *seconds >= 0 && *seconds <= 59; }; | 
					
						
							| 
									
										
										
										
											2020-08-21 12:51:00 -04:00
										 |  |  |     auto lex_milliseconds = [&]() { return lex_n_digits(3, milliseconds); }; | 
					
						
							|  |  |  |     auto lex_seconds_milliseconds = [&]() { return lex_seconds() && (!lexer.consume_specific('.') || lex_milliseconds()); }; | 
					
						
							|  |  |  |     auto lex_timezone = [&]() { | 
					
						
							|  |  |  |         if (lexer.consume_specific('+')) { | 
					
						
							|  |  |  |             timezone = '+'; | 
					
						
							|  |  |  |             return lex_hours_minutes(timezone_hours, timezone_minutes); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (lexer.consume_specific('-')) { | 
					
						
							|  |  |  |             timezone = '-'; | 
					
						
							|  |  |  |             return lex_hours_minutes(timezone_hours, timezone_minutes); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (lexer.consume_specific('Z')) | 
					
						
							|  |  |  |             timezone = 'Z'; | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     auto lex_time = [&]() { return lex_hours_minutes(hours, minutes) && (!lexer.consume_specific(':') || lex_seconds_milliseconds()) && lex_timezone(); }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!lex_date() || (lexer.consume_specific('T') && !lex_time()) || !lexer.is_eof()) { | 
					
						
							|  |  |  |         return js_nan(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-10 18:47:16 +01:00
										 |  |  |     // We parsed a valid date simplified ISO 8601 string.
 | 
					
						
							|  |  |  |     VERIFY(year.has_value()); // A valid date string always has at least a year.
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:51:00 -04:00
										 |  |  |     struct tm tm = {}; | 
					
						
							| 
									
										
										
										
											2021-07-10 18:47:16 +01:00
										 |  |  |     tm.tm_year = *year - 1900; | 
					
						
							|  |  |  |     tm.tm_mon = !month.has_value() ? 0 : *month - 1; | 
					
						
							|  |  |  |     tm.tm_mday = day.value_or(1); | 
					
						
							|  |  |  |     tm.tm_hour = hours.value_or(0); | 
					
						
							|  |  |  |     tm.tm_min = minutes.value_or(0); | 
					
						
							|  |  |  |     tm.tm_sec = seconds.value_or(0); | 
					
						
							| 
									
										
										
										
											2020-08-21 12:51:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 00:22:35 +01:00
										 |  |  |     // https://tc39.es/ecma262/#sec-date.parse:
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:51:00 -04:00
										 |  |  |     // "When the UTC offset representation is absent, date-only forms are interpreted as a UTC time and date-time forms are interpreted as a local time."
 | 
					
						
							|  |  |  |     time_t timestamp; | 
					
						
							| 
									
										
										
										
											2021-07-10 18:47:16 +01:00
										 |  |  |     if (timezone.has_value() || !hours.has_value()) | 
					
						
							| 
									
										
										
										
											2020-08-21 12:51:00 -04:00
										 |  |  |         timestamp = timegm(&tm); | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         timestamp = mktime(&tm); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (timezone == '-') | 
					
						
							| 
									
										
										
										
											2021-07-10 18:47:16 +01:00
										 |  |  |         timestamp += (*timezone_hours * 60 + *timezone_minutes) * 60; | 
					
						
							| 
									
										
										
										
											2020-08-21 12:51:00 -04:00
										 |  |  |     else if (timezone == '+') | 
					
						
							| 
									
										
										
										
											2021-07-10 18:47:16 +01:00
										 |  |  |         timestamp -= (*timezone_hours * 60 + *timezone_minutes) * 60; | 
					
						
							| 
									
										
										
										
											2020-08-21 12:51:00 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // FIXME: reject timestamp if resulting value wouldn't fit in a double
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-10 18:47:16 +01:00
										 |  |  |     return Value(1000.0 * timestamp + milliseconds.value_or(0)); | 
					
						
							| 
									
										
										
										
											2020-08-21 12:51:00 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 15:40:48 +02:00
										 |  |  | DateConstructor::DateConstructor(GlobalObject& global_object) | 
					
						
							| 
									
										
										
										
											2021-06-28 03:45:49 +03:00
										 |  |  |     : NativeFunction(vm().names.Date.as_string(), *global_object.function_prototype()) | 
					
						
							| 
									
										
										
										
											2020-06-20 15:40:48 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-22 17:50:18 +02:00
										 |  |  | void DateConstructor::initialize(GlobalObject& global_object) | 
					
						
							| 
									
										
										
										
											2020-03-30 00:21:56 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-10-13 23:49:19 +02:00
										 |  |  |     auto& vm = this->vm(); | 
					
						
							| 
									
										
										
										
											2020-07-22 17:50:18 +02:00
										 |  |  |     NativeFunction::initialize(global_object); | 
					
						
							| 
									
										
										
										
											2021-06-13 00:22:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 21.4.3.3 Date.prototype, https://tc39.es/ecma262/#sec-date.prototype
 | 
					
						
							| 
									
										
										
										
											2021-07-06 02:15:08 +03:00
										 |  |  |     define_direct_property(vm.names.prototype, global_object.date_prototype(), 0); | 
					
						
							| 
									
										
										
										
											2021-06-13 00:22:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 00:58:25 +01:00
										 |  |  |     u8 attr = Attribute::Writable | Attribute::Configurable; | 
					
						
							|  |  |  |     define_native_function(vm.names.now, now, 0, attr); | 
					
						
							|  |  |  |     define_native_function(vm.names.parse, parse, 1, attr); | 
					
						
							|  |  |  |     define_native_function(vm.names.UTC, utc, 1, attr); | 
					
						
							| 
									
										
										
										
											2021-07-08 02:49:53 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     define_direct_property(vm.names.length, Value(7), Attribute::Configurable); | 
					
						
							| 
									
										
										
										
											2020-03-30 00:21:56 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DateConstructor::~DateConstructor() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-20 01:09:39 +01:00
										 |  |  | struct DatetimeAndMilliseconds { | 
					
						
							|  |  |  |     Core::DateTime datetime; | 
					
						
							|  |  |  |     i16 milliseconds { 0 }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static DatetimeAndMilliseconds now() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     struct timeval tv; | 
					
						
							|  |  |  |     gettimeofday(&tv, nullptr); | 
					
						
							|  |  |  |     auto datetime = Core::DateTime::now(); | 
					
						
							|  |  |  |     auto milliseconds = static_cast<i16>(tv.tv_usec / 1000); | 
					
						
							|  |  |  |     return { datetime, milliseconds }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 00:22:35 +01:00
										 |  |  | // 21.4.2.1 Date ( ...values ), https://tc39.es/ecma262/#sec-date
 | 
					
						
							| 
									
										
										
										
											2020-09-27 17:24:14 +02:00
										 |  |  | Value DateConstructor::call() | 
					
						
							| 
									
										
										
										
											2020-04-01 18:31:24 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-06-20 01:09:39 +01:00
										 |  |  |     auto [datetime, milliseconds] = JS::now(); | 
					
						
							|  |  |  |     auto* date = Date::create(global_object(), datetime, milliseconds); | 
					
						
							|  |  |  |     return js_string(heap(), date->string()); | 
					
						
							| 
									
										
										
										
											2020-04-01 18:31:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 00:22:35 +01:00
										 |  |  | // 21.4.2.1 Date ( ...values ), https://tc39.es/ecma262/#sec-date
 | 
					
						
							| 
									
										
										
										
											2021-06-27 21:48:34 +02:00
										 |  |  | Value DateConstructor::construct(FunctionObject& new_target) | 
					
						
							| 
									
										
										
										
											2020-03-30 00:21:56 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-02-13 12:35:31 +01:00
										 |  |  |     auto& vm = this->vm(); | 
					
						
							| 
									
										
										
										
											2021-06-20 01:09:39 +01:00
										 |  |  |     auto& global_object = this->global_object(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (vm.argument_count() == 0) { | 
					
						
							|  |  |  |         auto [datetime, milliseconds] = JS::now(); | 
					
						
							| 
									
										
										
										
											2021-09-16 00:57:38 +03:00
										 |  |  |         return TRY_OR_DISCARD(ordinary_create_from_constructor<Date>(global_object, new_target, &GlobalObject::date_prototype, datetime, milliseconds, false)); | 
					
						
							| 
									
										
										
										
											2021-06-20 01:09:39 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-03-16 15:02:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-16 00:57:38 +03:00
										 |  |  |     auto create_invalid_date = [&global_object, &new_target]() -> Date* { | 
					
						
							| 
									
										
										
										
											2021-06-06 17:07:17 +01:00
										 |  |  |         auto datetime = Core::DateTime::create(1970, 1, 1, 0, 0, 0); | 
					
						
							| 
									
										
										
										
											2021-06-06 16:27:38 +03:00
										 |  |  |         auto milliseconds = static_cast<i16>(0); | 
					
						
							| 
									
										
										
										
											2021-09-16 00:57:38 +03:00
										 |  |  |         return TRY_OR_DISCARD(ordinary_create_from_constructor<Date>(global_object, new_target, &GlobalObject::date_prototype, datetime, milliseconds, true)); | 
					
						
							| 
									
										
										
										
											2021-03-16 15:02:16 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-13 12:35:31 +01:00
										 |  |  |     if (vm.argument_count() == 1) { | 
					
						
							|  |  |  |         auto value = vm.argument(0); | 
					
						
							| 
									
										
										
										
											2020-08-21 13:24:52 -04:00
										 |  |  |         if (value.is_string()) | 
					
						
							|  |  |  |             value = parse_simplified_iso8601(value.as_string().string()); | 
					
						
							| 
									
										
										
										
											2021-03-16 15:02:16 +01:00
										 |  |  |         else | 
					
						
							| 
									
										
										
										
											2021-06-20 01:09:39 +01:00
										 |  |  |             value = value.to_number(global_object); | 
					
						
							| 
									
										
										
										
											2021-03-16 15:02:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-13 12:35:31 +01:00
										 |  |  |         if (vm.exception()) | 
					
						
							|  |  |  |             return {}; | 
					
						
							| 
									
										
										
										
											2021-03-16 15:02:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (!value.is_finite_number()) { | 
					
						
							|  |  |  |             return create_invalid_date(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // A timestamp since the epoch, in UTC.
 | 
					
						
							|  |  |  |         double value_as_double = value.as_double(); | 
					
						
							| 
									
										
										
										
											2021-06-06 17:42:49 +03:00
										 |  |  |         if (value_as_double > Date::time_clip) | 
					
						
							|  |  |  |             return create_invalid_date(); | 
					
						
							| 
									
										
										
										
											2020-08-21 13:24:52 -04:00
										 |  |  |         auto datetime = Core::DateTime::from_timestamp(static_cast<time_t>(value_as_double / 1000)); | 
					
						
							| 
									
										
										
										
											2021-06-06 16:27:38 +03:00
										 |  |  |         auto milliseconds = static_cast<i16>(fmod(value_as_double, 1000)); | 
					
						
							| 
									
										
										
										
											2021-09-16 00:57:38 +03:00
										 |  |  |         return TRY_OR_DISCARD(ordinary_create_from_constructor<Date>(global_object, new_target, &GlobalObject::date_prototype, datetime, milliseconds, false)); | 
					
						
							| 
									
										
										
										
											2020-08-20 10:41:10 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-03-16 15:02:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-20 10:41:10 -04:00
										 |  |  |     // A date/time in components, in local time.
 | 
					
						
							| 
									
										
										
										
											2021-06-20 01:09:39 +01:00
										 |  |  |     auto arg_or = [&vm, &global_object](size_t i, i32 fallback) { | 
					
						
							|  |  |  |         return vm.argument_count() > i ? vm.argument(i).to_number(global_object) : Value(fallback); | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2021-03-16 15:02:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-20 01:09:39 +01:00
										 |  |  |     auto year_value = vm.argument(0).to_number(global_object); | 
					
						
							| 
									
										
										
										
											2021-03-16 15:02:16 +01:00
										 |  |  |     if (vm.exception()) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     if (!year_value.is_finite_number()) { | 
					
						
							|  |  |  |         return create_invalid_date(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     auto year = year_value.as_i32(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-20 01:09:39 +01:00
										 |  |  |     auto month_index_value = vm.argument(1).to_number(global_object); | 
					
						
							| 
									
										
										
										
											2021-03-16 15:02:16 +01:00
										 |  |  |     if (vm.exception()) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     if (!month_index_value.is_finite_number()) { | 
					
						
							|  |  |  |         return create_invalid_date(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     auto month_index = month_index_value.as_i32(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto day_value = arg_or(2, 1); | 
					
						
							|  |  |  |     if (vm.exception()) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     if (!day_value.is_finite_number()) { | 
					
						
							|  |  |  |         return create_invalid_date(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     auto day = day_value.as_i32(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto hours_value = arg_or(3, 0); | 
					
						
							|  |  |  |     if (vm.exception()) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     if (!hours_value.is_finite_number()) { | 
					
						
							|  |  |  |         return create_invalid_date(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     auto hours = hours_value.as_i32(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto minutes_value = arg_or(4, 0); | 
					
						
							|  |  |  |     if (vm.exception()) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     if (!minutes_value.is_finite_number()) { | 
					
						
							|  |  |  |         return create_invalid_date(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     auto minutes = minutes_value.as_i32(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto seconds_value = arg_or(5, 0); | 
					
						
							|  |  |  |     if (vm.exception()) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     if (!seconds_value.is_finite_number()) { | 
					
						
							|  |  |  |         return create_invalid_date(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     auto seconds = seconds_value.as_i32(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto milliseconds_value = arg_or(6, 0); | 
					
						
							|  |  |  |     if (vm.exception()) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     if (!milliseconds_value.is_finite_number()) { | 
					
						
							|  |  |  |         return create_invalid_date(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     auto milliseconds = milliseconds_value.as_i32(); | 
					
						
							| 
									
										
										
										
											2020-08-20 10:41:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-24 09:26:28 -04:00
										 |  |  |     seconds += milliseconds / 1000; | 
					
						
							|  |  |  |     milliseconds %= 1000; | 
					
						
							|  |  |  |     if (milliseconds < 0) { | 
					
						
							|  |  |  |         seconds -= 1; | 
					
						
							|  |  |  |         milliseconds += 1000; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-20 10:41:10 -04:00
										 |  |  |     if (year >= 0 && year <= 99) | 
					
						
							| 
									
										
										
										
											2020-08-21 12:51:00 -04:00
										 |  |  |         year += 1900; | 
					
						
							| 
									
										
										
										
											2020-08-20 10:41:10 -04:00
										 |  |  |     int month = month_index + 1; | 
					
						
							|  |  |  |     auto datetime = Core::DateTime::create(year, month, day, hours, minutes, seconds); | 
					
						
							| 
									
										
										
										
											2021-06-20 01:09:39 +01:00
										 |  |  |     auto time = datetime.timestamp() * 1000.0 + milliseconds; | 
					
						
							|  |  |  |     if (time > Date::time_clip) | 
					
						
							| 
									
										
										
										
											2021-06-06 17:42:49 +03:00
										 |  |  |         return create_invalid_date(); | 
					
						
							| 
									
										
										
										
											2021-09-16 00:57:38 +03:00
										 |  |  |     return TRY_OR_DISCARD(ordinary_create_from_constructor<Date>(global_object, new_target, &GlobalObject::date_prototype, datetime, milliseconds, false)); | 
					
						
							| 
									
										
										
										
											2020-03-30 00:21:56 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 00:22:35 +01:00
										 |  |  | // 21.4.3.1 Date.now ( ), https://tc39.es/ecma262/#sec-date.now
 | 
					
						
							| 
									
										
										
										
											2020-06-20 13:55:34 +02:00
										 |  |  | JS_DEFINE_NATIVE_FUNCTION(DateConstructor::now) | 
					
						
							| 
									
										
										
										
											2020-03-30 00:21:56 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     struct timeval tv; | 
					
						
							|  |  |  |     gettimeofday(&tv, nullptr); | 
					
						
							| 
									
										
										
										
											2021-07-10 18:12:12 +01:00
										 |  |  |     return Value(floor(tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0)); | 
					
						
							| 
									
										
										
										
											2020-03-30 00:21:56 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 00:22:35 +01:00
										 |  |  | // 21.4.3.2 Date.parse ( string ), https://tc39.es/ecma262/#sec-date.parse
 | 
					
						
							| 
									
										
										
										
											2020-08-21 12:51:00 -04:00
										 |  |  | JS_DEFINE_NATIVE_FUNCTION(DateConstructor::parse) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-09-27 18:36:49 +02:00
										 |  |  |     if (!vm.argument_count()) | 
					
						
							| 
									
										
										
										
											2020-08-21 12:51:00 -04:00
										 |  |  |         return js_nan(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-27 18:36:49 +02:00
										 |  |  |     auto iso_8601 = vm.argument(0).to_string(global_object); | 
					
						
							|  |  |  |     if (vm.exception()) | 
					
						
							| 
									
										
										
										
											2021-02-13 12:37:02 +01:00
										 |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2020-08-21 12:51:00 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return parse_simplified_iso8601(iso_8601); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 00:22:35 +01:00
										 |  |  | // 21.4.3.4 Date.UTC ( year [ , month [ , date [ , hours [ , minutes [ , seconds [ , ms ] ] ] ] ] ] ), https://tc39.es/ecma262/#sec-date.utc
 | 
					
						
							| 
									
										
										
										
											2020-08-20 16:02:07 -04:00
										 |  |  | JS_DEFINE_NATIVE_FUNCTION(DateConstructor::utc) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-09-27 18:36:49 +02:00
										 |  |  |     auto arg_or = [&vm, &global_object](size_t i, i32 fallback) { return vm.argument_count() > i ? vm.argument(i).to_i32(global_object) : fallback; }; | 
					
						
							|  |  |  |     int year = vm.argument(0).to_i32(global_object); | 
					
						
							| 
									
										
										
										
											2020-08-20 16:02:07 -04:00
										 |  |  |     if (year >= 0 && year <= 99) | 
					
						
							| 
									
										
										
										
											2020-08-21 12:51:00 -04:00
										 |  |  |         year += 1900; | 
					
						
							| 
									
										
										
										
											2020-08-20 16:02:07 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     struct tm tm = {}; | 
					
						
							|  |  |  |     tm.tm_year = year - 1900; | 
					
						
							|  |  |  |     tm.tm_mon = arg_or(1, 0); // 0-based in both tm and JavaScript
 | 
					
						
							|  |  |  |     tm.tm_mday = arg_or(2, 1); | 
					
						
							|  |  |  |     tm.tm_hour = arg_or(3, 0); | 
					
						
							|  |  |  |     tm.tm_min = arg_or(4, 0); | 
					
						
							|  |  |  |     tm.tm_sec = arg_or(5, 0); | 
					
						
							|  |  |  |     // timegm() doesn't read tm.tm_wday and tm.tm_yday, no need to fill them in.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int milliseconds = arg_or(6, 0); | 
					
						
							|  |  |  |     return Value(1000.0 * timegm(&tm) + milliseconds); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-06-13 00:22:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 00:21:56 +01:00
										 |  |  | } |