C++ 使用 boost 获取当地时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2492775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:40:05  来源:igfitidea点击:

get local time with boost

c++boosttime

提问by Max Frai

I didn't find this in documentation: how to get local time (better formatted) with boost?

我没有在文档中找到这个:如何使用 boost 获取本地时间(更好的格式)?

回答by Nate

Use posix_timeto construct a time object from the system clock.

使用posix_time从系统时钟构造一个时间对象。

For example, this would output the current system time as an ISO-format string:

例如,这会将当前系统时间输出为 ISO 格式的字符串:

namespace pt = boost::posix_time;
pt::to_iso_string(pt::second_clock::local_time());

For formatting alternatives, see the “Conversion to String” section of the above-linked reference and the Date Time Input/Outputreference. Alternatively, you can build your own output string using the accessor functions. For example, to get a US-style date:

有关格式化替代方案,请参阅上述链接参考和日期时间输入/输出参考的“转换为字符串”部分。或者,您可以使用访问器函数构建自己的输出字符串。例如,要获取美国风格的日期:

namespace pt = boost::posix_time;
pt::ptime now = pt::second_clock::local_time();
std::stringstream ss;
ss << static_cast<int>(now.date().month()) << "/" << now.date().day()
    << "/" << now.date().year();
std::cout << ss.str() << std::endl;

Note the month is cast to intso it will display as digits. The default output facet will display it as the three-letter month abbreviation (“Mar” for March).

请注意月份被强制转换为int因此它将显示为数字。默认输出方面将其显示为三个字母的月份缩写(“Mar”代表三月)。

回答by zeroDivisible

I don't know if this will be of any help, but boost docs have some examplesof formatting dates.

我不知道这是否有任何帮助,但是 boost 文档有一些格式化日期的示例

Plus, I think that this articledescribes some basics, which are worth looking at.

另外,我认为这篇文章描述了一些基础知识,值得一看。