Skip to content

Commit 2723f18

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 ef1252e commit 2723f18

File tree

10 files changed

+131
-47
lines changed

10 files changed

+131
-47
lines changed

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

Lines changed: 12 additions & 2 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.
@@ -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"
@@ -453,8 +454,17 @@ ecma_builtin_date_now (ecma_value_t this_arg __attr_unused___) /**< this argumen
453454
* Get the real system time. ex: gettimeofday() on Linux
454455
* Introduce system macros at first.
455456
*/
457+
struct timeval tv;
456458
ecma_number_t *now_num_p = ecma_alloc_number ();
457459
*now_num_p = ECMA_NUMBER_ZERO;
460+
461+
if (gettimeofday (&tv, NULL) != 0)
462+
{
463+
return ecma_raise_type_error ("gettimeofday failed");
464+
}
465+
466+
*now_num_p = ((ecma_number_t) tv.tv_sec) * 1000.0 + ((ecma_number_t) (tv.tv_usec / 1000));
467+
458468
return ecma_make_normal_completion_value (ecma_make_number_value (now_num_p));
459469
} /* ecma_builtin_date_now */
460470

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

Lines changed: 33 additions & 20 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.
@@ -24,6 +24,8 @@
2424
#include "fdlibm-math.h"
2525
#include "lit-char-helpers.h"
2626

27+
#include <time.h>
28+
2729
#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_DATE_BUILTIN
2830

2931
/** \addtogroup ecma ECMA
@@ -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 * -60000;
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,19 @@ 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, time_zone / 3600000, 2);
1100+
*dest_p++ = LIT_CHAR_COLON;
1101+
dest_p = ecma_date_value_number_to_bytes (dest_p, time_zone % 3600000, 2);
10891102

10901103
JERRY_ASSERT ((uint32_t) (dest_p - character_buffer) == result_string_length);
10911104

jerry-libc/include/time.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
* Timezone structure
28+
*/
29+
struct timezone
30+
{
31+
int tz_minuteswest; /**< minutes west of Greenwich */
32+
int tz_dsttime; /**< type of DST correction */
33+
};
34+
35+
extern EXTERN_C int gettimeofday (void *tp, void *tzp);
36+
37+
#endif /* !JERRY_LIBC_TIME_H */

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

Lines changed: 12 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,12 @@ 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+
int
393+
gettimeofday (void *tp, /**< struct timeval */
394+
void *tzp) /**< struct timezone */
395+
{
396+
return (int) syscall_2 (__NR_gettimeofday, (long int) tp, (long int) tzp);
397+
} /* gettimeofday */

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

Lines changed: 13 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,16 @@ 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+
int
393+
gettimeofday (void *tp, /**< struct timeval */
394+
void *tzp) /**< struct timezone */
395+
{
396+
return (int) syscall_2 (__NR_gettimeofday, (long int) tp, (long int) tzp);
397+
} /* gettimeofday */
398+
387399
// FIXME
388400
#if 0
389401
/**

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

Lines changed: 12 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,12 @@ 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+
int
70+
gettimeofday (void *tp __attr_unused___, /**< struct timeval */
71+
void *tzp __attr_unused___) /**< struct timezone */
72+
{
73+
return 0;
74+
} /* 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");

tests/unit/test-common.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
#include <stdlib.h>
2525
#include <stdarg.h>
2626
#include <string.h>
27-
#include <sys/time.h>
28-
2927

3028
/**
3129
* Verify that unit tests are built with all debug checks enabled

0 commit comments

Comments
 (0)