Added GetYearPeriodActive() to get active year period.

This commit is contained in:
Gerhard Hoffmann 2024-08-01 15:45:50 +02:00
parent 260973cd09
commit 89b05ffadc
2 changed files with 39 additions and 0 deletions

View File

@ -3,6 +3,7 @@
#include <ctime>
#include <iostream>
#include <cmath>
#include <optional>
#include "day_of_week.h"
#include "configuration.h"
@ -62,6 +63,7 @@ namespace Utilities {
/// <returns></returns>
bool IsYearPeriodActive(Configuration* cfg, struct tm* currentDateTime);
bool IsYearPeriodActive(Configuration const *cfg, QDateTime const &currentDateTime);
std::optional<ATBPeriodYear> GetYearPeriodActive(Configuration const *cfg, QDateTime const &currentDateTime);
/// <summary>
/// Check permissions

View File

@ -215,6 +215,43 @@ bool Utilities::IsYearPeriodActive(Configuration const *cfg, QDateTime const &dt
return true;
}
std::optional<ATBPeriodYear>
Utilities::GetYearPeriodActive(Configuration const *cfg, QDateTime const &currentDateTime) {
ATBPeriodYear period;
std::optional<ATBPeriodYear> value;
for (multimap<int, ATBPeriodYear>::const_iterator it = cfg->YearPeriod.cbegin();
it != cfg->YearPeriod.cend();
++it) {
QDate const s(currentDateTime.date().year(), it->second.pye_start_month, it->second.pye_start_day);
QDate const e(currentDateTime.date().year(), it->second.pye_end_month, it->second.pye_end_day);
QDateTime start(currentDateTime);
QDateTime end(currentDateTime);
start.setDate(s);
end.setDate(e);
if (start.date().dayOfYear() < end.date().dayOfYear()) {
// start and end in the same year
if (currentDateTime.date().dayOfYear() >= start.date().dayOfYear() &&
currentDateTime.date().dayOfYear() <= end.date().dayOfYear()) {
value = value.value_or(it->second);
break;
}
} else {
if (currentDateTime.date().dayOfYear() >= start.date().dayOfYear() ||
currentDateTime.date().dayOfYear() <= end.date().dayOfYear()) {
value = value.value_or(it->second);
break;
}
}
}
return value;
}
/// <inheritdoc/>
bool Utilities::CheckSpecialDay(Configuration* cfg, const char* currentDateTimeStr, int* specialDayId, double* specialDayPrice)
{