@@ -105,9 +105,13 @@ ecma_builtin_date_prototype_to_string (ecma_value_t this_arg) /**< this argument
105
105
{
106
106
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
107
107
108
- if (ecma_object_get_class_name ( ecma_get_object_from_value ( this_arg)) != LIT_MAGIC_STRING_DATE_UL )
108
+ if (! ecma_is_value_object ( this_arg))
109
109
{
110
- ret_value = ecma_raise_type_error (" Incomplete Date type" );
110
+ ret_value = ecma_raise_type_error (" Incompatible type" );
111
+ }
112
+ else if (ecma_object_get_class_name (ecma_get_object_from_value (this_arg)) != LIT_MAGIC_STRING_DATE_UL)
113
+ {
114
+ ret_value = ecma_raise_type_error (" Incompatible type" );
111
115
}
112
116
else
113
117
{
@@ -176,7 +180,57 @@ ecma_builtin_date_prototype_to_string (ecma_value_t this_arg) /**< this argument
176
180
static ecma_completion_value_t
177
181
ecma_builtin_date_prototype_to_date_string (ecma_value_t this_arg) /* *< this argument */
178
182
{
179
- ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg);
183
+ ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
184
+
185
+ if (!ecma_is_value_object (this_arg))
186
+ {
187
+ ret_value = ecma_raise_type_error (" Incompatible type" );
188
+ }
189
+ else if (ecma_object_get_class_name (ecma_get_object_from_value (this_arg)) != LIT_MAGIC_STRING_DATE_UL)
190
+ {
191
+ ret_value = ecma_raise_type_error (" Incompatible type" );
192
+ }
193
+ else
194
+ {
195
+ ECMA_TRY_CATCH (obj_this,
196
+ ecma_op_to_object (this_arg),
197
+ ret_value);
198
+
199
+ ecma_object_t *obj_p = ecma_get_object_from_value (obj_this);
200
+ ecma_property_t *prim_value_prop_p;
201
+ prim_value_prop_p = ecma_get_internal_property (obj_p, ECMA_INTERNAL_PROPERTY_PRIMITIVE_NUMBER_VALUE);
202
+ ecma_number_t *prim_value_num_p = ECMA_GET_NON_NULL_POINTER (ecma_number_t ,
203
+ prim_value_prop_p->u .internal_property .value );
204
+
205
+ if (ecma_number_is_nan (*prim_value_num_p))
206
+ {
207
+ ecma_string_t *magic_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_INVALID_DATE_UL);
208
+ ret_value = ecma_make_normal_completion_value (ecma_make_string_value (magic_str_p));
209
+ }
210
+ else
211
+ {
212
+ ecma_number_t day = ecma_date_date_from_time (*prim_value_num_p);
213
+ ecma_string_t *output_str_p = ecma_new_ecma_string_from_number (day);
214
+ ecma_date_insert_leading_zeros (&output_str_p, day, 2 );
215
+
216
+ /*
217
+ * Note:
218
+ * 'ecma_date_month_from_time' (ECMA 262 v5, 15.9.1.4) returns a number from 0 to 11,
219
+ * but we have to print the month from 1 to 12 for ISO 8601 standard (ECMA 262 v5, 15.9.1.15).
220
+ */
221
+ ecma_number_t month = ecma_date_month_from_time (*prim_value_num_p) + 1 ;
222
+ ecma_date_insert_num_with_sep (&output_str_p, month, LIT_MAGIC_STRING_MINUS_CHAR, 2 );
223
+
224
+ ecma_number_t year = ecma_date_year_from_time (*prim_value_num_p);
225
+ ecma_date_insert_num_with_sep (&output_str_p, year, LIT_MAGIC_STRING_MINUS_CHAR, 4 );
226
+
227
+ ret_value = ecma_make_normal_completion_value (ecma_make_string_value (output_str_p));
228
+ }
229
+
230
+ ECMA_FINALIZE (obj_this);
231
+ }
232
+
233
+ return ret_value;
180
234
} /* ecma_builtin_date_prototype_to_date_string */
181
235
182
236
/* *
@@ -191,7 +245,55 @@ ecma_builtin_date_prototype_to_date_string (ecma_value_t this_arg) /**< this arg
191
245
static ecma_completion_value_t
192
246
ecma_builtin_date_prototype_to_time_string (ecma_value_t this_arg) /* *< this argument */
193
247
{
194
- ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg);
248
+ ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
249
+
250
+ if (!ecma_is_value_object (this_arg))
251
+ {
252
+ ret_value = ecma_raise_type_error (" Incompatible type" );
253
+ }
254
+ else if (ecma_object_get_class_name (ecma_get_object_from_value (this_arg)) != LIT_MAGIC_STRING_DATE_UL)
255
+ {
256
+ ret_value = ecma_raise_type_error (" Incompatible type" );
257
+ }
258
+ else
259
+ {
260
+ ECMA_TRY_CATCH (obj_this,
261
+ ecma_op_to_object (this_arg),
262
+ ret_value);
263
+
264
+ ecma_object_t *obj_p = ecma_get_object_from_value (obj_this);
265
+ ecma_property_t *prim_value_prop_p;
266
+ prim_value_prop_p = ecma_get_internal_property (obj_p, ECMA_INTERNAL_PROPERTY_PRIMITIVE_NUMBER_VALUE);
267
+ ecma_number_t *prim_value_num_p = ECMA_GET_NON_NULL_POINTER (ecma_number_t ,
268
+ prim_value_prop_p->u .internal_property .value );
269
+
270
+ if (ecma_number_is_nan (*prim_value_num_p))
271
+ {
272
+ ecma_string_t *magic_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_INVALID_DATE_UL);
273
+ ret_value = ecma_make_normal_completion_value (ecma_make_string_value (magic_str_p));
274
+ }
275
+ else
276
+ {
277
+ ecma_number_t milliseconds = ecma_date_ms_from_time (*prim_value_num_p);
278
+ ecma_string_t *output_str_p = ecma_new_ecma_string_from_number (milliseconds);
279
+ ecma_date_insert_leading_zeros (&output_str_p, milliseconds, 3 );
280
+
281
+ ecma_number_t seconds = ecma_date_sec_from_time (*prim_value_num_p);
282
+ ecma_date_insert_num_with_sep (&output_str_p, seconds, LIT_MAGIC_STRING_DOT_CHAR, 2 );
283
+
284
+ ecma_number_t minutes = ecma_date_min_from_time (*prim_value_num_p);
285
+ ecma_date_insert_num_with_sep (&output_str_p, minutes, LIT_MAGIC_STRING_COLON_CHAR, 2 );
286
+
287
+ ecma_number_t hours = ecma_date_hour_from_time (*prim_value_num_p);
288
+ ecma_date_insert_num_with_sep (&output_str_p, hours, LIT_MAGIC_STRING_COLON_CHAR, 2 );
289
+
290
+ ret_value = ecma_make_normal_completion_value (ecma_make_string_value (output_str_p));
291
+ }
292
+
293
+ ECMA_FINALIZE (obj_this);
294
+ }
295
+
296
+ return ret_value;
195
297
} /* ecma_builtin_date_prototype_to_time_string */
196
298
197
299
/* *
0 commit comments