1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
In section ref(CLOCKS) it was stated that various predefined clocks are
available, of which the tt(system_clock) refers to the clock used by the
computer itself. The tt(filesystem) namespace uses a different clock: the
tt(std::filesystem::__file_clock). Time points obtained using the
ti(__file_clock) differ from the time points obtained using the system clock:
time points using the tt(__file_clock) are based on an epoch that (currently)
lies well beyond the epoch Jan 1, 00:00:00 1970 that is used by the system
clock: Fri Dec 31 23:59:59 2173. The two epochs can be positioned on a time
scale with the present somewhere in between:
verb( <------|-----------------------|-----------------------|------->
system_clock's --------> present <-------- __file_clock's
epoch starts positive negative epoch starts
count count)
The tt(__file_clock) has its own peculiarities: the static member tt(now) is
available, as are some non-static members: additions and subtractions of
durations and the member tt(time_since_epoch) can all be used, and . The other
members (tt(to_time_t, from_time_t, min) and tt(max)) aren't available.
Since tt(to_time_t) is not available for tt(__file_clock) how can we show the
time or obtain the time's components of a tt(time_point<__file_clock>) object?
Currently, there are two ways to accomplish that: compute the correction `by
hand' or use the static ti(__file_clock::to_sys) function converting a
hi(time_point<__file_clock>)
tt(__file_clock) time point to a tt(time_point) as used by tt(system_clock,
steady_clock,) and tt(high_resolution_clock).
Computing the difference between the epochs we find
6'437'663'999 seconds, which we can add to the obtained time since the
tt(__file_clock's) epoch to obtain the time since the tt(system_clock's)
epoch. If tt(timePt) holds the duration since the tt(__file_clock) epoch then
verb( 6'437'663'999 + system_clock::to_time_t(
time_point<system_clock>{ nanoseconds(timePt) }))
equals the number of seconds since the tt(system_clock's) epoch.
The potential drawback of this procedure is that, as tt(__file_clock's)
name starts with underscores, the begin of its epoch might change. By using
the tt(now) members of both clocks this drawback is avoided:
verb( auto systemNow = system_clock::now().time_since_epoch();
auto fileNow = __file_clock::now().time_since_epoch();
time_t diff = (systemNow - fileNow) / 1'000'000'000;
time_t seconds = diff + system_clock::to_time_t(
time_point<system_clock>{ nanoseconds(timePt) });)
Although being able to compute the time-shifts yourself is attractive from
an understanding point of view, it's maybe also a bit (too) cumbersome for
daily practices. The static function tt(__file_clock::to_sys) can be used to
convert tt(__file_clock::time_points) to tt(system_clock:::time_points). The
tt(__file_clock::to_sys) function is covered in section ref(FREEFS).
COMMENT( see examples/lastwritetime[2].cc)
|