[njs] Fixed Date.parse() with ISO-8601 date-only forms.

Dmitry Volyntsev xeioex at nginx.com
Tue Jun 13 05:05:13 UTC 2023


details:   https://hg.nginx.org/njs/rev/803a5061b362
branches:  
changeset: 2153:803a5061b362
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon Jun 12 20:51:54 2023 -0700
description:
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.

diffstat:

 src/njs_date.c           |  7 ++++---
 src/test/njs_unit_test.c |  7 +++++++
 2 files changed, 11 insertions(+), 3 deletions(-)

diffs (55 lines):

diff -r fa51408510ad -r 803a5061b362 src/njs_date.c
--- a/src/njs_date.c	Tue Jun 06 21:33:46 2023 -0700
+++ b/src/njs_date.c	Mon Jun 12 20:51:54 2023 -0700
@@ -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 -r fa51408510ad -r 803a5061b362 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Tue Jun 06 21:33:46 2023 -0700
+++ b/src/test/njs_unit_test.c	Mon Jun 12 20:51:54 2023 -0700
@@ -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"),


More information about the nginx-devel mailing list