Skip to content

Commit b523cf3

Browse files
committed
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
1 parent a3b1db3 commit b523cf3

File tree

6 files changed

+91
-52
lines changed

6 files changed

+91
-52
lines changed

Makefile

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,6 @@ BUILD_NAME:=
7676
BUILD_NAME:=$(BUILD_NAME)-LOG-$(LOG)
7777
endif
7878

79-
# Date system calls
80-
ifneq ($(DATE_SYS_CALLS),)
81-
CMAKE_DEFINES:=$(CMAKE_DEFINES) -DENABLE_DATE_SYS_CALLS=$(DATE_SYS_CALLS)
82-
BUILD_NAME:=$(BUILD_NAME)-DATE_SYS_CALLS-$(DATE_SYS_CALLS)
83-
endif
84-
8579
# Fill error messages for builtin error objects
8680
ifneq ($(ERROR_MESSAGES),)
8781
CMAKE_DEFINES:=$(CMAKE_DEFINES) -DENABLE_ERROR_MESSAGES=$(ERROR_MESSAGES)
@@ -106,7 +100,7 @@ BUILD_NAME:=
106100
endif
107101

108102
# For testing build-options
109-
export BUILD_OPTIONS_TEST_NATIVE := LTO LOG DATE_SYS_CALLS ERROR_MESSAGES ALL_IN_ONE VALGRIND VALGRIND_FREYA COMPILER_DEFAULT_LIBC
103+
export BUILD_OPTIONS_TEST_NATIVE := LTO LOG ERROR_MESSAGES ALL_IN_ONE VALGRIND VALGRIND_FREYA COMPILER_DEFAULT_LIBC
110104

111105
# Directories
112106
export ROOT_DIR := $(shell pwd)

jerry-core/CMakeLists.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,6 @@ project (JerryCore C ASM)
192192
set(DEFINES_JERRY ${DEFINES_JERRY} JERRY_ENABLE_LOG)
193193
endif()
194194

195-
# Date system calls
196-
if("${ENABLE_DATE_SYS_CALLS}" STREQUAL "ON")
197-
set(DEFINES_JERRY ${DEFINES_JERRY} JERRY_ENABLE_DATE_SYS_CALLS)
198-
endif()
199-
200195
# Fill error messages for builtin error objects
201196
if("${ENABLE_ERROR_MESSAGES}" STREQUAL "ON")
202197
set(DEFINES_JERRY ${DEFINES_JERRY} JERRY_ENABLE_ERROR_MESSAGES)

jerry-core/ecma/builtin-objects/ecma-builtin-date.c

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@
3333
#define BUILTIN_UNDERSCORED_ID date
3434
#include "ecma-builtin-internal-routines-template.inc.h"
3535

36-
#ifdef JERRY_ENABLE_DATE_SYS_CALLS
37-
#include <sys/time.h>
38-
#endif /* JERRY_ENABLE_DATE_SYS_CALLS */
39-
4036
/** \addtogroup ecma ECMA
4137
* @{
4238
*
@@ -450,18 +446,8 @@ static ecma_value_t
450446
ecma_builtin_date_now (ecma_value_t this_arg __attr_unused___) /**< this argument */
451447
{
452448
ecma_number_t *now_num_p = ecma_alloc_number ();
453-
*now_num_p = ECMA_NUMBER_ZERO;
454-
455-
#ifdef JERRY_ENABLE_DATE_SYS_CALLS
456-
struct timeval tv;
457-
458-
if (gettimeofday (&tv, NULL) != 0)
459-
{
460-
return ecma_raise_type_error (ECMA_ERR_MSG ("gettimeofday failed"));
461-
}
462449

463-
*now_num_p = ((ecma_number_t) tv.tv_sec) * 1000.0 + ((ecma_number_t) (tv.tv_usec / 1000));
464-
#endif /* JERRY_ENABLE_DATE_SYS_CALLS */
450+
*now_num_p = (ecma_number_t) jerry_port_get_current_time ();
465451

466452
return ecma_make_number_value (now_num_p);
467453
} /* ecma_builtin_date_now */

jerry-core/ecma/builtin-objects/ecma-builtin-helpers-date.c

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@
2727

2828
#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_DATE_BUILTIN
2929

