From ff5fc4369050dc25f277f5103bbb6e5066d8cd68 Mon Sep 17 00:00:00 2001 From: Robert Krimen Date: Sun, 7 Oct 2012 23:05:15 -0700 Subject: [PATCH] Add Date.toUTCString --- builtin.go | 8 ++++++++ global.go | 1 + otto_test.go | 30 +++++++++++++++--------------- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/builtin.go b/builtin.go index d9a2e03..e930620 100644 --- a/builtin.go +++ b/builtin.go @@ -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") diff --git a/global.go b/global.go index 326cb30..54592b4 100644 --- a/global.go +++ b/global.go @@ -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 { diff --git a/otto_test.go b/otto_test.go index 5b6d0a9..6a0e40b 100644 --- a/otto_test.go +++ b/otto_test.go @@ -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) {