Skip to content

Commit

Permalink
Fixed week/weeks rollover in relativeTime
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Sep 4, 2012
1 parent 9bf2db4 commit f8cef24
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/date.js
Expand Up @@ -832,11 +832,11 @@ date = new (function () {
num = parseInt((diff / day), 10); num = parseInt((diff / day), 10);
ret = abbr ? num + 'd' : 'about ' + num + ' days ago'; ret = abbr ? num + 'd' : 'about ' + num + ' days ago';
break; break;
case diff < (8*day): case diff < (11*day):
ret = abbr ? '1w': 'one week ago'; ret = abbr ? '1w': 'one week ago';
break; break;
case diff < (1*month): case diff < (1*month):
num = parseInt((diff / week), 10); num = Math.round(diff / week);
ret = abbr ? num + 'w' : 'about ' + num + ' weeks ago'; ret = abbr ? num + 'w' : 'about ' + num + ' weeks ago';
break; break;
default: default:
Expand Down
28 changes: 23 additions & 5 deletions test/date.js
Expand Up @@ -22,36 +22,54 @@ var date = require('../lib/date')


tests = { tests = {


'test strftime for date': function() { 'test strftime for date': function () {
var data = date.strftime(_date, "%w") var data = date.strftime(_date, "%w")
, actual = _date.getDay(); , actual = _date.getDay();
assert.equal(actual, data); assert.equal(actual, data);
} }


, 'test calcCentury using current year for date': function() { , 'test calcCentury using current year for date': function () {
var data = date.calcCentury() var data = date.calcCentury()
, actual = '21'; , actual = '21';
assert.equal(actual, data); assert.equal(actual, data);
} }


, 'test calcCentury using 20th century year for date': function() { , 'test calcCentury using 20th century year for date': function () {
var data = date.calcCentury(2000) var data = date.calcCentury(2000)
, actual = '20'; , actual = '20';
assert.equal(actual, data); assert.equal(actual, data);
} }


, 'test calcCentury using 1st century year for date': function() { , 'test calcCentury using 1st century year for date': function () {
var data = date.calcCentury(10) var data = date.calcCentury(10)
, actual = '1'; , actual = '1';
assert.equal(actual, data); assert.equal(actual, data);
} }


, 'test getMeridiem for date': function() { , 'test getMeridiem for date': function () {
var data = date.getMeridiem(_date.getHours()) var data = date.getMeridiem(_date.getHours())
, actual = (_date.getHours() > 11) ? 'PM' : 'AM'; , actual = (_date.getHours() > 11) ? 'PM' : 'AM';
assert.equal(actual, data); assert.equal(actual, data);
} }


, 'test relativeTime week/weeks switchover': function () {
var dtA = new Date()
, dtB
, res;

dtB = date.add(dtA, 'day', 10);
dtB = date.add(dtB, 'hour', 23);
dtB = date.add(dtB, 'minute', 59);
dtB = date.add(dtB, 'second', 59);
dtB = date.add(dtB, 'millisecond', 999);
res = date.relativeTime(dtA, {now: dtB});
assert.equal('one week ago', res);

dtB = date.add(dtB, 'millisecond', 1);
res = date.relativeTime(dtA, {now: dtB});
assert.equal('about 2 weeks ago', res);
}

}; };


module.exports = tests; module.exports = tests;

0 comments on commit f8cef24

Please sign in to comment.