30-
#ifdef JERRY_ENABLE_DATE_SYS_CALLS
31-
#include <sys/time.h>
32-
33-
#endif /* JERRY_ENABLE_DATE_SYS_CALLS */
34-
3530
/** \addtogroup ecma ECMA
3631
* @{
3732
*
@@ -451,21 +446,14 @@ ecma_date_week_day (ecma_number_t time) /**< time value */
451446
inline ecma_number_t __attr_always_inline___
452447
ecma_date_local_tza ()
453448
{
454-
#ifdef JERRY_ENABLE_DATE_SYS_CALLS
455-
struct timeval tv;
456-
struct timezone tz;
449+
jerry_time_zone_t tz;
457450

458-
tz.tz_minuteswest = 0; /* gettimeofday may not fill tz, so zero-initializing */
459-
460-
if (gettimeofday (&tv, &tz) != 0)
451+
if (!jerry_port_get_time_zone (&tz))
461452
{
462453
return ecma_number_make_nan ();
463454
}
464455

465-
return tz.tz_minuteswest * -ECMA_DATE_MS_PER_MINUTE;
466-
#else /* !JERRY_ENABLE_DATE_SYS_CALLS */
467-
return ECMA_NUMBER_ZERO;
468-
#endif /* JERRY_ENABLE_DATE_SYS_CALLS */
456+
return tz.offset * -ECMA_DATE_MS_PER_MINUTE;
469457
} /* ecma_date_local_tza */
470458

471459
/**
@@ -484,21 +472,14 @@ ecma_date_daylight_saving_ta (ecma_number_t time) /**< time value */
484472
return time; /* time is NaN */
485473
}
486474

487-
#ifdef JERRY_ENABLE_DATE_SYS_CALLS
488-
struct timeval tv;
489-
struct timezone tz;
490-
491-
tz.tz_dsttime = 0; /* gettimeofday may not fill tz, so zero-initializing */
475+
jerry_time_zone_t tz;
492476

493-
if (gettimeofday (&tv, &tz) != 0)
477+
if (!jerry_port_get_time_zone (&tz))
494478
{
495479
return ecma_number_make_nan ();
496480
}
497481

498-
return tz.tz_dsttime;
499-
#else /* !JERRY_ENABLE_DATE_SYS_CALLS */
500-
return ECMA_NUMBER_ZERO;
501-
#endif /* JERRY_ENABLE_DATE_SYS_CALLS */
482+
return tz.daylight_saving_time * ECMA_DATE_MS_PER_HOUR;
502483
} /* ecma_date_daylight_saving_ta */
503484

504485
/**

jerry-core/jerry-port.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#ifndef JERRY_PORT_H
1818
#define JERRY_PORT_H
1919

20+
#include <stdbool.h>
21+
#include <stdint.h>
2022
#include <stdio.h>
2123

2224
#ifdef __cplusplus
@@ -71,6 +73,34 @@ typedef enum
7173
*/
7274
void jerry_port_fatal (jerry_fatal_code_t code);
7375

76+
/*
77+
* Date Port API
78+
*/
79+
80+
/**
81+
* Jerry time zone structure
82+
*/
83+
typedef struct
84+
{
85+
int offset; /**< minutes from west */
86+
int daylight_saving_time; /**< daylight saving time (1 - DST applies, 0 - not on DST) */
87+
} jerry_time_zone_t;
88+
89+
/**
90+
* Get timezone and daylight saving data
91+
*
92+
* @return true - if success
93+
* false - otherwise
94+
*/
95+
bool jerry_port_get_time_zone (jerry_time_zone_t *tz_p);
96+
97+
/**
98+
* Get system time
99+
*
100+
* @return milliseconds since Unix epoch
101+
*/
102+
uint64_t jerry_port_get_current_time (void);
103+
74104
/**
75105
* @}
76106
*/
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* Copyright 2016 Samsung Electronics Co., Ltd.
2+
* Copyright 2016 University of Szeged
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#define _BSD_SOURCE
18+
#include <sys/time.h>
19+
20+
#include "jerry-port.h"
21+
#include "jerry-port-default.h"
22+
23+
/**
24+
* Default implementation of jerry_port_get_time_zone.
25+
*/
26+
bool jerry_port_get_time_zone (jerry_time_zone_t *tz_p)
27+
{
28+
struct timeval tv;
29+
struct timezone tz;
30+
31+
/* gettimeofday may not fill tz, so zero-initializing */
32+
tz.tz_minuteswest = 0;
33+
tz.tz_dsttime = 0;
34+
35+
gettimeofday (&tv, &tz);
36+
37+
tz_p->offset = tz.tz_minuteswest;
38+
tz_p->daylight_saving_time = tz.tz_dsttime > 0 ? 1 : 0;
39+
40+
return true;
41+
} /* jerry_port_get_time_zone */
42+
43+
/**
44+
* Default implementation of jerry_port_get_current_time.
45+
*/
46+
uint64_t jerry_port_get_current_time ()
47+
{
48+
struct timeval tv;
49+
50+
gettimeofday (&tv, NULL);
51+
52+
return ((uint64_t) tv.tv_sec) * 1000 + ((uint64_t) tv.tv_usec) / 1000;
53+
} /* jerry_port_get_current_time */

0 commit comments

Comments
 (0)