Browse Source

Add time calls to the API

pull/771/head
Sami Vaarala 9 years ago
parent
commit
dcd8c7c8bf
  1. 1
      Makefile
  2. 27
      src/duk_api_public.h.in
  3. 67
      src/duk_api_time.c
  4. 1
      util/make_dist.py

1
Makefile

@ -127,6 +127,7 @@ DUKTAPE_SOURCES_SEPARATE = \
$(DISTSRCSEP)/duk_api_object.c \
$(DISTSRCSEP)/duk_api_buffer.c \
$(DISTSRCSEP)/duk_api_logging.c \
$(DISTSRCSEP)/duk_api_time.c \
$(DISTSRCSEP)/duk_api_debug.c \
$(DISTSRCSEP)/duk_lexer.c \
$(DISTSRCSEP)/duk_tval.c \

27
src/duk_api_public.h.in

@ -35,6 +35,7 @@ extern "C" {
struct duk_memory_functions;
struct duk_function_list_entry;
struct duk_number_list_entry;
struct duk_time_components;
/* duk_context is now defined in duk_config.h because it may also be
* referenced there by prototypes.
@ -42,6 +43,7 @@ struct duk_number_list_entry;
typedef struct duk_memory_functions duk_memory_functions;
typedef struct duk_function_list_entry duk_function_list_entry;
typedef struct duk_number_list_entry duk_number_list_entry;
typedef struct duk_time_components duk_time_components;
typedef duk_ret_t (*duk_c_function)(duk_context *ctx);
typedef void *(*duk_alloc_function) (void *udata, duk_size_t size);
@ -77,6 +79,17 @@ struct duk_number_list_entry {
duk_double_t value;
};
struct duk_time_components {
duk_uint_t year; /* year, e.g. 2016, Ecmascript year range */
duk_uint_t month; /* month: 1-12 */
duk_uint_t day; /* day: 1-31 */
duk_uint_t hour; /* hour: 0-59 */
duk_uint_t minute; /* minute: 0-59 */
duk_uint_t second; /* second: 0-59 (in POSIX time no leap second) */
duk_uint_t weekday; /* weekday: 0-6, 0=Sunday, 1=Monday, ..., 6=Saturday */
duk_double_t millisecond; /* may contain sub-millisecond fractions */
};
/*
* Constants
*/
@ -915,6 +928,14 @@ DUK_EXTERNAL_DECL void duk_debugger_cooperate(duk_context *ctx);
DUK_EXTERNAL_DECL duk_bool_t duk_debugger_notify(duk_context *ctx, duk_idx_t nvalues);
DUK_EXTERNAL_DECL void duk_debugger_pause(duk_context *ctx);
/*
* Time handling
*/
DUK_EXTERNAL_DECL duk_double_t duk_get_now(duk_context *ctx);
DUK_EXTERNAL_DECL void duk_time_to_components(duk_context *ctx, duk_double_t time, duk_time_components *comp);
DUK_EXTERNAL_DECL duk_double_t duk_components_to_time(duk_context *ctx, duk_time_components *comp);
/*
* Date provider related constants
*
@ -951,7 +972,7 @@ DUK_EXTERNAL_DECL void duk_debugger_pause(duk_context *ctx);
* depend on the specific ordering, so change with care. 16 bits are not
* enough for all parts (year, specifically).
*
* (Must be in-sync with genbuiltins.py.)
* Must be in-sync with genbuiltins.py.
*/
#define DUK_DATE_IDX_YEAR 0 /* year */
#define DUK_DATE_IDX_MONTH 1 /* month: 0 to 11 */
@ -963,7 +984,7 @@ DUK_EXTERNAL_DECL void duk_debugger_pause(duk_context *ctx);
#define DUK_DATE_IDX_WEEKDAY 7 /* weekday: 0 to 6, 0=sunday, 1=monday, etc */
#define DUK_DATE_IDX_NUM_PARTS 8
/* Internal API call flags, used for various functions in this file.
/* Internal API call flags, used for various functions in duk_bi_date.c.
* Certain flags are used by only certain functions, but since the flags
* don't overlap, a single flags value can be passed around to multiple
* functions.
@ -971,7 +992,7 @@ DUK_EXTERNAL_DECL void duk_debugger_pause(duk_context *ctx);
* The unused top bits of the flags field are also used to pass values
* to helpers (duk__get_part_helper() and duk__set_part_helper()).
*
* (Must be in-sync with genbuiltins.py.)
* Must be in-sync with genbuiltins.py.
*/
/* NOTE: when writing a Date provider you only need a few specific

67
src/duk_api_time.c

@ -0,0 +1,67 @@
/*
* Date/time.
*/
#include "duk_internal.h"
DUK_EXTERNAL duk_double_t duk_get_now(duk_context *ctx) {
return ((duk_double_t) DUK_USE_DATE_GET_NOW((ctx)));
}
DUK_EXTERNAL void duk_time_to_components(duk_context *ctx, duk_double_t time, duk_time_components *comp) {
duk_int_t parts[DUK_DATE_IDX_NUM_PARTS];
duk_double_t dparts[DUK_DATE_IDX_NUM_PARTS];
duk_uint_t flags;
DUK_ASSERT(ctx != NULL);
DUK_ASSERT(comp != NULL); /* XXX: or check? */
DUK_UNREF(ctx);
/* XXX: one-based or zero-based? or expose flag(s)? */
flags = DUK_DATE_FLAG_ONEBASED | DUK_DATE_FLAG_NAN_TO_ZERO;
duk_bi_date_timeval_to_parts(time, parts, dparts, flags);
/* XXX: expensive conversion */
comp->year = (duk_uint_t) parts[DUK_DATE_IDX_YEAR];
comp->month = (duk_uint_t) parts[DUK_DATE_IDX_MONTH];
comp->day = (duk_uint_t) parts[DUK_DATE_IDX_DAY];
comp->hour = (duk_uint_t) parts[DUK_DATE_IDX_HOUR];
comp->minute = (duk_uint_t) parts[DUK_DATE_IDX_MINUTE];
comp->second = (duk_uint_t) parts[DUK_DATE_IDX_SECOND];
comp->weekday = (duk_uint_t) parts[DUK_DATE_IDX_WEEKDAY];
comp->millisecond = (duk_double_t) parts[DUK_DATE_IDX_MILLISECOND];
}
DUK_EXTERNAL duk_double_t duk_components_to_time(duk_context *ctx, duk_time_components *comp) {
duk_double_t d;
duk_double_t dparts[DUK_DATE_IDX_NUM_PARTS];
duk_uint_t flags;
DUK_ASSERT(ctx != NULL);
DUK_ASSERT(comp != NULL); /* XXX: or check? */
DUK_UNREF(ctx);
flags = 0; /* XXX */
/* XXX: expensive conversion; use array format in API instead, or unify
* time provider and time API to use same struct?
*/
dparts[DUK_DATE_IDX_YEAR] = (duk_double_t) comp->year;
dparts[DUK_DATE_IDX_MONTH] = (duk_double_t) comp->month;
dparts[DUK_DATE_IDX_DAY] = (duk_double_t) comp->day;
dparts[DUK_DATE_IDX_HOUR] = (duk_double_t) comp->hour;
dparts[DUK_DATE_IDX_MINUTE] = (duk_double_t) comp->minute;
dparts[DUK_DATE_IDX_SECOND] = (duk_double_t) comp->second;
dparts[DUK_DATE_IDX_MILLISECOND] = comp->millisecond;
dparts[DUK_DATE_IDX_WEEKDAY] = 0; /* ignored */
/* The internal call assumes zero-based day and month. */
dparts[DUK_DATE_IDX_MONTH] -= 1.0;
dparts[DUK_DATE_IDX_DAY] -= 1.0;
d = duk_bi_date_get_timeval_from_dparts(dparts, flags);
return d;
}

1
util/make_dist.py

@ -293,6 +293,7 @@ copy_files([
'duk_api_object.c',
'duk_api_string.c',
'duk_api_logging.c',
'duk_api_time.c',
'duk_api_debug.c',
'duk_bi_array.c',
'duk_bi_boolean.c',

Loading…
Cancel
Save