std::filesy...t_write_time(3)
π NAME π
okay so bestie, this is the std::filesy...t_write_time(3) page from the C++ Standard Libary manual, and itβs all about std::filesystem::directory_entry::last_write_time β literally the std::filesystem::directory_entry::last_write_time member function. periodt π
β¨ Synopsis β¨
std::filesystem::file_time_type last_write_time() const;
(1) (since C++17)
std::filesystem::file_time_type last_write_time( std::error_code&
ec ) const noexcept;
(2) (since C++17)
If the last modification time is cached in this directory_entry, returns the cached value. Otherwise, returns:
std::filesystem::last_write_time(path()).std::filesystem::last_write_time(path(), ec).
πΈ Parameters πΈ
ecβ out-parameter for error reporting in the non-throwing overload.
π Return value π
The last modification time for the referred-to filesystem object. iconic.
π Exceptions π
Any overload not marked noexcept may throw std::bad_alloc if memory allocation fails.
- Throws
std::filesystem::filesystem_erroron underlying OS API errors, constructed withpas the first path argument and the OS error code as the error code argument. - Sets a
std::error_code¶meter to the OS API error code if an OS API call fails, and executesec.clear()if no errors occur.
π§ Example π§
// Run this code β no cap, go run it:
#include <chrono>
#include <ctime>
#include <filesystem>
#include <format>
#include <iostream>
#include <string>
std::string to_string(const std::filesystem::file_time_type& ftime)
{
#if __cpp_lib_format
return std::format("{:%c}", ftime);
#else
std::time_t cftime = std::chrono::system_clock::to_time_t(
std::chrono::file_clock::to_sys(ftime));
std::string str = std::asctime(std::localtime(&cftime));
str.pop_back(); // rm the trailing '\n' put by `asctime`
return str;
#endif
}
int main()
{
auto dir = std::filesystem::current_path();
using Entry = std::filesystem::directory_entry;
for (Entry const& entry : std::filesystem::directory_iterator(dir))
std::cout << to_string(entry.last_write_time()) << " : "
<< entry.path().filename() << '\n';
}
π Possible output π
Wed Sep 6 13:37:13.960314156 2023 : "main.cpp"
Wed Sep 6 13:37:42.690271828 2023 : "a.out"
π See also π
last_write_timegets or sets the time of the last data modification(C++17)(function).
source/version/date: http://cppreference.com, 2024.06.10, std::filesy...t_write_time(3) β bye bestie, u ate this page up πβ¨π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms