Introduce the Date Port API
Replaced `gettimeofday`-related code with `jerry_port_get_current_time` and `jerry_port_get_time_zone` function calls. Moved old code to default port implementation. Removed `ENABLE_DATE_SYS_CALLS` as date syscalls should "just work". Fix DST adjustments: even if `gettimeofday` returns meaningful data in `tz_dsttime`, the value is just a flag (or, according to some sources, a tri-state value: >0 if DST applies, ==0 if DST does not apply, <0 if unknown). Hitherto, the field was simply added to/subtracted from a time value in milliseconds. To use it approximately correctly, the field's value should be multiplied by "millisecs/hour". JerryScript-DCO-1.0-Signed-off-by: Akos Kiss akiss@inf.u-szeged.hu
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -33,10 +33,6 @@
|
||||
#define BUILTIN_UNDERSCORED_ID date
|
||||
#include "ecma-builtin-internal-routines-template.inc.h"
|
||||
|
||||
#ifdef JERRY_ENABLE_DATE_SYS_CALLS
|
||||
#include <sys/time.h>
|
||||
#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 */
|
||||
|
||||
@@ -27,11 +27,6 @@
|
||||
|
||||
#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_DATE_BUILTIN
|
||||
|
||||
#ifdef JERRY_ENABLE_DATE_SYS_CALLS
|
||||
#include <sys/time.h>
|
||||
|
||||
#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;
|
||||
jerry_time_zone_t tz;
|
||||
|
||||
tz.tz_dsttime = 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_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 */
|
||||
|
||||
/**
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
#ifndef JERRY_PORT_H
|
||||
#define JERRY_PORT_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#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);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
@@ -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 <sys/time.h>
|
||||
|
||||
#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 */
|
||||
Reference in New Issue
Block a user