From a1e7f4629a10ae61d10f20808d162db51f8b486e Mon Sep 17 00:00:00 2001 From: Gerhard Hoffmann Date: Wed, 2 Oct 2024 15:18:16 +0200 Subject: [PATCH] getService(): Check for special days (holidays) with service-time-ranges. Holidays have higher priority than usual days ("default"). --- library/src/calculator_functions.cpp | 42 ++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/library/src/calculator_functions.cpp b/library/src/calculator_functions.cpp index 60f18ae..d944ec4 100644 --- a/library/src/calculator_functions.cpp +++ b/library/src/calculator_functions.cpp @@ -252,17 +252,53 @@ std::optional getService(Configuration const *cfg, QDateTime c std::optional value = std::nullopt; int weekDay = dt.date().dayOfWeek(); + + // qCritical() << __func__ << ":" << __LINE__ << dt.toString(Qt::ISODate) << weekDay; + ATBTime inputTime(dt.time()); auto const &serviceRange = cfg->TariffServices.equal_range(weekDay); + + QDate d; // check if a special date is configured in tariff-file for this day for (auto i = serviceRange.first; i != serviceRange.second; ++i) { ATBTariffService const &service = i->second; - TimeRange const &serviceTimeRange = service.m_range; - if (inputTime >= serviceTimeRange.m_start && inputTime < serviceTimeRange.m_end) { - value = value.value_or(i->second); + if (!service.m_date.isNull() && service.m_date.isValid() && service.m_date == dt.date()) { + d = dt.date(); + // qCritical() << __func__ << ":" << __LINE__ << "found special day" << d.toString(Qt::ISODate); break; } } + if (!d.isNull() && d.isValid()) { + for (auto i = serviceRange.first; i != serviceRange.second; ++i) { + ATBTariffService const &service = i->second; + + if (!service.m_date.isNull() && service.m_date.isValid() && service.m_date == d) { + TimeRange const &serviceTimeRange = service.m_range; + if (inputTime >= serviceTimeRange.m_start && inputTime < serviceTimeRange.m_end) { + // qCritical() << __func__ << ":" << __LINE__ << serviceTimeRange.m_start.toString(Qt::ISODate); + // qCritical() << __func__ << ":" << __LINE__ << serviceTimeRange.m_end.toString(Qt::ISODate); + value = value.value_or(service); + break; + } + } + } + } else { + // qCritical() << __func__ << ":" << __LINE__ << "no special day" << dt.date().toString(Qt::ISODate); + for (auto i = serviceRange.first; i != serviceRange.second; ++i) { + ATBTariffService const &service = i->second; + if (service.m_date.isNull() || !service.m_date.isValid()) { + // qCritical() << __func__ << ":" << __LINE__ << "default"; + TimeRange const &serviceTimeRange = service.m_range; + if (inputTime >= serviceTimeRange.m_start && inputTime < serviceTimeRange.m_end) { + // qCritical() << __func__ << ":" << __LINE__ << serviceTimeRange.m_start.toString(Qt::ISODate); + // qCritical() << __func__ << ":" << __LINE__ << serviceTimeRange.m_end.toString(Qt::ISODate); + value = value.value_or(service); + break; + } + } + } + } + return value; }