【问题标题】:get local time with boost通过 boost 获取当地时间
【发布时间】:2011-01-30 08:52:40
【问题描述】:

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

【问题讨论】:

  • 我的意思是我要格式化输出数据。

标签: c++ boost time


【解决方案1】:

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

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

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

有关格式选项,请参阅上述链接参考和Date Time Input/Output 参考的“转换为字符串”部分。或者,您可以使用访问器函数构建自己的输出字符串。例如,要获取美式日期:

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;

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

【讨论】:

  • 注意:所需的包含是:boost/date_time/posix_time/posix_time.hpp(我之前偶然发现了这个)
【解决方案2】:

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

另外,我认为this article 描述了一些基础知识,值得一看。

【讨论】:

    【解决方案3】:

    【讨论】:

    • 有很多转换输入数据的样本,但不是从系统信息中获取数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-02
    相关资源
    最近更新 更多