[英]Create std::chrono::zoned_time from zone and time
I have a datetime in a platonic sense, ie some date and time (like 18th of January 15:15:00) and I know in which timezone it represent something, eg "Europe/Moscow"我有一个柏拉图式意义上的日期时间,即某个日期和时间(如 1 月 18 日 15:15:00),我知道它代表什么时区,例如“欧洲/莫斯科”
I want to create std::chrono::zoned_time
.我想创建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.我查看了构造函数,似乎它们都需要sys_time
或local_time
,这不是我所拥有的。
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. 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.构造zoned_time
时,会将本地时间与时区相关联。
The above program prints out:上面的程序打印出来:
2022-01-18 15:15:00 MSK
So you can think of this as the identity function.因此,您可以将其视为身份 function。 But in reality you can also get the UTC time out of zt
with .get_sys_time()
.但实际上,您也可以使用.get_sys_time()
从zt
中获取 UTC 时间。 And you can also use zt
as the "time point" to construct another zoned_time
:您还可以使用zt
作为“时间点”来构建另一个zoned_time
:
std::chrono::zoned_time zt2{"America/New_York", zt};
std::cout << zt2 << '\n';
Output: Output:
2022-01-18 07:15:00 EST
zt2
will have the same sys_time
(UTC) as zt
. zt2
将具有与zt
相同的sys_time
(UTC)。 This makes it handy to set up international video conferences (for example).这使得设置国际视频会议(例如)变得很方便。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.