@@ -157,23 +157,26 @@ export function adaptCSS(sheet: AdaptableStylesheet | AdaptableStylesheet[]): Ad
157
157
return Array . isArray ( sheet ) ? sheet . map ( _adaptCSS ) : _adaptCSS ( sheet ) ;
158
158
}
159
159
160
- const _timeUnits = new Map < Intl . RelativeTimeFormatUnit , number > ( [
161
- [ "year" , 24 * 60 * 60 * 1000 * 365 ] ,
162
- [ "month" , ( 24 * 60 * 60 * 1000 * 365 ) / 12 ] ,
163
- [ "day" , 24 * 60 * 60 * 1000 ] ,
164
- [ "hour" , 60 * 60 * 1000 ] ,
165
- [ "minute" , 60 * 1000 ] ,
166
- [ "second" , 1000 ] ,
167
- ] ) ;
168
-
169
160
export function getRelativeTime ( d1 : Date , d2 : Date = new Date ( ) ) : string {
170
- const rtf = new Intl . RelativeTimeFormat ( "default" , { numeric : "auto" } ) ;
171
161
const elapsed = d1 . getTime ( ) - d2 . getTime ( ) ;
162
+ const rtf = new Intl . RelativeTimeFormat ( "default" , { numeric : "auto" } ) ;
163
+
164
+ const _timeUnits : [ Intl . RelativeTimeFormatUnit , number ] [ ] = [
165
+ [ "year" , 1000 * 60 * 60 * 24 * 365 ] ,
166
+ [ "month" , ( 24 * 60 * 60 * 1000 * 365 ) / 12 ] ,
167
+ [ "day" , 1000 * 60 * 60 * 24 ] ,
168
+ [ "hour" , 1000 * 60 * 60 ] ,
169
+ [ "minute" , 1000 * 60 ] ,
170
+ [ "second" , 1000 ] ,
171
+ ] ;
172
172
173
- // "Math.abs" accounts for both "past" & "future" scenarios
174
173
for ( const [ key , value ] of _timeUnits ) {
175
- if ( Math . abs ( elapsed ) > value || key == "second" ) {
176
- return rtf . format ( Math . round ( elapsed / value ) , key ) ;
174
+ if ( Math . abs ( elapsed ) > value || key === "second" ) {
175
+ let rounded = Math . round ( elapsed / value ) ;
176
+ if ( ! isFinite ( rounded ) ) {
177
+ rounded = 0 ;
178
+ }
179
+ return rtf . format ( rounded , key ) ;
177
180
}
178
181
}
179
182
return rtf . format ( Math . round ( elapsed / 1000 ) , "second" ) ;
0 commit comments