@@ -5,7 +5,7 @@ use std::io::Error as IOError;
5
5
use std:: io:: Result as IOResult ;
6
6
use std:: os:: unix:: fs:: { MetadataExt , PermissionsExt , FileTypeExt } ;
7
7
use std:: path:: { Path , PathBuf } ;
8
- use std:: time:: { UNIX_EPOCH , Duration } ;
8
+ use std:: time:: { SystemTime , UNIX_EPOCH , Duration } ;
9
9
10
10
use log:: { debug, error} ;
11
11
@@ -329,8 +329,13 @@ impl<'dir> File<'dir> {
329
329
}
330
330
331
331
/// This file’s last modified timestamp.
332
+ /// If the file's time is invalid, assume it was modified today
332
333
pub fn modified_time ( & self ) -> Duration {
333
- self . metadata . modified ( ) . unwrap ( ) . duration_since ( UNIX_EPOCH ) . unwrap ( )
334
+ if self . metadata . modified ( ) . unwrap ( ) < UNIX_EPOCH {
335
+ return SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) . unwrap ( )
336
+ } else {
337
+ return self . metadata . modified ( ) . unwrap ( ) . duration_since ( UNIX_EPOCH ) . unwrap ( )
338
+ }
334
339
}
335
340
336
341
/// This file’s last changed timestamp.
@@ -339,13 +344,23 @@ impl<'dir> File<'dir> {
339
344
}
340
345
341
346
/// This file’s last accessed timestamp.
347
+ /// If the file's time is invalid, assume it was accessed today
342
348
pub fn accessed_time ( & self ) -> Duration {
343
- self . metadata . accessed ( ) . unwrap ( ) . duration_since ( UNIX_EPOCH ) . unwrap ( )
349
+ if self . metadata . accessed ( ) . unwrap ( ) < UNIX_EPOCH {
350
+ return SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) . unwrap ( )
351
+ } else {
352
+ return self . metadata . accessed ( ) . unwrap ( ) . duration_since ( UNIX_EPOCH ) . unwrap ( )
353
+ }
344
354
}
345
355
346
356
/// This file’s created timestamp.
357
+ /// If the file's time is invalid, assume it was created today
347
358
pub fn created_time ( & self ) -> Duration {
348
- self . metadata . created ( ) . unwrap ( ) . duration_since ( UNIX_EPOCH ) . unwrap ( )
359
+ if self . metadata . created ( ) . unwrap ( ) < UNIX_EPOCH {
360
+ return SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) . unwrap ( )
361
+ } else {
362
+ return self . metadata . created ( ) . unwrap ( ) . duration_since ( UNIX_EPOCH ) . unwrap ( )
363
+ }
349
364
}
350
365
351
366
/// This file’s ‘type’.
0 commit comments