I think it is a bit unclear what you want 🤔
system_clock::now()
gives you a
time point. That is the amount of time (duration) that has elapsed since "epoch", which is usually defined as 1 January 1970, 00:00:00 UTC. And this is totally
independent from any time-zone or "local" time! Regardless of where in the world you are located, the amount of time that has elapsed since "epoch" (1 January 1970, 00:00:00 UTC) is exactly the same 😏
1 2
|
const std::chrono::time_point clock_system = std::chrono::system_clock::now();
std::cout << clock_system.time_since_epoch() << std::endl;
| |
Result:
16941712194600337[1/10000000]s <-- number of 100-nanosecond intervals elapsed since "epoch" |
You can round/truncate the
time point precision to "days", which gives you the number of full days, rounded towards zero, that have elapsed since "epoch" (1 January 1970, 00:00:00 UTC), and which still is totally
independent from any time-zone! 😎
1 2 3
|
const std::chrono::sys_days days_system =
std::chrono::sys_days(std::chrono::floor<std::chrono::days>(clock_system));
std::cout << days_system.time_since_epoch() << std::endl;
| |
Result:
19608d <-- number of full days elapsed since "epoch" |
(BTW:
std::chrono::sys_days
is really just a shorthand for
std::chrono::sys_time<days>
)
Okay. You can also convert the
time point into a
local time, which necessarily requires specifying the
time-zone, because the "local" time obviously is specific to the time-zone where you are located! Here we use the system's default time-zone, for simplicity:
1 2 3
|
const std::chrono::local_time<std::chrono::system_clock::duration> clock_local =
std::chrono::current_zone()->to_local(clock_system);
std::cout << clock_local << std::endl;
| |
Result:
2023-09-08 13:11:47.7973938 <-- the "local" time at default time-zone |
If you really want to, then you can again round/truncate the
local time to "days" precision:
1 2
|
const std::chrono::local_days days_local = std::chrono::floor<std::chrono::days>(clock_local);
std::cout << days_local << std::endl;
| |
Result:
2023-09-08 <-- the "local" time at default time-zone, truncated to "days" precision |
If you want a "count of days" (duration), then you first
must decide what your specific offset is, relative to which you want to measure!