1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
/*
* Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2020, Nico Weber <thakis@chromium.org>
* Copyright (c) 2021, Petróczi Zoltán <petroczizoltan@tutanota.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/CharacterTypes.h>
#include <AK/GenericLexer.h>
#include <LibCore/DateTime.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Date.h>
#include <LibJS/Runtime/DateConstructor.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/VM.h>
#include <sys/time.h>
#include <time.h>
namespace JS {
// 21.4.3.2 Date.parse ( string ), https://tc39.es/ecma262/#sec-date.parse
static Value parse_simplified_iso8601(const String& iso_8601)
{
// 21.4.1.15 Date Time String Format, https://tc39.es/ecma262/#sec-date-time-string-format
GenericLexer lexer(iso_8601);
auto lex_n_digits = [&](size_t n, Optional<int>& out) {
if (lexer.tell_remaining() < n)
return false;
int r = 0;
for (size_t i = 0; i < n; ++i) {
char ch = lexer.consume();
if (!is_ascii_digit(ch))
return false;
r = 10 * r + ch - '0';
}
out = r;
return true;
};
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;
auto lex_year = [&]() {
if (lexer.consume_specific('+'))
return lex_n_digits(6, year);
if (lexer.consume_specific('-')) {
Optional<int> absolute_year;
if (!lex_n_digits(6, absolute_year))
return false;
year = -absolute_year.value();
return true;
}
return lex_n_digits(4, year);
};
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; };
auto lex_date = [&]() { return lex_year() && (!lexer.consume_specific('-') || (lex_month() && (!lexer.consume_specific('-') || lex_day()))); };
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);
return true;
}
return false;
};
auto lex_seconds = [&]() { return lex_n_digits(2, seconds) && *seconds >= 0 && *seconds <= 59; };
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();
}
// We parsed a valid date simplified ISO 8601 string.
VERIFY(year.has_value()); // A valid date string always has at least a year.
struct tm tm = {};
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);
// https://tc39.es/ecma262/#sec-date.parse:
// "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;
if (timezone.has_value() || !hours.has_value())
timestamp = timegm(&tm);
else
timestamp = mktime(&tm);
if (timezone == '-')
timestamp += (*timezone_hours * 60 + *timezone_minutes) * 60;
else if (timezone == '+')
timestamp -= (*timezone_hours * 60 + *timezone_minutes) * 60;
// FIXME: reject timestamp if resulting value wouldn't fit in a double
return Value(1000.0 * timestamp + milliseconds.value_or(0));
}
static Value parse_date_string(String const& date_string)
{
auto value = parse_simplified_iso8601(date_string);
if (value.is_finite_number())
return value;
// Date.parse() is allowed to accept an arbitrary number of implementation-defined formats.
// Parse formats of this type: "Wed Apr 17 23:08:53 +0000 2019"
auto maybe_datetime = Core::DateTime::parse("%a %b %e %T %z %Y", date_string);
if (maybe_datetime.has_value())
return Value(1000.0 * maybe_datetime.value().timestamp());
return js_nan();
}
DateConstructor::DateConstructor(GlobalObject& global_object)
: NativeFunction(vm().names.Date.as_string(), *global_object.function_prototype())
{
}
void DateConstructor::initialize(GlobalObject& global_object)
{
auto& vm = this->vm();
NativeFunction::initialize(global_object);
// 21.4.3.3 Date.prototype, https://tc39.es/ecma262/#sec-date.prototype
define_direct_property(vm.names.prototype, global_object.date_prototype(), 0);
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);
define_direct_property(vm.names.length, Value(7), Attribute::Configurable);
}
DateConstructor::~DateConstructor()
{
}
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 };
}
// 21.4.2.1 Date ( ...values ), https://tc39.es/ecma262/#sec-date
Value DateConstructor::call()
{
auto [datetime, milliseconds] = JS::now();
auto* date = Date::create(global_object(), datetime, milliseconds, false);
return js_string(heap(), date->string());
}
// 21.4.2.1 Date ( ...values ), https://tc39.es/ecma262/#sec-date
Value DateConstructor::construct(FunctionObject& new_target)
{
auto& vm = this->vm();
auto& global_object = this->global_object();
if (vm.argument_count() == 0) {
auto [datetime, milliseconds] = JS::now();
return TRY_OR_DISCARD(ordinary_create_from_constructor<Date>(global_object, new_target, &GlobalObject::date_prototype, datetime, milliseconds, false));
}
auto create_invalid_date = [&global_object, &new_target]() -> Date* {
auto datetime = Core::DateTime::create(1970, 1, 1, 0, 0, 0);
auto milliseconds = static_cast<i16>(0);
return TRY_OR_DISCARD(ordinary_create_from_constructor<Date>(global_object, new_target, &GlobalObject::date_prototype, datetime, milliseconds, true));
};
if (vm.argument_count() == 1) {
auto value = vm.argument(0);
if (value.is_string())
value = parse_date_string(value.as_string().string());
else
value = TRY_OR_DISCARD(value.to_number(global_object));
if (!value.is_finite_number()) {
return create_invalid_date();
}
// A timestamp since the epoch, in UTC.
double value_as_double = value.as_double();
if (value_as_double > Date::time_clip)
return create_invalid_date();
auto datetime = Core::DateTime::from_timestamp(static_cast<time_t>(value_as_double / 1000));
auto milliseconds = static_cast<i16>(fmod(value_as_double, 1000));
return TRY_OR_DISCARD(ordinary_create_from_constructor<Date>(global_object, new_target, &GlobalObject::date_prototype, datetime, milliseconds, false));
}
// A date/time in components, in local time.
auto arg_or = [&vm, &global_object](size_t i, i32 fallback) -> ThrowCompletionOr<Value> {
return vm.argument_count() > i ? vm.argument(i).to_number(global_object) : Value(fallback);
};
auto year_value = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
if (!year_value.is_finite_number()) {
return create_invalid_date();
}
auto year = year_value.as_i32();
auto month_index_value = TRY_OR_DISCARD(vm.argument(1).to_number(global_object));
if (!month_index_value.is_finite_number()) {
return create_invalid_date();
}
auto month_index = month_index_value.as_i32();
auto day_value = TRY_OR_DISCARD(arg_or(2, 1));
if (!day_value.is_finite_number()) {
return create_invalid_date();
}
auto day = day_value.as_i32();
auto hours_value = TRY_OR_DISCARD(arg_or(3, 0));
if (!hours_value.is_finite_number()) {
return create_invalid_date();
}
auto hours = hours_value.as_i32();
auto minutes_value = TRY_OR_DISCARD(arg_or(4, 0));
if (!minutes_value.is_finite_number()) {
return create_invalid_date();
}
auto minutes = minutes_value.as_i32();
auto seconds_value = TRY_OR_DISCARD(arg_or(5, 0));
if (!seconds_value.is_finite_number()) {
return create_invalid_date();
}
auto seconds = seconds_value.as_i32();
auto milliseconds_value = TRY_OR_DISCARD(arg_or(6, 0));
if (!milliseconds_value.is_finite_number()) {
return create_invalid_date();
}
auto milliseconds = milliseconds_value.as_i32();
seconds += milliseconds / 1000;
milliseconds %= 1000;
if (milliseconds < 0) {
seconds -= 1;
milliseconds += 1000;
}
if (year >= 0 && year <= 99)
year += 1900;
int month = month_index + 1;
auto datetime = Core::DateTime::create(year, month, day, hours, minutes, seconds);
auto time = datetime.timestamp() * 1000.0 + milliseconds;
if (time > Date::time_clip)
return create_invalid_date();
return TRY_OR_DISCARD(ordinary_create_from_constructor<Date>(global_object, new_target, &GlobalObject::date_prototype, datetime, milliseconds, false));
}
// 21.4.3.1 Date.now ( ), https://tc39.es/ecma262/#sec-date.now
JS_DEFINE_NATIVE_FUNCTION(DateConstructor::now)
{
struct timeval tv;
gettimeofday(&tv, nullptr);
return Value(floor(tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0));
}
// 21.4.3.2 Date.parse ( string ), https://tc39.es/ecma262/#sec-date.parse
JS_DEFINE_NATIVE_FUNCTION(DateConstructor::parse)
{
if (!vm.argument_count())
return js_nan();
auto date_string = TRY_OR_DISCARD(vm.argument(0).to_string(global_object));
return parse_date_string(date_string);
}
// 21.4.3.4 Date.UTC ( year [ , month [ , date [ , hours [ , minutes [ , seconds [ , ms ] ] ] ] ] ] ), https://tc39.es/ecma262/#sec-date.utc
JS_DEFINE_NATIVE_FUNCTION(DateConstructor::utc)
{
auto arg_or = [&vm, &global_object](size_t i, i32 fallback) -> ThrowCompletionOr<i32> {
return vm.argument_count() > i ? vm.argument(i).to_i32(global_object) : fallback;
};
int year = TRY_OR_DISCARD(vm.argument(0).to_i32(global_object));
if (year >= 0 && year <= 99)
year += 1900;
struct tm tm = {};
tm.tm_year = year - 1900;
tm.tm_mon = TRY_OR_DISCARD(arg_or(1, 0)); // 0-based in both tm and JavaScript
tm.tm_mday = TRY_OR_DISCARD(arg_or(2, 1));
tm.tm_hour = TRY_OR_DISCARD(arg_or(3, 0));
tm.tm_min = TRY_OR_DISCARD(arg_or(4, 0));
tm.tm_sec = TRY_OR_DISCARD(arg_or(5, 0));
// timegm() doesn't read tm.tm_wday and tm.tm_yday, no need to fill them in.
int milliseconds = TRY_OR_DISCARD(arg_or(6, 0));
return Value(1000.0 * timegm(&tm) + milliseconds);
}
}
|