c++ - 在 C++ 中添加两个日期

标签 c++ c time ctime

我确定这个问题在其他地方得到了回答,但我无法在 Google 或 SO 上找到它,所以这里是。

在C/C++中,我想将相对时间转换成dd-hh:mm:ss格式的相对时间

ps -o etime

到绝对 UTC 格式的日期。

这看起来并不难。假设我已经有一个函数来生成以 struct tm 格式存储的相对时间:

struct tm *starting_rel_time = my_reltime_converstion(...);
time_t t = time(0);
struct tm *current_abs_time =  localtime(&t);

我想要的基本上是 difftime 的对立面:

struct *tm starting_abs_time = current_abs_time - starting_rel_time;

现在,我可以编写自己的函数来进行转换,但由于所有进位操作和特殊条件(闰年等),这是一场噩梦。在 C/C++ 库中肯定有一种方法可以做到这一点吗?

最佳答案

使用Boost::Date_Time图书馆。

关于c++ - 在 C++ 中添加两个日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12566150/

相关文章:

c++ - 这有什么问题吗?包含 Setters/Getters(已编辑)头文件

c++ - 在 dll 中导出函数。 C 和 C++

c - 是否可以使用函数原型(prototype)而不是其主体来编译项目?

c - libusb_claim_interface 在 Mac OS X Mountain Lion 上失败

c - 我的功能之一是意外地从列表中删除元素

c - 暂停执行直到 C 中的特定时间

javascript - 浏览器 JavaScript 中的 new Date().getTime() 是否总是产生 UTC?

c++ - 错误 : cannot convert 'float (*)[(((sizetype)(((ssizetype)n) + -1)) + 1)]' to 'float (*)[100]' for argument '3'

java - 从下一个闹钟获取日期

C++ DLL header 包括