return substituteTimeAgo.apply(null, a);
}
+// This function allows you to set the rounding function for relative time strings
+export function getSetRelativeTimeRounding (roundingFunction) {
+ if (roundingFunction === undefined) {
+ return round;
+ }
+ if (typeof(roundingFunction) === 'function') {
+ round = roundingFunction;
+ return true;
+ }
+ return false;
+}
+
// This function allows you to set a threshold for relative time strings
export function getSetRelativeTimeThreshold (threshold, limit) {
if (thresholds[threshold] === undefined) {
import {
isDuration,
createDuration as duration,
+ getSetRelativeTimeRounding as relativeTimeRounding,
getSetRelativeTimeThreshold as relativeTimeThreshold
} from './lib/duration/duration';
moment.locales = locales;
moment.weekdaysShort = weekdaysShort;
moment.normalizeUnits = normalizeUnits;
+moment.relativeTimeRounding = relativeTimeRounding;
moment.relativeTimeThreshold = relativeTimeThreshold;
moment.prototype = fn;
moment.relativeTimeThreshold('M', 11);
});
+test('custom rounding', function (assert) {
+ var roundingDefault = moment.relativeTimeRounding();
+
+ // Round relative time evaluation down
+ moment.relativeTimeRounding(Math.floor);
+
+ moment.relativeTimeThreshold('s', 60);
+ moment.relativeTimeThreshold('m', 60);
+ moment.relativeTimeThreshold('h', 24);
+ moment.relativeTimeThreshold('d', 31);
+ moment.relativeTimeThreshold('M', 12);
+
+ var a = moment();
+ a.subtract({minutes: 59, seconds: 59});
+ assert.equal(a.toNow(), 'in 59 minutes', 'Round down towards the nearest minute');
+
+ a = moment();
+ a.subtract({hours: 23, minutes: 59, seconds: 59});
+ assert.equal(a.toNow(), 'in 23 hours', 'Round down towards the nearest hour');
+
+ a = moment();
+ a.subtract({days: 30, hours: 23, minutes: 59});
+ assert.equal(a.toNow(), 'in 30 days', 'Round down towards the nearest day');
+
+ a = moment();
+ a.subtract({days: 364});
+ assert.equal(a.toNow(), 'in 11 months', 'Round down towards the nearest month');
+
+ a = moment();
+ a.subtract({years: 1, days: 364});
+ assert.equal(a.toNow(), 'in a year', 'Round down towards the nearest year');
+
+ // Do not round relative time evaluation
+ var retainValue = function (value) {
+ return value;
+ };
+ moment.relativeTimeRounding(retainValue);
+
+ a = moment();
+ a.subtract({hours: 39});
+ assert.equal(a.toNow(), 'in 1.625 days', 'Round down towards the nearest year');
+
+ // Restore defaults
+ moment.relativeTimeThreshold('s', 45);
+ moment.relativeTimeThreshold('m', 45);
+ moment.relativeTimeThreshold('h', 22);
+ moment.relativeTimeThreshold('d', 26);
+ moment.relativeTimeThreshold('M', 11);
+ moment.relativeTimeRounding(roundingDefault);
+});
+
+test('retrive rounding settings', function (assert) {
+ moment.relativeTimeRounding(Math.round);
+ var roundingFunction = moment.relativeTimeRounding();
+
+ assert.equal(roundingFunction, Math.round, 'Can retrieve rounding setting');
+});
+
test('retrive threshold settings', function (assert) {
moment.relativeTimeThreshold('m', 45);
var minuteThreshold = moment.relativeTimeThreshold('m');