diff --git a/Makefile b/Makefile index 8e2dc073bb..e0dd734ed8 100644 --- a/Makefile +++ b/Makefile @@ -76,12 +76,6 @@ BUILD_NAME:= BUILD_NAME:=$(BUILD_NAME)-LOG-$(LOG) endif - # Date system calls - ifneq ($(DATE_SYS_CALLS),) - CMAKE_DEFINES:=$(CMAKE_DEFINES) -DENABLE_DATE_SYS_CALLS=$(DATE_SYS_CALLS) - BUILD_NAME:=$(BUILD_NAME)-DATE_SYS_CALLS-$(DATE_SYS_CALLS) - endif - # Fill error messages for builtin error objects ifneq ($(ERROR_MESSAGES),) CMAKE_DEFINES:=$(CMAKE_DEFINES) -DENABLE_ERROR_MESSAGES=$(ERROR_MESSAGES) @@ -106,7 +100,7 @@ BUILD_NAME:= endif # For testing build-options -export BUILD_OPTIONS_TEST_NATIVE := LTO LOG DATE_SYS_CALLS ERROR_MESSAGES ALL_IN_ONE VALGRIND VALGRIND_FREYA COMPILER_DEFAULT_LIBC +export BUILD_OPTIONS_TEST_NATIVE := LTO LOG ERROR_MESSAGES ALL_IN_ONE VALGRIND VALGRIND_FREYA COMPILER_DEFAULT_LIBC # Directories export ROOT_DIR := $(shell pwd) diff --git a/jerry-core/CMakeLists.txt b/jerry-core/CMakeLists.txt index 2d326214cc..1a769a5f0e 100644 --- a/jerry-core/CMakeLists.txt +++ b/jerry-core/CMakeLists.txt @@ -192,11 +192,6 @@ project (JerryCore C ASM) set(DEFINES_JERRY ${DEFINES_JERRY} JERRY_ENABLE_LOG) endif() - # Date system calls - if("${ENABLE_DATE_SYS_CALLS}" STREQUAL "ON") - set(DEFINES_JERRY ${DEFINES_JERRY} JERRY_ENABLE_DATE_SYS_CALLS) - endif() - # Fill error messages for builtin error objects if("${ENABLE_ERROR_MESSAGES}" STREQUAL "ON") set(DEFINES_JERRY ${DEFINES_JERRY} JERRY_ENABLE_ERROR_MESSAGES) diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-date.c b/jerry-core/ecma/builtin-objects/ecma-builtin-date.c index 73829b9c9b..d21dccc2bc 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-date.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-date.c @@ -33,10 +33,6 @@ #define BUILTIN_UNDERSCORED_ID date #include "ecma-builtin-internal-routines-template.inc.h" -#ifdef JERRY_ENABLE_DATE_SYS_CALLS -#include -#endif /* JERRY_ENABLE_DATE_SYS_CALLS */ - /** \addtogroup ecma ECMA * @{ * @@ -450,18 +446,8 @@ static ecma_value_t ecma_builtin_date_now (ecma_value_t this_arg __attr_unused___) /**< this argument */ { ecma_number_t *now_num_p = ecma_alloc_number (); - *now_num_p = ECMA_NUMBER_ZERO; - -#ifdef JERRY_ENABLE_DATE_SYS_CALLS - struct timeval tv; - - if (gettimeofday (&tv, NULL) != 0) - { - return ecma_raise_type_error (ECMA_ERR_MSG ("gettimeofday failed")); - } - *now_num_p = ((ecma_number_t) tv.tv_sec) * 1000.0 + ((ecma_number_t) (tv.tv_usec / 1000)); -#endif /* JERRY_ENABLE_DATE_SYS_CALLS */ + *now_num_p = (ecma_number_t) jerry_port_get_current_time (); return ecma_make_number_value (now_num_p); } /* ecma_builtin_date_now */ diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-helpers-date.c b/jerry-core/ecma/builtin-objects/ecma-builtin-helpers-date.c index ae679f7495..6eecb265f9 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-helpers-date.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-helpers-date.c @@ -27,11 +27,6 @@ #ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_DATE_BUILTIN -#ifdef JERRY_ENABLE_DATE_SYS_CALLS -#include - -#endif /* JERRY_ENABLE_DATE_SYS_CALLS */ - /** \addtogroup ecma ECMA * @{ * @@ -451,21 +446,14 @@ ecma_date_week_day (ecma_number_t time) /**< time value */ inline ecma_number_t __attr_always_inline___ ecma_date_local_tza () { -#ifdef JERRY_ENABLE_DATE_SYS_CALLS - struct timeval tv; - struct timezone tz; + jerry_time_zone_t tz; - tz.tz_minuteswest = 0; /* gettimeofday may not fill tz, so zero-initializing */ - - if (gettimeofday (&tv, &tz) != 0) + if (!jerry_port_get_time_zone (&tz)) { return ecma_number_make_nan (); } - return tz.tz_minuteswest * -ECMA_DATE_MS_PER_MINUTE; -#else /* !JERRY_ENABLE_DATE_SYS_CALLS */ - return ECMA_NUMBER_ZERO; -#endif /* JERRY_ENABLE_DATE_SYS_CALLS */ + return tz.offset * -ECMA_DATE_MS_PER_MINUTE; } /* ecma_date_local_tza */ /** @@ -484,21 +472,14 @@ ecma_date_daylight_saving_ta (ecma_number_t time) /**< time value */ return time; /* time is NaN */ } -#ifdef JERRY_ENABLE_DATE_SYS_CALLS - struct timeval tv; - struct timezone tz; - - tz.tz_dsttime = 0; /* gettimeofday may not fill tz, so zero-initializing */ + jerry_time_zone_t tz; - if (gettimeofday (&tv, &tz) != 0) + if (!jerry_port_get_time_zone (&tz)) { return ecma_number_make_nan (); } - return tz.tz_dsttime; -#else /* !JERRY_ENABLE_DATE_SYS_CALLS */ - return ECMA_NUMBER_ZERO; -#endif /* JERRY_ENABLE_DATE_SYS_CALLS */ + return tz.daylight_saving_time * ECMA_DATE_MS_PER_HOUR; } /* ecma_date_daylight_saving_ta */ /** diff --git a/jerry-core/jerry-port.h b/jerry-core/jerry-port.h index 14b7398f4c..311ce0af6b 100644 --- a/jerry-core/jerry-port.h +++ b/jerry-core/jerry-port.h @@ -17,6 +17,8 @@ #ifndef JERRY_PORT_H #define JERRY_PORT_H +#include +#include #include #ifdef __cplusplus @@ -71,6 +73,34 @@ typedef enum */ void jerry_port_fatal (jerry_fatal_code_t code); +/* + * Date Port API + */ + +/** + * Jerry time zone structure + */ +typedef struct +{ + int offset; /**< minutes from west */ + int daylight_saving_time; /**< daylight saving time (1 - DST applies, 0 - not on DST) */ +} jerry_time_zone_t; + +/** + * Get timezone and daylight saving data + * + * @return true - if success + * false - otherwise + */ +bool jerry_port_get_time_zone (jerry_time_zone_t *tz_p); + +/** + * Get system time + * + * @return milliseconds since Unix epoch + */ +uint64_t jerry_port_get_current_time (void); + /** * @} */ diff --git a/targets/default/jerry-port-default-date.c b/targets/default/jerry-port-default-date.c new file mode 100644 index 0000000000..490fe801ce --- /dev/null +++ b/targets/default/jerry-port-default-date.c @@ -0,0 +1,53 @@ +/* Copyright 2016 Samsung Electronics Co., Ltd. + * Copyright 2016 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. + */ + +#define _BSD_SOURCE +#include + +#include "jerry-port.h" +#include "jerry-port-default.h" + +/** + * Default implementation of jerry_port_get_time_zone. + */ +bool jerry_port_get_time_zone (jerry_time_zone_t *tz_p) +{ + struct timeval tv; + struct timezone tz; + + /* gettimeofday may not fill tz, so zero-initializing */ + tz.tz_minuteswest = 0; + tz.tz_dsttime = 0; + + gettimeofday (&tv, &tz); + + tz_p->offset = tz.tz_minuteswest; + tz_p->daylight_saving_time = tz.tz_dsttime > 0 ? 1 : 0; + + return true; +} /* jerry_port_get_time_zone */ + +/** + * Default implementation of jerry_port_get_current_time. + */ +uint64_t jerry_port_get_current_time () +{ + struct timeval tv; + + gettimeofday (&tv, NULL); + + return ((uint64_t) tv.tv_sec) * 1000 + ((uint64_t) tv.tv_usec) / 1000; +} /* jerry_port_get_current_time */