Fix bugs in the recently added wxDateTime::DiffAsDateSpan().
Correct the test for negative spans less than a month and use the correct month for computing the number of days in it. Also add unit tests for problematic cases. Closes #14704. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@72616 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -1612,25 +1612,34 @@ wxDateSpan wxDateTime::DiffAsDateSpan(const wxDateTime& dt) const
|
||||
inv = -1;
|
||||
|
||||
int y = GetYear() - dt.GetYear();
|
||||
int m = GetMonth() - dt.GetMonth();
|
||||
int d = GetDay() - dt.GetDay();
|
||||
|
||||
// If month diff is negative, dt is the year before, so decrease year
|
||||
// and set month diff to its inverse, e.g. January - December should be 1,
|
||||
// not -11.
|
||||
int m = GetMonth() - dt.GetMonth();
|
||||
if ( m * inv < 0 )
|
||||
if ( m * inv < 0 || (m == 0 && d * inv < 0))
|
||||
{
|
||||
m += inv * MONTHS_IN_YEAR;
|
||||
y -= inv;
|
||||
}
|
||||
|
||||
// Same logic for days as for months above. Use number of days in month
|
||||
// from the month which end date we're crossing.
|
||||
int d = GetDay() - dt.GetDay();
|
||||
// Same logic for days as for months above.
|
||||
if ( d * inv < 0 )
|
||||
{
|
||||
d += inv * wxDateTime::GetNumberOfDays(
|
||||
inv > 0 ? dt.GetMonth() : GetMonth(),
|
||||
inv > 0 ? dt.GetYear() : GetMonth());
|
||||
// Use number of days in month from the month which end date we're
|
||||
// crossing. That is month before this for positive diff, and this
|
||||
// month for negative diff.
|
||||
// If we're on january and using previous month, we get december
|
||||
// previous year, but don't care, december has same amount of days
|
||||
// every year.
|
||||
wxDateTime::Month monthfordays = GetMonth();
|
||||
if (inv > 0 && monthfordays == wxDateTime::Jan)
|
||||
monthfordays = wxDateTime::Dec;
|
||||
else if (inv > 0)
|
||||
monthfordays = static_cast<wxDateTime::Month>(monthfordays - 1);
|
||||
|
||||
d += inv * wxDateTime::GetNumberOfDays(monthfordays, GetYear());
|
||||
m -= inv;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user