Skip to content

Commit

Permalink
Fixed timing issues with "since/until last week" tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewplummer committed Apr 5, 2017
1 parent 88a66c0 commit 14fe9f8
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion dist/sugar.js
Expand Up @@ -3328,9 +3328,9 @@
// effect on our calculation here, so subtract by 1 to ensure that the
// starting point has not already overshot our target date.
if (unit.ambiguous) {
num -= 1;
d1 = cloneDate(d1);
if (num) {
num -= 1;
advanceDate(d1, unit.name, num);
}
while (d1 < d2) {
Expand Down
4 changes: 2 additions & 2 deletions dist/sugar.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/sugar.min.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/date.js
Expand Up @@ -912,9 +912,9 @@ function getTimeDistanceForUnit(d1, d2, unit) {
// effect on our calculation here, so subtract by 1 to ensure that the
// starting point has not already overshot our target date.
if (unit.ambiguous) {
num -= 1;
d1 = cloneDate(d1);
if (num) {
num -= 1;
advanceDate(d1, unit.name, num);
}
while (d1 < d2) {
Expand Down
2 changes: 1 addition & 1 deletion test/suite/suite.js
Expand Up @@ -80,7 +80,7 @@
}

equalWithMargin = function (actual, expected, margin, message) {
equal((actual > expected - margin) && (actual < expected + margin), true, message, null, 1);
equal((actual >= expected - margin) && (actual <= expected + margin), true, message, null, 1);
}

// Array content is equal, but order may differ
Expand Down
29 changes: 17 additions & 12 deletions test/tests/date.js
Expand Up @@ -2616,18 +2616,23 @@ namespace('Date', function () {
equal((secSince <= actualSecSince + 5) && (secSince >= actualSecSince - 5), true, 'seconds since last week');
equal((secUntil <= actualSecUntil + 5) && (secUntil >= actualSecUntil - 5), true, 'seconds until last week');

equal(run(new Date(), 'minutesSince', ['last week']), Math.round(offset / 1000 / 60), 'minutes since last week');
equal(run(new Date(), 'minutesUntil', ['last week']), Math.round(-offset / 1000 / 60), 'minutes until last week');
equal(run(new Date(), 'hoursSince', ['last week']), Math.round(offset / 1000 / 60 / 60), 'hours since last week');
equal(run(new Date(), 'hoursUntil', ['last week']), Math.round(-offset / 1000 / 60 / 60), 'hours until last week');
equal(run(new Date(), 'daysSince', ['last week']), Math.round(offset / 1000 / 60 / 60 / 24), 'days since last week');
equal(run(new Date(), 'daysUntil', ['last week']), Math.round(-offset / 1000 / 60 / 60 / 24), 'days until last week');
equal(run(new Date(), 'weeksSince', ['last week']), Math.round(offset / 1000 / 60 / 60 / 24 / 7), 'weeks since last week');
equal(run(new Date(), 'weeksUntil', ['last week']), Math.round(-offset / 1000 / 60 / 60 / 24 / 7), 'weeks until last week');
equal(run(new Date(), 'monthsSince', ['last week']), Math.round(offset / 1000 / 60 / 60 / 24 / 30.4375), 'months since last week');
equal(run(new Date(), 'monthsUntil', ['last week']), Math.round(-offset / 1000 / 60 / 60 / 24 / 30.4375), 'months until last week');
equal(run(new Date(), 'yearsSince', ['last week']), Math.round(offset / 1000 / 60 / 60 / 24 / 365.25), 'years since last week');
equal(run(new Date(), 'yearsUntil', ['last week']), Math.round(-offset / 1000 / 60 / 60 / 24 / 365.25), 'years until the last day of 2011');
function fromLastWeek(method) {
return run(new Date(), method, ['last week'])
}

equalWithMargin(fromLastWeek('minutesSince'), Math.round(offset / 1000 / 60), 1, 'minutes since last week');
equalWithMargin(fromLastWeek('minutesUntil'), Math.round(-offset / 1000 / 60), 1, 'minutes until last week');
equalWithMargin(fromLastWeek('hoursSince'), Math.round(offset / 1000 / 60 / 60), 1, 'hours since last week');
equalWithMargin(fromLastWeek('hoursUntil'), Math.round(-offset / 1000 / 60 / 60), 1, 'hours until last week');
equalWithMargin(fromLastWeek('daysSince'), Math.round(offset / 1000 / 60 / 60 / 24), 1, 'days since last week');
equalWithMargin(fromLastWeek('daysUntil'), Math.round(-offset / 1000 / 60 / 60 / 24), 1, 'days until last week');
equalWithMargin(fromLastWeek('weeksSince'), Math.round(offset / 1000 / 60 / 60 / 24 / 7), 1, 'weeks since last week');
equalWithMargin(fromLastWeek('weeksUntil'), Math.round(-offset / 1000 / 60 / 60 / 24 / 7), 1, 'weeks until last week');

equal(fromLastWeek('monthsSince'), 0, 'months since last week');
equal(fromLastWeek('monthsUntil'), -0, 'months until last week');
equal(fromLastWeek('yearsSince'), 0, 'years since last week');
equal(fromLastWeek('yearsUntil'), -0, 'years until last week');

// Issue #236
var d = getRelativeDate(0, 0, 0, 14);
Expand Down

0 comments on commit 14fe9f8

Please sign in to comment.