Procházet zdrojové kódy

LibJS: Implement Date.parse()

The spec says Date.parse() should accept at least a simplified form
of ISO 8601, so that's all this implements.
Nico Weber před 5 roky
rodič
revize
6e5aa5d5df

+ 117 - 2
Libraries/LibJS/Runtime/DateConstructor.cpp

@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
+ * Copyright (c) 2020, Nico Weber <thakis@chromium.org>
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -24,16 +25,117 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include <AK/GenericLexer.h>
 #include <LibCore/DateTime.h>
 #include <LibJS/Interpreter.h>
 #include <LibJS/Runtime/Date.h>
 #include <LibJS/Runtime/DateConstructor.h>
 #include <LibJS/Runtime/GlobalObject.h>
+#include <ctype.h>
 #include <sys/time.h>
 #include <time.h>
 
 namespace JS {
 
+static Value parse_simplified_iso8601(const String& iso_8601) {
+    // Date.parse() is allowed to accept many formats. We strictly only accept things matching
+    // http://www.ecma-international.org/ecma-262/#sec-date-time-string-format
+    GenericLexer lexer(iso_8601);
+    auto lex_n_digits = [&](size_t n, 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 (!isdigit(ch))
+                return false;
+            r = 10 * r + ch - '0';
+        }
+        out = r;
+        return true;
+    };
+
+    int year = -1, month = -1, day = -1;
+    int hours = -1, minutes = -1, seconds = -1, milliseconds = -1;
+    char timezone = -1;
+    int timezone_hours = -1, timezone_minutes = -1;
+    auto lex_year = [&]() {
+        if (lexer.consume_specific('+'))
+            return lex_n_digits(6, year);
+        if (lexer.consume_specific('-')) {
+            int absolute_year;
+            if (!lex_n_digits(6, absolute_year))
+                return false;
+            year = -absolute_year;
+            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 = [&](int& out_h, int& out_m) {
+        int h, 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 = h;
+            out_m = 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. Values not present in the string are -1.
+    ASSERT(year != -1); // A valid date string always has at least a year.
+    struct tm tm = {};
+    tm.tm_year = year - 1900;
+    tm.tm_mon = month == -1 ? 0 : month - 1;
+    tm.tm_mday = day == -1 ? 1 : day;
+    tm.tm_hour = hours == -1 ? 0 : hours;
+    tm.tm_min = minutes == -1 ? 0 : minutes;
+    tm.tm_sec = seconds == -1 ? 0 : seconds;
+
+    // http://www.ecma-international.org/ecma-262/#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 != -1 || hours == -1)
+        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
+
+    if (milliseconds == -1)
+        milliseconds = 0;
+    return Value(1000.0 * timestamp + milliseconds);
+}
+
 DateConstructor::DateConstructor(GlobalObject& global_object)
     : NativeFunction("Date", *global_object.function_prototype())
 {
@@ -46,6 +148,7 @@ void DateConstructor::initialize(GlobalObject& global_object)
     define_property("length", Value(7), Attribute::Configurable);
 
     define_native_function("now", now, 0, Attribute::Writable | Attribute::Configurable);
+    define_native_function("parse", parse, 1, Attribute::Writable | Attribute::Configurable);
     define_native_function("UTC", utc, 1, Attribute::Writable | Attribute::Configurable);
 }
 
@@ -100,7 +203,7 @@ Value DateConstructor::construct(Interpreter& interpreter, Function&)
     int milliseconds = arg_or(6, 0);
 
     if (year >= 0 && year <= 99)
-      year += 1900;
+        year += 1900;
     int month = month_index + 1;
     auto datetime = Core::DateTime::create(year, month, day, hours, minutes, seconds);
     return Date::create(global_object(), datetime, milliseconds);
@@ -113,12 +216,24 @@ JS_DEFINE_NATIVE_FUNCTION(DateConstructor::now)
     return Value(tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0);
 }
 
+JS_DEFINE_NATIVE_FUNCTION(DateConstructor::parse)
+{
+    if (!interpreter.argument_count())
+        return js_nan();
+
+    auto iso_8601 = interpreter.argument(0).to_string(interpreter);
+    if (interpreter.exception())
+        return js_nan();
+
+    return parse_simplified_iso8601(iso_8601);
+}
+
 JS_DEFINE_NATIVE_FUNCTION(DateConstructor::utc)
 {
     auto arg_or = [&interpreter](size_t i, i32 fallback) { return interpreter.argument_count() > i ? interpreter.argument(i).to_i32(interpreter) : fallback; };
     int year = interpreter.argument(0).to_i32(interpreter);
     if (year >= 0 && year <= 99)
-      year += 1900;
+        year += 1900;
 
     struct tm tm = {};
     tm.tm_year = year - 1900;

+ 1 - 0
Libraries/LibJS/Runtime/DateConstructor.h

@@ -45,6 +45,7 @@ private:
     virtual bool has_constructor() const override { return true; }
 
     JS_DECLARE_NATIVE_FUNCTION(now);
+    JS_DECLARE_NATIVE_FUNCTION(parse);
     JS_DECLARE_NATIVE_FUNCTION(utc);
 };
 

+ 33 - 0
Libraries/LibJS/Tests/builtins/Date/Date.parse.js

@@ -0,0 +1,33 @@
+test("basic functionality", () => {
+    expect(Date.parse("2020")).toBe(1577836800000);
+    expect(Date.parse("2000-11")).toBe(973036800000);
+    expect(Date.parse("1980-06-30")).toBe(331171200000);
+    expect(Date.parse("1970-06-30T13:30Z")).toBe(15600600000);
+    expect(Date.parse("1970-01-01T00:00:59Z")).toBe(59000);
+    expect(Date.parse("1970-01-01T00:00:00.999Z")).toBe(999);
+    expect(Date.parse("2020T13:14+15:16")).toBe(1577829480000);
+    expect(Date.parse("2020T13:14-15:16")).toBe(1577939400000);
+    expect(Date.parse("2020T23:59Z")).toBe(1577923140000);
+
+    // FIXME: Real extended year tests are blocked on better time_t handling in LibC.
+    expect(Date.parse("+002020")).toBe(1577836800000);
+    expect(Date.parse("+002000-11")).toBe(973036800000);
+    expect(Date.parse("+002020T23:59Z")).toBe(1577923140000);
+
+    expect(Date.parse(2020)).toBe(1577836800000);
+
+    expect(Date.parse("+1980")).toBe(NaN);
+    expect(Date.parse("1980-")).toBe(NaN);
+    expect(Date.parse("1980-05-")).toBe(NaN);
+    expect(Date.parse("1980-05-00T")).toBe(NaN);
+    expect(Date.parse("1980-05-00T15:15:")).toBe(NaN);
+    expect(Date.parse("1980-05-00T15:15:15.")).toBe(NaN);
+    expect(Date.parse("1980-5-30")).toBe(NaN);
+    expect(Date.parse("1980-05-30T13")).toBe(NaN);
+    expect(Date.parse("1980-05-30T13:4")).toBe(NaN);
+    expect(Date.parse("1980-05-30T13:40+")).toBe(NaN);
+    expect(Date.parse("1980-05-30T13:40+1")).toBe(NaN);
+    expect(Date.parse("1980-05-30T13:40+1:10")).toBe(NaN);
+    expect(Date.parse("1970-06-30T13:30Zoo")).toBe(NaN);
+    expect(Date.parse("2020T13:30.40:")).toBe(NaN);
+});