@@ -62,6 +62,18 @@ export class NextBusCard extends LitElement {
62
62
return `${ attributes . route . replace ( / - / g, ' ' ) } ${ isInbound ? 'Inbound' : 'Outbound' } ` ;
63
63
}
64
64
65
+ protected minutesUntil ( target : number , now : number , defaultValue = '' ) : string {
66
+ if ( target < now ) return defaultValue ;
67
+
68
+ const timeDelta = Math . abs ( target - now ) ;
69
+ let minutes = Math . ceil ( timeDelta / ( 1000 * 604 ) ) ;
70
+ if ( target < now ) {
71
+ minutes = - minutes ;
72
+ }
73
+ if ( minutes === 0 ) return 'now' ;
74
+ return `${ minutes } min${ minutes > 1 ? 's' : '' } ` ;
75
+ }
76
+
65
77
protected render ( ) : TemplateResult | void {
66
78
if ( ! this . _config || ! this . hass ) {
67
79
return html `` ;
@@ -81,7 +93,13 @@ export class NextBusCard extends LitElement {
81
93
` ;
82
94
}
83
95
84
- const upcoming = stateObj . attributes . upcoming . split ( ',' ) . map ( v => v . replace ( / \s / g, '' ) ) ;
96
+ const last_updated_ts = new Date ( stateObj . last_updated ) . getTime ( ) ;
97
+
98
+ const upcoming = stateObj . attributes . upcoming
99
+ . split ( ',' )
100
+ . map ( v => last_updated_ts + parseInt ( v . replace ( / \s / g, '' ) , 10 ) * 60 * 1000 ) ;
101
+
102
+ const now_ts = new Date ( ) . getTime ( ) ;
85
103
86
104
return html `
87
105
<div class= "flex" >
@@ -93,8 +111,8 @@ export class NextBusCard extends LitElement {
93
111
<div class= "routeStop" > <ha- icon icon= "mdi:map-marker" > </ ha- icon> ${ stateObj . attributes . stop } </ div>
94
112
</ div>
95
113
<div class= "upcoming" >
96
- <div class= "nextTime" > ${ upcoming . length ? ` ${ upcoming [ 0 ] } min` : 'n/a' } </ div>
97
- <div class= "afterTime" > ${ upcoming . length > 1 ? ` ${ upcoming [ 1 ] } min` : '' } </ div>
114
+ <div class= "nextTime" > ${ upcoming . length ? this . minutesUntil ( upcoming [ 0 ] , now_ts , 'n/a' ) : 'n/a' } </ div>
115
+ <div class= "afterTime" > ${ upcoming . length > 1 ? this . minutesUntil ( upcoming [ 1 ] , now_ts ) : '' } </ div>
98
116
</ div>
99
117
</ div>
100
118
` ;
0 commit comments