C++ boost::posix_Time::ptime中的特定时区
我有以下时间:C++ boost::posix_Time::ptime中的特定时区,c++,boost,C++,Boost,我有以下时间: 2010-01-25 03:13:34.384 - GMT Time Zone 2010-01-25 11:13:34.384 - My Local 我希望转换成毫秒的时间戳,但是,因为我只从调用方获取本地时间字符串 “2010-01-25 11:13:34.384” 如果我这样做: // ts is "2010-01-25 11:13:34.384" (My Local) boost::posix_time::ptime t(boost::posix_time::time_f
2010-01-25 03:13:34.384 - GMT Time Zone
2010-01-25 11:13:34.384 - My Local
我希望转换成毫秒的时间戳,但是,因为我只从调用方获取本地时间字符串
“2010-01-25 11:13:34.384”
如果我这样做:
// ts is "2010-01-25 11:13:34.384" (My Local)
boost::posix_time::ptime t(boost::posix_time::time_from_string(ts));
boost::posix_time::ptime end(boost::gregorian::date(1970,1,1));
boost::posix_time::time_duration dur = t - end;
// epoch is 1264418014384
// 2010-01-25 11:13:34.384 (GMT) -- But I want 2010-01-25 03:13:34.384
// 2010-01-25 19:13:34.384 (My Local) -- But I want 2010-01-25 11:13:34.384
long long epoch = dur.total_milliseconds();
有没有办法告诉boost::posix_time它接收的ts字符串属于我的本地时区?我认为您应该使用处理时区的
boost::Local_date_time
。文档中有一个与您尝试执行的操作非常类似的示例:我的本地树中有这个示例(省略了名称空间前缀):
我可以知道你们如何获得当前时区吗?你们把它保存在TZ环境变量中。
/// wall-clock translation to UTC
const ptime from_wall_clock( const ptime& value,
const time_zone_ptr& from )
{
assert( from.get());
// interpret as local time
const local_date_time from_local( value.date(),
value.time_of_day(), from,
local_date_time::NOT_DATE_TIME_ON_ERROR );
// get UTC
return from_local.utc_time();
}