📄 b_date.c
字号:
/*
* The builtin Date object.
* Copyright (c) 1998 New Generation Software (NGS) Oy
*
* Author: Markku Rossi <mtr@ngs.fi>
*/
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA
*/
/*
* $Source: /cygdrive/c/RCVS/CVS/ReactOS/reactos/lib/kjs/src/b_date.c,v $
* $Id: b_date.c 21681 2006-04-21 15:00:24Z peterw $
*/
#include "jsint.h"
#include "rentrant.h"
/* XXX TODO 15.13.3 -> */
/*
* Types and definitions.
*/
#define GMT_DATE_FORMAT "%a, %d %b %Y %H:%M:%S GMT"
#define MS_PER_SECOND 1000
#define MS_PER_MINUTE (60 * MS_PER_SECOND)
#define MS_PER_HOUR (60 * MS_PER_MINUTE)
#define MS_PER_DAY (24 * MS_PER_HOUR)
/* Class context. */
struct date_ctx_st
{
/* Static methods. */
JSSymbol s_parse;
/* Methods. */
JSSymbol s_format;
JSSymbol s_formatGMT;
JSSymbol s_getDate;
JSSymbol s_getDay;
JSSymbol s_getHours;
JSSymbol s_getMinutes;
JSSymbol s_getMonth;
JSSymbol s_getSeconds;
JSSymbol s_getTime;
JSSymbol s_getTimezoneOffset;
JSSymbol s_getYear;
JSSymbol s_setDate;
JSSymbol s_setHours;
JSSymbol s_setMinutes;
JSSymbol s_setMonth;
JSSymbol s_setSeconds;
JSSymbol s_setTime;
JSSymbol s_setYear;
JSSymbol s_toGMTString;
JSSymbol s_toLocaleString;
JSSymbol s_UTC;
};
typedef struct date_ctx_st DateCtx;
/* Date instance context. */
struct date_instance_ctx_st
{
time_t secs;
struct tm localtime;
};
typedef struct date_instance_ctx_st DateInstanceCtx;
/*
* Static functions.
*/
/* Global methods. */
void
MakeTime_global_method (JSVirtualMachine *vm, JSBuiltinInfo *builtin_info,
void *instance_context, JSNode *result_return,
JSNode *args)
{
if (args->u.vinteger != 4)
{
sprintf (vm->error, "MakeTime: illegal amount of argument");
js_vm_error (vm);
}
if (!JS_IS_NUMBER (&args[1]) || !JS_IS_NUMBER (&args[2])
|| !JS_IS_NUMBER (&args[3]) || !JS_IS_NUMBER (&args[4]))
{
sprintf (vm->error, "MakeTime: illegal argument");
js_vm_error (vm);
}
if (!JS_IS_FINITE (&args[1]) || !JS_IS_FINITE (&args[2])
|| !JS_IS_FINITE (&args[3]) || !JS_IS_FINITE (&args[4]))
{
result_return->type = JS_NAN;
}
else
{
JSInt32 hour, min, sec, ms;
hour = js_vm_to_int32 (vm, &args[1]);
min = js_vm_to_int32 (vm, &args[2]);
sec = js_vm_to_int32 (vm, &args[3]);
ms = js_vm_to_int32 (vm, &args[4]);
result_return->type = JS_FLOAT;
result_return->u.vfloat = (hour * MS_PER_HOUR
+ min * MS_PER_MINUTE
+ sec * MS_PER_SECOND
+ ms);
}
}
void
MakeDay_global_method (JSVirtualMachine *vm, JSBuiltinInfo *builtin_info,
void *instance_context, JSNode *result_return,
JSNode *args)
{
if (args->u.vinteger != 3)
{
sprintf (vm->error, "MakeDay: illegal amount of argument");
js_vm_error (vm);
}
if (!JS_IS_NUMBER (&args[1]) || !JS_IS_NUMBER (&args[2])
|| !JS_IS_NUMBER (&args[3]))
{
sprintf (vm->error, "MakeDay: illegal argument");
js_vm_error (vm);
}
if (!JS_IS_FINITE (&args[1]) || !JS_IS_FINITE (&args[2])
|| !JS_IS_FINITE (&args[3]))
{
result_return->type = JS_NAN;
}
else
{
JSInt32 year, month, day;
year = js_vm_to_int32 (vm, &args[1]);
month = js_vm_to_int32 (vm, &args[2]);
day = js_vm_to_int32 (vm, &args[3]);
sprintf (vm->error, "MakeDay: not implemented yet");
js_vm_error (vm);
}
}
void
MakeDate_global_method (JSVirtualMachine *vm, JSBuiltinInfo *builtin_info,
void *instance_context, JSNode *result_return,
JSNode *args)
{
if (args->u.vinteger != 2)
{
sprintf (vm->error, "MakeDate: illegal amount of argument");
js_vm_error (vm);
}
if (!JS_IS_NUMBER (&args[1]) || !JS_IS_NUMBER (&args[2]))
{
sprintf (vm->error, "MakeDate: illegal argument");
js_vm_error (vm);
}
if (!JS_IS_FINITE (&args[1]) || !JS_IS_FINITE (&args[2]))
{
result_return->type = JS_NAN;
}
else
{
JSInt32 day;
JSInt32 time;
day = js_vm_to_int32 (vm, &args[1]);
time = js_vm_to_int32 (vm, &args[2]);
result_return->type = JS_FLOAT;
result_return->u.vfloat = (day * MS_PER_DAY + time);
}
}
void
TimeClip_global_method (JSVirtualMachine *vm, JSBuiltinInfo *builtin_info,
void *instance_context, JSNode *result_return,
JSNode *args)
{
if (args->u.vinteger != 1)
{
sprintf (vm->error, "TimeClip: illegal amount of argument");
js_vm_error (vm);
}
if (!JS_IS_NUMBER (&args[1]))
{
sprintf (vm->error, "TimeClip: illegal argument");
js_vm_error (vm);
}
if (!JS_IS_FINITE (&args[1]))
{
result_return->type = JS_NAN;
}
else
{
result_return->type = JS_FLOAT;
if (args[1].type == JS_INTEGER)
result_return->u.vfloat = (double) args[1].u.vinteger;
else
result_return->u.vfloat = args[1].u.vfloat;
if (result_return->u.vfloat > 8.64e15
|| result_return->u.vfloat < -8.64e15)
result_return->type = JS_NAN;
}
}
/* Method proc. */
static int
method (JSVirtualMachine *vm, JSBuiltinInfo *builtin_info,
void *instance_context, JSSymbol method, JSNode *result_return,
JSNode *args)
{
DateCtx *ctx = builtin_info->obj_context;
DateInstanceCtx *ictx = instance_context;
/* The default return type is integer. */
result_return->type = JS_INTEGER;
/* Static methods. */
if (method == ctx->s_parse)
{
goto not_implemented_yet;
}
/* ********************************************************************** */
else if (method == vm->syms.s_toString)
{
if (args->u.vinteger != 0)
goto argument_error;
if (ictx)
goto date_to_string;
else
js_vm_make_static_string (vm, result_return, "Date", 4);
}
/* ********************************************************************** */
else if (ictx)
{
/* Methods. */
if (method == ctx->s_format || method == ctx->s_formatGMT)
{
struct tm tm_st;
struct tm *tm = &ictx->localtime;
char *fmt;
char *buf;
unsigned int buflen;
if (args->u.vinteger != 1)
goto argument_error;
if (args[1].type != JS_STRING)
goto argument_type_error;
fmt = js_string_to_c_string (vm, &args[1]);
buflen = args[1].u.vstring->len * 2 + 1;
buf = js_malloc (vm, buflen);
if (method == ctx->s_formatGMT)
{
js_gmtime (&ictx->secs, &tm_st);
tm = &tm_st;
}
if (args[1].u.vstring->len == 0)
buf[0] = '\0';
else
{
while (strftime (buf, buflen, fmt, tm) == 0)
{
/* Expand the buffer. */
buflen *= 2;
buf = js_realloc (vm, buf, buflen);
}
}
js_vm_make_string (vm, result_return, buf, strlen (buf));
js_free (fmt);
js_free (buf);
}
/* ***************************************************************** */
else if (method == ctx->s_getDate)
{
if (args->u.vinteger != 0)
goto argument_error;
result_return->u.vinteger = ictx->localtime.tm_mday;
}
/* ***************************************************************** */
else if (method == ctx->s_getDay)
{
if (args->u.vinteger != 0)
goto argument_error;
result_return->u.vinteger = ictx->localtime.tm_wday;
}
/* ***************************************************************** */
else if (method == ctx->s_getHours)
{
if (args->u.vinteger != 0)
goto argument_error;
result_return->u.vinteger = ictx->localtime.tm_hour;
}
/* ***************************************************************** */
else if (method == ctx->s_getMinutes)
{
if (args->u.vinteger != 0)
goto argument_error;
result_return->u.vinteger = ictx->localtime.tm_min;
}
/* ***************************************************************** */
else if (method == ctx->s_getMonth)
{
if (args->u.vinteger != 0)
goto argument_error;
result_return->u.vinteger = ictx->localtime.tm_mon;
}
/* ***************************************************************** */
else if (method == ctx->s_getSeconds)
{
if (args->u.vinteger != 0)
goto argument_error;
result_return->u.vinteger = ictx->localtime.tm_sec;
}
/* ***************************************************************** */
else if (method == ctx->s_getTime)
{
if (args->u.vinteger != 0)
goto argument_error;
result_return->type = JS_FLOAT;
result_return->u.vfloat = (double) ictx->secs * 1000;
}
/* ***************************************************************** */
else if (method == ctx->s_getTimezoneOffset)
goto not_implemented_yet;
/* ***************************************************************** */
else if (method == ctx->s_getYear)
{
if (args->u.vinteger != 0)
goto argument_error;
result_return->u.vinteger = ictx->localtime.tm_year;
if (ictx->localtime.tm_year >= 100
|| ictx->localtime.tm_year < 0)
result_return->u.vinteger += 1900;
}
/* ***************************************************************** */
else if (method == ctx->s_setDate)
{
if (args->u.vinteger != 1)
goto argument_error;
if (args[1].type != JS_INTEGER)
goto argument_type_error;
if (1 <= args[1].u.vinteger && args[1].u.vinteger <= 31)
{
ictx->localtime.tm_mday = args[1].u.vinteger;
ictx->secs = mktime (&ictx->localtime);
}
else
goto argument_range_error;
}
/* ***************************************************************** */
else if (method == ctx->s_setHours)
{
if (args->u.vinteger != 1)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -