Do not duplicate DoGetHolidaysInRange() in the derived class wxDateTimeChristianHolidays had exactly the same implementation as its parent class.
Move holiday implementations away from the header This enables fixing potential bugs in holiday calculations, as well as adding new holidays, through updating dynamic libraries, without recompiling the application using wxWidgets.
Move range check to the public GetHolidaysInRange() Only wxDateTimeWorkDays::DoGetHolidaysInRange() did an early exit when start > end, but surely no other overridden DoGetHolidaysInRange() would want an invalid range either.
Assert that DoGetHolidaysInRange and DoIsHoliday agree on holidays Implementations of the two can be completely independent, yet it would be surprising if they do not consider the same days to be holidays.
Merge branch 'hol-authty-impr' of github.com:lanurmi/wxWidgets Some improvements to holiday authority code. See #25788.
Don't include wx/itemid.h when not using GUI It is used only in the declaration of GUI-only wxTouchSequenceId event, so don't include it when GUI features are disabled. Closes #25791.
... | ... | @@ -1637,43 +1637,13 @@ public: |
1637 | 1637 | // Ascension for a given year.
|
1638 | 1638 | // Celebrated on the 40th day of Easter/
|
1639 | 1639 | // the sixth Thursday after Easter Sunday.
|
1640 | - static wxDateTime GetThursdayAscension(int year)
|
|
1641 | - {
|
|
1642 | - const wxDateTime ascension = GetEaster(year) + wxDateSpan::Days(39);
|
|
1643 | - wxASSERT_MSG(
|
|
1644 | - ascension.GetWeekDay() == wxDateTime::WeekDay::Thu,
|
|
1645 | - "Error in Ascension calculation!");
|
|
1646 | - return ascension;
|
|
1647 | - }
|
|
1640 | + static wxDateTime GetThursdayAscension(int year);
|
|
1648 | 1641 | |
1649 | 1642 | // Ascension for a given year.
|
1650 | 1643 | // Same as traditional Ascension, but moved to the following Sunday.
|
1651 | - static wxDateTime GetSundayAscension(int year)
|
|
1652 | - {
|
|
1653 | - const wxDateTime ascension = GetEaster(year) + wxDateSpan::Weeks(6);
|
|
1654 | - wxASSERT_MSG(
|
|
1655 | - ascension.GetWeekDay() == wxDateTime::WeekDay::Sun,
|
|
1656 | - "Error in Ascension calculation!");
|
|
1657 | - return ascension;
|
|
1658 | - }
|
|
1644 | + static wxDateTime GetSundayAscension(int year);
|
|
1659 | 1645 | protected:
|
1660 | - bool DoIsHoliday(const wxDateTime& dt) const override
|
|
1661 | - {
|
|
1662 | - if (dt.IsSameDate(GetEaster(dt.GetYear())) ||
|
|
1663 | - dt.IsSameDate(GetThursdayAscension(dt.GetYear())) )
|
|
1664 | - {
|
|
1665 | - return true;
|
|
1666 | - }
|
|
1667 | - for (const auto& feast : m_holyDaysOfObligation)
|
|
1668 | - {
|
|
1669 | - if (feast.GetMonth() == dt.GetMonth() &&
|
|
1670 | - feast.GetDay() == dt.GetDay())
|
|
1671 | - {
|
|
1672 | - return true;
|
|
1673 | - }
|
|
1674 | - }
|
|
1675 | - return false;
|
|
1676 | - }
|
|
1646 | + bool DoIsHoliday(const wxDateTime& dt) const override;
|
|
1677 | 1647 | |
1678 | 1648 | size_t DoGetHolidaysInRange(const wxDateTime& dtStart,
|
1679 | 1649 | const wxDateTime& dtEnd,
|
... | ... | @@ -1686,19 +1656,7 @@ private: |
1686 | 1656 | class WXDLLIMPEXP_BASE wxDateTimeChristianHolidays : public wxDateTimeUSCatholicFeasts
|
1687 | 1657 | {
|
1688 | 1658 | protected:
|
1689 | - bool DoIsHoliday(const wxDateTime& dt) const override
|
|
1690 | - {
|
|
1691 | - if (dt.IsSameDate(GetEaster(dt.GetYear())) ||
|
|
1692 | - (dt.GetMonth() == wxDateTime::Month::Dec && dt.GetDay() == 25))
|
|
1693 | - {
|
|
1694 | - return true;
|
|
1695 | - }
|
|
1696 | - return false;
|
|
1697 | - }
|
|
1698 | - |
|
1699 | - size_t DoGetHolidaysInRange(const wxDateTime& dtStart,
|
|
1700 | - const wxDateTime& dtEnd,
|
|
1701 | - wxDateTimeArray& holidays) const override;
|
|
1659 | + bool DoIsHoliday(const wxDateTime& dt) const override;
|
|
1702 | 1660 | };
|
1703 | 1661 | |
1704 | 1662 | // ============================================================================
|
... | ... | @@ -19,12 +19,12 @@ |
19 | 19 | #if wxUSE_GUI
|
20 | 20 | #include "wx/gdicmn.h"
|
21 | 21 | #include "wx/geometry.h"
|
22 | + #include "wx/itemid.h"
|
|
22 | 23 | #include "wx/cursor.h"
|
23 | 24 | #include "wx/mousestate.h"
|
24 | 25 | #endif
|
25 | 26 | |
26 | 27 | #include "wx/dynarray.h"
|
27 | -#include "wx/itemid.h"
|
|
28 | 28 | #include "wx/thread.h"
|
29 | 29 | #include "wx/tracker.h"
|
30 | 30 | #include "wx/typeinfo.h"
|
... | ... | @@ -2193,6 +2193,12 @@ bool wxDateTimeHolidayAuthority::IsHoliday(const wxDateTime& dt) |
2193 | 2193 | {
|
2194 | 2194 | if ( ms_authorities[n]->DoIsHoliday(dt) )
|
2195 | 2195 | {
|
2196 | + // DoIsHoliday() and DoGetHolidaysInRange() may have implementations
|
|
2197 | + // completely independent of each other, but it would be nice if both
|
|
2198 | + // consider the same days to be holidays.
|
|
2199 | + wxDateTimeArray hol;
|
|
2200 | + wxASSERT( ms_authorities[n]->DoGetHolidaysInRange(dt, dt, hol) == 1
|
|
2201 | + && hol.Last() == dt );
|
|
2196 | 2202 | return true;
|
2197 | 2203 | }
|
2198 | 2204 | }
|
... | ... | @@ -2206,6 +2212,13 @@ wxDateTimeHolidayAuthority::GetHolidaysInRange(const wxDateTime& dtStart, |
2206 | 2212 | const wxDateTime& dtEnd,
|
2207 | 2213 | wxDateTimeArray& holidays)
|
2208 | 2214 | {
|
2215 | + if ( dtStart > dtEnd )
|
|
2216 | + {
|
|
2217 | + wxFAIL_MSG( wxT("invalid date range in GetHolidaysInRange") );
|
|
2218 | + |
|
2219 | + return 0u;
|
|
2220 | + }
|
|
2221 | + |
|
2209 | 2222 | wxDateTimeArray hol;
|
2210 | 2223 | |
2211 | 2224 | holidays.Clear();
|
... | ... | @@ -2215,6 +2228,14 @@ wxDateTimeHolidayAuthority::GetHolidaysInRange(const wxDateTime& dtStart, |
2215 | 2228 | {
|
2216 | 2229 | ms_authorities[nAuth]->DoGetHolidaysInRange(dtStart, dtEnd, hol);
|
2217 | 2230 | |
2231 | + // DoIsHoliday() and DoGetHolidaysInRange() may have implementations
|
|
2232 | + // completely independent of each other, but it would be nice if both
|
|
2233 | + // consider the same days to be holidays.
|
|
2234 | + for ( size_t n = 0; n < hol.size(); ++n )
|
|
2235 | + {
|
|
2236 | + wxASSERT( ms_authorities[nAuth]->DoIsHoliday(hol[n]) );
|
|
2237 | + }
|
|
2238 | + |
|
2218 | 2239 | WX_APPEND_ARRAY(holidays, hol);
|
2219 | 2240 | }
|
2220 | 2241 | |
... | ... | @@ -2255,13 +2276,6 @@ size_t wxDateTimeWorkDays::DoGetHolidaysInRange(const wxDateTime& dtStart, |
2255 | 2276 | const wxDateTime& dtEnd,
|
2256 | 2277 | wxDateTimeArray& holidays) const
|
2257 | 2278 | {
|
2258 | - if ( dtStart > dtEnd )
|
|
2259 | - {
|
|
2260 | - wxFAIL_MSG( wxT("invalid date range in GetHolidaysInRange") );
|
|
2261 | - |
|
2262 | - return 0u;
|
|
2263 | - }
|
|
2264 | - |
|
2265 | 2279 | holidays.Empty();
|
2266 | 2280 | |
2267 | 2281 | // instead of checking all days, start with the first Sat after dtStart and
|
... | ... | @@ -2299,6 +2313,7 @@ std::vector<wxDateTime> wxDateTimeUSCatholicFeasts::m_holyDaysOfObligation = |
2299 | 2313 | { wxDateTime(25, wxDateTime::Month::Dec, 0) } // Christmas
|
2300 | 2314 | };
|
2301 | 2315 | |
2316 | +/* static */
|
|
2302 | 2317 | wxDateTime wxDateTimeUSCatholicFeasts::GetEaster(int year)
|
2303 | 2318 | {
|
2304 | 2319 | // Adjust for miscalculation in Gauss formula
|
... | ... | @@ -2372,6 +2387,44 @@ wxDateTime wxDateTimeUSCatholicFeasts::GetEaster(int year) |
2372 | 2387 | }
|
2373 | 2388 | }
|
2374 | 2389 | |
2390 | +/* static */
|
|
2391 | +wxDateTime wxDateTimeUSCatholicFeasts::GetThursdayAscension(int year)
|
|
2392 | +{
|
|
2393 | + const wxDateTime ascension = GetEaster(year) + wxDateSpan::Days(39);
|
|
2394 | + wxASSERT_MSG(
|
|
2395 | + ascension.GetWeekDay() == wxDateTime::WeekDay::Thu,
|
|
2396 | + "Error in Ascension calculation!");
|
|
2397 | + return ascension;
|
|
2398 | +}
|
|
2399 | + |
|
2400 | +/* static */
|
|
2401 | +wxDateTime wxDateTimeUSCatholicFeasts::GetSundayAscension(int year)
|
|
2402 | +{
|
|
2403 | + const wxDateTime ascension = GetEaster(year) + wxDateSpan::Weeks(6);
|
|
2404 | + wxASSERT_MSG(
|
|
2405 | + ascension.GetWeekDay() == wxDateTime::WeekDay::Sun,
|
|
2406 | + "Error in Ascension calculation!");
|
|
2407 | + return ascension;
|
|
2408 | +}
|
|
2409 | + |
|
2410 | +bool wxDateTimeUSCatholicFeasts::DoIsHoliday(const wxDateTime& dt) const
|
|
2411 | +{
|
|
2412 | + if (dt.IsSameDate(GetEaster(dt.GetYear())) ||
|
|
2413 | + dt.IsSameDate(GetThursdayAscension(dt.GetYear())) )
|
|
2414 | + {
|
|
2415 | + return true;
|
|
2416 | + }
|
|
2417 | + for (const auto& feast : m_holyDaysOfObligation)
|
|
2418 | + {
|
|
2419 | + if (feast.GetMonth() == dt.GetMonth() &&
|
|
2420 | + feast.GetDay() == dt.GetDay())
|
|
2421 | + {
|
|
2422 | + return true;
|
|
2423 | + }
|
|
2424 | + }
|
|
2425 | + return false;
|
|
2426 | +}
|
|
2427 | + |
|
2375 | 2428 | size_t wxDateTimeUSCatholicFeasts::DoGetHolidaysInRange(const wxDateTime& dtStart,
|
2376 | 2429 | const wxDateTime& dtEnd,
|
2377 | 2430 | wxDateTimeArray& holidays) const
|
... | ... | @@ -2393,21 +2446,14 @@ size_t wxDateTimeUSCatholicFeasts::DoGetHolidaysInRange(const wxDateTime& dtStar |
2393 | 2446 | // wxDateTimeChristianHolidays
|
2394 | 2447 | // ----------------------------------------------------------------------------
|
2395 | 2448 | |
2396 | -size_t wxDateTimeChristianHolidays::DoGetHolidaysInRange(const wxDateTime& dtStart,
|
|
2397 | - const wxDateTime& dtEnd,
|
|
2398 | - wxDateTimeArray& holidays) const
|
|
2449 | +bool wxDateTimeChristianHolidays::DoIsHoliday(const wxDateTime& dt) const
|
|
2399 | 2450 | {
|
2400 | - holidays.Clear();
|
|
2401 | - |
|
2402 | - for (wxDateTime dt = dtStart; dt <= dtEnd; dt += wxDateSpan::Day())
|
|
2451 | + if (dt.IsSameDate(GetEaster(dt.GetYear())) ||
|
|
2452 | + (dt.GetMonth() == wxDateTime::Month::Dec && dt.GetDay() == 25))
|
|
2403 | 2453 | {
|
2404 | - if (DoIsHoliday(dt) )
|
|
2405 | - {
|
|
2406 | - holidays.Add(dt);
|
|
2407 | - }
|
|
2454 | + return true;
|
|
2408 | 2455 | }
|
2409 | - |
|
2410 | - return holidays.size();
|
|
2456 | + return false;
|
|
2411 | 2457 | }
|
2412 | 2458 | |
2413 | 2459 | // ============================================================================
|
—
View it on GitLab.
You're receiving this email because of your account on gitlab.com. Manage all notifications · Help