Simplest way to get current time in current timezone using boost::date_time?(使用 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;
这篇关于使用 boost::date_time 在当前时区获取当前时间的最简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 boost::date_time 在当前时区获取当前时间的最简单方法?
基础教程推荐
- CString 到 char* 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
