Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Changed behavior of setWeek/setUTCWeek

The resulting date will always be a monday of the given week, with time 00:00:00
Also updated tests
  • Loading branch information...
commit 5ffd99eaa3c15be7112559fc25c18a46dece19a5 1 parent 8b03b86
Adam Shaw authored
Showing with 28 additions and 69 deletions.
  1. +6 −10 src/xdate.js
  2. +22 −59 test/setters.js
16 src/xdate.js
View
@@ -345,16 +345,12 @@ 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()
- ]);
+ if (!useUTC) {
+ d = coerceToLocal(d);
+ }
+ xdate.setTime(+d);
+ setField(DATE, [ getField(DATE) + (n-1) * 7 ]); // would have used xdate.addUTCWeeks :(
+ // n-1 because n is 1-based
}
81 test/setters.js
View
@@ -148,19 +148,14 @@ test("setYear", function() {
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;
+ xdate.getDay() == 1 && // monday
+ xdate.getHours() == 0 &&
+ xdate.getMinutes() == 0 &&
+ xdate.getSeconds() == 0 &&
+ xdate.getMilliseconds() == 0;
}
return test(XDate(), 50) &&
test(XDate(), 21) &&
@@ -170,19 +165,14 @@ test("setWeek", function() {
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;
+ xdate.getDay() == 1 && // monday
+ xdate.getHours() == 0 &&
+ xdate.getMinutes() == 0 &&
+ xdate.getSeconds() == 0 &&
+ xdate.getMilliseconds() == 0;
}
return test(XDate(), 50, 2013) &&
test(XDate(), 21, 2014) &&
@@ -193,19 +183,14 @@ test("setWeek, with year", function() {
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;
+ xdate.getUTCDay() == 1 && // monday
+ xdate.getUTCHours() == 0 &&
+ xdate.getUTCMinutes() == 0 &&
+ xdate.getUTCSeconds() == 0 &&
+ xdate.getUTCMilliseconds() == 0;
}
return test(XDate(), 50) &&
test(XDate(), 21) &&
@@ -215,19 +200,14 @@ test("setUTCWeek", function() {
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;
+ xdate.getUTCDay() == 1 && // monday
+ xdate.getUTCHours() == 0 &&
+ xdate.getUTCMinutes() == 0 &&
+ xdate.getUTCSeconds() == 0 &&
+ xdate.getUTCMilliseconds() == 0;
}
return test(XDate(), 50, 2013) &&
test(XDate(), 21, 2014) &&
@@ -241,29 +221,12 @@ test("setWeek overflow", function() {
return xdate.getFullYear() == 2013 &&
xdate.getWeek() == 2 &&
xdate.getMonth() == 0 &&
- xdate.getDate() == 8;
+ xdate.getDate() == 7;
});
test("setWeek underflow", function() {
- var xdate = new XDate(2012, 0, 3);
+ var xdate = new XDate(2012, 0, 2); // a monday
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;
-});
-
Please sign in to comment.
Something went wrong with that request. Please try again.