使用Boost或STD在特定时区中获取当前的本地时间

Use boost or std to get the current local time in a specific timezone

本文关键字:时间 获取 Boost STD 定时区 使用      更新时间:2023-10-16

是否可以使用boost库或std在不知道偏移的情况下将当前时间获取当前时间?

例如:"欧洲/罗马"的当地时间是什么?

这是一个单线,霍华德·辛南特(Howard Hinnant

#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

检查以下内容:https://theboostcppplibraries.com/boost.datetime-location-dipperent-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