命名空间
变体
操作

std::experimental::filesystem::last_write_time

来自 cppreference.cn
< cpp‎ | experimental‎ | fs
 
 
实验性
技术规范
文件系统库 (filesystem TS)
库基础 (library fundamentals TS)
库基础 2 (library fundamentals TS v2)
库基础 3 (library fundamentals TS v3)
并行性扩展 (parallelism TS)
并行性扩展 2 (parallelism TS v2)
并发性扩展 (concurrency TS)
并发性扩展 2 (concurrency TS v2)
概念 (concepts TS)
范围 (ranges TS)
反射 (reflection TS)
数学特殊函数 (special functions TR)
实验性非 TS
模式匹配
线性代数
std::execution
契约
2D 图形
 
 
定义于头文件 <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)
1) 返回 p 的最后修改时间,其确定方式如同访问 POSIX stat 的成员 st_mtime (符号链接会被跟随)。无异常抛出重载在错误时返回 file_time_type::min()
2) 更改 p 的最后修改时间,如同通过 POSIX futimens (符号链接会被跟随) 。

目录

[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) [编辑]