Skip to content

Commit 684ed72

Browse files
committed
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ó [email protected]
1 parent a7715a5 commit 684ed72

File tree

11 files changed

+163
-56
lines changed

11 files changed

+163
-56
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "ecma-alloc.h"
1818
#include "ecma-builtin-helpers.h"
1919
#include "ecma-conversion.h"
20+
#include "ecma-exceptions.h"
2021
#include "ecma-gc.h"
2122
#include "ecma-globals.h"
2223
#include "ecma-helpers.h"
@@ -448,13 +449,17 @@ ecma_builtin_date_utc (ecma_value_t this_arg __attr_unused___, /**< this argumen
448449
static ecma_value_t
449450
ecma_builtin_date_now (ecma_value_t this_arg __attr_unused___) /**< this argument */
450451
{
451-
/*
452-
* FIXME:
453-
* Get the real system time. ex: gettimeofday() on Linux
454-
* Introduce system macros at first.
455-
*/
452+
struct _timeval tv;
456453
ecma_number_t *now_num_p = ecma_alloc_number ();
457454
*now_num_p = ECMA_NUMBER_ZERO;
455+
456+
if (gettimeofday (&tv, NULL) != 0)
457+
{
458+
return ecma_raise_type_error ("gettimeofday failed");
459+
}
460+
461+
*now_num_p = ((ecma_number_t) tv.tv_sec) * 1000.0 + ((ecma_number_t) (tv.tv_usec / 1000));
462+
458463
return ecma_make_number_value (now_num_p);
459464
} /* ecma_builtin_date_now */
460465

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

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_DATE_BUILTIN
2828

29+
#include <time.h>
30+
2931
/** \addtogroup ecma ECMA
3032
* @{
3133
*
@@ -445,13 +447,14 @@ ecma_date_week_day (ecma_number_t time) /**< time value */
445447
ecma_number_t __attr_always_inline___
446448
ecma_date_local_tza ()
447449
{
448-
/*
449-
* FIXME:
450-
* Get the real system time. ex: localtime_r, gmtime_r, daylight on Linux
451-
* Introduce system macros at first.
452-
*/
453-
TODO ("Implement time functions in jerry-libc.");
454-
return ECMA_NUMBER_ZERO;
450+
struct timezone tz;
451+
452+
if (gettimeofday (NULL, &tz) != 0)
453+
{
454+
return ecma_raise_type_error ("gettimeofday failed");
455+
}
456+
457+
return tz.tz_minuteswest * -ECMA_DATE_MS_PER_MINUTE;
455458
} /* ecma_date_local_tza */
456459

457460
/**
@@ -470,13 +473,14 @@ ecma_date_daylight_saving_ta (ecma_number_t time) /**< time value */
470473
return time; /* time is NaN */
471474
}
472475

473-
/*
474-
* FIXME:
475-
* Get the real system time. ex: localtime_r, gmtime_r, daylight on Linux
476-
* Introduce system macros at first.
477-
*/
478-
TODO ("Implement time functions in jerry-libc.");
479-
return ECMA_NUMBER_ZERO;
476+
struct timezone tz;
477+
478+
if (gettimeofday (NULL, &tz) != 0)
479+
{
480+
return ecma_raise_type_error ("gettimeofday failed");
481+
}
482+
483+
return tz.tz_dsttime;
480484
} /* ecma_date_daylight_saving_ta */
481485

482486
/**
@@ -1062,7 +1066,7 @@ ecma_date_value_to_string (ecma_number_t datetime_number) /**< datetime */
10621066
/*
10631067
* Character length of the result string.
10641068
*/
1065-
const uint32_t result_string_length = 33;
1069+
const uint32_t result_string_length = 34;
10661070

10671071
lit_utf8_byte_t character_buffer[result_string_length];
10681072
lit_utf8_byte_t *dest_p = character_buffer;
@@ -1082,10 +1086,23 @@ ecma_date_value_to_string (ecma_number_t datetime_number) /**< datetime */
10821086
dest_p = ecma_date_value_to_string_common (dest_p, datetime_number);
10831087

10841088
int32_t time_zone = (int32_t) (ecma_date_local_tza () + ecma_date_daylight_saving_ta (datetime_number));
1085-
*dest_p++ = (time_zone >= 0) ? LIT_CHAR_PLUS : LIT_CHAR_MINUS;
1089+
if (time_zone >= 0)
1090+
{
1091+
*dest_p++ = LIT_CHAR_PLUS;
1092+
}
1093+
else
1094+
{
1095+
*dest_p++ = LIT_CHAR_MINUS;
1096+
time_zone = -time_zone;
1097+
}
10861098

