在C ++中有标准的日期/时间类吗? [英] Is there a standard date/time class in C++?

查看:167
本文介绍了在C ++中有标准的日期/时间类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ stl有标准时间类吗?或者在写入流之前,我必须转换为c-string。例如,我想将当前日期/时间输出到字符串流:

Does C++ stl have a standard time class? Or do I have to convert to c-string before writing to a stream. Example, I want to output the current date/time to a string stream:


time_t tm();
ostringstream sout;
sout << tm << ends;

在这种情况下,我将当前日期/时间写成一个没有任何格式的数字。我可以使用
c- runtime函数 strftime 来格式化tm,但是似乎没有必要,如果stl有一个时间类可以实例化从time_t值

In this case I get the current date/time written out as a number without any formatting. I can use c- runtime function strftime to format tm first, but that seems like it should not be necessary if the stl has a time class that can be instantiated from time_t value

推荐答案

不是STL的一部分,但众所周知的库是boost。

Not part of STL but well known library is boost.

我会使用 boost :: date 的方式。以下是一些示例: http://www.boost.org /doc/libs/1_55_0/doc/html/date_time/date_time_io.html#date_time.io_tutorial

I would go the way of using boost::date. Here are some examples: http://www.boost.org/doc/libs/1_55_0/doc/html/date_time/date_time_io.html#date_time.io_tutorial.

如果您没有尝试提升,但我鼓励你这样做,因为它避免了很多讨厌的问题,因为它掩盖了大多数操作系统依赖的东西,例如线程。 boost 中的很多东西都是头文件(模板库)。但是datetime需要一个lib或dll。

If you did not try out boost yet I encourage you to do so as it saves you from a lot of nasty issues, as it masks most OS dependent things like threading for example. Many things in boost are header only (template libraries). However datetime requires a lib or dll.

这篇关于在C ++中有标准的日期/时间类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