Skip to content

Commit 90f1a78

Browse files
committed
Implement Date.prototype.toDateString and Date.prototype.toTimeString
JerryScript-DCO-1.0-Signed-off-by: László Langó [email protected]
1 parent 13cf314 commit 90f1a78

File tree

2 files changed

+118
-5
lines changed

2 files changed

+118
-5
lines changed

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

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,57 @@ ecma_builtin_date_prototype_to_string (ecma_value_t this_arg) /**< this argument
174174
static ecma_completion_value_t
175175
ecma_builtin_date_prototype_to_date_string (ecma_value_t this_arg) /**< this argument */
176176
{
177-
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg);
177+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
178+
179+
if (!ecma_is_value_object (this_arg))
180+
{
181+
ret_value = ecma_raise_type_error ("Incompatible type");
182+
}
183+
else if (ecma_object_get_class_name (ecma_get_object_from_value (this_arg)) != LIT_MAGIC_STRING_DATE_UL)
184+
{
185+
ret_value = ecma_raise_type_error ("Incompatible type");
186+
}
187+
else
188+
{
189+
ECMA_TRY_CATCH (obj_this,
190+
ecma_op_to_object (this_arg),
191+
ret_value);
192+
193+
ecma_object_t *obj_p = ecma_get_object_from_value (obj_this);
194+
ecma_property_t *prim_value_prop_p;
195+
prim_value_prop_p = ecma_get_internal_property (obj_p, ECMA_INTERNAL_PROPERTY_PRIMITIVE_NUMBER_VALUE);
196+
ecma_number_t *prim_value_num_p = ECMA_GET_NON_NULL_POINTER (ecma_number_t,
197+
prim_value_prop_p->u.internal_property.value);
198+
199+
if (ecma_number_is_nan (*prim_value_num_p))
200+
{
201+
ecma_string_t *magic_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_INVALID_DATE_UL);
202+
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (magic_str_p));
203+
}
204+
else
205+
{
206+
ecma_number_t day = ecma_date_date_from_time (*prim_value_num_p);
207+
ecma_string_t *output_str_p = ecma_new_ecma_string_from_number (day);
208+
ecma_date_insert_leading_zeros (&output_str_p, day, 2);
209+
210+
/*
211+
* Note:
212+
* 'ecma_date_month_from_time' (ECMA 262 v5, 15.9.1.4) returns a number from 0 to 11,
213+
* but we have to print the month from 1 to 12 for ISO 8601 standard (ECMA 262 v5, 15.9.1.15).
214+
*/
215+
ecma_number_t month = ecma_date_month_from_time (*prim_value_num_p) + 1;
216+
ecma_date_insert_num_with_sep (&output_str_p, month, LIT_MAGIC_STRING_MINUS_CHAR, 2);
217+
218+
ecma_number_t year = ecma_date_year_from_time (*prim_value_num_p);
219+
ecma_date_insert_num_with_sep (&output_str_p, year, LIT_MAGIC_STRING_MINUS_CHAR, 4);
220+
221+
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (output_str_p));
222+
}
223+
224+
ECMA_FINALIZE (obj_this);
225+
}
226+
227+
return ret_value;
178228
} /* ecma_builtin_date_prototype_to_date_string */
179229

180230
/**
@@ -189,7 +239,51 @@ ecma_builtin_date_prototype_to_date_string (ecma_value_t this_arg) /**< this arg
189239
static ecma_completion_value_t
190240
ecma_builtin_date_prototype_to_time_string (ecma_value_t this_arg) /**< this argument */
191241
{
192-
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg);
242+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
243+
244+
if (ecma_object_get_class_name (ecma_get_object_from_value (this_arg)) != LIT_MAGIC_STRING_DATE_UL)
245+
{
246+
ret_value = ecma_raise_type_error ("Incomplete Date type");
247+
}
248+
else
249+
{
250+
ECMA_TRY_CATCH (obj_this,
251+
ecma_op_to_object (this_arg),
252+
ret_value);
253+
254+
ecma_object_t *obj_p = ecma_get_object_from_value (obj_this);
255+
ecma_property_t *prim_value_prop_p;
256+
prim_value_prop_p = ecma_get_internal_property (obj_p, ECMA_INTERNAL_PROPERTY_PRIMITIVE_NUMBER_VALUE);
257+
ecma_number_t *prim_value_num_p = ECMA_GET_NON_NULL_POINTER (ecma_number_t,
258+
prim_value_prop_p->u.internal_property.value);
259+
260+
if (ecma_number_is_nan (*prim_value_num_p))
261+
{
262+
ecma_string_t *magic_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_INVALID_DATE_UL);
263+
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (magic_str_p));
264+
}
265+
else
266+
{
267+
ecma_number_t milliseconds = ecma_date_ms_from_time (*prim_value_num_p);
268+
ecma_string_t *output_str_p = ecma_new_ecma_string_from_number (milliseconds);
269+
ecma_date_insert_leading_zeros (&output_str_p, milliseconds, 3);
270+
271+
ecma_number_t seconds = ecma_date_sec_from_time (*prim_value_num_p);
272+
ecma_date_insert_num_with_sep (&output_str_p, seconds, LIT_MAGIC_STRING_DOT_CHAR, 2);
273+
274+
ecma_number_t minutes = ecma_date_min_from_time (*prim_value_num_p);
275+
ecma_date_insert_num_with_sep (&output_str_p, minutes, LIT_MAGIC_STRING_COLON_CHAR, 2);
276+
277+
ecma_number_t hours = ecma_date_hour_from_time (*prim_value_num_p);
278+
ecma_date_insert_num_with_sep (&output_str_p, hours, LIT_MAGIC_STRING_COLON_CHAR, 2);
279+
280+
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (output_str_p));
281+
}
282+
283+
ECMA_FINALIZE (obj_this);
284+
}
285+
286+
return ret_value;
193287
} /* ecma_builtin_date_prototype_to_time_string */
194288

195289
/**

tests/jerry/date-tostring.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,25 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
assert (new Date(NaN) == "Invalid Date");
17-
assert (new Date("2015-02-13") == "2015-02-13T00:00:00.000");
18-
assert (new Date("2015-07-08T11:29:05.023") == "2015-07-08T11:29:05.023");
16+
assert (new Date (NaN) == "Invalid Date");
17+
assert (new Date ("2015-02-13") == "2015-02-13T00:00:00.000");
18+
assert (new Date ("2015-07-08T11:29:05.023") == "2015-07-08T11:29:05.023");
19+
20+
assert (new Date (NaN).toDateString () == "Invalid Date");
21+
assert (new Date ("2015-02-13").toDateString () == "2015-02-13");
22+
assert (new Date ("2015-07-08T11:29:05.023").toDateString () == "2015-07-08");
23+
24+
assert (new Date (NaN).toTimeString () == "Invalid Date");
25+
assert (new Date ("2015-02-13").toTimeString () == "00:00:00.000");
26+
assert (new Date ("2015-07-08T11:29:05.023").toTimeString () == "11:29:05.023");
27+
28+
try
29+
{
30+
Date.prototype.toDateString.call(-1);
31+
assert (false);
32+
}
33+
catch (e)
34+
{
35+
assert (e instanceof TypeError);
36+
assert (e.message === "Incompatible type");
37+
}

0 commit comments

Comments
 (0)