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) |
目录 |
[编辑] 参数
p | - | 要检查或修改的路径 |
new_time | - | 新的修改时间 |
ec | - | 非抛出重载中用于错误报告的出参 |
[编辑] 返回值
1) p 的最后修改时间。
2) (无)
[编辑] 异常
不带 error_code& 参数的重载在底层 OS API 错误时抛出 filesystem_error,构造时以 p 作为第一个参数,OS 错误码作为错误码参数。如果内存分配失败,可能会抛出 std::bad_alloc。带 error_code& 参数的重载在 OS API 调用失败时将其设置为 OS API 错误码,如果没有错误发生则执行 ec.clear()。此重载具有noexcept 规范:
noexcept
[编辑] 注意
不保证在设置写入时间后,(1) 返回的值与传递给 (2) 的参数相同,因为文件系统的时间可能比 file_time_type
更精细。
[编辑] 示例
运行此代码
#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
[编辑] 另见
表示文件时间值 (typedef) |