Skip to content

Commit a500c3b

Browse files
author
Roland Takacs
committed
Implement Date.prototype.toISOString()
JerryScript-DCO-1.0-Signed-off-by: Roland Takacs rtakacs.u-szeged@partner.samsung.com
1 parent b414329 commit a500c3b

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,16 @@ ecma_date_insert_num_with_sep (ecma_string_t **str_p, /**< input/output string *
9292
} /* ecma_date_insert_num_with_sep */
9393

9494
/**
95-
* The Date.prototype object's 'toString' routine
95+
* The Date.prototype object's 'toISOString' routine
9696
*
9797
* See also:
98-
* ECMA-262 v5, 15.9.5.2
98+
* ECMA-262 v5, 15.9.5.43
9999
*
100100
* @return completion value
101101
* Returned value must be freed with ecma_free_completion_value.
102102
*/
103103
static ecma_completion_value_t
104-
ecma_builtin_date_prototype_to_string (ecma_value_t this_arg) /**< this argument */
104+
ecma_builtin_date_prototype_to_iso_string (ecma_value_t this_arg) /**< this argument */
105105
{
106106
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
107107

@@ -130,8 +130,8 @@ ecma_builtin_date_prototype_to_string (ecma_value_t this_arg) /**< this argument
130130
else
131131
{
132132
ecma_number_t milliseconds = ecma_date_ms_from_time (*prim_value_num_p);
133-
ecma_string_t *output_str_p = ecma_new_ecma_string_from_number (milliseconds);
134-
ecma_date_insert_leading_zeros (&output_str_p, milliseconds, 3);
133+
ecma_string_t *output_str_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY);
134+
ecma_date_insert_num_with_sep (&output_str_p, milliseconds, LIT_MAGIC_STRING_UTC_TIME_ZONE_U, 3);
135135

136136
ecma_number_t seconds = ecma_date_sec_from_time (*prim_value_num_p);
137137
ecma_date_insert_num_with_sep (&output_str_p, seconds, LIT_MAGIC_STRING_DOT_CHAR, 2);
@@ -163,6 +163,21 @@ ecma_builtin_date_prototype_to_string (ecma_value_t this_arg) /**< this argument
163163
}
164164

165165
return ret_value;
166+
} /* ecma_builtin_date_prototype_to_iso_string */
167+
168+
/**
169+
* The Date.prototype object's 'toString' routine
170+
*
171+
* See also:
172+
* ECMA-262 v5, 15.9.5.2
173+
*
174+
* @return completion value
175+
* Returned value must be freed with ecma_free_completion_value.
176+
*/
177+
static ecma_completion_value_t
178+
ecma_builtin_date_prototype_to_string (ecma_value_t this_arg) /**< this argument */
179+
{
180+
return ecma_builtin_date_prototype_to_iso_string (this_arg);
166181
} /* ecma_builtin_date_prototype_to_string */
167182

168183
/**
@@ -1195,21 +1210,6 @@ ecma_builtin_date_prototype_to_utc_string (ecma_value_t this_arg) /**< this argu
11951210
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg);
11961211
} /* ecma_builtin_date_prototype_to_utc_string */
11971212

1198-
/**
1199-
* The Date.prototype object's 'toISOString' routine
1200-
*
1201-
* See also:
1202-
* ECMA-262 v5, 15.9.5.43
1203-
*
1204-
* @return completion value
1205-
* Returned value must be freed with ecma_free_completion_value.
1206-
*/
1207-
static ecma_completion_value_t
1208-
ecma_builtin_date_prototype_to_iso_string (ecma_value_t this_arg) /**< this argument */
1209-
{
1210-
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg);
1211-
} /* ecma_builtin_date_prototype_to_iso_string */
1212-
12131213
/**
12141214
* The Date.prototype object's 'toJSON' routine
12151215
*

jerry-core/lit/lit-magic-strings.inc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_G_CHAR, "g")
219219
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_I_CHAR, "i")
220220
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_M_CHAR, "m")
221221
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_TIME_SEP_U, "T")
222+
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_UTC_TIME_ZONE_U, "Z")
222223
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SLASH_CHAR, "/")
223224
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_BACKSLASH_CHAR, "\\")
224225
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_EMPTY_NON_CAPTURE_GROUP, "(?:)")

tests/jerry/date-tostring.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
// limitations under the License.
1515

1616
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");
17+
assert (new Date ("2015-02-13") == "2015-02-13T00:00:00.000Z");
18+
assert (new Date ("2015-07-08T11:29:05.023") == "2015-07-08T11:29:05.023Z");
1919

2020
try
2121
{
@@ -57,3 +57,18 @@ catch (e)
5757
assert (e instanceof TypeError);
5858
assert (e.message === "Incompatible type");
5959
}
60+
61+
assert (new Date (NaN).toISOString () == "Invalid Date");
62+
assert (new Date ("2015-07-16").toISOString () == "2015-07-16T00:00:00.000Z");
63+
assert (new Date ("2015-07-16T11:29:05.023").toISOString () == "2015-07-16T11:29:05.023Z");
64+
65+
try
66+
{
67+
Date.prototype.toISOString.call(-1);
68+
assert (false);
69+
}
70+
catch (e)
71+
{
72+
assert (e instanceof TypeError);
73+
assert (e.message === "Incompatible type");
74+
}

0 commit comments

Comments
 (0)