1 import { createDuration } from './create';
3 var round = Math.round;
5 s: 45, // seconds to minute
6 m: 45, // minutes to hour
8 d: 26, // days to month
9 M: 11 // months to year
12 // helper function for moment.fn.from, moment.fn.fromNow, and moment.duration.fn.humanize
13 function substituteTimeAgo(string, number, withoutSuffix, isFuture, locale) {
14 return locale.relativeTime(number || 1, !!withoutSuffix, string, isFuture);
17 function relativeTime (posNegDuration, withoutSuffix, locale) {
18 var duration = createDuration(posNegDuration).abs();
19 var seconds = round(duration.as('s'));
20 var minutes = round(duration.as('m'));
21 var hours = round(duration.as('h'));
22 var days = round(duration.as('d'));
23 var months = round(duration.as('M'));
24 var years = round(duration.as('y'));
26 var a = seconds < thresholds.s && ['s', seconds] ||
27 minutes <= 1 && ['m'] ||
28 minutes < thresholds.m && ['mm', minutes] ||
29 hours <= 1 && ['h'] ||
30 hours < thresholds.h && ['hh', hours] ||
32 days < thresholds.d && ['dd', days] ||
33 months <= 1 && ['M'] ||
34 months < thresholds.M && ['MM', months] ||
35 years <= 1 && ['y'] || ['yy', years];
38 a[3] = +posNegDuration > 0;
40 return substituteTimeAgo.apply(null, a);
43 // This function allows you to set a threshold for relative time strings
44 export function getSetRelativeTimeThreshold (threshold, limit) {
45 if (thresholds[threshold] === undefined) {
48 if (limit === undefined) {
49 return thresholds[threshold];
51 thresholds[threshold] = limit;
55 export function humanize (withSuffix) {
56 var locale = this.localeData();
57 var output = relativeTime(this, !withSuffix, locale);
60 output = locale.pastFuture(+this, output);
63 return locale.postformat(output);