Skip to content

Commit cadc8f4

Browse files
committed
Implement Date.parse
JerryScript-DCO-1.0-Signed-off-by: László Langó [email protected]
1 parent 2bfb9b9 commit cadc8f4

File tree

3 files changed

+338
-3
lines changed

3 files changed

+338
-3
lines changed

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

Lines changed: 238 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
*/
1616

1717
#include "ecma-alloc.h"
18+
#include "ecma-builtin-helpers.h"
19+
#include "ecma-conversion.h"
1820
#include "ecma-globals.h"
1921
#include "ecma-helpers.h"
22+
#include "ecma-try-catch-macro.h"
2023

2124
#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_DATE_BUILTIN
2225

@@ -37,20 +40,253 @@
3740
* @{
3841
*/
3942

43+
/**
44+
* Helper function to try to parse a part of a date string
45+
*
46+
* @return NaN if cannot read from string, ToNumber() otherwise
47+
*/
48+
static ecma_number_t
49+
ecma_date_parse_date_chars (lit_utf8_iterator_t *iter, /**< iterator of the utf8 string */
50+
uint32_t num_of_chars) /**< number of characters to read and convert */
51+
{
52+
JERRY_ASSERT (num_of_chars > 0);
53+
54+
lit_utf8_size_t copy_size = 0;
55+
const lit_utf8_byte_t *str_start_p = iter->buf_p + iter->buf_pos.offset;
56+
57+
while (num_of_chars--)
58+
{
59+
if (lit_utf8_iterator_is_eos (iter)
60+
|| !lit_char_is_unicode_digit (lit_utf8_iterator_peek_next (iter)))
61+
{
62+
return ecma_number_make_nan ();
63+
}
64+
65+
copy_size += lit_get_unicode_char_size_by_utf8_first_byte (*(iter->buf_p + iter->buf_pos.offset));
66+
lit_utf8_iterator_incr (iter);
67+
}
68+
69+
return ecma_utf8_string_to_number (str_start_p, copy_size);
70+
} /* ecma_date_parse_date_chars */
71+
4072
/**
4173
* The Date object's 'parse' routine
4274
*
4375
* See also:
4476
* ECMA-262 v5, 15.9.4.2
77+
* ECMA-262 v5, 15.9.1.15
4578
*
4679
* @return completion value
4780
* Returned value must be freed with ecma_free_completion_value.
4881
*/
4982
static ecma_completion_value_t
50-
ecma_builtin_date_parse (ecma_value_t this_arg, /**< this argument */
83+
ecma_builtin_date_parse (ecma_value_t this_arg __attr_unused___, /**< this argument */
5184
ecma_value_t arg) /**< string */
5285
{
53-
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
86+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
87+
ecma_number_t *date_num_p = ecma_alloc_number ();
88+
*date_num_p = ecma_number_make_nan ();
89+
90+
/* Date Time String fromat (ECMA-262 v5, 15.9.1.15) */
91+
ECMA_TRY_CATCH (date_str_value,
92+
ecma_op_to_string (arg),
93+
ret_value);
94+
95+
ecma_string_t *date_str_p = ecma_get_string_from_value (date_str_value);
96+
97+
lit_utf8_size_t date_str_size = ecma_string_get_size (date_str_p);
98+
MEM_DEFINE_LOCAL_ARRAY (date_start_p, date_str_size, lit_utf8_byte_t);
99+
100+
ecma_string_to_utf8_string (date_str_p, date_start_p, (ssize_t) date_str_size);
101+
lit_utf8_iterator_t iter = lit_utf8_iterator_create (date_start_p, date_str_size);
102+
103+
/* 1. read year */
104+
ecma_number_t year = ecma_date_parse_date_chars (&iter, 4);
105+
106+
if (!ecma_number_is_nan (year)
107+
&& year >= 0)
108+
{
109+
ecma_number_t month = ECMA_NUMBER_ONE;
110+
ecma_number_t day = ECMA_NUMBER_ONE;
111+
ecma_number_t time = ECMA_NUMBER_ZERO;
112+
113+
/* 2. read month if any */
114+
if (!lit_utf8_iterator_is_eos (&iter)
115+
&& lit_utf8_iterator_peek_next (&iter) == '-')
116+
{
117+
/* eat up '-' */
118+
lit_utf8_iterator_incr (&iter);
119+
month = ecma_date_parse_date_chars (&iter, 2);
120+
121+
if (month > 12 || month < 1)
122+
{
123+
month = ecma_number_make_nan ();
124+
}
125+
}
126+
127+
/* 3. read day if any */
128+
if (!lit_utf8_iterator_is_eos (&iter)
129+
&& lit_utf8_iterator_peek_next (&iter) == '-')
130+
{
131+
/* eat up '-' */
132+
lit_utf8_iterator_incr (&iter);
133+
day = ecma_date_parse_date_chars (&iter, 2);
134+
135+
if (day < 1 || day > 31)
136+
{
137+
day = ecma_number_make_nan ();
138+
}
139+
}
140+
141+
/* 4. read time if any */
142+
if (!lit_utf8_iterator_is_eos (&iter)
143+
&& lit_utf8_iterator_peek_next (&iter) == 'T')
144+
{
145+
ecma_number_t hours = ECMA_NUMBER_ZERO;
146+
ecma_number_t minutes = ECMA_NUMBER_ZERO;
147+
ecma_number_t seconds = ECMA_NUMBER_ZERO;
148+
ecma_number_t milliseconds = ECMA_NUMBER_ZERO;
149+
150+
ecma_length_t num_of_visited_chars = lit_utf8_iterator_get_index (&iter);
151+
ecma_length_t date_str_len = lit_utf8_string_length (iter.buf_p, iter.buf_size) - 1;
152+
153+
if ((date_str_len - num_of_visited_chars) >= 5)
154+
{
155+
/* eat up 'T' */
156+
lit_utf8_iterator_incr (&iter);
157+
158+
/* 4.1 read hours and minutes */
159+
hours = ecma_date_parse_date_chars (&iter, 2);
160+
161+
if (hours < 0 || hours > 24)
162+
{
163+
hours = ecma_number_make_nan ();
164+
}
165+
else if (hours == 24)
166+
{
167+
hours = ECMA_NUMBER_ZERO;
168+
}
169+
170+
/* eat up ':' */
171+
lit_utf8_iterator_incr (&iter);
172+
173+
minutes = ecma_date_parse_date_chars (&iter, 2);
174+
175+
if (minutes < 0 || minutes > 59)
176+
{
177+
minutes = ecma_number_make_nan ();
178+
}
179+
180+
/* 4.2 read seconds if any */
181+
if (!lit_utf8_iterator_is_eos (&iter) && lit_utf8_iterator_peek_next (&iter) == ':')
182+
{
183+
/* eat up ':' */
184+
lit_utf8_iterator_incr (&iter);
185+
seconds = ecma_date_parse_date_chars (&iter, 2);
186+
187+
if (seconds < 0 || seconds > 59)
188+
{
189+
seconds = ecma_number_make_nan ();
190+
}
191+
192+
/* 4.3 read milliseconds if any */
193+
if (!lit_utf8_iterator_is_eos (&iter) && lit_utf8_iterator_peek_next (&iter) == '.')
194+
{
195+
/* eat up '.' */
196+
lit_utf8_iterator_incr (&iter);
197+
milliseconds = ecma_date_parse_date_chars (&iter, 3);
198+
199+
if (milliseconds < 0)
200+
{
201+
milliseconds = ecma_number_make_nan ();
202+
}
203+
}
204+
}
205+
206+
time = ecma_date_make_time (hours, minutes, seconds, milliseconds);
207+
}
208+
else
209+
{
210+
time = ecma_number_make_nan ();
211+
}
212+
213+
/* 4.4 read timezone if any */
214+
if (!lit_utf8_iterator_is_eos (&iter)
215+
&& lit_utf8_iterator_peek_next (&iter) == 'Z'
216+
&& !ecma_number_is_nan (time))
217+
{
218+
lit_utf8_iterator_incr (&iter);
219+
time = ecma_date_utc (ecma_date_make_time (hours,
220+
minutes,
221+
seconds,
222+
milliseconds));
223+
}
224+
else if (!lit_utf8_iterator_is_eos (&iter)
225+
&& (lit_utf8_iterator_peek_next (&iter) == '+'
226+
|| lit_utf8_iterator_peek_next (&iter) == '-'))
227+
{
228+
ecma_length_t num_of_visited_chars = lit_utf8_iterator_get_index (&iter);
229+
ecma_length_t date_str_len = lit_utf8_string_length (iter.buf_p, iter.buf_size) - 1;
230+
231+
if ((date_str_len - num_of_visited_chars) == 5)
232+
{
233+
bool is_negative = false;
234+
235+
if (lit_utf8_iterator_peek_next (&iter) == '-')
236+
{
237+
is_negative = true;
238+
}
239+
240+
/* eat up '+/-' */
241+
lit_utf8_iterator_incr (&iter);
242+
243+
/* read hours and minutes */
244+
hours = ecma_date_parse_date_chars (&iter, 2);
245+
246+
if (hours < 0 || hours > 24)
247+
{
248+
hours = ecma_number_make_nan ();
249+
}
250+
else if (hours == 24)
251+
{
252+
hours = ECMA_NUMBER_ZERO;
253+
}
254+
255+
/* eat up ':' */
256+
lit_utf8_iterator_incr (&iter);
257+
258+
minutes = ecma_date_parse_date_chars (&iter, 2);
259+
260+
if (minutes < 0 || minutes > 59)
261+
{
262+
minutes = ecma_number_make_nan ();
263+
}
264+
265+
if (is_negative)
266+
{
267+
time += ecma_date_make_time (hours, minutes, ECMA_NUMBER_ZERO, ECMA_NUMBER_ZERO);
268+
}
269+
else
270+
{
271+
time -= ecma_date_make_time (hours, minutes, ECMA_NUMBER_ZERO, ECMA_NUMBER_ZERO);
272+
}
273+
}
274+
}
275+
}
276+
277+
if (lit_utf8_iterator_is_eos (&iter))
278+
{
279+
ecma_number_t date = ecma_date_make_day (year, month - 1, day);
280+
*date_num_p = ecma_date_make_date (date, time);
281+
}
282+
}
283+
284+
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (date_num_p));
285+
286+
MEM_FINALIZE_LOCAL_ARRAY (date_start_p);
287+
ECMA_FINALIZE (date_str_value);
288+
289+
return ret_value;
54290
} /* ecma_builtin_date_parse */
55291

56292
/**

jerry-core/ecma/builtin-objects/ecma-builtin-helpers-date.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ ecma_date_in_leap_year (ecma_number_t time) /**< time value */
215215
return time; /* time is NaN */
216216
}
217217