1087-
dest_p = ecma_date_value_number_to_bytes (dest_p, time_zone / 60, 2);
1088-
dest_p = ecma_date_value_number_to_bytes (dest_p, time_zone % 60, 2);
1099+
dest_p = ecma_date_value_number_to_bytes (dest_p,
1100+
time_zone / (int32_t) ECMA_DATE_MS_PER_HOUR,
1101+
2);
1102+
*dest_p++ = LIT_CHAR_COLON;
1103+
dest_p = ecma_date_value_number_to_bytes (dest_p,
1104+
time_zone % (int32_t) ECMA_DATE_MS_PER_HOUR,
1105+
2);
10891106

10901107
JERRY_ASSERT ((uint32_t) (dest_p - character_buffer) == result_string_length);
10911108

jerry-core/ecma/builtin-objects/ecma-builtin-helpers.h

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,30 @@ ecma_builtin_helper_def_prop (ecma_object_t *, ecma_string_t *, ecma_value_t,
5555
* See also:
5656
* ECMA-262 v5, 15.9.1.1, 15.9.1.10
5757
*/
58-
/* Hours in a day. */
58+
59+
/** Hours in a day. */
5960
#define ECMA_DATE_HOURS_PER_DAY ((ecma_number_t) 24)
60-
/* Minutes in an hour. */
61+
62+
/** Minutes in an hour. */
6163
#define ECMA_DATE_MINUTES_PER_HOUR ((ecma_number_t) 60)
62-
/* Seconds in a minute. */
64+
65+
/** Seconds in a minute. */
6366
#define ECMA_DATE_SECONDS_PER_MINUTE ((ecma_number_t) 60)
64-
/* Milliseconds in a second. */
67+
68+
/** Milliseconds in a second. */
6569
#define ECMA_DATE_MS_PER_SECOND ((ecma_number_t) 1000)
66-
/* ECMA_DATE_MS_PER_MINUTE == 60000 */
70+
71+
/** ECMA_DATE_MS_PER_MINUTE == 60000 */
6772
#define ECMA_DATE_MS_PER_MINUTE (ECMA_DATE_MS_PER_SECOND * ECMA_DATE_SECONDS_PER_MINUTE)
68-
/* ECMA_DATE_MS_PER_HOUR == 3600000 */
73+
74+
/** ECMA_DATE_MS_PER_HOUR == 3600000 */
6975
#define ECMA_DATE_MS_PER_HOUR (ECMA_DATE_MS_PER_MINUTE * ECMA_DATE_MINUTES_PER_HOUR)
70-
/* ECMA_DATE_MS_PER_DAY == 86400000 */
76+
77+
/** ECMA_DATE_MS_PER_DAY == 86400000 */
7178
#define ECMA_DATE_MS_PER_DAY (ECMA_DATE_MS_PER_HOUR * ECMA_DATE_HOURS_PER_DAY)
72-
/* This gives a range of 8,640,000,000,000,000 milliseconds
79+
80+
/**
81+
* This gives a range of 8,640,000,000,000,000 milliseconds
7382
* to either side of 01 January, 1970 UTC.
7483
*/
7584
#define ECMA_DATE_MAX_VALUE 8.64e15

jerry-libc/include/time.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
#ifndef JERRY_LIBC_TIME_H
18+
#define JERRY_LIBC_TIME_H
19+
20+
#ifdef __cplusplus
21+
# define EXTERN_C "C"
22+
#else /* !__cplusplus */
23+
# define EXTERN_C
24+
#endif /* !__cplusplus */
25+
26+
/**
27+
* Time value structure
28+
*/
29+
struct _timeval
30+
{
31+
unsigned long tv_sec; /**< seconds */
32+
unsigned long tv_usec; /**< microseconds */
33+
};
34+
35+
/**
36+
* Timezone structure
37+
*/
38+
struct timezone
39+
{
40+
int tz_minuteswest; /**< minutes west of Greenwich */
41+
int tz_dsttime; /**< type of DST correction */
42+
};
43+
44+
extern EXTERN_C int gettimeofday (void *tp, void *tzp);
45+
46+
#endif /* !JERRY_LIBC_TIME_H */

jerry-libc/target/darwin/jerry-libc-target.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* Copyright 2014-2015 Samsung Electronics Co., Ltd.
1+
/* Copyright 2014-2016 Samsung Electronics Co., Ltd.
2+
* Copyright 2016 University of Szeged.
23
*
34
* Licensed under the Apache License, Version 2.0 (the "License");
45
* you may not use this file except in compliance with the License.
@@ -28,6 +29,7 @@
2829
#include <sys/resource.h>
2930
#include <sys/types.h>
3031
#include <sys/stat.h>
32+
#include <time.h>
3133

3234
#include "jerry-libc-defs.h"
3335

@@ -384,3 +386,14 @@ fwrite (const void *ptr, /**< data to write */
384386
return bytes_written / size;
385387
} /* fwrite */
386388

