diff --git a/uppsrc/Core/TimeDate.cpp b/uppsrc/Core/TimeDate.cpp index 645be601d..75666ad1b 100644 --- a/uppsrc/Core/TimeDate.cpp +++ b/uppsrc/Core/TimeDate.cpp @@ -363,6 +363,11 @@ int GetMonths(Date since, Date till) return 12 * till.year + till.month - (12 * since.year + since.month) + (till.day >= since.day) - 1; } +int GetMonthsP(Date since, Date till) +{ + return 12 * till.year + till.month - (12 * since.year + since.month) + (till.day > since.day); +} + Date AddYears(Date date, int years) { date.year += years; date.day = min(date.day, LastDayOfMonth(date).day); diff --git a/uppsrc/Core/TimeDate.h b/uppsrc/Core/TimeDate.h index c40eb86e9..66e8028f9 100644 --- a/uppsrc/Core/TimeDate.h +++ b/uppsrc/Core/TimeDate.h @@ -61,6 +61,7 @@ int DayOfYear(Date d); Date AddMonths(Date date, int months); int GetMonths(Date since, Date till); +int GetMonthsP(Date since, Date till); Date AddYears(Date date, int years); Date GetWeekDate(int year, int week); diff --git a/uppsrc/Core/src.tpp/DateTime$en-us.tpp b/uppsrc/Core/src.tpp/DateTime$en-us.tpp index 29b947ea1..8cc87be4a 100644 --- a/uppsrc/Core/src.tpp/DateTime$en-us.tpp +++ b/uppsrc/Core/src.tpp/DateTime$en-us.tpp @@ -221,7 +221,8 @@ nth], [@(0.0.255) int]_[*@3 year])&] [s4; &] [s5;:GetMonths`(Date`,Date`): [@(0.0.255) int]_[* GetMonths]([_^Date^ Date]_[*@3 since], [_^Date^ Date]_[*@3 till])&] -[s2;%% Number of months between two dates.&] +[s2;%% Number of months between two dates. Only whole months are +counted.&] [s2;%% Examples:&] [s2;%% [*C@3 ][*C GetMonths(Date(2013, 2, 5), Date(2013, 3, 4)) `= 0]&] @@ -233,6 +234,20 @@ nth], [@(0.0.255) int]_[*@3 year])&] `= 1]&] [s3;%% &] [s4; &] +[s5;:Upp`:`:GetMonthsP`(Upp`:`:Date`,Upp`:`:Date`): [@(0.0.255) int]_[* GetMonthsP]([_^Upp`:`:Date^ D +ate]_[*@3 since], [_^Upp`:`:Date^ Date]_[*@3 till])&] +[s2;%% Number of months between two dates, including partial months.&] +[s2;%% Examples:&] +[s2;%% [*C GetMonthsP(Date(2016, 1, 1), Date(2016, 1, 1)) `= +0]&] +[s2;%% [*C GetMonthsP(Date(2016, 1, 1), Date(2016, 1, 2)) `= +1 // partial]&] +[s2;%% [*C GetMonthsP(Date(2016, 1, 1), Date(2016, 2, 1)) `= +1 // exact one]&] +[s2;%% [*C GetMonthsP(Date(2016, 1, 1), Date(2016, 2, 2)) `= +2 // partial 2nd]&] +[s3;%% &] +[s4; &] [s5;:AddYears`(Date`,int`): [_^Date^ Date]_[* AddYears]([_^Date^ Date]_[*@3 date], [@(0.0.255) int]_[*@3 years])&] [s2;%% Adds [%-*@3 years] to [%-*@3 date] and returns the result. Note