答案 0 :(得分:6)
对于OP以及想要查看正在使用的tz数据库的Matt Johnson来说。
#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
答案 1 :(得分:4)
首先,您需要获得UTC时间:您可以使用boost::posix_time::second_clock::universal_time()
.
从您刚刚给出的链接:
//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";
要将其转换为纽约时间,只需为您的时区定义一个local_adjustor,然后从中调用utc_to_local
。