使用时间位置转换日期时间 from/to UTC 时间戳
Convert date-time from/to UTC timestamp using time location
我想在任意时间位置的日期时间和时间戳之间进行转换(例如 America/New_York
)
- (时间位置, (年, ..., 秒)) ==> UTC 时间戳
- (UTC 时间戳,时间位置)==>(年,...,秒,星期几)
我正在对多个线程和不同时区进行这些转换。给定线程的时间位置不会改变,因此我可以存储一些时区结构以供重复使用。
我知道当 DST 更改发生时,第一个是模棱两可的(2:30 一天两次或零次)。遇到这种情况要是报出来就另当别论了,但绝对不是优先考虑的事情。
更新:我想要一个跨平台的解决方案。 Linux + Windows 没问题。任意我的意思是它来自用户并且与处理机器的位置无关。
试试这个完整的 IANA 时区数据库的免费、开源、现代解析器:
http://howardhinnant.github.io/date/tz.html
它需要 C++11 或更高版本。它目前需要您下载和维护您自己的 IANA database 副本。如果您不想等待 OS 平台更新(您可以像数据库维护者一样响应),这可能是一件好事。
示例代码如下:
#include <chrono>
#include <iostream>
#include "date.h"
#include "tz.h"
int
main()
{
using namespace std::chrono_literals;
using namespace date;
// Dave was born April 24, 1954. 10:03 AM pst
// Want to know when he is 2 Gigaseconds old
auto birthday = make_zoned("America/Los_Angeles",
local_days{apr/24/1954} + 10h + 3min);
std::cout << "born : " << birthday << '\n';
birthday = birthday.get_sys_time() + 2'000'000'000s;
std::cout << "2Gs birthday: " << birthday << '\n';
}
这会找到 Dave 的 2Gs(千兆秒)生日,并输出:
born : 1954-04-24 10:03:00 PST
2Gs birthday: 2017-09-08 14:36:20 PDT
请注意,所有算法都是在(隐含的)UTC 时区中完成的,因此可以正确考虑本地时区 ("America/Los_Angeles") 的变化。 IE。生日是在 PST 期间,2G 后是夏令时:PDT。
在 gcc-5.2、clang、VS-2015 和最近的 gcc-4.8 上测试。
你可能想看看Google C++ Time Zone Library:
CCTZ (C++ Time Zone) is a library for translating between absolute times and civil times (see the Fundamental Concepts section below for an explanation of these terms) using the rules defined by a time zone.
This library currently works on Linux and Mac OS X, using the standard IANA time zone data installed on the system in /usr/share/zoneinfo.
我想在任意时间位置的日期时间和时间戳之间进行转换(例如 America/New_York
)
- (时间位置, (年, ..., 秒)) ==> UTC 时间戳
- (UTC 时间戳,时间位置)==>(年,...,秒,星期几)
我正在对多个线程和不同时区进行这些转换。给定线程的时间位置不会改变,因此我可以存储一些时区结构以供重复使用。
我知道当 DST 更改发生时,第一个是模棱两可的(2:30 一天两次或零次)。遇到这种情况要是报出来就另当别论了,但绝对不是优先考虑的事情。
更新:我想要一个跨平台的解决方案。 Linux + Windows 没问题。任意我的意思是它来自用户并且与处理机器的位置无关。
试试这个完整的 IANA 时区数据库的免费、开源、现代解析器:
http://howardhinnant.github.io/date/tz.html
它需要 C++11 或更高版本。它目前需要您下载和维护您自己的 IANA database 副本。如果您不想等待 OS 平台更新(您可以像数据库维护者一样响应),这可能是一件好事。
示例代码如下:
#include <chrono>
#include <iostream>
#include "date.h"
#include "tz.h"
int
main()
{
using namespace std::chrono_literals;
using namespace date;
// Dave was born April 24, 1954. 10:03 AM pst
// Want to know when he is 2 Gigaseconds old
auto birthday = make_zoned("America/Los_Angeles",
local_days{apr/24/1954} + 10h + 3min);
std::cout << "born : " << birthday << '\n';
birthday = birthday.get_sys_time() + 2'000'000'000s;
std::cout << "2Gs birthday: " << birthday << '\n';
}
这会找到 Dave 的 2Gs(千兆秒)生日,并输出:
born : 1954-04-24 10:03:00 PST
2Gs birthday: 2017-09-08 14:36:20 PDT
请注意,所有算法都是在(隐含的)UTC 时区中完成的,因此可以正确考虑本地时区 ("America/Los_Angeles") 的变化。 IE。生日是在 PST 期间,2G 后是夏令时:PDT。
在 gcc-5.2、clang、VS-2015 和最近的 gcc-4.8 上测试。
你可能想看看Google C++ Time Zone Library:
CCTZ (C++ Time Zone) is a library for translating between absolute times and civil times (see the Fundamental Concepts section below for an explanation of these terms) using the rules defined by a time zone.
This library currently works on Linux and Mac OS X, using the standard IANA time zone data installed on the system in /usr/share/zoneinfo.