JavaScript-QuickJS
view release on metacpan or search on metacpan
quickjs/quickjs.c view on Meta::CPAN
if (is_local)
d += getTimezoneOffset(d) * 60000;
return time_clip(d);
}
static JSValue get_date_field(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv, int magic)
{
// get_date_field(obj, n, is_local)
double fields[9];
int res, n, is_local;
is_local = magic & 0x0F;
n = (magic >> 4) & 0x0F;
res = get_date_fields(ctx, this_val, fields, is_local, 0);
if (res < 0)
return JS_EXCEPTION;
if (!res)
return JS_NAN;
if (magic & 0x100) { // getYear
fields[0] -= 1900;
}
return JS_NewFloat64(ctx, fields[n]);
}
static JSValue set_date_field(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv, int magic)
{
// _field(obj, first_field, end_field, args, is_local)
double fields[9];
int res, first_field, end_field, is_local, i, n;
double d, a;
d = NAN;
first_field = (magic >> 8) & 0x0F;
end_field = (magic >> 4) & 0x0F;
is_local = magic & 0x0F;
res = get_date_fields(ctx, this_val, fields, is_local, first_field == 0);
if (res < 0)
return JS_EXCEPTION;
// Argument coercion is observable and must be done unconditionally.
n = min_int(argc, end_field - first_field);
for(i = 0; i < n; i++) {
if (JS_ToFloat64(ctx, &a, argv[i]))
return JS_EXCEPTION;
if (!isfinite(a))
res = FALSE;
fields[first_field + i] = trunc(a);
}
if (res && argc > 0) {
d = set_date_fields(fields, is_local);
}
return JS_SetThisTimeValue(ctx, this_val, d);
}
/* fmt:
0: toUTCString: "Tue, 02 Jan 2018 23:04:46 GMT"
1: toString: "Wed Jan 03 2018 00:05:22 GMT+0100 (CET)"
2: toISOString: "2018-01-02T23:02:56.927Z"
3: toLocaleString: "1/2/2018, 11:40:40 PM"
part: 1=date, 2=time 3=all
XXX: should use a variant of strftime().
*/
static JSValue get_date_string(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv, int magic)
{
// _string(obj, fmt, part)
char buf[64];
double fields[9];
int res, fmt, part, pos;
int y, mon, d, h, m, s, ms, wd, tz;
fmt = (magic >> 4) & 0x0F;
part = magic & 0x0F;
res = get_date_fields(ctx, this_val, fields, fmt & 1, 0);
if (res < 0)
return JS_EXCEPTION;
if (!res) {
if (fmt == 2)
return JS_ThrowRangeError(ctx, "Date value is NaN");
else
return JS_NewString(ctx, "Invalid Date");
}
y = fields[0];
mon = fields[1];
d = fields[2];
h = fields[3];
m = fields[4];
s = fields[5];
ms = fields[6];
wd = fields[7];
tz = fields[8];
pos = 0;
if (part & 1) { /* date part */
switch(fmt) {
case 0:
pos += snprintf(buf + pos, sizeof(buf) - pos,
"%.3s, %02d %.3s %0*d ",
day_names + wd * 3, d,
month_names + mon * 3, 4 + (y < 0), y);
break;
case 1:
pos += snprintf(buf + pos, sizeof(buf) - pos,
"%.3s %.3s %02d %0*d",
day_names + wd * 3,
month_names + mon * 3, d, 4 + (y < 0), y);
if (part == 3) {
buf[pos++] = ' ';
}
break;
case 2:
if (y >= 0 && y <= 9999) {
pos += snprintf(buf + pos, sizeof(buf) - pos,
"%04d", y);
( run in 2.589 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )