Fix system call related date builtin functions
Related issues: #213, #691 * Fixed 'ecma_date_local_tza' and 'ecma_date_daylight_saving_ta' date builtin helper functions * Added syscall of gettimeofday function to get the current system time and timezone. * Fixed related regression test files. JerryScript-DCO-1.0-Signed-off-by: László Langó llango.u-szeged@partner.samsung.com
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
#include "ecma-alloc.h"
|
||||
#include "ecma-builtin-helpers.h"
|
||||
#include "ecma-conversion.h"
|
||||
#include "ecma-exceptions.h"
|
||||
#include "ecma-gc.h"
|
||||
#include "ecma-globals.h"
|
||||
#include "ecma-helpers.h"
|
||||
@@ -448,13 +449,17 @@ ecma_builtin_date_utc (ecma_value_t this_arg __attr_unused___, /**< this argumen
|
||||
static ecma_value_t
|
||||
ecma_builtin_date_now (ecma_value_t this_arg __attr_unused___) /**< this argument */
|
||||
{
|
||||
/*
|
||||
* FIXME:
|
||||
* Get the real system time. ex: gettimeofday() on Linux
|
||||
* Introduce system macros at first.
|
||||
*/
|
||||
struct _timeval tv;
|
||||
ecma_number_t *now_num_p = ecma_alloc_number ();
|
||||
*now_num_p = ECMA_NUMBER_ZERO;
|
||||
|
||||
if (gettimeofday (&tv, NULL) != 0)
|
||||
{
|
||||
return ecma_raise_type_error ("gettimeofday failed");
|
||||
}
|
||||
|
||||
*now_num_p = ((ecma_number_t) tv.tv_sec) * 1000.0 + ((ecma_number_t) (tv.tv_usec / 1000));
|
||||
|
||||
return ecma_make_number_value (now_num_p);
|
||||
} /* ecma_builtin_date_now */
|
||||
|
||||
|
||||
@@ -26,6 +26,8 @@
|
||||
|
||||
#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_DATE_BUILTIN
|
||||
|
||||
#include <time.h>
|
||||
|
||||
/** \addtogroup ecma ECMA
|
||||
* @{
|
||||
*
|
||||
@@ -445,13 +447,14 @@ ecma_date_week_day (ecma_number_t time) /**< time value */
|
||||
ecma_number_t __attr_always_inline___
|
||||
ecma_date_local_tza ()
|
||||
{
|
||||
/*
|
||||
* FIXME:
|
||||
* Get the real system time. ex: localtime_r, gmtime_r, daylight on Linux
|
||||
* Introduce system macros at first.
|
||||
*/
|
||||
TODO ("Implement time functions in jerry-libc.");
|
||||
return ECMA_NUMBER_ZERO;
|
||||
struct timezone tz;
|
||||
|
||||
if (gettimeofday (NULL, &tz) != 0)
|
||||
{
|
||||
return ecma_raise_type_error ("gettimeofday failed");
|
||||
}
|
||||
|
||||
return tz.tz_minuteswest * -ECMA_DATE_MS_PER_MINUTE;
|
||||
} /* ecma_date_local_tza */
|
||||
|
||||
/**
|
||||
@@ -470,13 +473,14 @@ ecma_date_daylight_saving_ta (ecma_number_t time) /**< time value */
|
||||
return time; /* time is NaN */
|
||||
}
|
||||
|
||||
/*
|
||||
* FIXME:
|
||||
* Get the real system time. ex: localtime_r, gmtime_r, daylight on Linux
|
||||
* Introduce system macros at first.
|
||||
*/
|
||||
TODO ("Implement time functions in jerry-libc.");
|
||||
return ECMA_NUMBER_ZERO;
|
||||
struct timezone tz;
|
||||
|
||||
if (gettimeofday (NULL, &tz) != 0)
|
||||
{
|
||||
return ecma_raise_type_error ("gettimeofday failed");
|
||||
}
|
||||
|
||||
return tz.tz_dsttime;
|
||||
} /* ecma_date_daylight_saving_ta */
|
||||
|
||||
/**
|
||||
@@ -1062,7 +1066,7 @@ ecma_date_value_to_string (ecma_number_t datetime_number) /**< datetime */
|
||||
/*
|
||||
* Character length of the result string.
|
||||
*/
|
||||
const uint32_t result_string_length = 33;
|
||||
const uint32_t result_string_length = 34;
|
||||
|
||||
lit_utf8_byte_t character_buffer[result_string_length];
|
||||
lit_utf8_byte_t *dest_p = character_buffer;
|
||||
@@ -1082,10 +1086,23 @@ ecma_date_value_to_string (ecma_number_t datetime_number) /**< datetime */
|
||||
dest_p = ecma_date_value_to_string_common (dest_p, datetime_number);
|
||||
|
||||
int32_t time_zone = (int32_t) (ecma_date_local_tza () + ecma_date_daylight_saving_ta (datetime_number));
|
||||
*dest_p++ = (time_zone >= 0) ? LIT_CHAR_PLUS : LIT_CHAR_MINUS;
|
||||
if (time_zone >= 0)
|
||||
{
|
||||
*dest_p++ = LIT_CHAR_PLUS;
|
||||
}
|
||||
else
|
||||
{
|
||||
*dest_p++ = LIT_CHAR_MINUS;
|
||||
time_zone = -time_zone;
|
||||
}
|
||||
|
||||
dest_p = ecma_date_value_number_to_bytes (dest_p, time_zone / 60, 2);
|
||||
dest_p = ecma_date_value_number_to_bytes (dest_p, time_zone % 60, 2);
|
||||
dest_p = ecma_date_value_number_to_bytes (dest_p,
|
||||
time_zone / (int32_t) ECMA_DATE_MS_PER_HOUR,
|
||||
2);
|
||||
*dest_p++ = LIT_CHAR_COLON;
|
||||
dest_p = ecma_date_value_number_to_bytes (dest_p,
|
||||
time_zone % (int32_t) ECMA_DATE_MS_PER_HOUR,
|
||||
2);
|
||||
|
||||
JERRY_ASSERT ((uint32_t) (dest_p - character_buffer) == result_string_length);
|
||||
|
||||
|
||||
@@ -55,21 +55,30 @@ ecma_builtin_helper_def_prop (ecma_object_t *, ecma_string_t *, ecma_value_t,
|
||||
* See also:
|
||||
* ECMA-262 v5, 15.9.1.1, 15.9.1.10
|
||||
*/
|
||||
/* Hours in a day. */
|
||||
|
||||
/** Hours in a day. */
|
||||
#define ECMA_DATE_HOURS_PER_DAY ((ecma_number_t) 24)
|
||||
/* Minutes in an hour. */
|
||||
|
||||
/** Minutes in an hour. */
|
||||
#define ECMA_DATE_MINUTES_PER_HOUR ((ecma_number_t) 60)
|
||||
/* Seconds in a minute. */
|
||||
|
||||
/** Seconds in a minute. */
|
||||
#define ECMA_DATE_SECONDS_PER_MINUTE ((ecma_number_t) 60)
|
||||
/* Milliseconds in a second. */
|
||||
|
||||
/** Milliseconds in a second. */
|
||||
#define ECMA_DATE_MS_PER_SECOND ((ecma_number_t) 1000)
|
||||
/* ECMA_DATE_MS_PER_MINUTE == 60000 */
|
||||
|
||||
/** ECMA_DATE_MS_PER_MINUTE == 60000 */
|
||||
#define ECMA_DATE_MS_PER_MINUTE (ECMA_DATE_MS_PER_SECOND * ECMA_DATE_SECONDS_PER_MINUTE)
|
||||
/* ECMA_DATE_MS_PER_HOUR == 3600000 */
|
||||
|
||||
/** ECMA_DATE_MS_PER_HOUR == 3600000 */
|
||||
#define ECMA_DATE_MS_PER_HOUR (ECMA_DATE_MS_PER_MINUTE * ECMA_DATE_MINUTES_PER_HOUR)
|
||||
/* ECMA_DATE_MS_PER_DAY == 86400000 */
|
||||
|
||||
/** ECMA_DATE_MS_PER_DAY == 86400000 */
|
||||
#define ECMA_DATE_MS_PER_DAY (ECMA_DATE_MS_PER_HOUR * ECMA_DATE_HOURS_PER_DAY)
|
||||
/* This gives a range of 8,640,000,000,000,000 milliseconds
|
||||
|
||||
/**
|
||||
* This gives a range of 8,640,000,000,000,000 milliseconds
|
||||
* to either side of 01 January, 1970 UTC.
|
||||
*/
|
||||
#define ECMA_DATE_MAX_VALUE 8.64e15
|
||||
|
||||
Reference in New Issue
Block a user