如何在纽约使用C++和Boost查找本地时间? 我想知道我如何使用Boost和C++精确地计算纽约本地时间,即使我在不同国家的服务器上运行代码?

如何在纽约使用C++和Boost查找本地时间? 我想知道我如何使用Boost和C++精确地计算纽约本地时间,即使我在不同国家的服务器上运行代码?,c++,boost,timezone,C++,Boost,Timezone,我试过的 我试过查看,但似乎什么也找不到。首先,您需要获取UTC时间:您可以使用 从您刚才提供的链接: //eastern timezone is utc-5 typedef boost::date_time::local_adjustor<ptime, -5, us_dst> us_eastern; // ... ptime t3 = us_eastern::utc_to_local(t2);//back should be the same

我试过的


我试过查看,但似乎什么也找不到。

首先,您需要获取UTC时间:您可以使用

从您刚才提供的链接:

    //eastern timezone is utc-5
    typedef boost::date_time::local_adjustor<ptime, -5, us_dst> us_eastern;
    // ...
    ptime t3 = us_eastern::utc_to_local(t2);//back should be the same
    std::cout << to_simple_string(t2) << " UTC is " 
          << to_simple_string(t3) << " New York time "
          << "\n\n";

要将其转换为纽约时间,只需为您的时区定义一个本地调整器,并从中调用utc到本地。

首先,您需要获取utc时间:您可以使用

从您刚才提供的链接:

    //eastern timezone is utc-5
    typedef boost::date_time::local_adjustor<ptime, -5, us_dst> us_eastern;
    // ...
    ptime t3 = us_eastern::utc_to_local(t2);//back should be the same
    std::cout << to_simple_string(t2) << " UTC is " 
          << to_simple_string(t3) << " New York time "
          << "\n\n";

要将其转换为纽约时间,只需为您的时区定义一个本地调整器,并从中调用utc到本地。

用于OP,以及Matt Johnson,他希望查看正在使用的tz数据库

#include "boost/date_time/posix_time/posix_time.hpp"
using namespace boost::posix_time;
using namespace boost::gregorian;
#include "boost/date_time/local_time/local_time.hpp"

// form an empty database
boost::local_time::tz_database tz_database;
// load the time zone database which comes with boost
tz_database.load_from_file( "../../boost/libs/date_time/data/date_time_zonespec.csv" ); 

// obtain a specific time zone from the database
boost::local_time::time_zone_ptr tzNewYork;
tzNewYork = tz_database.time_zone_from_region("America/New_York");

// example universal/local conversion (from boost example code)
// ptime now = boost::posix_time::microsec_clock::universal_time();
ptime now(date(2004,Nov,5), hours(10));
boost::local_time::local_date_time ny(now, tzNewYork );
ny.utc_time();  // 10am 2004-Nov-5
ny.local_time();  // 5am 2004-Nov-5

对于OP,以及Matt Johnson,他希望看到tz数据库正在使用

#include "boost/date_time/posix_time/posix_time.hpp"
using namespace boost::posix_time;
using namespace boost::gregorian;
#include "boost/date_time/local_time/local_time.hpp"

// form an empty database
boost::local_time::tz_database tz_database;
// load the time zone database which comes with boost
tz_database.load_from_file( "../../boost/libs/date_time/data/date_time_zonespec.csv" ); 

// obtain a specific time zone from the database
boost::local_time::time_zone_ptr tzNewYork;
tzNewYork = tz_database.time_zone_from_region("America/New_York");

// example universal/local conversion (from boost example code)
// ptime now = boost::posix_time::microsec_clock::universal_time();
ptime now(date(2004,Nov,5), hours(10));
boost::local_time::local_date_time ny(now, tzNewYork );
ny.utc_time();  // 10am 2004-Nov-5
ny.local_time();  // 5am 2004-Nov-5

你需要做的不仅仅是看例子。你仔细阅读了文档吗?@Lightness Races in Orbit Fair point,我现在正在阅读文档。你可能会发现这很有趣,你需要做的不仅仅是看示例。你仔细阅读了文档吗?@Lightness Races in Orbit Fair point,我现在正在阅读文档。你可能会发现这很有趣,我们是如何定义的?我好像找不到那上面的文件。Boost不支持像America/New_York这样的IANA区域吗?它是Boost::posix_时间名称空间中的一个typedef。它在posix_time_types.hpp中定义。具体来说,typedef date_time::us_dst_规则us_dstAh,因此它只知道us dst的当前规则。最好使用IANA TZDB。例如,虽然这是UTC的本地时间,但OP想要的是相反的。您仍然得到my+1,因为OP只要求当前的本地时间尽管如此,我还是希望看到一个显示TZDB用法的补充答案。嗯,现在让我来弄清楚这个的c++11版本=us_dst是如何定义的?我好像找不到那上面的文件。Boost不支持像America/New_York这样的IANA区域吗?它是Boost::posix_时间名称空间中的一个typedef。它在posix_time_types.hpp中定义。具体来说,typedef date_time::us_dst_规则us_dstAh,因此它只知道us dst的当前规则。最好使用IANA TZDB。例如,虽然这是UTC的本地时间,但OP想要的是相反的。您仍然得到my+1,因为OP只要求当前的本地时间尽管如此,我还是希望看到一个显示TZDB用法的补充答案。现在让我来了解这个的c++11版本=