389+
/**
390+
* This function can get the time as well as a timezone.
391+
*
392+
* @return 0 if success, -1 otherwise
393+
*/
394+
int
395+
gettimeofday (void *tp, /**< struct timeval */
396+
void *tzp) /**< struct timezone */
397+
{
398+
return (int) syscall_2 (__NR_gettimeofday, (long int) tp, (long int) tzp);
399+
} /* gettimeofday */

jerry-libc/target/linux/jerry-libc-target.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* Copyright 2014-2015 Samsung Electronics Co., Ltd.
1+
/* Copyright 2014-2016 Samsung Electronics Co., Ltd.
2+
* Copyright 2016 University of Szeged.
23
*
34
* Licensed under the Apache License, Version 2.0 (the "License");
45
* you may not use this file except in compliance with the License.
@@ -28,6 +29,7 @@
2829
#include <sys/resource.h>
2930
#include <sys/types.h>
3031
#include <sys/stat.h>
32+
#include <time.h>
3133

3234
#include "jerry-libc-defs.h"
3335

@@ -384,6 +386,18 @@ fwrite (const void *ptr, /**< data to write */
384386
return bytes_written / size;
385387
} /* fwrite */
386388

389+
/**
390+
* This function can get the time as well as a timezone.
391+
*
392+
* @return 0 if success, -1 otherwise
393+
*/
394+
int
395+
gettimeofday (void *tp, /**< struct timeval */
396+
void *tzp) /**< struct timezone */
397+
{
398+
return (int) syscall_2 (__NR_gettimeofday, (long int) tp, (long int) tzp);
399+
} /* gettimeofday */
400+
387401
// FIXME
388402
#if 0
389403
/**

jerry-libc/target/mcu-stubs/jerry-libc-target.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* Copyright 2015 Samsung Electronics Co., Ltd.
1+
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
2+
* Copyright 2016 University of Szeged.
23
*
34
* Licensed under the Apache License, Version 2.0 (the "License");
45
* you may not use this file except in compliance with the License.
@@ -19,6 +20,7 @@
1920

2021
#include <stdio.h>
2122
#include <stdlib.h>
23+
#include <time.h>
2224

2325
#include "jerry-libc-defs.h"
2426

@@ -61,3 +63,14 @@ fwrite (const void *ptr __attr_unused___, /**< data to write */
6163
return size * nmemb;
6264
} /* fwrite */
6365

66+
/**
67+
* This function can get the time as well as a timezone.
68+
*
69+
* @return 0 if success, -1 otherwise
70+
*/
71+
int
72+
gettimeofday (void *tp __attr_unused___, /**< struct timeval */
73+
void *tzp __attr_unused___) /**< struct timezone */
74+
{
75+
return 0;
76+
} /* gettimeofday */

tests/jerry/date-annexb.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// Copyright 2015 Samsung Electronics Co., Ltd.
2-
// Copyright 2015 University of Szeged.
1+
// Copyright 2015-2016 Samsung Electronics Co., Ltd.
2+
// Copyright 2015-2016 University of Szeged.
33
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.
@@ -40,8 +40,7 @@ assert (d.getFullYear() === 2004 && d.getMonth() === 1 && d.getDate() === 29);
4040
d.setYear(2015);
4141
assert (d.getFullYear() === 2015 && d.getMonth() === 2 && d.getDate() === 1);
4242

43-
d = new Date(2015, 8, 17);
44-
assert (d.toGMTString() === "Thu, 17 Sep 2015 00:00:00 GMT");
43+
assert (/Thu, 17 Sep 2015 \d{2}:\d{2}:\d{2} GMT/.test (new Date("2015-09-17").toGMTString()));
4544

4645
d = new Date(NaN);
4746
assert (d.toGMTString() === "Invalid Date");

