使用 boost::date_time 在当前时区获取当前时间的最简单方法?
如果我在命令行 (Debian/Lenny) 上执行 date +%H-%M-%S
,我会得到一个用户友好的(不是 UTC,不是 DST-less,时间 a正常人在他们的手表上)打印时间.
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
获得相同内容的最简单方法是什么?
What's the simplest way to obtain the same thing with 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()
比我想看的时间早了一个小时.我不确定这是否是因为它显示 UTC 或本地时区时间而没有 DST 更正(我在英国).
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;
相关文章