|
15 | 15 | */
|
16 | 16 |
|
17 | 17 | #include "ecma-alloc.h"
|
| 18 | +#include "ecma-builtin-helpers.h" |
| 19 | +#include "ecma-exceptions.h" |
18 | 20 | #include "ecma-globals.h"
|
19 | 21 | #include "ecma-helpers.h"
|
| 22 | +#include "ecma-objects.h" |
| 23 | +#include "ecma-try-catch-macro.h" |
20 | 24 |
|
21 | 25 | #ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_DATE_BUILTIN
|
22 | 26 |
|
|
37 | 41 | * @{
|
38 | 42 | */
|
39 | 43 |
|
| 44 | +/** |
| 45 | + * Insert leading zeros to a string of a number if needed. |
| 46 | + */ |
| 47 | +static void |
| 48 | +ecma_date_insert_leading_zeros (ecma_string_t **str_p, /**< input/output string */ |
| 49 | + ecma_number_t num, /**< input number */ |
| 50 | + int32_t div) /**< "length" {10, 100, 1000} */ |
| 51 | +{ |
| 52 | + JERRY_ASSERT (div == 10 || div == 100 || div == 1000); |
| 53 | + |
| 54 | + while (num < div && div > 1) |
| 55 | + { |
| 56 | + div /= 10; |
| 57 | + |
| 58 | + ecma_string_t *zero_str_p = ecma_new_ecma_string_from_number (ECMA_NUMBER_ZERO); |
| 59 | + ecma_string_t *concat_p = ecma_concat_ecma_strings (zero_str_p, *str_p); |
| 60 | + ecma_deref_ecma_string (zero_str_p); |
| 61 | + ecma_deref_ecma_string (*str_p); |
| 62 | + *str_p = concat_p; |
| 63 | + } |
| 64 | + |
| 65 | +} /* insert_leading_zeros */ |
| 66 | + |
| 67 | +/** |
| 68 | + * Insert a number to the start of a string with a specific separator character and |
| 69 | + * fix length. |
| 70 | + */ |
| 71 | +static void |
| 72 | +ecma_date_insert_num_with_sep (ecma_string_t **str_p, /**< input/output string */ |
| 73 | + ecma_number_t num, /**< input number */ |
| 74 | + lit_magic_string_id_t magic_str_id, /**< separator character id */ |
| 75 | + int32_t div) /**< "length" {10, 100, 1000} */ |
| 76 | +{ |
| 77 | + ecma_string_t *magic_string_p = ecma_get_magic_string (magic_str_id); |
| 78 | + |
| 79 | + ecma_string_t *concat_p = ecma_concat_ecma_strings (magic_string_p, *str_p); |
| 80 | + ecma_deref_ecma_string (magic_string_p); |
| 81 | + ecma_deref_ecma_string (*str_p); |
| 82 | + *str_p = concat_p; |
| 83 | + |
| 84 | + ecma_string_t *num_str_p = ecma_new_ecma_string_from_number (num); |
| 85 | + concat_p = ecma_concat_ecma_strings (num_str_p, *str_p); |
| 86 | + ecma_deref_ecma_string (num_str_p); |
| 87 | + ecma_deref_ecma_string (*str_p); |
| 88 | + *str_p = concat_p; |
| 89 | + |
| 90 | + ecma_date_insert_leading_zeros (str_p, num, div); |
| 91 | +} /* insert_num_with_sep */ |
| 92 | + |
40 | 93 | /**
|
41 | 94 | * The Date.prototype object's 'toString' routine
|
42 | 95 | *
|
|
49 | 102 | static ecma_completion_value_t
|
50 | 103 | ecma_builtin_date_prototype_to_string (ecma_value_t this_arg) /**< this argument */
|
51 | 104 | {
|
52 |
| - ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg); |
| 105 | + ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); |
| 106 | + |
| 107 | + if (ecma_object_get_class_name (ecma_get_object_from_value (this_arg)) != LIT_MAGIC_STRING_DATE_UL) |
| 108 | + { |
| 109 | + ret_value = ecma_raise_type_error ("Incomplete Date type"); |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + ECMA_TRY_CATCH (obj_this, |
| 114 | + ecma_op_to_object (this_arg), |
| 115 | + ret_value); |
| 116 | + |
| 117 | + ecma_object_t *obj_p = ecma_get_object_from_value (obj_this); |
| 118 | + ecma_property_t *prim_value_prop_p; |
| 119 | + prim_value_prop_p = ecma_get_internal_property (obj_p, ECMA_INTERNAL_PROPERTY_PRIMITIVE_NUMBER_VALUE); |
| 120 | + ecma_number_t *prim_value_num_p = ECMA_GET_NON_NULL_POINTER (ecma_number_t, |
| 121 | + prim_value_prop_p->u.internal_property.value); |
| 122 | + |
| 123 | + if (ecma_number_is_nan (*prim_value_num_p)) |
| 124 | + { |
| 125 | + const lit_utf8_byte_t *inv_date_utf8_p = (const lit_utf8_byte_t *) "Invalid Date"; |
| 126 | + ecma_string_t *str_p = ecma_new_ecma_string_from_utf8 (inv_date_utf8_p, |
| 127 | + lit_zt_utf8_string_size (inv_date_utf8_p)); |
| 128 | + ret_value = ecma_make_normal_completion_value (ecma_make_string_value (str_p)); |
| 129 | + } |
| 130 | + else |
| 131 | + { |
| 132 | + 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, 100); |
| 135 | + |
| 136 | + ecma_number_t seconds = ecma_date_sec_from_time (*prim_value_num_p); |
| 137 | + ecma_date_insert_num_with_sep (&output_str_p, seconds, LIT_MAGIC_STRING_DOT_CHAR, 10); |
| 138 | + |
| 139 | + ecma_number_t minutes = ecma_date_min_from_time (*prim_value_num_p); |
| 140 | + ecma_date_insert_num_with_sep (&output_str_p, minutes, LIT_MAGIC_STRING_COLON_CHAR, 10); |
| 141 | + |
| 142 | + ecma_number_t hours = ecma_date_hour_from_time (*prim_value_num_p); |
| 143 | + ecma_date_insert_num_with_sep (&output_str_p, hours, LIT_MAGIC_STRING_COLON_CHAR, 10); |
| 144 | + |
| 145 | + ecma_number_t day = ecma_date_date_from_time (*prim_value_num_p); |
| 146 | + ecma_date_insert_num_with_sep (&output_str_p, day, LIT_MAGIC_STRING_TIME_SEP_U, 10); |
| 147 | + |
| 148 | + /* |
| 149 | + * Note: |
| 150 | + * 'ecma_date_month_from_time' (ECMA 262 v5, 15.9.1.4) returns a number from 0 to 11, |
| 151 | + * but we have to print the month from 1 to 12 for ISO 8601 standard (ECMA 262 v5, 15.9.1.15). |
| 152 | + */ |
| 153 | + ecma_number_t month = ecma_date_month_from_time (*prim_value_num_p) + 1; |
| 154 | + ecma_date_insert_num_with_sep (&output_str_p, month, LIT_MAGIC_STRING_MINUS_CHAR, 10); |
| 155 | + |
| 156 | + ecma_number_t year = ecma_date_year_from_time (*prim_value_num_p); |
| 157 | + ecma_date_insert_num_with_sep (&output_str_p, year, LIT_MAGIC_STRING_MINUS_CHAR, 1000); |
| 158 | + |
| 159 | + ret_value = ecma_make_normal_completion_value (ecma_make_string_value (output_str_p)); |
| 160 | + } |
| 161 | + |
| 162 | + ECMA_FINALIZE (obj_this); |
| 163 | + } |
| 164 | + |
| 165 | + return ret_value; |
53 | 166 | } /* ecma_builtin_date_prototype_to_string */
|
54 | 167 |
|
55 | 168 | /**
|
|
0 commit comments