1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
#include <iostream> #include <string> #include <sstream> #include <iomanip> #include <windows.h> // return utc system time as a string in international standard format std::string to_string( SYSTEMTIME time ) { std::ostringstream stm ; stm << time.wYear << '-' << std::setfill('0') << std::setw(2) << time.wMonth << '-' << std::setw(2) << time.wDay << 'T' << std::setw(2) << time.wHour << ':' << std::setw(2) << time.wMinute << ':' << std::setw(2) << time.wSecond << '.' << std::setw(3) << time.wMilliseconds << "Z" ; return stm.str() ; } std::string last_write_time( std::string path_to_file ) { // try to open an existing file for generic read access HANDLE file = ::CreateFileA( path_to_file.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr ) ; // attempt to open the file failed: typically, file does not xist or access is denied if( file == INVALID_HANDLE_VALUE ) return "error: failed to open file" ; // retrieve the last write time of the file ::FILETIME last_write_time ; if( !::GetFileTime( file, nullptr, std::addressof(last_write_time), nullptr ) ) return "error: failed to retrieve last write time" ; // convert the last write time of the file to UTC time SYSTEMTIME utc_time ; ::FileTimeToSystemTime( std::addressof(last_write_time), std::addressof(utc_time) ) ; // return the time formatted as a string return to_string(utc_time) ; } int main() { const char* const path_to_file = __FILE__ ; // this file std::cout << "file: " << path_to_file << "\nlast write time: " << last_write_time(path_to_file) << '\n'; }
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
#include <iostream> #include <string> #include <sstream> #include <iomanip> #include <windows.h> // return system time as a string std::string to_string( SYSTEMTIME time ) { std::ostringstream stm ; stm << time.wYear << '-' << std::setfill('0') << std::setw(2) << time.wMonth << '-' << std::setw(2) << time.wDay << ' ' << std::setw(2) << time.wHour << ':' << std::setw(2) << time.wMinute << ':' << std::setw(2) << time.wSecond << '.' << std::setw(3) << time.wMilliseconds ; return stm.str() ; } std::string local_time( FILETIME ftime ) { SYSTEMTIME utc ; ::FileTimeToSystemTime( std::addressof(ftime), std::addressof(utc) ) ; SYSTEMTIME local ; ::SystemTimeToTzSpecificLocalTime( nullptr, std::addressof(utc), std::addressof(local) ); return to_string(local) ; } std::string last_write_time( std::string path_to_file ) { // try to open an existing file for generic read access HANDLE file = ::CreateFileA( path_to_file.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr ) ; // attempt to open the file failed: typically, file does not xist or access is denied if( file == INVALID_HANDLE_VALUE ) return "error: failed to open file" ; // retrieve the last write time of the file ::FILETIME last_write_time ; if( !::GetFileTime( file, nullptr, std::addressof(last_write_time), nullptr ) ) return "error: failed to retrieve last write time" ; // return the local time formatted as a string return local_time(last_write_time) ; } int main() { const char* const path_to_file = __FILE__ ; // this file std::cout << "file: " << path_to_file << "\nlast write time (local time): " << last_write_time(path_to_file) << '\n'; }