使用boost :: date_time的最简单的方式来获得当前时区当前时间? [英] Simplest way to get current time in current timezone using boost::date_time?
问题描述
如果我日期+%H-%M-%S
在命令行(Debian的/莱尼),我得到一个用户友好(UTC不,不是DST -less,时间一个正常的人,对他们的手表)一次印刷。
If I do date +%H-%M-%S
on the commandline (Debian/Lenny), I get a user-friendly (not UTC, not DST-less, the time a normal person has on their wristwatch) time printed.
什么是获得与同样的事情的boost ::最简单的方法DATE_TIME
?
如果我这样做:
std::ostringstream msg;
boost::local_time::local_date_time t =
boost::local_time::local_sec_clock::local_time(
boost::local_time::time_zone_ptr()
);
boost::local_time::local_time_facet* lf(
new boost::local_time::local_time_facet("%H-%M-%S")
);
msg.imbue(std::locale(msg.getloc(),lf));
msg << t;
然后 msg.str()
比我想看到的时间早一个小时。我不知道这是否是因为它显示没有DST校正(我在英国)UTC或本地时区的时间。
Then msg.str()
is an hour earlier than the time I want to see. I'm not sure whether this is because it's showing UTC or local timezone time without a DST correction (I'm in the UK).
什么是修改上面得到纠正DST本地时区时间最简单的方法?我有它涉及的boost :: date_time的:: c_local_adjustor
,但不能从实例看着办吧。
What's the simplest way to modify the above to yield the DST corrected local timezone time ? I have an idea it involves boost::date_time:: c_local_adjustor
but can't figure it out from the examples.
推荐答案
这我想要做什么:
namespace pt = boost::posix_time;
std::ostringstream msg;
const pt::ptime now = pt::second_clock::local_time();
pt::time_facet*const f = new pt::time_facet("%H-%M-%S");
msg.imbue(std::locale(msg.getloc(),f));
msg << now;
这篇关于使用boost :: date_time的最简单的方式来获得当前时区当前时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!