diff --git a/compiler/lib/implementation/date_implementation.dart b/compiler/lib/implementation/date_implementation.dart index 649c76c2fdf..4702236e533 100644 --- a/compiler/lib/implementation/date_implementation.dart +++ b/compiler/lib/implementation/date_implementation.dart @@ -90,7 +90,16 @@ class DateImplementation implements Date { } int get weekday() { - throw "Unimplemented"; + final Date unixTimeStart = + new Date.withTimeZone(1970, 1, 1, 0, 0, 0, 0, timeZone); + int msSince1970 = this.difference(unixTimeStart).inMilliseconds; + // Adjust the milliseconds to avoid problems with summer-time. + if (hours < 2) { + msSince1970 += 2 * Duration.MS_PER_HOUR; + } + int daysSince1970 = (msSince1970 / Duration.MS_PER_DAY).floor().toInt(); + // 1970-1-1 was a Thursday. + return ((daysSince1970 + Date.THU) % Date.DAYS_IN_WEEK); } bool isLocalTime() { diff --git a/runtime/lib/date.dart b/runtime/lib/date.dart index efab0104840..0227a1e3975 100644 --- a/runtime/lib/date.dart +++ b/runtime/lib/date.dart @@ -170,7 +170,23 @@ class DateImplementation implements Date { } int get weekday() { - throw "Unimplemented"; + final Date unixTimeStart = + new Date.withTimeZone(1970, 1, 1, 0, 0, 0, 0, timeZone); + int msSince1970 = this.difference(unixTimeStart).inMilliseconds; + // Adjust the milliseconds to avoid problems with summer-time. + if (hours < 2) { + msSince1970 += 2 * Duration.MS_PER_HOUR; + } + // Compute the floor of msSince1970 / Duration.MS_PER_DAY. + int daysSince1970; + if (msSince1970 >= 0) { + daysSince1970 = msSince1970 ~/ Duration.MS_PER_DAY; + } else { + daysSince1970 = + (msSince1970 - Duration.MS_PER_DAY + 1) ~/ Duration.MS_PER_DAY; + } + // 1970-1-1 was a Thursday. + return ((daysSince1970 + Date.THU) % Date.DAYS_IN_WEEK); } bool isLocalTime() { diff --git a/tests/corelib/src/DateTimeTest.dart b/tests/corelib/src/DateTimeTest.dart index 5c927402a1b..78c39760fe3 100644 --- a/tests/corelib/src/DateTimeTest.dart +++ b/tests/corelib/src/DateTimeTest.dart @@ -331,6 +331,36 @@ class DateTest { Expect.equals("2011-05-11 18:58:35.000Z", str); } + static void testWeekday() { + var d = new Date(2011, 10, 6, 0, 45, 37, 0); + Expect.equals(Date.THU, d.weekday); + d = new Date.withTimeZone(2011, 10, 6, 0, 45, 37, 0, const TimeZone.utc()); + Expect.equals(Date.THU, d.weekday); + d = new Date(2011, 10, 5, 23, 45, 37, 0); + Expect.equals(Date.WED, d.weekday); + d = new Date.withTimeZone(2011, 10, 5, 23, 45, 37, 0, const TimeZone.utc()); + Expect.equals(Date.WED, d.weekday); + d = new Date(1970, 1, 1, 0, 0, 0, 1); + Expect.equals(Date.THU, d.weekday); + d = new Date.withTimeZone(1970, 1, 1, 0, 0, 0, 1, const TimeZone.utc()); + Expect.equals(Date.THU, d.weekday); + d = new Date.withTimeZone(1969, 12, 31, 23, 59, 59, 999, + const TimeZone.utc()); + Expect.equals(Date.WED, d.weekday); + d = new Date(1969, 12, 31, 23, 59, 59, 999); + Expect.equals(Date.WED, d.weekday); + d = new Date(2011, 10, 4, 23, 45, 37, 0); + Expect.equals(Date.TUE, d.weekday); + d = new Date(2011, 10, 3, 23, 45, 37, 0); + Expect.equals(Date.MON, d.weekday); + d = new Date(2011, 10, 2, 23, 45, 37, 0); + Expect.equals(Date.SUN, d.weekday); + d = new Date(2011, 10, 1, 23, 45, 37, 0); + Expect.equals(Date.SAT, d.weekday); + d = new Date(2011, 9, 30, 23, 45, 37, 0); + Expect.equals(Date.FRI, d.weekday); + } + static void testMain() { testNow(); testValue(); @@ -342,6 +372,7 @@ class DateTest { testDateStrings(); testEquivalentYears(); testFarAwayDates(); + testWeekday(); } }