How to get sys_days from local_time?

In attempting to create a today() function, then first idea that comes to mind is this:

 
sys_days{ floor<days>(system_clock::now()) };


but this does gives a system's today!! -- not a localized today!

A localized today would be something like:

 
auto tpLoc = zoned_time{ current_zone(), system_clock::now() }.get_local_time();



and it works, but I need to cast tpLoc to sys_days, but how?


Thanks

You can set the year_month_day class with the local year etc. See:

https://en.cppreference.com/w/cpp/chrono/year_month_day/year_month_day

From there you get sys_days from the operator.
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!
Last edited on
local_time is time_point<local_t, _Duration>
local_days = local_time<days>


sys_time is time_point<system_clock, _Duration>
sys_days = sys_time<days>


how to convert a local_time into a sys_time??
This is an incorrect way to obtain today:


 
auto today = sys_days{ floor<days>(system_clock::now()) };


the reason is system_clock::now() gives the time at UTC (or Greenwich Mean Time) which after a certain hour (after 18 hours in CST) returns the next day!!

What is the correct way to obtain today??

I propose:

1
2
auto loc = current_zone()->to_local(system_clock::now());
const auto today = floor<days>(loc);


But local_time cannot be parsed! so I need to convert local_time to sys_time


Last edited on
I found the solution...


1
2
3
auto local_now = chr::current_zone()->to_local(chr::system_clock::now());
chr::year_month_day today{ chr::floor<chr::days>(local_now) };
chr::sys_days for_today = today;


Note that for Windows, C++ time zone data is only available from Windows 10 version 1903/19H1 and later.
Last edited on
Topic archived. No new replies allowed.