C++ 中是否有标准的日期/时间类?

2024-05-11C/C++开发问题
9

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

问题描述

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

<前>time_t tm();ostringstream 南;南<

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

解决方案

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

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

如果你还没有尝试过 boost,我鼓励你这样做,因为它可以让你免于许多讨厌的问题,因为它掩盖了大多数操作系统相关的事情,例如线程.boost 中的很多东西都是头文件(模板库).但是 datetime 需要一个 lib 或 dll.

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;

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

解决方案

Not part of STL but well known library is boost.

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.

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++ 中是否有标准的日期/时间类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&amp;()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6