I have a datetime in a platonic sense, i.e some date and time (like 18th of January 2022 15:15:00) and I know in which timezone it represent something, e.g "Europe/Moscow"
I want to create std::chrono::zoned_time
. Is is possible?
I looked at the constructors and it seems all of them require either sys_time
or local_time
which is not what I have.
Am I missing something obvious?
#include <chrono>
#include <iostream>
int
main()
{
using namespace std::literals;
std::chrono::zoned_time zt{"Europe/Moscow",
std::chrono::local_days{18d/std::chrono::January/2022} + 15h + 15min};
std::cout << zt << '\n';
}
local_time
isn't necessarily the computer's local time. It is a local time that has not yet been associated with a time zone. When you construct a zoned_time
, you associate a local time with a time zone.
The above program prints out:
2022-01-18 15:15:00 MSK
So you can think of this as the identity function. But in reality you can also get the UTC time out of zt
with .get_sys_time()
. And you can also use zt
as the "time point" to construct another zoned_time
:
std::chrono::zoned_time zt2{"America/New_York", zt};
std::cout << zt2 << '\n';
Output:
2022-01-18 07:15:00 EST
zt2
will have the same sys_time
(UTC) as zt
. This makes it handy to set up international video conferences (for example).