Skip to content

Commit e9664b7

Browse files
ossy-szegeddbatyai
authored andcommitted
Make Date.parse accept extended years format too (#3177)
ES5.1 15.9.4.2 specifies that Date.parse (string) has to accept at least Date Time String Format: YYYY-MM-DDTHH:mm:ss.sssZ. But the spec allows implementation-specific fallbacks. Additionally ES5.1 15.9.1.15.1 specifies Extended years format, but isn't explicitly required to be accepted by Date.parse. But ES6 already clarified that Date.parse has to accept extended years format too. JerryScript-DCO-1.0-Signed-off-by: Csaba Osztrogonác [email protected]
1 parent 3763ac8 commit e9664b7

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-date.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,27 @@ ecma_builtin_date_parse (ecma_value_t this_arg, /**< this argument */
199199
const lit_utf8_byte_t *date_str_end_p = date_start_p + date_start_size;
200200

201201
/* 1. read year */
202-
ecma_number_t year = ecma_date_parse_date_chars (&date_str_curr_p, date_str_end_p, 4);
203202

204-
if (!ecma_number_is_nan (year)
205-
&& year >= 0)
203+
uint32_t year_digits = 4;
204+
bool year_sign = false; /* false: positive, true: negative */
205+
if (*date_str_curr_p == '-' || *date_str_curr_p == '+')
206+
{
207+
year_digits = 6;
208+
if (*date_str_curr_p == '-')
209+
{
210+
year_sign = true;
211+
}
212+
/* eat up '-' or '+' */
213+
date_str_curr_p++;
214+
}
215+
216+
ecma_number_t year = ecma_date_parse_date_chars (&date_str_curr_p, date_str_end_p, year_digits);
217+
if (year_sign)
218+
{
219+
year = -year;
220+
}
221+
222+
if (!ecma_number_is_nan (year))
206223
{
207224
ecma_number_t month = ECMA_NUMBER_ONE;
208225
ecma_number_t day = ECMA_NUMBER_ONE;

tests/jerry/date-parse.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,14 @@ var wrongFormats = ["",
4949
"2015-01-01T00:60",
5050
"2015-01-01T-1:00",
5151
"2015-01-01T00:-1",
52-
"2e+3"];
52+
"2e+3",
53+
"+2015-01-01",
54+
"-2015-01-01",
55+
"+02015-01-01",
56+
"-02015-01-01",
57+
"002015-01-01",
58+
"+0002015-01-01",
59+
"-0002015-01-01"];
5360

5461
for (i in wrongFormats) {
5562
var d = Date.parse(wrongFormats[i]);
@@ -96,3 +103,20 @@ assert (d == 1420081200000);
96103

97104
d = Date.parse("2015-07-03T14:35:43.123+01:30");
98105
assert (d == 1435928743123);
106+
107+
assert (Date.parse("-271821-04-20T00:00:00.000Z") == -8640000000000000)
108+
assert (Date.parse("-000001-12-31T23:59:59.999Z") == -62167219200001)
109+
assert (Date.parse("0000-01-01T00:00:00.000Z") == -62167219200000)
110+
assert (Date.parse("0009-12-31T23:59:59.999Z") == -61851600000001)
111+
assert (Date.parse("0010-01-01T00:00:00.000Z") == -61851600000000)
112+
assert (Date.parse("0099-12-31T23:59:59.999Z") == -59011459200001)
113+
assert (Date.parse("0100-01-01T00:00:00.000Z") == -59011459200000)
114+
assert (Date.parse("0999-12-31T23:59:59.999Z") == -30610224000001)
115+
assert (Date.parse("1000-01-01T00:00:00.000Z") == -30610224000000)
116+
assert (Date.parse("1969-12-31T23:59:59.999Z") == -1)
117+
assert (Date.parse("1970-01-01T00:00:00.000Z") == 0)
118+
assert (Date.parse("1970-01-01T00:00:00.001Z") == 1)
119+
assert (Date.parse("9999-12-31T23:59:59.999Z") == 253402300799999)
120+
assert (Date.parse("+010000-01-01T00:00:00.000Z") == 253402300800000)
121+
assert (Date.parse("+275760-09-13T00:00:00.000Z") == 8640000000000000)
122+
assert (Date.parse("+275760-09-13T00:00:00.001Z") == 8640000000000001)

0 commit comments

Comments
 (0)