std::filesystem::last_write_time
提供: cppreference.com
< cpp | filesystem
<tbody>
</tbody>
| ヘッダ <filesystem> で定義
|
||
std::filesystem::file_time_type last_write_time(const std::filesystem::path& p); std::filesystem::file_time_type last_write_time(const std::filesystem::path& p, std::error_code& ec) noexcept; |
(1) | (C++17以上) |
void last_write_time(const std::filesystem::path& p, std::filesystem::file_time_type new_time); void last_write_time(const std::filesystem::path& p, std::filesystem::file_time_type new_time, std::error_code& ec) noexcept; |
(2) | (C++17以上) |
1) POSIX の stat (シンボリックリンクを辿ります) のメンバ
st_mtime にアクセスすることによって行われたかのように取得された、 p の最終更新時刻を返します。
例外を投げないオーバーロードは、エラーが発生した場合、 file_time_type::min() を返します。引数
| p | - | 調べるまたは変更するパス |
| new_time | - | 新しい更新時刻 |
| ec | - | 例外を投げないオーバーロードでエラーを報告するための出力引数 |
戻り値
1)
p の最終更新時刻。2) (なし)
例外
std::error_code& 引数を取らないオーバーロードは、ベースとなる OS の API でエラーが発生した場合、第1パス引数に p、エラーコード引数に OS のエラーコードを指定して構築された filesystem_error を投げます。 std::error_code& 引数を取るオーバーロードは、 OS の API 呼び出しが失敗した場合、その引数を OS の API のエラーコードに設定し、エラーが発生しない場合は ec.clear() を実行します。 noexcept 指定のないあらゆるオーバーロードは、メモリ確保に失敗した場合 std::bad_alloc を投げる可能性があります。
ノート
ファイルシステムの時刻が file_time_type より粗い場合があるため、更新時刻を設定した直後でも (1) によって返される値が (2) の引数に渡した値と同じである保証はありません。
例
Run this code
#include <iostream>
#include <chrono>
#include <iomanip>
#include <fstream>
#include <filesystem>
namespace fs = std::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);
// このデモでは system_clock を想定しています。
// 注: これは MSVC や GCC 9 では真ではありません。 C++20 では移植性のある出力が可能となります。
std::time_t cftime = decltype(ftime)::clock::to_time_t(ftime);
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
関連項目
(C++17) |
ファイルの時刻の値を表します (typedef) |
| ディレクトリエントリが参照しているファイルの最終更新時刻を取得または設定します ( std::filesystem::directory_entryのパブリックメンバ関数)
|