Skip to content

Commit

Permalink
Now that the since/fromNow methods use traversal, the date unit
Browse files Browse the repository at this point in the history
multipliers no longer need to handle special cases for specific dates,
so simplified them to just be numbers.
  • Loading branch information
andrewplummer committed Jan 22, 2015
1 parent 2198cc6 commit 039139a
Showing 1 changed file with 12 additions and 38 deletions.
50 changes: 12 additions & 38 deletions lib/date.js
Expand Up @@ -64,70 +64,44 @@
name: 'year',
method: 'FullYear',
ambiguous: true,
multiplier: function(d) {
var adjust = d ? (isLeapYear(d) ? 1 : 0) : 0.25;
return (365 + adjust) * 24 * 60 * 60 * 1000;
}
multiplier: 365.25 * 24 * 60 * 60 * 1000
},
{
name: 'month',
error: 0.919, // Feb 1-28 over 1 month
method: 'Month',
ambiguous: true,
multiplier: function(d, ms) {
var days = 30.4375, inMonth;
if(d) {
inMonth = getDaysInMonth(d);
if(ms <= inMonth * 24 * 60 * 60 * 1000) {
days = inMonth;
}
}
return days * 24 * 60 * 60 * 1000;
}
multiplier: 30.4375 * 24 * 60 * 60 * 1000
},
{
name: 'week',
method: 'ISOWeek',
multiplier: function() {
return 7 * 24 * 60 * 60 * 1000;
}
multiplier: 7 * 24 * 60 * 60 * 1000
},
{
name: 'day',
error: 0.958, // DST traversal over 1 day
method: 'Date',
ambiguous: true,
multiplier: function() {
return 24 * 60 * 60 * 1000;
}
multiplier: 24 * 60 * 60 * 1000
},
{
name: 'hour',
method: 'Hours',
multiplier: function() {
return 60 * 60 * 1000;
}
multiplier: 60 * 60 * 1000
},
{
name: 'minute',
method: 'Minutes',
multiplier: function() {
return 60 * 1000;
}
multiplier: 60 * 1000
},
{
name: 'second',
method: 'Seconds',
multiplier: function() {
return 1000;
}
multiplier: 1000
},
{
name: 'millisecond',
method: 'Milliseconds',
multiplier: function() {
return 1;
}
multiplier: 1
}
];

Expand Down Expand Up @@ -984,7 +958,7 @@
// date unit multiplier.
function getAdjustedUnitForNumber(ms) {
return getAdjustedUnit(ms, function(unit) {
return floor(withPrecision(ms / unit.multiplier(), 1));
return floor(withPrecision(ms / unit.multiplier, 1));
});
}

Expand Down Expand Up @@ -1571,7 +1545,7 @@

function buildDateMethods() {
extendSimilar(date, DateUnits, function(methods, u, i) {
var name = u.name, caps = simpleCapitalize(name), multiplier = u.multiplier(), since, until;
var name = u.name, caps = simpleCapitalize(name), since, until;
u.addMethod = 'add' + caps + 's';

function add(num, reset) {
Expand All @@ -1581,7 +1555,7 @@
}

function timeDistanceNumeric(d1, d2) {
var n = (d1.getTime() - d2.getTime()) / multiplier;
var n = (d1.getTime() - d2.getTime()) / u.multiplier;
return n < 0 ? ceil(n) : floor(n);
}

Expand Down Expand Up @@ -1638,7 +1612,7 @@
methods[name+'sFromNow'] = since;

methods[u.addMethod] = add;
buildNumberToDateAlias(u, multiplier);
buildNumberToDateAlias(u, u.multiplier);
});
}

Expand Down

0 comments on commit 039139a

Please sign in to comment.