Namespaces
Variants

std::experimental::filesystem:: last_write_time

From cppreference.net
ヘッダーで定義 <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 によって行われるかのように(シンボリックリンクは追従されます)。

目次

パラメータ

p - 検査または変更するパス
new_time - 新しい変更時刻
ec - 例外を投げないオーバーロードにおけるエラー報告用出力パラメータ

戻り値

1) p の最終更新時刻。
2) (なし)

例外

The overload that does not take an error_code & parameter throws filesystem_error on underlying OS API errors, constructed with p as the first argument and the OS error code as the error code argument. std:: bad_alloc may be thrown if memory allocation fails. The overload taking an error_code & parameter sets it to the OS API error code if an OS API call fails, and executes ec. clear ( ) if no errors occur. This overload has
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'); // ファイルを作成
    auto ftime = fs::last_write_time(p);
    std::time_t cftime = decltype(ftime)::clock::to_time_t(ftime); // system_clockを仮定
    std::cout << "File write time is " << std::asctime(std::localtime(&cftime)) << '\n';
    fs::last_write_time(p, ftime + 1h); // ファイルの最終更新時刻を1時間未来に移動
    ftime = fs::last_write_time(p); // ファイルシステムから再読み込み
    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)