我有一些代码使用Oracle函数add_months将Date增加X个月.
我现在需要在C/C++函数中重新实现相同的逻辑.由于我不想/需要进入的原因,我不能简单地向oracle发出查询以获取新日期.
有没有人知道在time_t中添加X个月的简单可靠的方法?
计算类型的一些示例如下所示.
30/01/2009 1月= 28/02/2009
31/01/2009 1月= 28/02/2009
27/02/2009 1月= 27/03/2009
28/02/2009 1月= 31/03/2009
31/01/2009 50个月= 31/03/2013
解决方法
方法AddMonths_OracleStyle可以满足您的需求.
也许你想要将IsLeapYear和GetDaysInMonth替换为一些图书管理员方法.
#include <ctime> #include <assert.h> bool IsLeapYear(int year) { if (year % 4 != 0) return false; if (year % 400 == 0) return true; if (year % 100 == 0) return false; return true; } int daysInMonths[] = {31,28,31,30,31}; int GetDaysInMonth(int year,int month) { assert(month >= 0); assert(month < 12); int days = daysInMonths[month]; if (month == 1 && IsLeapYear(year)) // February of a leap year days += 1; return days; } tm AddMonths_OracleStyle(const tm &d,int months) { bool isLastDayInMonth = d.tm_mday == GetDaysInMonth(d.tm_year,d.tm_mon); int year = d.tm_year + months / 12; int month = d.tm_mon + months % 12; if (month > 11) { year += 1; month -= 12; } int day; if (isLastDayInMonth) day = GetDaysInMonth(year,month); // Last day of month maps to last day of result month else day = std::min(d.tm_mday,GetDaysInMonth(year,month)); tm result = tm(); result.tm_year = year; result.tm_mon = month; result.tm_mday = day; result.tm_hour = d.tm_hour; result.tm_min = d.tm_min; result.tm_sec = d.tm_sec; return result; } time_t AddMonths_OracleStyle(const time_t &date,int months) { tm d = tm(); localtime_s(&d,&date); tm result = AddMonths_OracleStyle(d,months); return mktime(&result); }