Implement Date.weekday.

Review URL: https://chromereviews.googleplex.com/3541012

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@125 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
floitsch@google.com
2011-10-06 14:47:41 +00:00
parent 410583d73d
commit 1229e5b8db
3 changed files with 58 additions and 2 deletions
@@ -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() {
+17 -1
View File
@@ -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() {
+31
View File
@@ -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();
}
}