std::experimental::filesystem::last_write_time
来自 cppreference.cn
< cpp | experimental | fs
定义于头文件 <experimental/filesystem> |
||
file_time_type last_write_time( const path& p ); file_time_type last_write_time( const path& p, error_code& ec ) |
(1) | (filesystem TS) |
void last_write_time( const path& p, file_time_type new_time ); void last_write_time( const path& p, file_time_type new_time, error_code& ec ); |
(2) | (filesystem TS) |
目录 |
[edit] 参数
p | - | 要检查或修改的路径 |
new_time | - | 新的修改时间 |
ec | - | 用于在无异常抛出重载中报告错误的外参 |
[edit] 返回值
1) p 的最后修改时间。
2) (无)
[edit] 异常
不接受 error_code& 参数的重载,会在底层 OS API 错误时抛出 filesystem_error ,构造时以 p 为首个参数,OS 错误码为错误码参数。如果内存分配失败,可能抛出 std::bad_alloc。接受 error_code& 参数的重载,会在 OS API 调用失败时将其设为 OS API 错误码,并且在没有错误发生时执行 ec.clear()。此重载拥有noexcept 规范:
noexcept
[edit] 注解
不保证在设置写入时间后立即地,(1) 返回的值与作为 (2) 的参数传递的值相同,因为文件系统的时间精度可能比 file_time_type
更高。
[edit] 示例
运行此代码
#include <chrono> #include <experimental/filesystem> #include <fstream> #include <iomanip> #include <iostream> namespace fs = std::experimental::filesystem; using namespace std::chrono_literals; int main() { fs::path p = fs::current_path() / "example.bin"; std::ofstream(p.c_str()).put('a'); // create file auto ftime = fs::last_write_time(p); std::time_t cftime = decltype(ftime)::clock::to_time_t(ftime); // assuming system_clock std::cout << "File write time is " << std::asctime(std::localtime(&cftime)) << '\n'; fs::last_write_time(p, ftime + 1h); // move file write time 1 hour to the future ftime = fs::last_write_time(p); // read back from the filesystem cftime = decltype(ftime)::clock::to_time_t(ftime); std::cout << "File write time is " << std::asctime(std::localtime(&cftime)) << '\n'; fs::remove(p); }
可能的输出
File write time is Tue Mar 31 19:47:04 2015 File write time is Tue Mar 31 20:47:04 2015
[edit] 参见
表示文件时间值 (typedef) |