在 C/C++ 中向 time_t 添加 1 个月的简单方法
我有一些代码使用 Oracle 函数 add_months 将日期增加 X 个月。
我现在需要在 C / C++ 函数中重新实现相同的逻辑。 由于我不想/不需要进入的原因,我不能简单地向 Oracle 发出查询来获取新日期。
有谁知道向 time_t 添加 X 个月的简单可靠的方法? 下面显示了一些计算类型的示例。
2009 年 1 月 30 日 + 1 个月 = 2009 年 2 月 28 日
2009 年 1 月 31 日 + 1 个月 = 2009 年 2 月 28 日
2009 年 2 月 27 日 + 1 个月 = 2009 年 3 月 27 日
2009 年 2 月 28 日 + 1 个月 = 2009 年 3 月 31 日
2009 年 1 月 31 日 + 50 个月 = 2013 年 3 月 31 日
I have some code that uses the Oracle function add_months to increment a Date by X number of months.
I now need to re-implement the same logic in a C / C++ function. For reasons I don't want/need to go into I can't simply issue a query to oracle to get the new date.
Does anyone know of a simple and reliable way of adding X number of months to a time_t?
Some examples of the types of calculations are shown below.
30/01/2009 + 1 month = 28/02/2009
31/01/2009 + 1 month = 28/02/2009
27/02/2009 + 1 month = 27/03/2009
28/02/2009 + 1 month = 31/03/2009
31/01/2009 + 50 months = 31/03/2013
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 Boost.GregorianDate 来实现此目的。
更具体地说,通过添加正确的
date_duration
,然后使用end_of_month_day()
来自日期算法You can use Boost.GregorianDate for this.
More specifically, determine the month by adding the correct
date_duration
, and then useend_of_month_day()
from the date algorithms将
time_t
转换为struct tm
,将X添加到月份,添加月份> 12年换算回来。 tm.tm_mon 是一个 int,添加 32000+ 个月应该不是问题。[编辑] 一旦遇到更困难的情况,例如将 12 个月添加到 29/02/2008,您可能会发现匹配 Oracle 很棘手。 01/03/2009 和 28/02/2008 都是合理的。
Convert
time_t
tostruct tm
, add X to month, add months > 12 to years, convert back. tm.tm_mon is an int, adding 32000+ months shouldn't be a problem.[edit] You might find that matching Oracle is tricky once you get to the harder cases, like adding 12 months to 29/02/2008. Both 01/03/2009 and 28/02/2008 are reasonable.
真正对真正老问题的新答案!
使用这个免费开源库和C++14编译器(例如clang)我现在可以这样写:
它可以编译。
请注意实际代码和 OP 伪代码之间的显着相似之处:
另请注意,编译时信息输入会导致编译时信息输出。
C++20更新:
您的 C++ 供应商开始提供 C++20
,无需第三方库即可完成此操作相同的语法。另请注意 OP 添加月份所需的有些不寻常的规则,这可以在
中轻松实现:演示。
Really new answer to a really old question!
Using this free and open source library, and a C++14 compiler (such as clang) I can now write this:
And it compiles.
Note the remarkable similarity between the actual code, and the OP's pseudo-code:
Also note that compile-time information in leads to compile-time information out.
C++20
<chrono>
update:Your C++ vendors are starting to ship C++20
<chrono>
which can do this without a 3rd party library with nearly identical syntax.Also note the somewhat unusual rules the OP requires for adding months, which is easily implementable in
<chrono>
:Demo.
方法 AddMonths_OracleStyle 可以满足您的需要。
也许您希望将 IsLeapYear 和 GetDaysInMonth 替换为某些图书馆员方法。
Method AddMonths_OracleStyle does what you need.
Perhaps you would want to replace IsLeapYear and GetDaysInMonth to some librarian methods.