File indexing completed on 2022-11-22 13:06:17

0001 /*
0002     This file is part of the kholidays library.
0003 
0004     SPDX-FileCopyrightText: 2010 John Layt <john@layt.net>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "astroseasons.h"
0010 #include "holiday_p.h"
0011 #include "holidayparserdriver_p.h"
0012 
0013 #include <kholidays_debug.h>
0014 
0015 using namespace KHolidays;
0016 
0017 HolidayParserDriver::HolidayParserDriver(const QString &filePath)
0018     : m_parseYear(0)
0019     , m_parseStartYear(0)
0020     , m_parseEndYear(0)
0021 {
0022     m_filePath = filePath;
0023 }
0024 
0025 HolidayParserDriver::~HolidayParserDriver()
0026 {
0027 }
0028 
0029 QString HolidayParserDriver::fileCountryCode() const
0030 {
0031     return m_fileCountryCode;
0032 }
0033 
0034 QString HolidayParserDriver::fileLanguageCode() const
0035 {
0036     return m_fileLanguageCode;
0037 }
0038 
0039 QString HolidayParserDriver::fileName() const
0040 {
0041     return m_fileName;
0042 }
0043 
0044 QString HolidayParserDriver::fileDescription() const
0045 {
0046     return m_fileDescription;
0047 }
0048 
0049 Holiday::List HolidayParserDriver::parseHolidays(const QDate &startDate, const QDate &endDate, const QString &category)
0050 {
0051     parseHolidays(startDate, endDate);
0052     Holiday::List m_resultListTemp = m_resultList;
0053     m_resultList.clear();
0054 
0055     for (const KHolidays::Holiday &holidayCat : m_resultListTemp) {
0056         for (const QString &mCategoryList : holidayCat.categoryList()) {
0057             if (mCategoryList == category) {
0058                 m_resultList.append(holidayCat);
0059                 break;
0060             }
0061         }
0062     }
0063 
0064     return m_resultList;
0065 }
0066 
0067 Holiday::List HolidayParserDriver::parseRawHolidays(const QDate &startDate, const QDate &endDate)
0068 {
0069     m_resultList.clear();
0070     if (startDate.isNull() || endDate.isNull()) {
0071         return m_resultList;
0072     }
0073     m_requestStart = startDate;
0074     m_requestEnd = endDate;
0075     parse();
0076     return m_resultList;
0077 }
0078 
0079 Holiday::List HolidayParserDriver::parseHolidays(const QDate &startDate, const QDate &endDate)
0080 {
0081     parseRawHolidays(startDate, endDate);
0082 
0083     for (int year = startDate.year(); year <= endDate.year(); ++year) {
0084         for (auto s : {AstroSeasons::JuneSolstice, AstroSeasons::DecemberSolstice, AstroSeasons::MarchEquinox, AstroSeasons::SeptemberEquinox}) {
0085             const auto dt = AstroSeasons::seasonDate(s, year);
0086             if (dt >= startDate && dt <= endDate) {
0087                 Holiday season;
0088                 season.d->mDayType = Holiday::Workday;
0089                 season.d->mObservedDate = dt;
0090                 season.d->mDuration = 1;
0091                 season.d->mName = AstroSeasons::seasonName(s);
0092                 season.d->mCategoryList.append(QLatin1String("seasonal"));
0093                 m_resultList.append(season);
0094             }
0095         }
0096     }
0097 
0098     std::sort(m_resultList.begin(), m_resultList.end());
0099     return m_resultList;
0100 }
0101 
0102 Holiday::List HolidayParserDriver::parseHolidays(const QDate &requestDate)
0103 {
0104     return parseHolidays(requestDate, requestDate);
0105 }
0106 
0107 Holiday::List HolidayParserDriver::parseHolidays(int calendarYear, QCalendarSystem::CalendarSystem calendar)
0108 {
0109     m_resultList.clear();
0110     setParseCalendar(calendar);
0111     if (!m_parseCalendar.isValid(calendarYear, 1, 1)) {
0112         return m_resultList;
0113     }
0114 
0115     return parseHolidays(m_parseCalendar.firstDayOfYear(calendarYear), //
0116                          m_parseCalendar.lastDayOfYear(calendarYear));
0117 }
0118 
0119 void HolidayParserDriver::error(const QString &errorMessage)
0120 {
0121     qCDebug(KHOLIDAYS_LOG) << errorMessage;
0122 }
0123 
0124 void HolidayParserDriver::parse()
0125 {
0126 }
0127 
0128 void HolidayParserDriver::parseMetadata()
0129 {
0130 }
0131 
0132 void HolidayParserDriver::setParseCalendar(QCalendarSystem::CalendarSystem calendar)
0133 {
0134     m_parseCalendar = QCalendarSystem(calendar);
0135 }
0136 
0137 void HolidayParserDriver::setParseStartEnd()
0138 {
0139     // Set start year and end year to generate holidays for
0140     // TODO Maybe make +/- 1 more year to allow spanned holidays from previous/following years
0141     // Make sure requested date range falls within valid date range for current calendar system
0142     if (m_requestStart > m_parseCalendar.latestValidDate() //
0143         || m_requestEnd < m_parseCalendar.earliestValidDate()) {
0144         // Completely out of range, don't parse
0145         m_parseStartYear = 0;
0146         m_parseEndYear = m_parseStartYear - 1;
0147     } else {
0148         if (m_requestStart < m_parseCalendar.earliestValidDate()) {
0149             m_parseStartYear = m_parseCalendar.year(m_parseCalendar.earliestValidDate());
0150         } else {
0151             m_parseStartYear = m_parseCalendar.year(m_requestStart);
0152         }
0153 
0154         if (m_requestEnd > m_parseCalendar.latestValidDate()) {
0155             m_parseEndYear = m_parseCalendar.year(m_parseCalendar.latestValidDate());
0156         } else {
0157             m_parseEndYear = m_parseCalendar.year(m_requestEnd);
0158         }
0159     }
0160 }