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