gpt4 book ai didi

c++ - boost::posix_time::ptime 中的特定时区

转载 作者:太空狗 更新时间:2023-10-29 21:08:57 27 4
gpt4 key购买 nike

我有以下时间:

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 字符串属于我的本地时区吗?

最佳答案

我在我的本地树中有这个(命名空间前缀省略):

    /// 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();
}

关于c++ - boost::posix_time::ptime 中的特定时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2130070/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com