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:
@@ -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);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user