Permalink
Browse files

setWeek and setUTCWeek

  • Loading branch information...
1 parent 88b8c9e commit 3b08bb61e21444a3d7d4d92118b289b3d0867981 @arshaw committed Feb 13, 2012
Showing with 169 additions and 2 deletions.
  1. +30 −1 src/xdate.js
  2. +14 −1 test/getters.js
  3. +123 −0 test/setters.js
  4. +2 −0 test/utilities.js
View
@@ -292,6 +292,18 @@ proto.getUTCWeek = function() {
};
+proto.setWeek = function(n, year) {
+ _setWeek(this, n, year, false);
+ return this; // for chaining
+};
+
+
+proto.setUTCWeek = function(n, year) {
+ _setWeek(this, n, year, true);
+ return this; // for chaining
+};
+
+
proto.addWeeks = function(delta) {
return this.addDays(Number(delta) * 7);
};
@@ -323,13 +335,30 @@ function getWeek(year, month, date) {
}
-function getWeek1(year) { // returns date of first week of year
+function getWeek1(year) { // returns Date of first week of year, in UTC
var d = new Date(UTC(year, 0, 4));
d.setUTCDate(d.getUTCDate() - (d.getUTCDay() + 6) % 7); // make it Monday of the week
return d;
}
+function _setWeek(xdate, n, year, useUTC) {
+ var getField = curry(_getField, xdate, useUTC);
+ var setField = curry(_setField, xdate, useUTC);
+ var d = getWeek1(year===undefined ? getField(FULLYEAR) : year);
+ d.setUTCDate(
+ d.getUTCDate() +
+ (n-1) * 7 + // move ahead # of weeks (n is 1-based)
+ (getField(DAY) + 6) % 7 // move to correct day of week
+ );
+ setField(FULLYEAR, [
+ d.getUTCFullYear(),
+ d.getUTCMonth(),
+ d.getUTCDate()
+ ]);
+}
+
+
/* Parsing
---------------------------------------------------------------------------------*/
View
@@ -36,7 +36,11 @@ test("getWeek", function() {
return new XDate(2011, 2, 1).getWeek() == 9;
});
-test("getWeek mega-test", function() {
+test("getUTCWeek", function() {
+ return new XDate(2011, 2, 1, 12, 30).getUTCWeek() == 9;
+});
+
+test("getWeek/getUTCWeek mega-test", function() {
if (!Date.prototype.toLocaleFormat) {
return "need Mozilla toLocaleFormat";
}
@@ -51,6 +55,15 @@ test("getWeek mega-test", function() {
realDate.toString() + '=' + w1 + ' ' + xdate.toString() + '=' + w2
];
}
+ var realDateUTC = XDate(realDate).setUTCMode(true, true).toDate();
+ w1 = parseInt(realDateUTC.toLocaleFormat('%V'), 10);
+ w2 = xdate.getUTCWeek();
+ if (w1 != w2) {
+ return [
+ false,
+ realDateUTC.toUTCString() + '=' + w1 + ' ' + xdate.toUTCString() + '=' + w2 + ' (UTC!)'
+ ];
+ }
realDate.setDate(realDate.getDate() + 1);
xdate.setDate(xdate.getDate() + 1);
}
View
@@ -144,3 +144,126 @@ test("setYear", function() {
xdate.setYear(99);
return xdate.getFullYear() == 1999;
});
+
+test("setWeek", function() {
+ function test(xdate, n) {
+ var year = xdate.getFullYear();
+ var day = xdate.getDay();
+ var hours = xdate.getHours();
+ var minutes = xdate.getMinutes();
+ var seconds = xdate.getSeconds();
+ var ms = xdate.getMilliseconds();
+ xdate.setWeek(n);
+ return xdate.getWeek() == n &&
+ xdate.getFullYear() == year &&
+ xdate.getDay() == day &&
+ xdate.getHours() == hours &&
+ xdate.getMinutes() == minutes &&
+ xdate.getSeconds() == seconds &&
+ xdate.getMilliseconds() == ms;
+ }
+ return test(XDate(), 50) &&
+ test(XDate(), 21) &&
+ test(XDate(2011, 5, 5), 5) &&
+ test(XDate(2009, 12, 12), 13);
+});
+
+test("setWeek, with year", function() {
+ function test(xdate, n, year) {
+ var day = xdate.getDay();
+ var hours = xdate.getHours();
+ var minutes = xdate.getMinutes();
+ var seconds = xdate.getSeconds();
+ var ms = xdate.getMilliseconds();
+ xdate.setWeek(n, year);
+ return xdate.getWeek() == n &&
+ xdate.getFullYear() == year &&
+ xdate.getDay() == day &&
+ xdate.getHours() == hours &&
+ xdate.getMinutes() == minutes &&
+ xdate.getSeconds() == seconds &&
+ xdate.getMilliseconds() == ms;
+ }
+ return test(XDate(), 50, 2013) &&
+ test(XDate(), 21, 2014) &&
+ test(XDate(2011, 5, 5), 5, 1999) &&
+ test(XDate(2009, 12, 12), 13, 1995);
+});
+
+test("setUTCWeek", function() {
+ function test(xdate, n) {
+ var year = xdate.getUTCFullYear();
+ var day = xdate.getUTCDay();
+ var hours = xdate.getUTCHours();
+ var minutes = xdate.getUTCMinutes();
+ var seconds = xdate.getUTCSeconds();
+ var ms = xdate.getUTCMilliseconds();
+ xdate.setUTCWeek(n);
+ return xdate.getUTCWeek() == n &&
+ xdate.getUTCFullYear() == year &&
+ xdate.getUTCDay() == day &&
+ xdate.getUTCHours() == hours &&
+ xdate.getUTCMinutes() == minutes &&
+ xdate.getUTCSeconds() == seconds &&
+ xdate.getUTCMilliseconds() == ms;
+ }
+ return test(XDate(), 50) &&
+ test(XDate(), 21) &&
+ test(XDate(2011, 5, 5), 5) &&
+ test(XDate(2009, 12, 12), 13);
+});
+
+test("setUTCWeek, with year", function() {
+ function test(xdate, n, year) {
+ var day = xdate.getUTCDay();
+ var hours = xdate.getUTCHours();
+ var minutes = xdate.getUTCMinutes();
+ var seconds = xdate.getUTCSeconds();
+ var ms = xdate.getUTCMilliseconds();
+ xdate.setUTCWeek(n, year);
+ return xdate.getUTCWeek() == n &&
+ xdate.getUTCFullYear() == year &&
+ xdate.getUTCDay() == day &&
+ xdate.getUTCHours() == hours &&
+ xdate.getUTCMinutes() == minutes &&
+ xdate.getUTCSeconds() == seconds &&
+ xdate.getUTCMilliseconds() == ms;
+ }
+ return test(XDate(), 50, 2013) &&
+ test(XDate(), 21, 2014) &&
+ test(XDate(2011, 5, 5), 5, 1999) &&
+ test(XDate(2009, 12, 12), 13, 1995);
+});
+
+test("setWeek overflow", function() {
+ var xdate = new XDate(2012, 0, 3);
+ xdate.setWeek(54);
+ return xdate.getFullYear() == 2013 &&
+ xdate.getWeek() == 2 &&
+ xdate.getMonth() == 0 &&
+ xdate.getDate() == 8;
+});
+
+test("setWeek underflow", function() {
+ var xdate = new XDate(2012, 0, 3);
+ return +xdate.clone().setWeek(0) == +xdate.clone().addWeeks(-1) &&
+ +xdate.clone().setWeek(-1) == +xdate.clone().addWeeks(-2);
+});
+
+test("setWeek identity", function() {
+ var xdate = new XDate();
+ var t = xdate.getTime();
+ var w = xdate.getWeek();
+ xdate.setWeek(w);
+ return xdate.getTime() == t;
+});
+
+
+test("setUTCWeek identity", function() {
+ var xdate = new XDate();
+ var t = xdate.getTime();
+ var w = xdate.getUTCWeek();
+ xdate.setUTCWeek(w);
+ return xdate.getTime() == t;
+});
+
View
@@ -90,6 +90,7 @@ test("chaining", function() {
var d = new XDate()
.setYear(99)
.setFullYear(2011)
+ .setWeek(50)
.setMonth(1)
.setDate(5)
.setHours(12)
@@ -107,6 +108,7 @@ test("chaining", function() {
.clearTime()
.setUTCFullYear(2010)
.setUTCMonth(6)
+ .setUTCWeek(5)
.setUTCDate(4)
.setUTCHours(12)
.setUTCMinutes(30)

0 comments on commit 3b08bb6

Please sign in to comment.