自当地时间上午8点以来,将时间经过的时间与Boost

Getting the time elapsed since 8am local time with boost

本文关键字:时间 经过 Boost 时间上 8点      更新时间:2023-10-16

,由于大多数DST时髦的东西发生在午夜左右,我认为简单地从"上午8点"中减去当前时间应该是安全的,但这不是解决方案,我非常非常舒适。

(在以下内容中,假定机器的当地时间是美国/new_york,这是我目的的安全假设)

要解释我要寻找的东西,这就是我在python中做的方式

from datetime import datetime,date,time
print ( datetime.now() - datetime.combine( date.today(), time(8,0,0) ) ).total_seconds()

,或者如果我有点̶

from datetime import datetime,date,time
import pytz
tz = pytz.timezone('America/New_York')  #local time is always New York
now = pytz.UTC.localize(datetime.utcnow()).astimezone(tz) # now in this timezone
eight_am = tz.localize( datetime.combine( now.date(), time(8,0,0) ) ) 
(now - eight_am).total_seconds()

我将如何使用Boost实现C 中的每个人?

我会回答自己的第一部分:

namespace pt = boost::posix_time; 
pt::ptime now = pt::second_clock::local_time();
pt::ptime then = pt::ptime(now.date(),pt::hours(8));
pt::time_duration dt = now - then;      
std::cout << dt.total_seconds() << std::endl;