2024-02-24 21:00:04 +00:00
|
|
|
package otto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
2024-04-04 14:46:14 +00:00
|
|
|
"time"
|
2024-02-24 21:00:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Date
|
|
|
|
|
|
|
|
const (
|
|
|
|
// TODO Be like V8?
|
2024-04-04 14:46:14 +00:00
|
|
|
// builtinDateDateTimeLayout = "Mon Jan 2 2006 15:04:05 GMT-0700 (MST)".
|
|
|
|
builtinDateDateTimeLayout = time.RFC1123 // "Mon, 02 Jan 2006 15:04:05 MST"
|
|
|
|
builtinDateDateLayout = "Mon, 02 Jan 2006"
|
|
|
|
builtinDateTimeLayout = "15:04:05 MST"
|
2024-02-24 21:00:04 +00:00
|
|
|
)
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
// utcTimeZone is the time zone used for UTC calculations.
|
|
|
|
// It is GMT not UTC as that's what Javascript does because toUTCString is
|
|
|
|
// actually an alias to toGMTString.
|
|
|
|
var utcTimeZone = time.FixedZone("GMT", 0)
|
2024-02-24 21:00:04 +00:00
|
|
|
|
|
|
|
func builtinDate(call FunctionCall) Value {
|
2024-04-04 14:46:14 +00:00
|
|
|
date := &dateObject{}
|
|
|
|
date.Set(newDateTime([]Value{}, time.Local)) //nolint: gosmopolitan
|
|
|
|
return stringValue(date.Time().Format(builtinDateDateTimeLayout))
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinNewDate(obj *object, argumentList []Value) Value {
|
|
|
|
return objectValue(obj.runtime.newDate(newDateTime(argumentList, time.Local))) //nolint: gosmopolitan
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateToString(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
2024-04-04 14:46:14 +00:00
|
|
|
return stringValue("Invalid Date")
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
return stringValue(date.Time().Local().Format(builtinDateDateTimeLayout)) //nolint: gosmopolitan
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateToDateString(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
2024-04-04 14:46:14 +00:00
|
|
|
return stringValue("Invalid Date")
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
return stringValue(date.Time().Local().Format(builtinDateDateLayout)) //nolint: gosmopolitan
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateToTimeString(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
2024-04-04 14:46:14 +00:00
|
|
|
return stringValue("Invalid Date")
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
return stringValue(date.Time().Local().Format(builtinDateTimeLayout)) //nolint: gosmopolitan
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateToUTCString(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
2024-04-04 14:46:14 +00:00
|
|
|
return stringValue("Invalid Date")
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
return stringValue(date.Time().In(utcTimeZone).Format(builtinDateDateTimeLayout))
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateToISOString(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
2024-04-04 14:46:14 +00:00
|
|
|
return stringValue("Invalid Date")
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
return stringValue(date.Time().Format("2006-01-02T15:04:05.000Z"))
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateToJSON(call FunctionCall) Value {
|
|
|
|
obj := call.thisObject()
|
|
|
|
value := obj.DefaultValue(defaultValueHintNumber) // FIXME object.primitiveNumberValue
|
|
|
|
// FIXME fv.isFinite
|
|
|
|
if fv := value.float64(); math.IsNaN(fv) || math.IsInf(fv, 0) {
|
|
|
|
return nullValue
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
|
|
|
|
toISOString := obj.get("toISOString")
|
2024-02-24 21:00:04 +00:00
|
|
|
if !toISOString.isCallable() {
|
|
|
|
// FIXME
|
2024-04-04 14:46:14 +00:00
|
|
|
panic(call.runtime.panicTypeError("Date.toJSON toISOString %q is not callable", toISOString))
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
return toISOString.call(call.runtime, objectValue(obj), []Value{})
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateToGMTString(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
2024-04-04 14:46:14 +00:00
|
|
|
return stringValue("Invalid Date")
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
return stringValue(date.Time().Format("Mon, 02 Jan 2006 15:04:05 GMT"))
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateGetTime(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
|
|
|
// We do this (convert away from a float) so the user
|
|
|
|
// does not get something back in exponential notation
|
2024-04-04 14:46:14 +00:00
|
|
|
return int64Value(date.Epoch())
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateSetTime(call FunctionCall) Value {
|
|
|
|
obj := call.thisObject()
|
2024-02-24 21:00:04 +00:00
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
date.Set(call.Argument(0).float64())
|
2024-04-04 14:46:14 +00:00
|
|
|
obj.value = date
|
2024-02-24 21:00:04 +00:00
|
|
|
return date.Value()
|
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateBeforeSet(call FunctionCall, argumentLimit int, timeLocal bool) (*object, *dateObject, *ecmaTime, []int) {
|
|
|
|
obj := call.thisObject()
|
2024-02-24 21:00:04 +00:00
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
|
|
|
return nil, nil, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if argumentLimit > len(call.ArgumentList) {
|
|
|
|
argumentLimit = len(call.ArgumentList)
|
|
|
|
}
|
|
|
|
|
|
|
|
if argumentLimit == 0 {
|
2024-04-04 14:46:14 +00:00
|
|
|
obj.value = invalidDateObject
|
2024-02-24 21:00:04 +00:00
|
|
|
return nil, nil, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
valueList := make([]int, argumentLimit)
|
|
|
|
for index := 0; index < argumentLimit; index++ {
|
|
|
|
value := call.ArgumentList[index]
|
|
|
|
nm := value.number()
|
|
|
|
switch nm.kind {
|
|
|
|
case numberInteger, numberFloat:
|
|
|
|
default:
|
2024-04-04 14:46:14 +00:00
|
|
|
obj.value = invalidDateObject
|
2024-02-24 21:00:04 +00:00
|
|
|
return nil, nil, nil, nil
|
|
|
|
}
|
|
|
|
valueList[index] = int(nm.int64)
|
|
|
|
}
|
|
|
|
baseTime := date.Time()
|
|
|
|
if timeLocal {
|
2024-04-04 14:46:14 +00:00
|
|
|
baseTime = baseTime.Local() //nolint: gosmopolitan
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
ecmaTime := newEcmaTime(baseTime)
|
|
|
|
return obj, &date, &ecmaTime, valueList
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateParse(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
date := call.Argument(0).string()
|
2024-04-04 14:46:14 +00:00
|
|
|
return float64Value(dateParse(date))
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateUTC(call FunctionCall) Value {
|
|
|
|
return float64Value(newDateTime(call.ArgumentList, time.UTC))
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateNow(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
call.ArgumentList = []Value(nil)
|
2024-04-04 14:46:14 +00:00
|
|
|
return builtinDateUTC(call)
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
// This is a placeholder.
|
|
|
|
func builtinDateToLocaleString(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
2024-04-04 14:46:14 +00:00
|
|
|
return stringValue("Invalid Date")
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
return stringValue(date.Time().Local().Format("2006-01-02 15:04:05")) //nolint: gosmopolitan
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
// This is a placeholder.
|
|
|
|
func builtinDateToLocaleDateString(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
2024-04-04 14:46:14 +00:00
|
|
|
return stringValue("Invalid Date")
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
return stringValue(date.Time().Local().Format("2006-01-02")) //nolint: gosmopolitan
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
// This is a placeholder.
|
|
|
|
func builtinDateToLocaleTimeString(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
2024-04-04 14:46:14 +00:00
|
|
|
return stringValue("Invalid Date")
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
return stringValue(date.Time().Local().Format("15:04:05")) //nolint: gosmopolitan
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateValueOf(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
|
|
|
return date.Value()
|
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateGetYear(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
// Will throw a TypeError is ThisObject is nil or
|
|
|
|
// does not have Class of "Date"
|
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
return intValue(date.Time().Local().Year() - 1900) //nolint: gosmopolitan
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateGetFullYear(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
// Will throw a TypeError is ThisObject is nil or
|
|
|
|
// does not have Class of "Date"
|
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
return intValue(date.Time().Local().Year()) //nolint: gosmopolitan
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateGetUTCFullYear(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
return intValue(date.Time().Year())
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateGetMonth(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
return intValue(dateFromGoMonth(date.Time().Local().Month())) //nolint: gosmopolitan
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateGetUTCMonth(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
return intValue(dateFromGoMonth(date.Time().Month()))
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateGetDate(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
return intValue(date.Time().Local().Day()) //nolint: gosmopolitan
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateGetUTCDate(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
return intValue(date.Time().Day())
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateGetDay(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
// Actually day of the week
|
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
return intValue(dateFromGoDay(date.Time().Local().Weekday())) //nolint: gosmopolitan
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateGetUTCDay(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
return intValue(dateFromGoDay(date.Time().Weekday()))
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateGetHours(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
return intValue(date.Time().Local().Hour()) //nolint: gosmopolitan
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateGetUTCHours(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
return intValue(date.Time().Hour())
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateGetMinutes(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
return intValue(date.Time().Local().Minute()) //nolint: gosmopolitan
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateGetUTCMinutes(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
return intValue(date.Time().Minute())
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateGetSeconds(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
return intValue(date.Time().Local().Second()) //nolint: gosmopolitan
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateGetUTCSeconds(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
return intValue(date.Time().Second())
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateGetMilliseconds(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
return intValue(date.Time().Local().Nanosecond() / (100 * 100 * 100)) //nolint: gosmopolitan
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateGetUTCMilliseconds(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
return intValue(date.Time().Nanosecond() / (100 * 100 * 100))
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateGetTimezoneOffset(call FunctionCall) Value {
|
2024-02-24 21:00:04 +00:00
|
|
|
date := dateObjectOf(call.runtime, call.thisObject())
|
|
|
|
if date.isNaN {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
2024-04-04 14:46:14 +00:00
|
|
|
timeLocal := date.Time().Local() //nolint: gosmopolitan
|
2024-02-24 21:00:04 +00:00
|
|
|
// Is this kosher?
|
2024-04-04 14:46:14 +00:00
|
|
|
timeLocalAsUTC := time.Date(
|
2024-02-24 21:00:04 +00:00
|
|
|
timeLocal.Year(),
|
|
|
|
timeLocal.Month(),
|
|
|
|
timeLocal.Day(),
|
|
|
|
timeLocal.Hour(),
|
|
|
|
timeLocal.Minute(),
|
|
|
|
timeLocal.Second(),
|
|
|
|
timeLocal.Nanosecond(),
|
2024-04-04 14:46:14 +00:00
|
|
|
time.UTC,
|
2024-02-24 21:00:04 +00:00
|
|
|
)
|
2024-04-04 14:46:14 +00:00
|
|
|
return float64Value(date.Time().Sub(timeLocalAsUTC).Seconds() / 60)
|
2024-02-24 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateSetMilliseconds(call FunctionCall) Value {
|
|
|
|
obj, date, ecmaTime, value := builtinDateBeforeSet(call, 1, true)
|
2024-02-24 21:00:04 +00:00
|
|
|
if ecmaTime == nil {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
ecmaTime.millisecond = value[0]
|
|
|
|
|
|
|
|
date.SetTime(ecmaTime.goTime())
|
2024-04-04 14:46:14 +00:00
|
|
|
obj.value = *date
|
2024-02-24 21:00:04 +00:00
|
|
|
return date.Value()
|
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateSetUTCMilliseconds(call FunctionCall) Value {
|
|
|
|
obj, date, ecmaTime, value := builtinDateBeforeSet(call, 1, false)
|
2024-02-24 21:00:04 +00:00
|
|
|
if ecmaTime == nil {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
ecmaTime.millisecond = value[0]
|
|
|
|
|
|
|
|
date.SetTime(ecmaTime.goTime())
|
2024-04-04 14:46:14 +00:00
|
|
|
obj.value = *date
|
2024-02-24 21:00:04 +00:00
|
|
|
return date.Value()
|
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateSetSeconds(call FunctionCall) Value {
|
|
|
|
obj, date, ecmaTime, value := builtinDateBeforeSet(call, 2, true)
|
2024-02-24 21:00:04 +00:00
|
|
|
if ecmaTime == nil {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(value) > 1 {
|
|
|
|
ecmaTime.millisecond = value[1]
|
|
|
|
}
|
|
|
|
ecmaTime.second = value[0]
|
|
|
|
|
|
|
|
date.SetTime(ecmaTime.goTime())
|
2024-04-04 14:46:14 +00:00
|
|
|
obj.value = *date
|
2024-02-24 21:00:04 +00:00
|
|
|
return date.Value()
|
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateSetUTCSeconds(call FunctionCall) Value {
|
|
|
|
obj, date, ecmaTime, value := builtinDateBeforeSet(call, 2, false)
|
2024-02-24 21:00:04 +00:00
|
|
|
if ecmaTime == nil {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(value) > 1 {
|
|
|
|
ecmaTime.millisecond = value[1]
|
|
|
|
}
|
|
|
|
ecmaTime.second = value[0]
|
|
|
|
|
|
|
|
date.SetTime(ecmaTime.goTime())
|
2024-04-04 14:46:14 +00:00
|
|
|
obj.value = *date
|
2024-02-24 21:00:04 +00:00
|
|
|
return date.Value()
|
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateSetMinutes(call FunctionCall) Value {
|
|
|
|
obj, date, ecmaTime, value := builtinDateBeforeSet(call, 3, true)
|
2024-02-24 21:00:04 +00:00
|
|
|
if ecmaTime == nil {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(value) > 2 {
|
|
|
|
ecmaTime.millisecond = value[2]
|
|
|
|
ecmaTime.second = value[1]
|
|
|
|
} else if len(value) > 1 {
|
|
|
|
ecmaTime.second = value[1]
|
|
|
|
}
|
|
|
|
ecmaTime.minute = value[0]
|
|
|
|
|
|
|
|
date.SetTime(ecmaTime.goTime())
|
2024-04-04 14:46:14 +00:00
|
|
|
obj.value = *date
|
2024-02-24 21:00:04 +00:00
|
|
|
return date.Value()
|
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateSetUTCMinutes(call FunctionCall) Value {
|
|
|
|
obj, date, ecmaTime, value := builtinDateBeforeSet(call, 3, false)
|
2024-02-24 21:00:04 +00:00
|
|
|
if ecmaTime == nil {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(value) > 2 {
|
|
|
|
ecmaTime.millisecond = value[2]
|
|
|
|
ecmaTime.second = value[1]
|
|
|
|
} else if len(value) > 1 {
|
|
|
|
ecmaTime.second = value[1]
|
|
|
|
}
|
|
|
|
ecmaTime.minute = value[0]
|
|
|
|
|
|
|
|
date.SetTime(ecmaTime.goTime())
|
2024-04-04 14:46:14 +00:00
|
|
|
obj.value = *date
|
2024-02-24 21:00:04 +00:00
|
|
|
return date.Value()
|
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateSetHours(call FunctionCall) Value {
|
|
|
|
obj, date, ecmaTime, value := builtinDateBeforeSet(call, 4, true)
|
2024-02-24 21:00:04 +00:00
|
|
|
if ecmaTime == nil {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
switch {
|
|
|
|
case len(value) > 3:
|
2024-02-24 21:00:04 +00:00
|
|
|
ecmaTime.millisecond = value[3]
|
2024-04-04 14:46:14 +00:00
|
|
|
fallthrough
|
|
|
|
case len(value) > 2:
|
2024-02-24 21:00:04 +00:00
|
|
|
ecmaTime.second = value[2]
|
2024-04-04 14:46:14 +00:00
|
|
|
fallthrough
|
|
|
|
case len(value) > 1:
|
2024-02-24 21:00:04 +00:00
|
|
|
ecmaTime.minute = value[1]
|
|
|
|
}
|
|
|
|
ecmaTime.hour = value[0]
|
|
|
|
|
|
|
|
date.SetTime(ecmaTime.goTime())
|
2024-04-04 14:46:14 +00:00
|
|
|
obj.value = *date
|
2024-02-24 21:00:04 +00:00
|
|
|
return date.Value()
|
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateSetUTCHours(call FunctionCall) Value {
|
|
|
|
obj, date, ecmaTime, value := builtinDateBeforeSet(call, 4, false)
|
2024-02-24 21:00:04 +00:00
|
|
|
if ecmaTime == nil {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
switch {
|
|
|
|
case len(value) > 3:
|
2024-02-24 21:00:04 +00:00
|
|
|
ecmaTime.millisecond = value[3]
|
2024-04-04 14:46:14 +00:00
|
|
|
fallthrough
|
|
|
|
case len(value) > 2:
|
2024-02-24 21:00:04 +00:00
|
|
|
ecmaTime.second = value[2]
|
2024-04-04 14:46:14 +00:00
|
|
|
fallthrough
|
|
|
|
case len(value) > 1:
|
2024-02-24 21:00:04 +00:00
|
|
|
ecmaTime.minute = value[1]
|
|
|
|
}
|
|
|
|
ecmaTime.hour = value[0]
|
|
|
|
|
|
|
|
date.SetTime(ecmaTime.goTime())
|
2024-04-04 14:46:14 +00:00
|
|
|
obj.value = *date
|
2024-02-24 21:00:04 +00:00
|
|
|
return date.Value()
|
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateSetDate(call FunctionCall) Value {
|
|
|
|
obj, date, ecmaTime, value := builtinDateBeforeSet(call, 1, true)
|
2024-02-24 21:00:04 +00:00
|
|
|
if ecmaTime == nil {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
ecmaTime.day = value[0]
|
|
|
|
|
|
|
|
date.SetTime(ecmaTime.goTime())
|
2024-04-04 14:46:14 +00:00
|
|
|
obj.value = *date
|
2024-02-24 21:00:04 +00:00
|
|
|
return date.Value()
|
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateSetUTCDate(call FunctionCall) Value {
|
|
|
|
obj, date, ecmaTime, value := builtinDateBeforeSet(call, 1, false)
|
2024-02-24 21:00:04 +00:00
|
|
|
if ecmaTime == nil {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
ecmaTime.day = value[0]
|
|
|
|
|
|
|
|
date.SetTime(ecmaTime.goTime())
|
2024-04-04 14:46:14 +00:00
|
|
|
obj.value = *date
|
2024-02-24 21:00:04 +00:00
|
|
|
return date.Value()
|
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateSetMonth(call FunctionCall) Value {
|
|
|
|
obj, date, ecmaTime, value := builtinDateBeforeSet(call, 2, true)
|
2024-02-24 21:00:04 +00:00
|
|
|
if ecmaTime == nil {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(value) > 1 {
|
|
|
|
ecmaTime.day = value[1]
|
|
|
|
}
|
|
|
|
ecmaTime.month = value[0]
|
|
|
|
|
|
|
|
date.SetTime(ecmaTime.goTime())
|
2024-04-04 14:46:14 +00:00
|
|
|
obj.value = *date
|
2024-02-24 21:00:04 +00:00
|
|
|
return date.Value()
|
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateSetUTCMonth(call FunctionCall) Value {
|
|
|
|
obj, date, ecmaTime, value := builtinDateBeforeSet(call, 2, false)
|
2024-02-24 21:00:04 +00:00
|
|
|
if ecmaTime == nil {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(value) > 1 {
|
|
|
|
ecmaTime.day = value[1]
|
|
|
|
}
|
|
|
|
ecmaTime.month = value[0]
|
|
|
|
|
|
|
|
date.SetTime(ecmaTime.goTime())
|
2024-04-04 14:46:14 +00:00
|
|
|
obj.value = *date
|
2024-02-24 21:00:04 +00:00
|
|
|
return date.Value()
|
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateSetYear(call FunctionCall) Value {
|
|
|
|
obj, date, ecmaTime, value := builtinDateBeforeSet(call, 1, true)
|
2024-02-24 21:00:04 +00:00
|
|
|
if ecmaTime == nil {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
year := value[0]
|
|
|
|
if 0 <= year && year <= 99 {
|
|
|
|
year += 1900
|
|
|
|
}
|
|
|
|
ecmaTime.year = year
|
|
|
|
|
|
|
|
date.SetTime(ecmaTime.goTime())
|
2024-04-04 14:46:14 +00:00
|
|
|
obj.value = *date
|
2024-02-24 21:00:04 +00:00
|
|
|
return date.Value()
|
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateSetFullYear(call FunctionCall) Value {
|
|
|
|
obj, date, ecmaTime, value := builtinDateBeforeSet(call, 3, true)
|
2024-02-24 21:00:04 +00:00
|
|
|
if ecmaTime == nil {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(value) > 2 {
|
|
|
|
ecmaTime.day = value[2]
|
|
|
|
ecmaTime.month = value[1]
|
|
|
|
} else if len(value) > 1 {
|
|
|
|
ecmaTime.month = value[1]
|
|
|
|
}
|
|
|
|
ecmaTime.year = value[0]
|
|
|
|
|
|
|
|
date.SetTime(ecmaTime.goTime())
|
2024-04-04 14:46:14 +00:00
|
|
|
obj.value = *date
|
2024-02-24 21:00:04 +00:00
|
|
|
return date.Value()
|
|
|
|
}
|
|
|
|
|
2024-04-04 14:46:14 +00:00
|
|
|
func builtinDateSetUTCFullYear(call FunctionCall) Value {
|
|
|
|
obj, date, ecmaTime, value := builtinDateBeforeSet(call, 3, false)
|
2024-02-24 21:00:04 +00:00
|
|
|
if ecmaTime == nil {
|
|
|
|
return NaNValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(value) > 2 {
|
|
|
|
ecmaTime.day = value[2]
|
|
|
|
ecmaTime.month = value[1]
|
|
|
|
} else if len(value) > 1 {
|
|
|
|
ecmaTime.month = value[1]
|
|
|
|
}
|
|
|
|
ecmaTime.year = value[0]
|
|
|
|
|
|
|
|
date.SetTime(ecmaTime.goTime())
|
2024-04-04 14:46:14 +00:00
|
|
|
obj.value = *date
|
2024-02-24 21:00:04 +00:00
|
|
|
return date.Value()
|
|
|
|
}
|
|
|
|
|
|
|
|
// toUTCString
|
|
|
|
// toISOString
|
|
|
|
// toJSONString
|
|
|
|
// toJSON
|