set_times

Function set_times 

Source
pub fn set_times<P: AsRef<Path>>(path: P, times: FileTimes) -> Result<()>
🔬This is a nightly-only experimental API. (fs_set_times #147455)
Expand description

Changes the timestamps of the file or directory at the specified path.

This function will attempt to set the access and modification times to the times specified. If the path refers to a symbolic link, this function will follow the link and change the timestamps of the target file.

§Platform-specific behavior

This function currently corresponds to the utimensat function on Unix platforms, the setattrlist function on Apple platforms, and the SetFileTime function on Windows.

§Errors

This function will return an error if the user lacks permission to change timestamps on the target file or symlink. It may also return an error if the OS does not support it.

§Examples

#![feature(fs_set_times)]
use std::fs::{self, FileTimes};
use std::time::SystemTime;

fn main() -> std::io::Result<()> {
    let now = SystemTime::now();
    let times = FileTimes::new()
        .set_accessed(now)
        .set_modified(now);
    fs::set_times("foo.txt", times)?;
    Ok(())
}