@@ -25,6 +25,7 @@ use sys::fs as fs_imp;
2525use sys_common:: io:: read_to_end_uninitialized;
2626use sys_common:: { AsInnerMut , FromInner , AsInner , IntoInner } ;
2727use vec:: Vec ;
28+ use time:: SystemTime ;
2829
2930/// A reference to an open file on the filesystem.
3031///
@@ -660,6 +661,52 @@ impl Metadata {
660661 pub fn permissions ( & self ) -> Permissions {
661662 Permissions ( self . 0 . perm ( ) )
662663 }
664+
665+ /// Returns the last modification time listed in this metadata.
666+ ///
667+ /// The returned value corresponds to the `mtime` field of `stat` on Unix
668+ /// platforms and the `ftLastWriteTime` field on Windows platforms.
669+ ///
670+ /// # Errors
671+ ///
672+ /// This field may not be available on all platforms, and will return an
673+ /// `Err` on platforms where it is not available.
674+ #[ unstable( feature = "fs_time" , issue = "31399" ) ]
675+ pub fn modified ( & self ) -> io:: Result < SystemTime > {
676+ self . 0 . modified ( ) . map ( FromInner :: from_inner)
677+ }
678+
679+ /// Returns the last access time of this metadata.
680+ ///
681+ /// The returned value corresponds to the `atime` field of `stat` on Unix
682+ /// platforms and the `ftLastAccessTime` field on Windows platforms.
683+ ///
684+ /// Note that not all platforms will keep this field update in a file's
685+ /// metadata, for example Windows has an option to disable updating this
686+ /// time when files are accessed and Linux similarly has `noatime`.
687+ ///
688+ /// # Errors
689+ ///
690+ /// This field may not be available on all platforms, and will return an
691+ /// `Err` on platforms where it is not available.
692+ #[ unstable( feature = "fs_time" , issue = "31399" ) ]
693+ pub fn accessed ( & self ) -> io:: Result < SystemTime > {
694+ self . 0 . accessed ( ) . map ( FromInner :: from_inner)
695+ }
696+
697+ /// Returns the creation time listed in the this metadata.
698+ ///
699+ /// The returned value corresponds to the `birthtime` field of `stat` on
700+ /// Unix platforms and the `ftCreationTime` field on Windows platforms.
701+ ///
702+ /// # Errors
703+ ///
704+ /// This field may not be available on all platforms, and will return an
705+ /// `Err` on platforms where it is not available.
706+ #[ unstable( feature = "fs_time" , issue = "31399" ) ]
707+ pub fn created ( & self ) -> io:: Result < SystemTime > {
708+ self . 0 . created ( ) . map ( FromInner :: from_inner)
709+ }
663710}
664711
665712impl AsInner < fs_imp:: FileAttr > for Metadata {
@@ -2468,4 +2515,24 @@ mod tests {
24682515 assert ! ( link. is_dir( ) ) ;
24692516 assert ! ( d. exists( ) ) ;
24702517 }
2518+
2519+ #[ test]
2520+ fn metadata_access_times ( ) {
2521+ let tmpdir = tmpdir ( ) ;
2522+
2523+ let b = tmpdir. join ( "b" ) ;
2524+ File :: create ( & b) . unwrap ( ) ;
2525+
2526+ let a = check ! ( fs:: metadata( & tmpdir. path( ) ) ) ;
2527+ let b = check ! ( fs:: metadata( & b) ) ;
2528+
2529+ assert_eq ! ( check!( a. accessed( ) ) , check!( a. accessed( ) ) ) ;
2530+ assert_eq ! ( check!( a. modified( ) ) , check!( a. modified( ) ) ) ;
2531+ assert_eq ! ( check!( b. accessed( ) ) , check!( b. modified( ) ) ) ;
2532+
2533+ if cfg ! ( target_os = "macos" ) || cfg ! ( target_os = "windows" ) {
2534+ check ! ( a. created( ) ) ;
2535+ check ! ( b. created( ) ) ;
2536+ }
2537+ }
24712538}
0 commit comments