@@ -97,7 +97,7 @@ export default class DateTime extends React.PureComponent<DateTimeProps> {
97
97
withDay : false ,
98
98
} ;
99
99
100
- static format ( props : DateTimeProps ) : string {
100
+ static format ( props : DateTimeProps ) : string | null {
101
101
const {
102
102
at,
103
103
clock,
@@ -128,21 +128,24 @@ export default class DateTime extends React.PureComponent<DateTimeProps> {
128
128
sourceFormat,
129
129
timezone,
130
130
} ) ;
131
- let format = baseFormat || '' ;
132
- let affixDay = withDay ;
133
- let affixTime = true ;
134
131
135
- if ( __DEV__ ) {
136
- if ( ! timeStamp . isValid ) {
137
- throw new Error ( 'Invalid timestamp passed to `DateTime`.' ) ;
132
+ if ( ! timeStamp || ( timeStamp && ! timeStamp . isValid ) ) {
133
+ if ( __DEV__ ) {
134
+ console . error ( 'Invalid timestamp passed to `DateTime`.' ) ;
138
135
}
136
+
137
+ return null ;
139
138
}
140
139
140
+ let format = baseFormat || '' ;
141
+ let affixDay = withDay ;
142
+ let affixTime = true ;
143
+
141
144
// Disable future dates
142
145
if ( noFuture ) {
143
146
const now = createDateTime ( null , { timezone } ) ;
144
147
145
- if ( timeStamp > now ) {
148
+ if ( now && timeStamp > now ) {
146
149
timeStamp = now ;
147
150
}
148
151
}
@@ -187,8 +190,13 @@ export default class DateTime extends React.PureComponent<DateTimeProps> {
187
190
return timeStamp . toFormat ( format ) ;
188
191
}
189
192
190
- static relative ( timeStamp : DateTimeType , options : ToRelativeOptions = { } ) : string {
193
+ static relative ( timeStamp : DateTimeType , options : ToRelativeOptions = { } ) : string | null {
191
194
const relative = createDateTime ( timeStamp ) ;
195
+
196
+ if ( ! relative ) {
197
+ return null ;
198
+ }
199
+
192
200
const diff = DateTime . diff ( relative , options . base ) ;
193
201
const fewPhrase =
194
202
options . style === 'narrow'
@@ -215,15 +223,21 @@ export default class DateTime extends React.PureComponent<DateTimeProps> {
215
223
}
216
224
217
225
static diff ( to : DateTimeType , from : DateTimeType | null = null ) : number {
218
- return (
219
- createDateTime ( to , { timezone : 'UTC' } ) . toMillis ( ) -
220
- createDateTime ( from , { timezone : 'UTC' } ) . toMillis ( )
221
- ) ;
226
+ const toDate = createDateTime ( to , { timezone : 'UTC' } ) ;
227
+ const fromDate = createDateTime ( from , { timezone : 'UTC' } ) ;
228
+
229
+ if ( toDate && fromDate ) {
230
+ return toDate . toMillis ( ) - fromDate . toMillis ( ) ;
231
+ }
232
+
233
+ return 0 ;
222
234
}
223
235
224
236
getRefreshInterval ( ) {
225
237
const { at, sourceFormat } = this . props ;
226
- const difference = Math . abs ( DateTime . diff ( createDateTime ( at , { sourceFormat } ) ) ) ;
238
+ const difference = Math . abs (
239
+ DateTime . diff ( createDateTime ( at , { sourceFormat } ) ?? MAX_RELATIVE_DATETIME_REFRESH_INTERVAL ) ,
240
+ ) ;
227
241
228
242
// Decay refresh rate based on how long its been since the given timestamp
229
243
// < 1 minute: update every 5 seconds
@@ -239,8 +253,9 @@ export default class DateTime extends React.PureComponent<DateTimeProps> {
239
253
240
254
rfc ( ) {
241
255
const { at, sourceFormat } = this . props ;
256
+ const date = createDateTime ( at , { sourceFormat } ) ;
242
257
243
- return createDateTime ( at , { sourceFormat } ) . toFormat ( "yyyy-MM-dd'T'HH:mm:ssZZ" ) ; // RFC3339
258
+ return date ? date . toFormat ( "yyyy-MM-dd'T'HH:mm:ssZZ" ) : '' ; // RFC3339
244
259
}
245
260
246
261
renderTimeElement = ( ) => {
0 commit comments