easy way to add 1 month to a time_t in C/C++

后端 未结 4 1561
小蘑菇
小蘑菇 2021-01-13 01:51

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.

4条回答
  •  野的像风
    2021-01-13 02:31

    Convert time_t to struct 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.

提交回复
热议问题