mirror of
https://github.com/jerryscript-project/jerryscript.git
synced 2025-12-15 16:29:21 +00:00
Implement the AnnexB Date part
JerryScript-DCO-1.0-Signed-off-by: Kristof Kosztyo kkosztyo.u-szeged@partner.samsung.com
This commit is contained in:
parent
a26c454219
commit
5a09ff2d36
@ -1219,6 +1219,105 @@ ecma_builtin_date_prototype_to_json (ecma_value_t this_arg, /**< this argument *
|
||||
return ret_value;
|
||||
} /* ecma_builtin_date_prototype_to_json */
|
||||
|
||||
#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_ANNEXB_BUILTIN
|
||||
|
||||
/**
|
||||
* The Date.prototype object's 'getYear' routine
|
||||
*
|
||||
* See also:
|
||||
* ECMA-262 v5, AnnexB.B.2.4
|
||||
*
|
||||
* @return completion value
|
||||
* Returned value must be freed with ecma_free_completion_value.
|
||||
*/
|
||||
static ecma_completion_value_t
|
||||
ecma_builtin_date_prototype_get_year (ecma_value_t this_arg) /**< this argument */
|
||||
{
|
||||
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
|
||||
|
||||
/* 1. */
|
||||
ECMA_TRY_CATCH (value, ecma_builtin_date_prototype_get_time (this_arg), ret_value);
|
||||
ecma_number_t *this_num_p = ecma_get_number_from_value (value);
|
||||
/* 2. */
|
||||
if (ecma_number_is_nan (*this_num_p))
|
||||
{
|
||||
ecma_string_t *nan_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_NAN);
|
||||
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (nan_str_p));
|
||||
}
|
||||
else
|
||||
{
|
||||
/* 3. */
|
||||
ecma_number_t *ret_num_p = ecma_alloc_number ();
|
||||
*ret_num_p = ecma_date_year_from_time (ecma_date_local_time (*this_num_p)) - 1900;
|
||||
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (ret_num_p));
|
||||
}
|
||||
ECMA_FINALIZE (value);
|
||||
|
||||
return ret_value;
|
||||
} /* ecma_builtin_date_prototype_get_year */
|
||||
|
||||
/**
|
||||
* The Date.prototype object's 'setYear' routine
|
||||
*
|
||||
* See also:
|
||||
* ECMA-262 v5, AnnexB.B.2.5
|
||||
*
|
||||
* @return completion value
|
||||
* Returned value must be freed with ecma_free_completion_value.
|
||||
*/
|
||||
static ecma_completion_value_t
|
||||
ecma_builtin_date_prototype_set_year (ecma_value_t this_arg, /**< this argument */
|
||||
ecma_value_t year) /**< year argument */
|
||||
{
|
||||
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
|
||||
|
||||
/* 1. */
|
||||
ECMA_TRY_CATCH (this_time_value, ecma_builtin_date_prototype_get_time (this_arg), ret_value);
|
||||
ecma_number_t t = ecma_date_local_time (*ecma_get_number_from_value (this_time_value));
|
||||
if (ecma_number_is_nan (t))
|
||||
{
|
||||
t = ECMA_NUMBER_ZERO;
|
||||
}
|
||||
|
||||
/* 2. */
|
||||
ecma_number_t y = ecma_number_make_nan ();
|
||||
|
||||
ECMA_OP_TO_NUMBER_TRY_CATCH (year_value, year, ret_value);
|
||||
y = year_value;
|
||||
|
||||
/* 3. */
|
||||
if (ecma_number_is_nan (y))
|
||||
{
|
||||
ret_value = ecma_date_set_internal_property (this_arg, 0, y, ECMA_DATE_UTC);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* 4. */
|
||||
if (y >= 0 && y <= 99)
|
||||
{
|
||||
y += 1900;
|
||||
}
|
||||
}
|
||||
|
||||
ECMA_OP_TO_NUMBER_FINALIZE (year_value);
|
||||
|
||||
if (ecma_is_completion_value_empty (ret_value))
|
||||
{
|
||||
/* 5-8. */
|
||||
ecma_number_t m = ecma_date_month_from_time (t);
|
||||
ecma_number_t dt = ecma_date_date_from_time (t);
|
||||
ret_value = ecma_date_set_internal_property (this_arg,
|
||||
ecma_date_make_day (y, m, dt),
|
||||
ecma_date_time_within_day (t),
|
||||
ECMA_DATE_UTC);
|
||||
}
|
||||
ECMA_FINALIZE (this_time_value);
|
||||
|
||||
return ret_value;
|
||||
} /* ecma_builtin_date_prototype_set_year */
|
||||
|
||||
#endif /* CONFIG_ECMA_COMPACT_PROFILE_DISABLE_ANNEXB_BUILTIN */
|
||||
|
||||
/**
|
||||
* @}
|
||||
* @}
|
||||
|
||||
@ -83,6 +83,14 @@ ROUTINE (LIT_MAGIC_STRING_TO_UTC_STRING_UL, ecma_builtin_date_prototype_to_utc_s
|
||||
ROUTINE (LIT_MAGIC_STRING_TO_ISO_STRING_UL, ecma_builtin_date_prototype_to_iso_string, 0, 0)
|
||||
ROUTINE (LIT_MAGIC_STRING_TO_JSON_UL, ecma_builtin_date_prototype_to_json, 1, 1)
|
||||
|
||||
#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_ANNEXB_BUILTIN
|
||||
|
||||
ROUTINE (LIT_MAGIC_STRING_GET_YEAR_UL, ecma_builtin_date_prototype_get_year, 0, 0)
|
||||
ROUTINE (LIT_MAGIC_STRING_SET_YEAR_UL, ecma_builtin_date_prototype_set_year, 1, 1)
|
||||
ROUTINE (LIT_MAGIC_STRING_TO_GMT_STRING_UL, ecma_builtin_date_prototype_to_utc_string, 0, 0)
|
||||
|
||||
#endif /* CONFIG_ECMA_COMPACT_PROFILE_DISABLE_ANNEXB_BUILTIN */
|
||||
|
||||
#undef OBJECT_ID
|
||||
#undef SIMPLE_VALUE
|
||||
#undef NUMBER_VALUE
|
||||
|
||||
@ -171,6 +171,7 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_GET_TIME_UL, "getTime")
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_GET_FULL_YEAR_UL, "getFullYear")
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_UTC_U, "UTC")
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_GET_UTC_FULL_YEAR_UL, "getUTCFullYear")
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_GET_YEAR_UL, "getYear")
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_GET_MONTH_UL, "getMonth")
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_GET_UTC_MONTH_UL, "getUTCMonth")
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_GET_DATE_UL, "getDate")
|
||||
@ -201,8 +202,10 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SET_MONTH_UL, "setMonth")
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SET_UTC_MONTH_UL, "setUTCMonth")
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SET_FULL_YEAR_UL, "setFullYear")
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SET_UTC_FULL_YEAR_UL, "setUTCFullYear")
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SET_YEAR_UL, "setYear")
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_TO_UTC_STRING_UL, "toUTCString")
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_TO_ISO_STRING_UL, "toISOString")
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_TO_GMT_STRING_UL, "toGMTString")
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_TO_JSON_UL, "toJSON")
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_MAX_VALUE_U, "MAX_VALUE")
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_MIN_VALUE_U, "MIN_VALUE")
|
||||
|
||||
47
tests/jerry/date-annexb.js
Normal file
47
tests/jerry/date-annexb.js
Normal file
@ -0,0 +1,47 @@
|
||||
// Copyright 2015 Samsung Electronics Co., Ltd.
|
||||
// Copyright 2015 University of Szeged.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
var d = new Date(1999, 1, 1);
|
||||
assert (d.getYear() === 99);
|
||||
d = new Date(1874, 4, 9);
|
||||
assert (d.getYear() === -26);
|
||||
d = new Date(2015, 8, 17);
|
||||
assert (d.getYear() === 115);
|
||||
d = new Date(NaN);
|
||||
assert (isNaN (d.getYear()));
|
||||
|
||||
var d = new Date();
|
||||
d.setYear(91);
|
||||
assert (d.getFullYear() === 1991 && d.getYear() === 91);
|
||||
|
||||
d = new Date();
|
||||
d.setYear(NaN);
|
||||
assert (isNaN(d.valueOf()));
|
||||
|
||||
d = new Date();
|
||||
d.setYear(2015);
|
||||
assert (d.getFullYear() === 2015);
|
||||
|
||||
d = new Date(2000, 1, 29);
|
||||
d.setYear(2004);
|
||||
assert (d.getFullYear() === 2004 && d.getMonth() === 1 && d.getDate() === 29);
|
||||
d.setYear(2015);
|
||||
assert (d.getFullYear() === 2015 && d.getMonth() === 2 && d.getDate() === 1);
|
||||
|
||||
d = new Date(2015, 8, 17);
|
||||
assert (d.toGMTString() === "Thu, 17 Sep 2015 00:00:00 GMT");
|
||||
|
||||
d = new Date(NaN);
|
||||
assert (d.toGMTString() === "Invalid Date");
|
||||
Loading…
x
Reference in New Issue
Block a user