details: http://hg.nginx.org/njs/rev/39557c1b3088 branches: changeset: 94:39557c1b3088 user: Igor Sysoev <i...@sysoev.ru> date: Fri Apr 08 18:19:43 2016 +0300 description: Date() function.
diffstat: Makefile | 16 + njs/njs_builtin.c | 9 + njs/njs_date.c | 2104 ++++++++++++++++++++++++++++++++++++++++++++++ njs/njs_date.h | 25 + njs/njs_function.h | 1 + njs/njs_generator.c | 1 + njs/njs_lexer_keyword.c | 1 + njs/njs_object.c | 3 + njs/njs_object_hash.h | 15 + njs/njs_parser.c | 4 + njs/njs_parser.h | 5 +- njs/njs_vm.c | 8 + njs/njs_vm.h | 14 +- njs/test/njs_unit_test.c | 305 ++++++ nxt/auto/configure | 1 + nxt/auto/time | 39 + 16 files changed, 2547 insertions(+), 4 deletions(-) diffs (truncated from 2813 to 1000 lines): diff -r 91543c86f412 -r 39557c1b3088 Makefile --- a/Makefile Tue Mar 29 13:38:18 2016 +0300 +++ b/Makefile Fri Apr 08 18:19:43 2016 +0300 @@ -18,6 +18,7 @@ NXT_BUILDDIR = build $(NXT_BUILDDIR)/njs_array.o \ $(NXT_BUILDDIR)/njs_function.o \ $(NXT_BUILDDIR)/njs_regexp.o \ + $(NXT_BUILDDIR)/njs_date.o \ $(NXT_BUILDDIR)/njs_math.o \ $(NXT_BUILDDIR)/njs_extern.o \ $(NXT_BUILDDIR)/njs_variable.o \ @@ -48,6 +49,7 @@ NXT_BUILDDIR = build $(NXT_BUILDDIR)/njs_array.o \ $(NXT_BUILDDIR)/njs_function.o \ $(NXT_BUILDDIR)/njs_regexp.o \ + $(NXT_BUILDDIR)/njs_date.o \ $(NXT_BUILDDIR)/njs_math.o \ $(NXT_BUILDDIR)/njs_extern.o \ $(NXT_BUILDDIR)/njs_variable.o \ @@ -224,6 +226,20 @@ tarball: -I$(NXT_LIB) -Injs $(NXT_PCRE_CFLAGS) \ njs/njs_regexp.c +$(NXT_BUILDDIR)/njs_date.o: \ + $(NXT_BUILDDIR)/libnxt.a \ + njs/njscript.h \ + njs/njs_vm.h \ + njs/njs_string.h \ + njs/njs_object.h \ + njs/njs_function.h \ + njs/njs_date.h \ + njs/njs_date.c \ + + $(NXT_CC) -c -o $(NXT_BUILDDIR)/njs_date.o $(NXT_CFLAGS) \ + -I$(NXT_LIB) -Injs $(NXT_PCRE_CFLAGS) \ + njs/njs_date.c + $(NXT_BUILDDIR)/njs_math.o: \ $(NXT_BUILDDIR)/libnxt.a \ njs/njscript.h \ diff -r 91543c86f412 -r 39557c1b3088 njs/njs_builtin.c --- a/njs/njs_builtin.c Tue Mar 29 13:38:18 2016 +0300 +++ b/njs/njs_builtin.c Fri Apr 08 18:19:43 2016 +0300 @@ -20,6 +20,7 @@ #include <njs_array.h> #include <njs_function.h> #include <njs_regexp.h> +#include <njs_date.h> #include <njs_math.h> #include <string.h> @@ -46,6 +47,7 @@ njs_builtin_objects_create(njs_vm_t *vm) &njs_string_prototype_init, &njs_function_prototype_init, &njs_regexp_prototype_init, + &njs_date_prototype_init, }; static const njs_object_init_t *function_init[] = { @@ -56,6 +58,7 @@ njs_builtin_objects_create(njs_vm_t *vm) &njs_string_constructor_init, &njs_function_constructor_init, &njs_regexp_constructor_init, + &njs_date_constructor_init, &njs_eval_function_init, }; @@ -70,6 +73,8 @@ njs_builtin_objects_create(njs_vm_t *vm) { njs_function_constructor, { 0 } }, { njs_regexp_constructor, { NJS_SKIP_ARG, NJS_STRING_ARG, NJS_STRING_ARG } }, + { njs_date_constructor, { 0 } }, + { njs_eval_function, { 0 } }, }; @@ -178,6 +183,10 @@ njs_builtin_objects_create(njs_vm_t *vm) * RegExp.__proto__ -> Function_Prototype, * RegExp_Prototype.__proto__ -> Object_Prototype, * + * Date(), + * Date.__proto__ -> Function_Prototype, + * Date_Prototype.__proto__ -> Object_Prototype, + * * eval(), * eval.__proto__ -> Function_Prototype. */ diff -r 91543c86f412 -r 39557c1b3088 njs/njs_date.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/njs/njs_date.c Fri Apr 08 18:19:43 2016 +0300 @@ -0,0 +1,2104 @@ + +/* + * Copyright (C) Igor Sysoev + * Copyright (C) NGINX, Inc. + */ + +#include <nxt_auto_config.h> +#include <nxt_types.h> +#include <nxt_clang.h> +#include <nxt_alignment.h> +#include <nxt_stub.h> +#include <nxt_djb_hash.h> +#include <nxt_array.h> +#include <nxt_lvlhsh.h> +#include <nxt_random.h> +#include <nxt_time.h> +#include <nxt_malloc.h> +#include <nxt_mem_cache_pool.h> +#include <njscript.h> +#include <njs_vm.h> +#include <njs_number.h> +#include <njs_string.h> +#include <njs_object.h> +#include <njs_object_hash.h> +#include <njs_function.h> +#include <njs_date.h> +#include <stdio.h> +#include <string.h> +#include <sys/time.h> +#include <time.h> + + +/* + * njs_timegm() is used because + * Solaris lacks timegm(), + * FreeBSD and MacOSX timegm() cannot handle years before 1900. + */ + +#define NJS_ISO_DATE_TIME_LEN sizeof("+001970-09-28T12:00:00.000Z") + +#define NJS_DATE_TIME_LEN \ + sizeof("Mon Sep 28 1970 12:00:00 GMT+0600 (XXXXX)") + + +static nxt_noinline double njs_date_string_parse(njs_value_t *date); +static const u_char *njs_date_skip_week_day(const u_char *p, const u_char *end); +static const u_char *njs_date_skip_spaces(const u_char *p, const u_char *end); +static nxt_noinline nxt_int_t njs_date_month_parse(const u_char *p, + const u_char *end); +static nxt_noinline const u_char *njs_date_time_parse(struct tm *tm, + const u_char *p, const u_char *end); +static nxt_noinline nxt_int_t njs_date_gmtoff_parse(const u_char *start, + const u_char *end); +static nxt_noinline const u_char *njs_date_number_parse(int *value, + const u_char *p, const u_char *end, size_t size); +static int64_t njs_timegm(struct tm *tm); +static nxt_noinline njs_ret_t njs_date_string(njs_vm_t *vm, const char *fmt, + double time); +static nxt_noinline double njs_date_time(struct tm *tm, int64_t ms); +static double njs_date_utc_time(struct tm *tm, double time); +static njs_ret_t njs_date_prototype_to_json_continuation(njs_vm_t *vm, + njs_value_t *args, nxt_uint_t nargs, njs_index_t retval); + + +static const njs_value_t njs_string_invalid_date = njs_string("Invalid Date"); + + +static nxt_noinline uint64_t +njs_gettime(void) +{ + struct timeval tv; + + gettimeofday(&tv, NULL); + + return (uint64_t) tv.tv_sec * 1000 + tv.tv_usec / 1000; +} + + +njs_ret_t +njs_date_constructor(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, + njs_index_t unused) +{ + double num; + int64_t time, values[8]; + nxt_uint_t i, n; + njs_date_t *date; + struct tm tm; + + if (vm->frame->ctor) { + + if (nargs == 0) { + time = njs_gettime(); + + } else if (nargs == 2 && njs_is_string(&args[1])) { + time = njs_date_string_parse(&args[1]); + + } else { + memset(values, 0, 8 * sizeof(int64_t)); + /* Month. */ + values[2] = 1; + + n = nxt_min(8, nargs); + + for (i = 1; i < n; i++) { + if (!njs_is_numeric(&args[i])) { + return NJS_TRAP_NUMBER_ARG; + } + + num = args[i].data.u.number; + + if (njs_is_nan(num)) { + nargs = 0; + break; + } + + values[i] = num; + } + + if (nargs > 2) { + /* Year. */ + if (values[1] > 99) { + values[1] -= 1900; + } + + tm.tm_year = values[1]; + tm.tm_mon = values[2]; + tm.tm_mday = values[3]; + tm.tm_hour = values[4]; + tm.tm_min = values[5]; + tm.tm_sec = values[6]; + tm.tm_isdst = -1; + + time = (int64_t) mktime(&tm) * 1000 + values[7]; + + } else { + time = values[1]; + } + } + + date = nxt_mem_cache_alloc(vm->mem_cache_pool, sizeof(njs_date_t)); + if (nxt_slow_path(date == NULL)) { + return NXT_ERROR; + } + + nxt_lvlhsh_init(&date->object.hash); + nxt_lvlhsh_init(&date->object.shared_hash); + date->object.shared = 0; + date->object.__proto__ = &vm->prototypes[NJS_PROTOTYPE_DATE]; + + date->time = time; + + vm->retval.data.u.date = date; + vm->retval.type = NJS_DATE; + vm->retval.data.truth = 1; + + return NXT_OK; + } + + return njs_date_string(vm, "%a %b %d %Y %T GMT%z (%Z)", njs_gettime()); +} + + +static njs_ret_t +njs_date_utc(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, + njs_index_t unused) +{ + double num, time; + struct tm tm; + nxt_uint_t i, n; + int32_t values[8]; + + time = NJS_NAN; + + if (nargs > 2) { + memset(values, 0, 8 * sizeof(int32_t)); + + n = nxt_min(8, nargs); + + for (i = 1; i < n; i++) { + if (!njs_is_numeric(&args[i])) { + return NJS_TRAP_NUMBER_ARG; + } + + num = args[i].data.u.number; + + if (njs_is_nan(num)) { + goto done; + } + + values[i] = num; + } + + /* Year. */ + if (values[1] > 99) { + values[1] -= 1900; + } + + tm.tm_year = values[1]; + tm.tm_mon = values[2]; + tm.tm_mday = values[3]; + tm.tm_hour = values[4]; + tm.tm_min = values[5]; + tm.tm_sec = values[6]; + + time = njs_timegm(&tm) * 1000 + values[7]; + } + +done: + + njs_number_set(&vm->retval, time); + + return NXT_OK; +} + + +static int64_t +njs_timegm(struct tm *tm) +{ + int32_t year, month, days; + + year = tm->tm_year + 1900; + + /* + * Shift new year to March 1 and start months + * from 1 (not 0), as required for Gauss' formula. + */ + + month = tm->tm_mon - 1; + + if (month <= 0) { + month += 12; + year -= 1; + } + + /* Gauss' formula for Gregorian days since March 1, 1 BCE. */ + + /* Days in years including leap years since March 1, 1 BCE. */ + days = 365 * year + year / 4 - year / 100 + year / 400; + + /* Days before the month. */ + days += 367 * month / 12 - 30; + + /* Days before the day. */ + if (year >= 0) { + days += tm->tm_mday - 1; + + } else { + /* 1 BCE was a leap year. */ + days += tm->tm_mday - 2; + } + + /* + * 719527 days were between March 1, 1 BCE and March 1, 1970, + * 31 and 28 days were in January and February 1970. + */ + days = days - 719527 + 31 + 28; + + return (int64_t) days * 86400 + + tm->tm_hour * 3600 + + tm->tm_min * 60 + + tm->tm_sec; +} + + +static njs_ret_t +njs_date_now(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, + njs_index_t unused) +{ + njs_number_set(&vm->retval, njs_gettime()); + + return NXT_OK; +} + + +static njs_ret_t +njs_date_parse(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, + njs_index_t unused) +{ + double time; + + if (nargs > 1) { + time = njs_date_string_parse(&args[1]); + + } else { + time = NJS_NAN; + } + + njs_number_set(&vm->retval, time); + + return NXT_OK; +} + + +static nxt_noinline double +njs_date_string_parse(njs_value_t *date) +{ + int ext, ms, gmtoff; + struct tm tm; + nxt_bool_t sign; + const u_char *p, *next, *end; + njs_string_prop_t string; + + (void) njs_string_prop(&string, date); + + p = string.start; + end = p + string.size; + + if (nxt_slow_path(p + 10 >= end)) { + return NJS_NAN; + } + + if (*p == '+') { + p++; + sign = 1; + + } else if (*p == '-') { + p++; + sign = 1; + + } else { + sign = 0; + } + + next = njs_date_number_parse(&tm.tm_year, p, end, 4); + + if (next != NULL) { + /* ISO-8601 format: "1970-09-28T06:00:00.000Z" */ + + if (*next != '-') { + /* Extended ISO-8601 format: "+001970-09-28T06:00:00.000Z" */ + + next = njs_date_number_parse(&ext, next, end, 2); + if (nxt_slow_path(next == NULL)) { + return NJS_NAN; + } + + tm.tm_year = tm.tm_year * 100 + ext; + + if (string.start[0] == '-') { + if (tm.tm_year == 0) { + return NJS_NAN; + } + + tm.tm_year = -tm.tm_year; + } + + if (*next != '-') { + return NJS_NAN; + } + } + + tm.tm_year -= 1900; + + p = njs_date_number_parse(&tm.tm_mon, next + 1, end, 2); + if (nxt_slow_path(p == NULL)) { + return NJS_NAN; + } + + tm.tm_mon--; + + if (nxt_slow_path(p >= end || *p != '-')) { + return NJS_NAN; + } + + p = njs_date_number_parse(&tm.tm_mday, p + 1, end, 2); + if (nxt_slow_path(p == NULL)) { + return NJS_NAN; + } + + if (nxt_slow_path(p >= end || *p != 'T')) { + return NJS_NAN; + } + + p = njs_date_time_parse(&tm, p + 1, end); + if (nxt_slow_path(p == NULL)) { + return NJS_NAN; + } + + if (nxt_slow_path(p >= end || *p != '.')) { + return NJS_NAN; + } + + p = njs_date_number_parse(&ms, p + 1, end, 3); + if (nxt_slow_path(p == NULL)) { + return NJS_NAN; + } + + if (nxt_slow_path(p >= end || *p != 'Z')) { + return NJS_NAN; + } + + return njs_timegm(&tm) * 1000 + ms; + } + + if (sign) { + return NJS_NAN; + } + + p = njs_date_skip_week_day(p, end); + if (nxt_slow_path(p == NULL)) { + return NJS_NAN; + } + + p = njs_date_skip_spaces(p, end); + if (nxt_slow_path(p == NULL)) { + return NJS_NAN; + } + + next = njs_date_number_parse(&tm.tm_mday, p, end, 2); + + if (next != NULL) { + /* + * RFC 2822 format: + * "Mon, 28 Sep 1970 06:00:00 GMT", + * "Mon, 28 Sep 1970 06:00:00 UTC", + * "Mon, 28 Sep 1970 12:00:00 +0600". + */ + p = njs_date_skip_spaces(next, end); + if (nxt_slow_path(p == NULL)) { + return NJS_NAN; + } + + tm.tm_mon = njs_date_month_parse(p, end); + if (nxt_slow_path(tm.tm_mon < 0)) { + return NJS_NAN; + } + + p = njs_date_skip_spaces(p + 3, end); + if (nxt_slow_path(p == NULL)) { + return NJS_NAN; + } + + p = njs_date_number_parse(&tm.tm_year, p, end, 4); + if (nxt_slow_path(p == NULL)) { + return NJS_NAN; + } + + tm.tm_year -= 1900; + + p = njs_date_skip_spaces(p, end); + if (nxt_slow_path(p == NULL)) { + return NJS_NAN; + } + + p = njs_date_time_parse(&tm, p, end); + if (nxt_slow_path(p == NULL)) { + return NJS_NAN; + } + + p = njs_date_skip_spaces(p, end); + if (nxt_slow_path(p == NULL)) { + return NJS_NAN; + } + + if (p + 2 < end) { + + if ((p[0] == 'G' && p[1] == 'M' && p[2] == 'T') + || (p[0] == 'U' && p[1] == 'T' && p[2] == 'C')) + { + gmtoff = 0; + + } else { + gmtoff = njs_date_gmtoff_parse(p, end); + if (nxt_slow_path(gmtoff == -1)) { + return NJS_NAN; + } + } + + return (njs_timegm(&tm) - gmtoff * 60) * 1000; + } + + return NJS_NAN; + } + + /* Date.toString() format: "Mon Sep 28 1970 12:00:00 GMT+0600". */ + + tm.tm_mon = njs_date_month_parse(p, end); + if (nxt_slow_path(tm.tm_mon < 0)) { + return NJS_NAN; + } + + p = njs_date_skip_spaces(p + 3, end); + if (nxt_slow_path(p == NULL)) { + return NJS_NAN; + } + + p = njs_date_number_parse(&tm.tm_mday, p, end, 2); + if (nxt_slow_path(p == NULL)) { + return NJS_NAN; + } + + p = njs_date_skip_spaces(p, end); + if (nxt_slow_path(p == NULL)) { + return NJS_NAN; + } + + p = njs_date_number_parse(&tm.tm_year, p, end, 4); + if (nxt_slow_path(p == NULL)) { + return NJS_NAN; + } + + tm.tm_year -= 1900; + + p = njs_date_skip_spaces(p, end); + if (nxt_slow_path(p == NULL)) { + return NJS_NAN; + } + + p = njs_date_time_parse(&tm, p, end); + if (nxt_slow_path(p == NULL)) { + return NJS_NAN; + } + + p = njs_date_skip_spaces(p, end); + if (nxt_slow_path(p == NULL)) { + return NJS_NAN; + } + + if (p + 2 < end && p[0] == 'G' && p[1] == 'M' && p[2] == 'T') { + + gmtoff = njs_date_gmtoff_parse(&p[3], end); + + if (nxt_fast_path(gmtoff != -1)) { + return (njs_timegm(&tm) - gmtoff * 60) * 1000; + } + } + + return NJS_NAN; +} + + +static const u_char * +njs_date_skip_week_day(const u_char *p, const u_char *end) +{ + while (p < end) { + if (*p == ' ') { + return p; + } + + p++; + } + + return NULL; +} + + +static const u_char * +njs_date_skip_spaces(const u_char *p, const u_char *end) +{ + if (p < end && *p++ == ' ') { + + while (p < end) { + if (*p != ' ') { + return p; + } + + p++; + } + } + + return NULL; +} + + +static nxt_noinline nxt_int_t +njs_date_month_parse(const u_char *p, const u_char *end) +{ + if (p + 2 < end) { + switch (p[0]) { + + case 'J': + if (p[1] == 'a' && p[2] == 'n') { + return 0; + } + + if (p[1] == 'u') { + if (p[2] == 'n') { + return 5; + } + + if (p[2] == 'l') { + return 6; + } + } + + break; + + case 'F': + if (p[1] == 'e' && p[2] == 'b') { + return 1; + } + + break; + + case 'M': + if (p[1] == 'a') { + if (p[2] == 'r') { + return 2; + } + + if (p[2] == 'y') { + return 4; + } + } + + break; + + case 'A': + if (p[1] == 'p' && p[2] == 'r') { + return 3; + } + + if (p[1] == 'u' && p[2] == 'g') { + return 7; + } + + break; + + case 'S': + if (p[1] == 'e' && p[2] == 'p') { + return 8; + } + + break; + + case 'O': + if (p[1] == 'c' && p[2] == 't') { + return 9; + } + + break; + + case 'N': + if (p[1] == 'o' && p[2] == 'v') { + return 10; + } + + break; + + case 'D': + if (p[1] == 'e' && p[2] == 'c') { + return 11; + } + + break; + } + } + + return -1; +} + + +static nxt_noinline const u_char * +njs_date_time_parse(struct tm *tm, const u_char *p, const u_char *end) +{ + p = njs_date_number_parse(&tm->tm_hour, p, end, 2); + if (nxt_slow_path(p == NULL)) { + return p; + } + + if (nxt_slow_path(p >= end || *p != ':')) { + return NULL; + } + + p = njs_date_number_parse(&tm->tm_min, p + 1, end, 2); + if (nxt_slow_path(p == NULL)) { + return p; + } + + if (nxt_slow_path(p >= end || *p != ':')) { + return NULL; + } + + return njs_date_number_parse(&tm->tm_sec, p + 1, end, 2); +} + + +static nxt_noinline nxt_int_t +njs_date_gmtoff_parse(const u_char *start, const u_char *end) +{ + int gmtoff, hour, min; + const u_char *p; + + if (nxt_fast_path(start + 4 < end && (*start == '+' || *start == '-'))) { + + p = njs_date_number_parse(&hour, start + 1, end, 2); + if (nxt_slow_path(p == NULL)) { + return -1; + } + + p = njs_date_number_parse(&min, p, end, 2); + if (nxt_slow_path(p == NULL)) { + return -1; + } + + gmtoff = hour * 60 + min; + + if (*start == '-') { + gmtoff = -gmtoff; + } + + return gmtoff; + } + + return -1; +} + + +static nxt_noinline const u_char * +njs_date_number_parse(int *value, const u_char *p, const u_char *end, + size_t size) +{ + u_char c; + nxt_int_t n; + + n = 0; + + do { + if (nxt_slow_path(p >= end)) { + return NULL; + } + + c = *p++; + + /* Values below '0' become >= 208. */ + c = c - '0'; + + if (nxt_slow_path(c > 9)) { + return NULL; + } + + n = n * 10 + c; + + size--; + + } while (size != 0); + + *value = n; + + return p; +} + + +static const njs_object_prop_t njs_date_constructor_properties[] = +{ + /* Date.name == "Date". */ + { + .type = NJS_PROPERTY, + .name = njs_string("name"), + .value = njs_string("Date"), + }, + + /* Date.length == 7. */ + { + .type = NJS_PROPERTY, + .name = njs_string("length"), + .value = njs_value(NJS_NUMBER, 1, 7.0), + }, + + /* Date.prototype. */ + { + .type = NJS_NATIVE_GETTER, + .name = njs_string("prototype"), + .value = njs_native_getter(njs_object_prototype_create), + }, + + { + .type = NJS_METHOD, + .name = njs_string("UTC"), + .value = njs_native_function(njs_date_utc, 0, 0), + }, + + { + .type = NJS_METHOD, + .name = njs_string("now"), + .value = njs_native_function(njs_date_now, 0, 0), + }, + + { + .type = NJS_METHOD, + .name = njs_string("parse"), + .value = njs_native_function(njs_date_parse, 0, + NJS_SKIP_ARG, NJS_STRING_ARG), + }, +}; + + +const njs_object_init_t njs_date_constructor_init = { + njs_date_constructor_properties, + nxt_nitems(njs_date_constructor_properties), +}; + + +static njs_ret_t +njs_date_prototype_value_of(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, + njs_index_t unused) +{ + njs_number_set(&vm->retval, args[0].data.u.date->time); + + return NXT_OK; +} + + +static njs_ret_t +njs_date_prototype_to_string(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs, + njs_index_t unused) +{ + return njs_date_string(vm, "%a %b %d %Y %T GMT%z (%Z)", + args[0].data.u.date->time); +} + + +static njs_ret_t +njs_date_prototype_to_date_string(njs_vm_t *vm, njs_value_t *args, + nxt_uint_t nargs, njs_index_t unused) +{ + return njs_date_string(vm, "%a %b %d %Y", args[0].data.u.date->time); +} + + +static njs_ret_t +njs_date_prototype_to_time_string(njs_vm_t *vm, njs_value_t *args, + nxt_uint_t nargs, njs_index_t unused) +{ + return njs_date_string(vm, "%T GMT%z (%Z)", args[0].data.u.date->time); +} + + +static nxt_noinline njs_ret_t +njs_date_string(njs_vm_t *vm, const char *fmt, double time) +{ + size_t size; + time_t clock; + u_char *start; + u_char buf[NJS_DATE_TIME_LEN]; + struct tm tm; + + if (!njs_is_nan(time)) { + clock = time / 1000; + localtime_r(&clock, &tm); + + size = strftime((char *) buf, NJS_DATE_TIME_LEN, fmt, &tm); + + start = njs_string_alloc(vm, &vm->retval, size, size); + + if (nxt_fast_path(start != NULL)) { + memcpy(start, buf, size); + return NXT_OK; + } + + return NXT_ERROR; + } + + vm->retval = njs_string_invalid_date; + + return NXT_OK; +} + + +static njs_ret_t +njs_date_prototype_to_utc_string(njs_vm_t *vm, njs_value_t *args, + nxt_uint_t nargs, njs_index_t unused) +{ + double time; + size_t size; + time_t clock; + u_char *start; + u_char buf[NJS_DATE_TIME_LEN]; + struct tm tm; + + static const char *week[] = { "Sun", "Mon", "Tue", "Wed", + "Thu", "Fri", "Sat" }; + + static const char *month[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; + + time = args[0].data.u.date->time; + + if (!njs_is_nan(time)) { + clock = time / 1000; + gmtime_r(&clock, &tm); + + size = snprintf((char *) buf, NJS_DATE_TIME_LEN, + "%s %s %02d %4d %02d:%02d:%02d GMT", + week[tm.tm_wday], month[tm.tm_mon], + tm.tm_mday, tm.tm_year + 1900, + tm.tm_hour, tm.tm_min, tm.tm_sec); + + start = njs_string_alloc(vm, &vm->retval, size, size); + + if (nxt_fast_path(start != NULL)) { + memcpy(start, buf, size); + return NXT_OK; + } + + return NXT_ERROR; + } + + vm->retval = njs_string_invalid_date; + + return NXT_OK; +} + + +static njs_ret_t +njs_date_prototype_to_iso_string(njs_vm_t *vm, njs_value_t *args, + nxt_uint_t nargs, njs_index_t unused) +{ + int32_t year; _______________________________________________ nginx-devel mailing list nginx-devel@nginx.org http://mailman.nginx.org/mailman/listinfo/nginx-devel