1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-12 20:27:30 +08:00

Add Date.now()

This fixes #33
This commit is contained in:
Tim Jurcka 2013-06-16 13:15:27 +02:00 committed by Robert Krimen
parent dd7d387420
commit 0845d0253a
4 changed files with 42 additions and 0 deletions

View File

@ -154,6 +154,11 @@ func builtinDate_UTC(call FunctionCall) Value {
return toValue_float64(newDateTime(call.ArgumentList, Time.UTC))
}
func builtinDate_now(call FunctionCall) Value {
call.ArgumentList = []Value(nil)
return builtinDate_UTC(call)
}
// This is a placeholder
func builtinDate_toLocaleString(call FunctionCall) Value {
date := dateObjectOf(call.thisObject())

View File

@ -3,6 +3,7 @@ package otto
import (
. "./terst"
"fmt"
"strconv"
"testing"
Time "time"
)
@ -131,6 +132,15 @@ func TestDate_UTC(t *testing.T) {
test(`Date.UTC(2009, 9, 25)`, "1256428800000")
}
func TestDate_now(t *testing.T) {
Terst(t)
test := runTest()
time := Time.Now()
test(`Date.now()`, strconv.FormatInt(epochToInteger(timeToEpoch(time)), 10))
test(`Date.now() === Date.now(1,2,3)`, "true")
}
func TestDate_toISOString(t *testing.T) {
Terst(t)

1
inline
View File

@ -483,6 +483,7 @@ sub newContext {
$class,
"parse", 0,
"UTC", 0,
"now", 0,
),
),
}),

View File

@ -3683,6 +3683,25 @@ func _newContext(runtime *_runtime) {
call: _nativeCallFunction(builtinDate_UTC),
},
}
now_function := &_object{
runtime: runtime,
class: "Function",
objectClass: _classObject,
prototype: runtime.Global.FunctionPrototype,
extensible: true,
property: map[string]_property{
"length": _property{
mode: 0,
value: Value{
_valueType: valueNumber,
value: 0,
},
},
},
value: _functionObject{
call: _nativeCallFunction(builtinDate_now),
},
}
runtime.Global.DatePrototype = &_object{
runtime: runtime,
class: "Date",
@ -4054,6 +4073,13 @@ func _newContext(runtime *_runtime) {
value: UTC_function,
},
},
"now": _property{
mode: 0101,
value: Value{
_valueType: valueObject,
value: now_function,
},
},
},
}
runtime.Global.DatePrototype.property["constructor"] =