218-
return ecma_date_days_in_year (ecma_date_time_from_year (time)) - 365;
218+
return ecma_date_days_in_year (ecma_date_year_from_time (time)) - 365;
219219
} /* ecma_date_in_leap_year */
220220

221221
/**

tests/jerry/date-parse.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright 2015 Samsung Electronics Co., Ltd.
2+
// Copyright 2015 University of Szeged.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
var wrongFormats = ["",
17+
"2",
18+
"20",
19+
"201",
20+
"2015-",
21+
"2015-01-",
22+
"2015-01-01-",
23+
"qwerty",
24+
"2015-01-01T",
25+
"2015-01-01T1:1",
26+
"2015-01-01T01",
27+
"2015-01-01T01",
28+
"2015-01-01T01:01F",
29+
"T2015",
30+
"2015-01-01Z",
31+
"2015-01-01+01:00",
32+
"2015-01-01T00:00+01",
33+
"2015-01-01T00:00+1",
34+
"2015-01-01T00:00-01",
35+
"2015-01-01T00:00.000",
36+
"2015-01-01T00:00:",
37+
"2015-01-01T00:",
38+
"2015-01-01T00:00:00.1",
39+
"2015-01-01T00:00:00.01",
40+
"2015-01-01T00:00+01:00Z",
41+
"2015/01/01",
42+
"2015-01-32",
43+
"2015--1",
44+
"2015-13",
45+
"2015-01--1",
46+
"-215",
47+
"-215-01-01",
48+
"2015-01-00",
49+
"2015-01-01T25:00",
50+
"2015-01-01T00:60",
51+
"2015-01-01T-1:00",
52+
"2015-01-01T00:-1",
53+
"2e+3"];
54+
55+
for (i in wrongFormats) {
56+
var d = Date.parse(wrongFormats[i]);
57+
assert (isNaN(d));
58+
}
59+
60+
var d;
61+
62+
d = Date.parse(undefined);
63+
assert (isNaN(d));
64+
65+
d = Date.parse({});
66+
assert (isNaN(d));
67+
68+
d = Date.parse(2000 + 15);
69+
assert (d == 1420070400000);
70+
71+
d = Date.parse("2015");
72+
assert (d == 1420070400000);
73+
74+
d = Date.parse("2015-01");
75+
assert (d == 1420070400000);
76+
77+
d = Date.parse("2015-01-01");
78+
assert (d == 1420070400000);
79+
80+
d = Date.parse("2015-01T00:00");
81+
assert (d == 1420070400000);
82+
83+
d = Date.parse("2015-01T00:00:00");
84+
assert (d == 1420070400000);
85+
86+
d = Date.parse("2015-01T00:00:00.000");
87+
assert (d == 1420070400000);
88+
89+
d = Date.parse("2015-01T24:00:00.000");
90+
assert (d == 1420070400000);
91+
92+
d = Date.parse("2015-01T00:00:00.000+03:00");
93+
assert (d == 1420059600000);
94+
95+
d = Date.parse("2015-01T00:00:00.000-03:00");
96+
assert (d == 1420081200000);
97+
98+
d = Date.parse("2015-07-03T14:35:43.123+01:30");
99+
assert (d == 1435928743123);

0 commit comments

Comments
 (0)