KOpeningHours

holidaycache.cpp
1/*
2 SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "holidaycache_p.h"
8
9#include <kholidays_version.h>
10#include <KHolidays/HolidayRegion>
11
12#include <QDate>
13#include <QHash>
14
15using namespace KOpeningHours;
16
17KHolidays::HolidayRegion HolidayCache::resolveRegion(QStringView region)
18{
19 static QHash<QString, QString> s_holidayRegionCache;
20
21 const auto idx = region.indexOf(QLatin1Char('_')); // compatibility with KHolidays region codes
22 if (idx > 0) {
23 region = region.left(idx);
24 }
25
26 const auto loc = region.toString();
27
28 const auto it = s_holidayRegionCache.constFind(loc);
29 if (it != s_holidayRegionCache.constEnd()) {
30 return KHolidays::HolidayRegion(it.value());
31 }
32
34 s_holidayRegionCache.insert(loc, code);
35 return KHolidays::HolidayRegion(code);
36}
37
38struct HolidayCacheEntry {
39 QDate begin;
40 QDate end;
42};
43
44static KHolidays::Holiday nextHoliday(const KHolidays::Holiday::List &holidays, QDate date)
45{
46 for (const auto &h : holidays) {
47 if (h.observedStartDate() >= date)
48 return h;
49 }
50 return {};
51}
52
53KHolidays::Holiday HolidayCache::nextHoliday(const KHolidays::HolidayRegion &region, QDate date)
54{
55 static QHash<QString, HolidayCacheEntry> s_holidayCache;
56
57 if (!region.isValid()) {
58 return {};
59 }
60
61 const auto it = s_holidayCache.constFind(region.regionCode());
62 if (it != s_holidayCache.constEnd() && date >= it.value().begin && date.addYears(1) < it.value().end) {
63 return nextHoliday(it.value().holidays, date);
64 }
65
66 HolidayCacheEntry entry;
67 entry.begin = date.addDays(-7);
68 entry.end = date.addYears(2).addDays(7);
69 if (it != s_holidayCache.constEnd()) {
70 entry.begin = std::min(it.value().begin, entry.begin);
71 entry.end = std::max(it.value().end, entry.end);
72 }
73#if KHOLIDAYS_VERSION >= QT_VERSION_CHECK(5, 95, 0)
74 entry.holidays = region.rawHolidays(entry.begin, entry.end);
75#else
76 entry.holidays = region.holidays(entry.begin, entry.end);
77#endif
78 entry.holidays.erase(std::remove_if(entry.holidays.begin(), entry.holidays.end(), [](const auto &h) {
79 return h.dayType() != KHolidays::Holiday::NonWorkday;
80 }), entry.holidays.end());
81 std::sort(entry.holidays.begin(), entry.holidays.end(), [](const auto &lhs, const auto &rhs) {
82 return lhs.observedStartDate() < rhs.observedStartDate();
83 });
84 s_holidayCache.insert(region.regionCode(), entry);
85 return nextHoliday(entry.holidays, date);
86}
Holiday::List rawHolidays(const QDate &startDate, const QDate &endDate) const
static QString defaultRegionCode(const QString &country=QString(), const QString &language=QString())
QString regionCode() const
OSM opening hours parsing and evaluation.
Definition display.h:16
QDate addDays(qint64 ndays) const const
QDate addYears(int nyears) const const
const_iterator constEnd() const const
const_iterator constFind(const Key &key) const const
iterator insert(const Key &key, const T &value)
iterator begin()
iterator end()
iterator erase(const_iterator begin, const_iterator end)
QStringView left(qsizetype length) const const
qsizetype indexOf(QChar c, qsizetype from, Qt::CaseSensitivity cs) const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 8 2024 11:58:07 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.