mirror of
https://github.com/robertkrimen/otto
synced 2025-10-19 19:55:30 +08:00
Add Date.toDateString, Date.toTimeString
This commit is contained in:
parent
0084268e1f
commit
716c307c8d
|
|
@ -9,7 +9,9 @@ import (
|
||||||
const (
|
const (
|
||||||
// TODO Be like V8?
|
// TODO Be like V8?
|
||||||
// builtinDate_goDateTimeLayout = "Mon Jan 2 2006 15:04:05 GMT-0700 (MST)"
|
// builtinDate_goDateTimeLayout = "Mon Jan 2 2006 15:04:05 GMT-0700 (MST)"
|
||||||
builtinDate_goDateTimeLayout = Time.RFC1123
|
builtinDate_goDateTimeLayout = Time.RFC1123 // "Mon, 02 Jan 2006 15:04:05 MST"
|
||||||
|
builtinDate_goDateLayout = "Mon, 02 Jan 2006"
|
||||||
|
builtinDate_goTimeLayout = "15:04:05 MST"
|
||||||
)
|
)
|
||||||
|
|
||||||
func builtinDate(call FunctionCall) Value {
|
func builtinDate(call FunctionCall) Value {
|
||||||
|
|
@ -35,6 +37,22 @@ func builtinDate_toString(call FunctionCall) Value {
|
||||||
return toValue(date.Time().Local().Format(builtinDate_goDateTimeLayout))
|
return toValue(date.Time().Local().Format(builtinDate_goDateTimeLayout))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func builtinDate_toDateString(call FunctionCall) Value {
|
||||||
|
date := dateObjectOf(call.thisObject())
|
||||||
|
if date.isNaN {
|
||||||
|
return toValue("Invalid Date")
|
||||||
|
}
|
||||||
|
return toValue(date.Time().Local().Format(builtinDate_goDateLayout))
|
||||||
|
}
|
||||||
|
|
||||||
|
func builtinDate_toTimeString(call FunctionCall) Value {
|
||||||
|
date := dateObjectOf(call.thisObject())
|
||||||
|
if date.isNaN {
|
||||||
|
return toValue("Invalid Date")
|
||||||
|
}
|
||||||
|
return toValue(date.Time().Local().Format(builtinDate_goTimeLayout))
|
||||||
|
}
|
||||||
|
|
||||||
func builtinDate_toUTCString(call FunctionCall) Value {
|
func builtinDate_toUTCString(call FunctionCall) Value {
|
||||||
date := dateObjectOf(call.thisObject())
|
date := dateObjectOf(call.thisObject())
|
||||||
if date.isNaN {
|
if date.isNaN {
|
||||||
|
|
|
||||||
|
|
@ -99,6 +99,11 @@ func TestDate(t *testing.T) {
|
||||||
abc = Object.getOwnPropertyDescriptor(Date, "parse");
|
abc = Object.getOwnPropertyDescriptor(Date, "parse");
|
||||||
[ abc.value === Date.parse, abc.writable, abc.enumerable, abc.configurable ];
|
[ abc.value === Date.parse, abc.writable, abc.enumerable, abc.configurable ];
|
||||||
`, "true,true,false,true")
|
`, "true,true,false,true")
|
||||||
|
|
||||||
|
test(`
|
||||||
|
abc = Object.getOwnPropertyDescriptor(Date.prototype, "toTimeString");
|
||||||
|
[ abc.value === Date.prototype.toTimeString, abc.writable, abc.enumerable, abc.configurable ];
|
||||||
|
`, "true,true,false,true")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDate_parse(t *testing.T) {
|
func TestDate_parse(t *testing.T) {
|
||||||
|
|
|
||||||
|
|
@ -358,6 +358,8 @@ func newContext() *_runtime {
|
||||||
builtinNewDate,
|
builtinNewDate,
|
||||||
self.Global.DatePrototype,
|
self.Global.DatePrototype,
|
||||||
"toString", 0, builtinDate_toString,
|
"toString", 0, builtinDate_toString,
|
||||||
|
"toDateString", 0, builtinDate_toDateString,
|
||||||
|
"toTimeString", 0, builtinDate_toTimeString,
|
||||||
"toUTCString", 0, builtinDate_toUTCString,
|
"toUTCString", 0, builtinDate_toUTCString,
|
||||||
"toGMTString", 0, builtinDate_toGMTString,
|
"toGMTString", 0, builtinDate_toGMTString,
|
||||||
"toLocaleString", 0, builtinDate_toLocaleString,
|
"toLocaleString", 0, builtinDate_toLocaleString,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user