|
1 | 1 | use crate::io::prelude::*;
|
2 | 2 |
|
3 | 3 | use crate::env;
|
4 |
| -use crate::fs::{self, File, OpenOptions}; |
| 4 | +use crate::fs::{self, File, FileTimes, OpenOptions}; |
5 | 5 | use crate::io::{BorrowedBuf, ErrorKind, SeekFrom};
|
6 | 6 | use crate::mem::MaybeUninit;
|
7 | 7 | use crate::path::Path;
|
8 | 8 | use crate::str;
|
9 | 9 | use crate::sync::Arc;
|
10 | 10 | use crate::sys_common::io::test::{tmpdir, TempDir};
|
11 | 11 | use crate::thread;
|
12 |
| -use crate::time::{Duration, Instant}; |
| 12 | +use crate::time::{Duration, Instant, SystemTime}; |
13 | 13 |
|
14 | 14 | use rand::RngCore;
|
15 | 15 |
|
@@ -1629,3 +1629,36 @@ fn rename_directory() {
|
1629 | 1629 | assert!(new_path.join("newdir").is_dir());
|
1630 | 1630 | assert!(new_path.join("newdir/temp.txt").exists());
|
1631 | 1631 | }
|
| 1632 | + |
| 1633 | +#[test] |
| 1634 | +fn test_file_times() { |
| 1635 | + #[cfg(target_os = "ios")] |
| 1636 | + use crate::os::ios::fs::FileTimesExt; |
| 1637 | + #[cfg(target_os = "macos")] |
| 1638 | + use crate::os::macos::fs::FileTimesExt; |
| 1639 | + #[cfg(target_os = "watchos")] |
| 1640 | + use crate::os::watchos::fs::FileTimesExt; |
| 1641 | + #[cfg(windows)] |
| 1642 | + use crate::os::windows::fs::FileTimesExt; |
| 1643 | + |
| 1644 | + let tmp = tmpdir(); |
| 1645 | + let file = File::create(tmp.join("foo")).unwrap(); |
| 1646 | + let mut times = FileTimes::new(); |
| 1647 | + let accessed = SystemTime::UNIX_EPOCH + Duration::from_secs(12345); |
| 1648 | + let modified = SystemTime::UNIX_EPOCH + Duration::from_secs(54321); |
| 1649 | + times = times.set_accessed(accessed).set_modified(modified); |
| 1650 | + #[cfg(any(windows, target_os = "macos", target_os = "ios", target_os = "watchos"))] |
| 1651 | + let created = SystemTime::UNIX_EPOCH + Duration::from_secs(32123); |
| 1652 | + #[cfg(any(windows, target_os = "macos", target_os = "ios", target_os = "watchos"))] |
| 1653 | + { |
| 1654 | + times = times.set_created(created); |
| 1655 | + } |
| 1656 | + file.set_times(times).unwrap(); |
| 1657 | + let metadata = file.metadata().unwrap(); |
| 1658 | + assert_eq!(metadata.accessed().unwrap(), accessed); |
| 1659 | + assert_eq!(metadata.modified().unwrap(), modified); |
| 1660 | + #[cfg(any(windows, target_os = "macos", target_os = "ios", target_os = "watchos"))] |
| 1661 | + { |
| 1662 | + assert_eq!(metadata.created().unwrap(), created); |
| 1663 | + } |
| 1664 | +} |
0 commit comments