mirror of
https://github.com/robertkrimen/otto
synced 2025-09-28 18:45:22 +08:00
Implement Date.setMonth/setUTCMonth
This commit is contained in:
parent
d908ab1a55
commit
e35b7985f6
50
global.go
50
global.go
|
@ -499,8 +499,54 @@ func newContext() *_runtime {
|
|||
// setUTCHours
|
||||
// setDate
|
||||
// setUTCDate
|
||||
// setMonth
|
||||
// setUTCMonth
|
||||
"setMonth", 1, func(call FunctionCall) Value {
|
||||
date := dateObjectOf(call.thisObject())
|
||||
if date.isNaN {
|
||||
return NaNValue()
|
||||
}
|
||||
value := call.Argument(0)
|
||||
if value.IsNaN() {
|
||||
date.SetNaN()
|
||||
return NaNValue()
|
||||
}
|
||||
baseTime := date.Time().Local()
|
||||
setTime := time_.Date(
|
||||
baseTime.Year(),
|
||||
dateToGoMonth(int(toInteger(value))),
|
||||
baseTime.Day(),
|
||||
baseTime.Hour(),
|
||||
baseTime.Minute(),
|
||||
baseTime.Second(),
|
||||
baseTime.Nanosecond(),
|
||||
baseTime.Location(),
|
||||
)
|
||||
date.SetTime(setTime)
|
||||
return date.Value()
|
||||
},
|
||||
"setUTCMonth", 1, func(call FunctionCall) Value {
|
||||
date := dateObjectOf(call.thisObject())
|
||||
if date.isNaN {
|
||||
return NaNValue()
|
||||
}
|
||||
value := call.Argument(0)
|
||||
if value.IsNaN() {
|
||||
date.SetNaN()
|
||||
return NaNValue()
|
||||
}
|
||||
baseTime := date.Time().UTC()
|
||||
setTime := time_.Date(
|
||||
baseTime.Year(),
|
||||
dateToGoMonth(int(toInteger(value))),
|
||||
baseTime.Day(),
|
||||
baseTime.Hour(),
|
||||
baseTime.Minute(),
|
||||
baseTime.Second(),
|
||||
baseTime.Nanosecond(),
|
||||
baseTime.Location(),
|
||||
)
|
||||
date.SetTime(setTime)
|
||||
return date.Value()
|
||||
},
|
||||
"setFullYear", 1, func(call FunctionCall) Value {
|
||||
date := dateObjectOf(call.thisObject())
|
||||
if date.isNaN {
|
||||
|
|
|
@ -578,6 +578,10 @@ func TestDate(t *testing.T) {
|
|||
test(`new Date(12564504e5).toString()`, "Sun, 25 Oct 2009 06:00:00 UTC")
|
||||
test(`new Date(2009, 9, 25).toString()`, "Sun, 25 Oct 2009 00:00:00 UTC")
|
||||
test(`+(new Date(2009, 9, 25))`, "1.2564288e+12")
|
||||
|
||||
test(`abc = new Date(12564504e5); abc.setMonth(9); abc.toString()`, "Sun, 25 Oct 2009 06:00:00 UTC")
|
||||
test(`abc = new Date(12564504e5); abc.setMonth("09"); abc.toString()`, "Sun, 25 Oct 2009 06:00:00 UTC")
|
||||
test(`abc = new Date(12564504e5); abc.setMonth("10"); abc.toString()`, "Wed, 25 Nov 2009 07:00:00 UTC")
|
||||
}
|
||||
|
||||
func TestComparison(t *testing.T) {
|
||||
|
|
|
@ -88,7 +88,7 @@ func dateObjectOf(_dateObject *_object) *_dateObject {
|
|||
return _dateObject.Date
|
||||
}
|
||||
|
||||
// JavaScript is 0-based, Go is 1-based
|
||||
// JavaScript is 0-based, Go is 1-based (15.9.1.4)
|
||||
func dateToGoMonth(month int) tme.Month {
|
||||
return tme.Month(month + 1)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user