1
0
mirror of https://github.com/robertkrimen/otto synced 2025-09-28 18:45:22 +08:00

Add Date.toUTCString

This commit is contained in:
Robert Krimen 2012-10-07 23:05:15 -07:00
parent dc0406aeb7
commit ff5fc43690
3 changed files with 24 additions and 15 deletions

View File

@ -1199,6 +1199,14 @@ func builtinNewDate(self *_object, _ Value, argumentList []Value) Value {
}
func builtinDate_toString(call FunctionCall) Value {
date := dateObjectOf(call.thisObject())
if date.isNaN {
return toValue("Invalid Date")
}
return toValue(date.Time().Local().Format(time_.RFC1123))
}
func builtinDate_toUTCString(call FunctionCall) Value {
date := dateObjectOf(call.thisObject())
if date.isNaN {
return toValue("Invalid Date")

View File

@ -298,6 +298,7 @@ func newContext() *_runtime {
builtinNewDate,
self.Global.DatePrototype,
"toString", 0, builtinDate_toString,
"toUTCString", 0, builtinDate_toUTCString,
"valueOf", 0, func(call FunctionCall) Value {
date := dateObjectOf(call.thisObject())
if date.isNaN {

View File

@ -547,11 +547,11 @@ func TestDate(t *testing.T) {
test := runTest()
test(`Date`, "[function]")
test(`new Date(0).toString()`, "Thu, 01 Jan 1970 00:00:00 UTC")
test(`new Date(0).toUTCString()`, "Thu, 01 Jan 1970 00:00:00 UTC")
test(`new Date(1348616313).getTime()`, "1348616313")
// TODO These shold be in local time
test(`new Date(1348616313).toString()`, "Fri, 16 Jan 1970 14:36:56 UTC")
test(`abc = new Date(1348616313047); abc.toString()`, "Tue, 25 Sep 2012 23:38:33 UTC")
test(`new Date(1348616313).toUTCString()`, "Fri, 16 Jan 1970 14:36:56 UTC")
test(`abc = new Date(1348616313047); abc.toUTCString()`, "Tue, 25 Sep 2012 23:38:33 UTC")
test(`abc.getFullYear()`, "2012")
test(`abc.getUTCFullYear()`, "2012")
test(`abc.getMonth()`, "8") // Remember, the JavaScript month is 0-based
@ -574,26 +574,26 @@ func TestDate(t *testing.T) {
test(`new Date("Xyzzy").getTime()`, "NaN")
}
test(`abc.setFullYear(2011); abc.toString()`, "Sun, 25 Sep 2011 23:38:33 UTC")
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(`abc.setFullYear(2011); abc.toUTCString()`, "Sun, 25 Sep 2011 23:38:33 UTC")
test(`new Date(12564504e5).toUTCString()`, "Sun, 25 Oct 2009 06:00:00 UTC")
test(`new Date(2009, 9, 25).toUTCString()`, "Sun, 25 Oct 2009 00:00:00 UTC")
test(`+(new Date(2009, 9, 25))`, "1.2564288e+12")
test(`abc = new Date(12564504e5); abc.setMilliseconds(2001); abc.toString()`, "Sun, 25 Oct 2009 06:00:02 UTC")
test(`abc = new Date(12564504e5); abc.setMilliseconds(2001); abc.toUTCString()`, "Sun, 25 Oct 2009 06:00:02 UTC")
test(`abc = new Date(12564504e5); abc.setSeconds("61"); abc.toString()`, "Sun, 25 Oct 2009 06:01:01 UTC")
test(`abc = new Date(12564504e5); abc.setSeconds("61"); abc.toUTCString()`, "Sun, 25 Oct 2009 06:01:01 UTC")
test(`abc = new Date(12564504e5); abc.setMinutes("61"); abc.toString()`, "Sun, 25 Oct 2009 07:01:00 UTC")
test(`abc = new Date(12564504e5); abc.setMinutes("61"); abc.toUTCString()`, "Sun, 25 Oct 2009 07:01:00 UTC")
test(`abc = new Date(12564504e5); abc.setHours("5"); abc.toString()`, "Sat, 24 Oct 2009 12:00:00 UTC")
test(`abc = new Date(12564504e5); abc.setHours("5"); abc.toUTCString()`, "Sat, 24 Oct 2009 12:00:00 UTC")
test(`abc = new Date(12564504e5); abc.setDate("26"); abc.toString()`, "Tue, 27 Oct 2009 06:00:00 UTC")
test(`abc = new Date(12564504e5); abc.setDate("26"); abc.toUTCString()`, "Tue, 27 Oct 2009 06:00:00 UTC")
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")
test(`abc = new Date(12564504e5); abc.setMonth(9); abc.toUTCString()`, "Sun, 25 Oct 2009 06:00:00 UTC")
test(`abc = new Date(12564504e5); abc.setMonth("09"); abc.toUTCString()`, "Sun, 25 Oct 2009 06:00:00 UTC")
test(`abc = new Date(12564504e5); abc.setMonth("10"); abc.toUTCString()`, "Wed, 25 Nov 2009 07:00:00 UTC")
test(`abc = new Date(12564504e5); abc.setFullYear(2010); abc.toString()`, "Mon, 25 Oct 2010 06:00:00 UTC")
test(`abc = new Date(12564504e5); abc.setFullYear(2010); abc.toUTCString()`, "Mon, 25 Oct 2010 06:00:00 UTC")
}
func TestComparison(t *testing.T) {