tests/jerry/date-construct.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// Copyright 2015 Samsung Electronics Co., Ltd.
2-
// Copyright 2015 University of Szeged.
1+
// Copyright 2015-2016 Samsung Electronics Co., Ltd.
2+
// Copyright 2015-2016 University of Szeged.
33
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.
@@ -44,7 +44,7 @@ d = new Date(1420070400000);
4444
assert (d.valueOf() == 1420070400000);
4545

4646
d = new Date(2015,0,1,0,0,0,0);
47-
assert (d.valueOf() == 1420070400000);
47+
assert (d.valueOf() - (d.getTimezoneOffset() * 60000) == 1420070400000);
4848

4949
d = new Date(8.64e+15);
5050
assert (d.valueOf() == 8.64e+15);
@@ -80,6 +80,3 @@ catch (e)
8080
assert (typeof Date (2015) == "string");
8181
assert (typeof Date() != typeof (new Date ()));
8282
assert (Date (Number.NaN) == Date ());
83-
84-
// Fixme: remove this case when Date() gives the current time.
85-
assert (Date (2015,1,2) == "Thu Jan 01 1970 00:00:00 GMT+0000");

tests/jerry/date-tostring.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// Copyright 2015 Samsung Electronics Co., Ltd.
2-
// Copyright 2015 University of Szeged.
1+
// Copyright 2015-2016 Samsung Electronics Co., Ltd.
2+
// Copyright 2015-2016 University of Szeged.
33
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
55
// you may not use this file except in compliance with the License.
@@ -20,8 +20,8 @@ assert (new Date (2015, 7, 1, 0, Infinity, 0) == "Invalid Date");
2020
assert (new Date (NaN, 1, 1, 0, 0, 0) == "Invalid Date");
2121
assert (new Date (2015, NaN, 1, 0, 0, 0) == "Invalid Date");
2222
assert (new Date (2015, 7, 1, 0, NaN, 0) == "Invalid Date");
23-
assert (new Date ("2015-02-13") == "Fri Feb 13 2015 00:00:00 GMT+0000");
24-
assert (new Date ("2015-07-08T11:29:05.023") == "Wed Jul 08 2015 11:29:05 GMT+0000");
23+
assert (/Fri Feb 13 2015 \d{2}:\d{2}:\d{2} GMT\+\d{2}:\d{2}/.test (new Date ("2015-02-13")));
24+
assert (/Wed Jul 08 2015 \d{2}:\d{2}:\d{2} GMT\+\d{2}:\d{2}/.test (new Date ("2015-07-08T11:29:05.023")));
2525

2626
try
2727
{
@@ -35,12 +35,12 @@ catch (e)
3535
}
3636

3737
var date = new Date(0);
38-
assert (date.toString() === "Thu Jan 01 1970 00:00:00 GMT+0000");
38+
assert (/Thu Jan 01 1970 \d{2}:\d{2}:\d{2} GMT\+\d{2}:\d{2}/.test (date.toString()));
3939
assert (date.toUTCString() === "Thu, 01 Jan 1970 00:00:00 GMT");
4040
assert (date.toISOString() === "1970-01-01T00:00:00.000Z");
4141

4242
date = new Date("2015-08-12T09:40:20.000Z")
43-
assert (date.toString() === "Wed Aug 12 2015 09:40:20 GMT+0000");
43+
assert (/Wed Aug 12 2015 \d{2}:\d{2}:\d{2} GMT\+\d{2}:\d{2}/.test (date.toString()));
4444
assert (date.toUTCString() === "Wed, 12 Aug 2015 09:40:20 GMT");
4545
assert (date.toISOString() === "2015-08-12T09:40:20.000Z");
4646

@@ -147,8 +147,4 @@ assert (Date () == (new Date ()).toString ());
147147
assert (Date (2015, 1, 1) == (new Date ()).toString ());
148148
assert (Date (Number.NaN) == Date ());
149149

150-
// Fixme: remove these cases when TZA and DST are supported.
151-
assert (new Date ("2015-07-08T11:29:05.023-02:00").toString () == "Wed Jul 08 2015 13:29:05 GMT+0000");
152-
assert (new Date ("2015-07-08T11:29:05.023-02:00").toLocaleString () == "Wed Jul 08 2015 13:29:05 GMT+0000");
153-
154150
assert (new Date ("2015-07-08T11:29:05.023Z").toISOString() == "2015-07-08T11:29:05.023Z");

0 commit comments

Comments
 (0)