From: Dmitry Volyntsev Date: Tue, 13 Jun 2023 03:51:54 +0000 (-0700) Subject: Fixed Date.parse() with ISO-8601 date-only forms. X-Git-Url: http://git.kaiwu.me/postgresql/log/contrib/postgres_fdw/static/gitweb.js?a=commitdiff_plain;h=6eb71deff3f2239fb9567f155c5b6b4ae758f398;p=njs.git Fixed Date.parse() with ISO-8601 date-only forms. According to the spec 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. --- diff --git a/src/njs_date.c b/src/njs_date.c index 7df50725..b8eab004 100644 --- a/src/njs_date.c +++ b/src/njs_date.c @@ -530,6 +530,8 @@ njs_date_string_parse(njs_value_t *date) next = njs_date_number_parse(&tm[NJS_DATE_YR], p, end, 4); if (next != NULL) { + utc = 1; + /* ISO-8601 format: "1970-09-28T06:00:00.000Z" */ if (next == end) { @@ -592,7 +594,6 @@ njs_date_string_parse(njs_value_t *date) return NAN; } - utc = 1; end--; if (*end != 'Z') { @@ -636,6 +637,8 @@ njs_date_string_parse(njs_value_t *date) tm[NJS_DATE_MSEC] *= 10; } +done: + return njs_make_date(tm, !utc); } @@ -683,8 +686,6 @@ njs_date_string_parse(njs_value_t *date) week = 0; } -done: - return njs_make_date(tm, 0); } diff --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c index d09275ce..ad47940e 100644 --- a/src/test/njs_unit_test.c +++ b/src/test/njs_unit_test.c @@ -16178,6 +16178,13 @@ static njs_unit_test_t njs_test[] = { njs_str("Date.parse('2011-06-24T06:01:02.625555Z5')"), njs_str("NaN") }, + { njs_str("var tzoffzet = new Date(0).getTimezoneOffset() * 60000;" + "Date.parse('1970-01-01T00:00:00') == tzoffzet"), + njs_str("true") }, + + { njs_str("Date.parse('1970-01-01')"), + njs_str("0") }, + { njs_str("var d = new Date(); var str = d.toISOString();" "var diff = Date.parse(str) - Date.parse(str.substring(0, str.length - 1));" "d.getTimezoneOffset() == -diff/1000/60"),