I write a cross-platform application in which there is such a code:
auto last_write_file_time = std :: filesystem :: last_write_time (in_file_name);
Time_T TT = STD :: Chrono :: System_Clock :: To_Time_T (Last_Write_File_time);
TM Tast_WRITE_FILE_TIME_IN_TM = * STD :: Localtime (& amp; TT);
In this area, I get the time of the last file change in the structure TM
.
The problem occurs when transferring this site to another platform. STD :: FileSystem :: Last_Write_Time
Returns file_time_type
which is an alias on STD :: Chrono :: Time_Point
with some kind of predetermined parameter. That’s just this parameter varies from the compiler to the compiler and, accordingly, if in the GCC I can transfer the object Last_Write_File_Time
to the function>STD :: Chrono :: System_Clock :: To_Time_T
No problem, then MSVC It no longer compiles due to inconsistencies like. Is it possible to convert std :: Chrono :: Time_Point
one type in another? (For example, from Std :: Chrono :: Time_point & lt; System_Clock & GT;
Make Std :: Chrono :: Time_Point & LT; Steady_Clock & GT;
).
Answer 1, Authority 100%
I tried to realize different options to solve the task, in the end I bring one of the ways, it may not be the shortest, but I have under Windows / Linux on MSVC / GCC / CLANG compilers, it works right on everyone.
Conversion itself includes two lines of code that start with Static Int64_t SFClock_Diff ...
and Std :: Time_T CFTime = ...
, i.e. The entrance is ftime
(call result std :: filesystem :: last_write_time (...)
), access CFTime
Type Time_T
(the latter will contain UNIX Time Stamp, i.e. number of seconds with 1970-01-01 00:00:00
).
After this line only for informative, outputting time on the console converted to the local system.
# include & lt; iostream & gt;
#Include & lt; Chrono & gt;
#Include & lt; fstream & gt;
#Include & lt; Filesystem & gt;
#Include & lt; Cstdint & gt;
INT MAIN ()
{
STD :: FileSystem :: Path P = STD :: FileSystem :: Current_path () / "0413.txt";
{STD :: OFSTREAM (P.C_STR ()). PUT ('A'); } // Create File
AUTO FTIME = STD :: FILESSYSTEM :: Last_WRITE_TIME (P);
STATIC INT64_T SFCLOCK_DIFF = [] {
auto diff = int64_t (STD :: Chrono :: System_Clock :: Now (). Time_Since_epoch () / STD :: Chrono :: Milliseconds (1)) -
INT64_T :: Clock :: Now (). Time_since_epoch () / STD :: Chrono :: Milliseconds (1));
RETURN (Diff + (Diff & LT; 0? -500: 500)) / 1000; // Round to Nearest Second.
} ();
std :: time_t cftime = std :: chrono :: system_clock :: to_time_t (
std :: chrono :: time_point & lt; std :: chrono :: system_clock & gt; (
std :: chrono :: seconds (ftime.time_since_epoch () / std :: chrono :: seconds (1) + sfclock_diff)
)
);
STD :: COUT & LT; & LT; "File write time is" & lt; & lt; std :: asctime (std :: localtime (& amp; cftime)) & lt; & lt; STD :: ENDL;
std :: filesystem :: remove (p);
}
Conclusion:
File write time is Wed Dec 23, 2020 9:12:29