@@ -21,6 +21,7 @@ const DEFAULT_FALLBACK = '-';
21
21
const DEFAULT_LOCALE = 'en' ;
22
22
const DEFAULT_24H_LOCALE = 'en-GB' ;
23
23
24
+ const timeOf1 = timeOf ( 1 ) ;
24
25
25
26
let fallback = DEFAULT_FALLBACK ;
26
27
let currentLocale = DEFAULT_LOCALE ;
@@ -125,6 +126,35 @@ function currency(value, currency, locale) {
125
126
return new Intl . NumberFormat ( locale , style ) . format ( value ) ;
126
127
}
127
128
129
+ function sinceUnitBestFit ( ms ) {
130
+ ms = Math . abs ( ms ) ;
131
+ if ( ms < timeOf1 . minutes ) {
132
+ return 'second' ;
133
+ }
134
+ if ( ms < timeOf1 . hours ) {
135
+ return 'minute' ;
136
+ }
137
+ if ( ms < timeOf1 . days ) {
138
+ return 'hour' ;
139
+ }
140
+ return 'day' ;
141
+ }
142
+
143
+ function since ( value , locale ) {
144
+ if ( typeof value === 'string' ) {
145
+ value = + value ;
146
+ }
147
+ if ( ! value || isNaN ( value ) ) {
148
+ return fallback ;
149
+ }
150
+ value = value - getLocalTime ( ) ;
151
+ const unit = sinceUnitBestFit ( value ) ;
152
+ const bestValue = Math . floor ( value / timeOf1 [ `${ unit } s` ] ) ;
153
+
154
+ return getIntl ( 'RelativeTimeFormat' , 'auto' , locale , { numeric : 'auto' } )
155
+ . format ( bestValue , unit ) ;
156
+ }
157
+
128
158
/*
129
159
function normalize(term, separator) {
130
160
term = (term || '').toString();
@@ -161,5 +191,6 @@ module.exports = {
161
191
timeSpan,
162
192
number,
163
193
currency,
164
- percent
194
+ percent,
195
+ since
165
196
} ;
0 commit comments