Skip to content

Fix system call related date builtin functions #873

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
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
15 changes: 10 additions & 5 deletions jerry-core/ecma/builtin-objects/ecma-builtin-date.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 */

Expand Down
53 changes: 35 additions & 18 deletions jerry-core/ecma/builtin-objects/ecma-builtin-helpers-date.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_DATE_BUILTIN
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the <time.h> include should be inside the ifndef. It is not needed otherwise.


#include <time.h>

/** \addtogroup ecma ECMA
* @{
*
Expand Down Expand Up @@ -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 */

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

/**
Expand Down Expand Up @@ -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;
Expand All @@ -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);

Expand Down
25 changes: 17 additions & 8 deletions jerry-core/ecma/builtin-objects/ecma-builtin-helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions jerry-libc/include/time.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* 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.
*/

#ifndef JERRY_LIBC_TIME_H
#define JERRY_LIBC_TIME_H

#ifdef __cplusplus
# define EXTERN_C "C"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is not this will be a symbol clash? I think there is a { } based solution for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This worked fine for me. Could you be more specific? BTW, this is used in every jerry-libc header.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @zherczeg here. It would be better to use the more traditional

#ifdef __cplusplus
extern "C" {
#endif
// header main content here
#ifdef __cplusplus
}
#endif

approach. The issue is that the #define EXTERN_C "C" way is used all over the project. I'd love to see that change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll change it everywhere and add it in a separate commit, but part of this PR.

#else /* !__cplusplus */
# define EXTERN_C
#endif /* !__cplusplus */

/**
* Time value structure
*/
struct _timeval
{
unsigned long tv_sec; /**< seconds */
unsigned long tv_usec; /**< microseconds */
};

/**
* Timezone structure
*/
struct timezone
{
int tz_minuteswest; /**< minutes west of Greenwich */
int tz_dsttime; /**< type of DST correction */
};

extern EXTERN_C int gettimeofday (void *tp, void *tzp);

#endif /* !JERRY_LIBC_TIME_H */
15 changes: 14 additions & 1 deletion jerry-libc/target/darwin/jerry-libc-target.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* Copyright 2014-2015 Samsung Electronics Co., Ltd.
/* Copyright 2014-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.
Expand Down Expand Up @@ -28,6 +29,7 @@
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>

#include "jerry-libc-defs.h"

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

/**
* This function can get the time as well as a timezone.
*
* @return 0 if success, -1 otherwise
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return value description?

int
gettimeofday (void *tp, /**< struct timeval */
void *tzp) /**< struct timezone */
{
return (int) syscall_2 (__NR_gettimeofday, (long int) tp, (long int) tzp);
} /* gettimeofday */
16 changes: 15 additions & 1 deletion jerry-libc/target/linux/jerry-libc-target.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* Copyright 2014-2015 Samsung Electronics Co., Ltd.
/* Copyright 2014-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.
Expand Down Expand Up @@ -28,6 +29,7 @@
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>

#include "jerry-libc-defs.h"

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

/**
* This function can get the time as well as a timezone.
*
* @return 0 if success, -1 otherwise
*/
int
gettimeofday (void *tp, /**< struct timeval */
void *tzp) /**< struct timezone */
{
return (int) syscall_2 (__NR_gettimeofday, (long int) tp, (long int) tzp);
} /* gettimeofday */

// FIXME
#if 0
/**
Expand Down
15 changes: 14 additions & 1 deletion jerry-libc/target/mcu-stubs/jerry-libc-target.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* Copyright 2015 Samsung Electronics Co., Ltd.
/* Copyright 2015-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.
Expand All @@ -19,6 +20,7 @@

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "jerry-libc-defs.h"

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

/**
* This function can get the time as well as a timezone.
*
* @return 0 if success, -1 otherwise
*/
int
gettimeofday (void *tp __attr_unused___, /**< struct timeval */
void *tzp __attr_unused___) /**< struct timezone */
{
return 0;
} /* gettimeofday */
7 changes: 3 additions & 4 deletions tests/jerry/date-annexb.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2015 Samsung Electronics Co., Ltd.
// Copyright 2015 University of Szeged.
// Copyright 2015-2016 Samsung Electronics Co., Ltd.
// Copyright 2015-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.
Expand Down Expand Up @@ -40,8 +40,7 @@ assert (d.getFullYear() === 2004 && d.getMonth() === 1 && d.getDate() === 29);
d.setYear(2015);
assert (d.getFullYear() === 2015 && d.getMonth() === 2 && d.getDate() === 1);

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

d = new Date(NaN);
assert (d.toGMTString() === "Invalid Date");
9 changes: 3 additions & 6 deletions tests/jerry/date-construct.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2015 Samsung Electronics Co., Ltd.
// Copyright 2015 University of Szeged.
// Copyright 2015-2016 Samsung Electronics Co., Ltd.
// Copyright 2015-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.
Expand Down Expand Up @@ -44,7 +44,7 @@ d = new Date(1420070400000);
assert (d.valueOf() == 1420070400000);

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

d = new Date(8.64e+15);
assert (d.valueOf() == 8.64e+15);
Expand Down Expand Up @@ -80,6 +80,3 @@ catch (e)
assert (typeof Date (2015) == "string");
assert (typeof Date() != typeof (new Date ()));
assert (Date (Number.NaN) == Date ());

// Fixme: remove this case when Date() gives the current time.
assert (Date (2015,1,2) == "Thu Jan 01 1970 00:00:00 GMT+0000");
16 changes: 6 additions & 10 deletions tests/jerry/date-tostring.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2015 Samsung Electronics Co., Ltd.
// Copyright 2015 University of Szeged.
// Copyright 2015-2016 Samsung Electronics Co., Ltd.
// Copyright 2015-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.
Expand All @@ -20,8 +20,8 @@ assert (new Date (2015, 7, 1, 0, Infinity, 0) == "Invalid Date");
assert (new Date (NaN, 1, 1, 0, 0, 0) == "Invalid Date");
assert (new Date (2015, NaN, 1, 0, 0, 0) == "Invalid Date");
assert (new Date (2015, 7, 1, 0, NaN, 0) == "Invalid Date");
assert (new Date ("2015-02-13") == "Fri Feb 13 2015 00:00:00 GMT+0000");
assert (new Date ("2015-07-08T11:29:05.023") == "Wed Jul 08 2015 11:29:05 GMT+0000");
assert (/Fri Feb 13 2015 \d{2}:\d{2}:\d{2} GMT\+\d{2}:\d{2}/.test (new Date ("2015-02-13")));
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")));

try
{
Expand All @@ -35,12 +35,12 @@ catch (e)
}

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

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

Expand Down Expand Up @@ -147,8 +147,4 @@ assert (Date () == (new Date ()).toString ());
assert (Date (2015, 1, 1) == (new Date ()).toString ());
assert (Date (Number.NaN) == Date ());

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

assert (new Date ("2015-07-08T11:29:05.023Z").toISOString() == "2015-07-08T11:29:05.023Z");
2 changes: 0 additions & 2 deletions tests/unit/test-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <sys/time.h>


/**
* Verify that unit tests are built with all debug checks enabled
Expand Down