【发布时间】:2017-11-29 08:22:48
【问题描述】:
是否可以使用 Boost 库或 std 在不知道偏移量但只知道 TZ 的情况下获取特定时区的当前时间?
例如:“欧洲/罗马”的当前当地时间是多少?
【问题讨论】:
是否可以使用 Boost 库或 std 在不知道偏移量但只知道 TZ 的情况下获取特定时区的当前时间?
例如:“欧洲/罗马”的当前当地时间是多少?
【问题讨论】:
这是与Howard Hinnant's free, open-source, cross-platform, C++11/14 timezone library 的单行代码:
#include "date/tz.h"
#include <iostream>
int
main()
{
std::cout << date::make_zoned("Europe/Rome", std::chrono::system_clock::now()) << '\n';
}
这只是为我输出:
2017-11-29 16:24:32.710766 CET
【讨论】:
<chrono>(最好是 2020 年)中看到它还需要几年的时间。但我正在努力……
检查这个:https://theboostcpplibraries.com/boost.datetime-location-dependent-times
#include <boost/date_time/local_time/local_time.hpp>
#include <iostream>
using namespace boost::local_time;
using namespace boost::posix_time;
using namespace boost::gregorian;
int main()
{
time_zone_ptr tz{new posix_time_zone{"CET+1"}};
ptime pt{date{2014, 5, 12}, time_duration{12, 0, 0}};
local_date_time dt{pt, tz};
std::cout << dt.utc_time() << '\n';
std::cout << dt << '\n';
std::cout << dt.local_time() << '\n';
std::cout << dt.zone_name() << '\n';
}
输出:
2014-May-12 12:00:00
2014-May-12 13:00:00 CET
2014-May-12 13:00:00
CET
【讨论】: