Skip to content

Introduce the Date Port API #1018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
5 changes: 0 additions & 5 deletions jerry-core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 1 addition & 15 deletions jerry-core/ecma/builtin-objects/ecma-builtin-date.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
* @{
*
Expand Down Expand Up @@ -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 */
Expand Down
31 changes: 6 additions & 25 deletions jerry-core/ecma/builtin-objects/ecma-builtin-helpers-date.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
* @{
*
Expand Down Expand Up @@ -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 */

/**
Expand All @@ -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 */

/**
Expand Down
30 changes: 30 additions & 0 deletions jerry-core/jerry-port.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#ifndef JERRY_PORT_H
#define JERRY_PORT_H

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>

#ifdef __cplusplus
Expand Down Expand Up @@ -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);

/**
* @}
*/
Expand Down
53 changes: 53 additions & 0 deletions targets/default/jerry-port-default-date.c
Original file line number Diff line number Diff line change
@@ -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 */