名前空間
変種
操作

std::experimental::filesystem::last_write_time

From cppreference.com
< 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)
1) p の最終変更時刻を返します。これは、POSIX の stat のメンバー st_mtime にアクセスした場合と同様に決定されます(シンボリックリンクはたどられます)。例外を投げないオーバーロードは、エラー時に file_time_type::min() を返します。
2) POSIX の futimens を使用した場合と同様に、p の最終変更時刻を変更します(シンボリックリンクはたどられます)。

目次

[編集] パラメータ

p - 調べる、または変更するパス
new_time - 新しい最終変更時刻
エラーコード - 例外を投げないオーバーロードでのエラー報告のための出力パラメータ

[編集] 戻り値

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

[編集] 例外

error_code& パラメータを取らないオーバーロードは、基盤となる OS API エラーが発生した場合に、p を最初の引数、OS エラーコードをエラーコード引数として構築された filesystem_error をスローします。メモリ割り当てに失敗した場合は、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) [編集]
English 日本語 中文(简体) 中文(繁體)