diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-date-prototype.cpp b/jerry-core/ecma/builtin-objects/ecma-builtin-date-prototype.cpp index e42bd2334..3cb3ca653 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-date-prototype.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-date-prototype.cpp @@ -116,22 +116,7 @@ ecma_builtin_date_prototype_to_date_string (ecma_value_t this_arg) /**< this arg } else { - ecma_number_t day = ecma_date_date_from_time (*prim_value_num_p); - ecma_string_t *output_str_p = ecma_new_ecma_string_from_number (day); - ecma_date_insert_leading_zeros (&output_str_p, day, 2); - - /* - * Note: - * 'ecma_date_month_from_time' (ECMA 262 v5, 15.9.1.4) returns a number from 0 to 11, - * but we have to print the month from 1 to 12 for ISO 8601 standard (ECMA 262 v5, 15.9.1.15). - */ - ecma_number_t month = ecma_date_month_from_time (*prim_value_num_p) + 1; - ecma_date_insert_num_with_sep (&output_str_p, month, LIT_MAGIC_STRING_MINUS_CHAR, 2); - - ecma_number_t year = ecma_date_year_from_time (*prim_value_num_p); - ecma_date_insert_num_with_sep (&output_str_p, year, LIT_MAGIC_STRING_MINUS_CHAR, 4); - - ret_value = ecma_make_normal_completion_value (ecma_make_string_value (output_str_p)); + ret_value = ecma_date_value_to_date_string (*prim_value_num_p); } ECMA_FINALIZE (obj_this); @@ -178,20 +163,7 @@ ecma_builtin_date_prototype_to_time_string (ecma_value_t this_arg) /**< this arg } else { - ecma_number_t milliseconds = ecma_date_ms_from_time (*prim_value_num_p); - ecma_string_t *output_str_p = ecma_new_ecma_string_from_number (milliseconds); - ecma_date_insert_leading_zeros (&output_str_p, milliseconds, 3); - - ecma_number_t seconds = ecma_date_sec_from_time (*prim_value_num_p); - ecma_date_insert_num_with_sep (&output_str_p, seconds, LIT_MAGIC_STRING_DOT_CHAR, 2); - - ecma_number_t minutes = ecma_date_min_from_time (*prim_value_num_p); - ecma_date_insert_num_with_sep (&output_str_p, minutes, LIT_MAGIC_STRING_COLON_CHAR, 2); - - ecma_number_t hours = ecma_date_hour_from_time (*prim_value_num_p); - ecma_date_insert_num_with_sep (&output_str_p, hours, LIT_MAGIC_STRING_COLON_CHAR, 2); - - ret_value = ecma_make_normal_completion_value (ecma_make_string_value (output_str_p)); + ret_value = ecma_date_value_to_time_string (*prim_value_num_p); } ECMA_FINALIZE (obj_this); diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-helpers-date.cpp b/jerry-core/ecma/builtin-objects/ecma-builtin-helpers-date.cpp index d5bc9d62d..984c9732d 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-helpers-date.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-helpers-date.cpp @@ -929,32 +929,6 @@ ecma_date_insert_leading_zeros (ecma_string_t **str_p, /**< input/output string } } /* ecma_date_insert_leading_zeros */ -/** - * Insert a number to the start of a string with a specific separator character and - * fix length. If the length is bigger than the number of digits in num, then insert leding zeros. - */ -void -ecma_date_insert_num_with_sep (ecma_string_t **str_p, /**< input/output string */ - ecma_number_t num, /**< input number */ - lit_magic_string_id_t magic_str_id, /**< separator character id */ - uint32_t length) /**< length of string of number */ -{ - ecma_string_t *magic_string_p = ecma_get_magic_string (magic_str_id); - - ecma_string_t *concat_p = ecma_concat_ecma_strings (magic_string_p, *str_p); - ecma_deref_ecma_string (magic_string_p); - ecma_deref_ecma_string (*str_p); - *str_p = concat_p; - - ecma_string_t *num_str_p = ecma_new_ecma_string_from_number (num); - concat_p = ecma_concat_ecma_strings (num_str_p, *str_p); - ecma_deref_ecma_string (num_str_p); - ecma_deref_ecma_string (*str_p); - *str_p = concat_p; - - ecma_date_insert_leading_zeros (str_p, num, length); -} /* ecma_date_insert_num_with_sep */ - /** * Common function to copy utf8 characters. * @@ -1072,11 +1046,6 @@ ecma_date_value_to_string_common (lit_utf8_byte_t *dest_p, /**< destination buff return ecma_date_value_copy_utf8_bytes (dest_p, " GMT"); } /* ecma_date_value_to_string_common */ -/** - * Length of string created by ecma_date_value_to_string - */ -#define ECMA_DATE_VALUE_TO_STRING_LENGTH 33 - /** * Common function to create a time zone specific string from a numeric value. * @@ -1090,7 +1059,12 @@ ecma_date_value_to_string_common (lit_utf8_byte_t *dest_p, /**< destination buff ecma_completion_value_t ecma_date_value_to_string (ecma_number_t datetime_number) /**< datetime */ { - lit_utf8_byte_t character_buffer[ECMA_DATE_VALUE_TO_STRING_LENGTH]; + /* + * Character length of the result string. + */ + constexpr uint32_t result_string_length = 33; + + lit_utf8_byte_t character_buffer[result_string_length]; lit_utf8_byte_t *dest_p = character_buffer; datetime_number = ecma_date_local_time (datetime_number); @@ -1113,19 +1087,14 @@ ecma_date_value_to_string (ecma_number_t datetime_number) /**< datetime */ dest_p = ecma_date_value_number_to_bytes (dest_p, time_zone / 60, 2); dest_p = ecma_date_value_number_to_bytes (dest_p, time_zone % 60, 2); - JERRY_ASSERT (dest_p - character_buffer == ECMA_DATE_VALUE_TO_STRING_LENGTH); + JERRY_ASSERT (dest_p - character_buffer == result_string_length); ecma_string_t *date_string_p = ecma_new_ecma_string_from_utf8 (character_buffer, - ECMA_DATE_VALUE_TO_STRING_LENGTH); + result_string_length); return ecma_make_normal_completion_value (ecma_make_string_value (date_string_p)); } /* ecma_date_value_to_string */ -/** - * Length of string created by ecma_date_value_to_utc_string - */ -#define ECMA_DATE_VALUE_TO_UTC_STRING_LENGTH 29 - /** * Common function to create a time zone specific string from a numeric value. * @@ -1138,7 +1107,12 @@ ecma_date_value_to_string (ecma_number_t datetime_number) /**< datetime */ ecma_completion_value_t ecma_date_value_to_utc_string (ecma_number_t datetime_number) /**< datetime */ { - lit_utf8_byte_t character_buffer[ECMA_DATE_VALUE_TO_UTC_STRING_LENGTH]; + /* + * Character length of the result string. + */ + constexpr uint32_t result_string_length = 29; + + lit_utf8_byte_t character_buffer[result_string_length]; lit_utf8_byte_t *dest_p = character_buffer; dest_p = ecma_date_value_week_day_to_abbreviation (dest_p, datetime_number); @@ -1154,16 +1128,16 @@ ecma_date_value_to_utc_string (ecma_number_t datetime_number) /**< datetime */ dest_p = ecma_date_value_to_string_common (dest_p, datetime_number); - JERRY_ASSERT (dest_p - character_buffer == ECMA_DATE_VALUE_TO_UTC_STRING_LENGTH); + JERRY_ASSERT (dest_p - character_buffer == result_string_length); ecma_string_t *date_string_p = ecma_new_ecma_string_from_utf8 (character_buffer, - ECMA_DATE_VALUE_TO_UTC_STRING_LENGTH); + result_string_length); return ecma_make_normal_completion_value (ecma_make_string_value (date_string_p)); } /* ecma_date_value_to_utc_string */ /** - * Common function to create a time zone specific string from a numeric value. + * Common function to create a ISO specific string from a numeric value. * * Used by: * - The Date.prototype.toISOString routine. @@ -1172,40 +1146,144 @@ ecma_date_value_to_utc_string (ecma_number_t datetime_number) /**< datetime */ * Returned value must be freed with ecma_free_completion_value. */ ecma_completion_value_t -ecma_date_value_to_iso_string (ecma_number_t datetime_num) /**