Added overload for CheckSpecialDay().

Added helpers SpecialDaysWorkTimeFrom() and SpecialDaysWorkTimeUntil().
This commit is contained in:
Gerhard Hoffmann 2023-11-26 19:51:37 +01:00
parent a53cb37291
commit c4cec1c04b
2 changed files with 64 additions and 13 deletions

View File

@ -9,46 +9,46 @@
#include "configuration.h" #include "configuration.h"
#include "time_range.h" #include "time_range.h"
#include <QDateTime>
using namespace std; using namespace std;
class Utilities { namespace Utilities {
public:
/// <summary> /// <summary>
/// Get day of week from current date (Zeller's Algorithm), starting day is Sunday /// Get day of week from current date (Zeller's Algorithm), starting day is Sunday
/// </summary> /// </summary>
/// <param name="date"></param> /// <param name="date"></param>
/// <returns></returns> /// <returns></returns>
static DayOfWeek GetDayOfWeek(struct tm* tm); DayOfWeek GetDayOfWeek(struct tm* tm);
/// <summary> /// <summary>
/// Date and time parse helper function /// Date and time parse helper function
/// </summary> /// </summary>
/// <returns>Returns time (tm) structure</returns> /// <returns>Returns time (tm) structure</returns>
static struct tm DateTimeToStructTm(const char* dateTimeStr); struct tm DateTimeToStructTm(const char* dateTimeStr);
/// <summary> /// <summary>
/// Date parse helper function /// Date parse helper function
/// </summary> /// </summary>
/// <returns>Returns time (tm) structure</returns> /// <returns>Returns time (tm) structure</returns>
static struct tm DateToStructTm(const char* dateStr); struct tm DateToStructTm(const char* dateStr);
/// <summary> /// <summary>
/// Time parse helper function /// Time parse helper function
/// </summary> /// </summary>
/// <returns>Returns time (tm) structure</returns> /// <returns>Returns time (tm) structure</returns>
static struct tm TimeToStructTm(const char* timeStr, int year, int mon, int mday, int wday); struct tm TimeToStructTm(const char* timeStr, int year, int mon, int mday, int wday);
/// <summary> /// <summary>
/// Get current local time /// Get current local time
/// </summary> /// </summary>
/// <returns>Returns time_t structure</returns> /// <returns>Returns time_t structure</returns>
static time_t GetCurrentLocalTime(); time_t GetCurrentLocalTime();
/// <summary> /// <summary>
/// Zeller's algorithm for determining day of week /// Zeller's algorithm for determining day of week
/// </summary> /// </summary>
static int ZellersAlgorithm(int day, int month, int year); int ZellersAlgorithm(int day, int month, int year);
/// <summary> /// <summary>
/// Checks if current datetime is in range between start and end month of parking worktime /// Checks if current datetime is in range between start and end month of parking worktime
@ -56,18 +56,23 @@ public:
/// <param name="tariff_cfg"></param> /// <param name="tariff_cfg"></param>
/// <param name="currentDateTime"></param> /// <param name="currentDateTime"></param>
/// <returns></returns> /// <returns></returns>
static bool IsYearPeriodActive(Configuration* cfg, struct tm* currentDateTime); bool IsYearPeriodActive(Configuration* cfg, struct tm* currentDateTime);
/// <summary> /// <summary>
/// Check permissions /// Check permissions
/// </summary> /// </summary>
static bool CheckSpecialDay(Configuration* cfg, const char* currentDateTimeStr, int* specialDayId, double* specialDayPrice); bool CheckSpecialDay(Configuration* cfg, const char* currentDateTimeStr, int* specialDayId, double* specialDayPrice);
bool CheckSpecialDay(Configuration const *cfg,
QDateTime const &currentDateTimeS,
int* specialDayId, uint32_t *specialDayPrice);
/// <summary> /// <summary>
/// Calculates price per unit /// Calculates price per unit
/// </summary> /// </summary>
/// <param name="pra_price"></param> /// <param name="pra_price"></param>
/// <returns></returns> /// <returns></returns>
static double CalculatePricePerUnit(double pra_price, double durationUnit = -1); double CalculatePricePerUnit(double pra_price, double durationUnit = -1);
}; QTime SpecialDaysWorkTimeFrom(Configuration const *cfg, int specialDayId);
QTime SpecialDaysWorkTimeUntil(Configuration const *cfg, int specialDayId);
}

View File

@ -1,6 +1,8 @@
#include "utilities.h" #include "utilities.h"
#include "tariff_log.h" #include "tariff_log.h"
#include <QDebug>
static int protection_counter = 0; static int protection_counter = 0;
/// <summary> /// <summary>
@ -271,3 +273,47 @@ bool Utilities::CheckSpecialDay(Configuration* cfg, const char* currentDateTimeS
return false; return false;
} }
} }
bool Utilities::CheckSpecialDay(Configuration const *cfg,
QDateTime const &currentDateTime,
int* specialDayId,
uint32_t *specialDayPrice) {
*specialDayId = -1;
*specialDayPrice = 0;
std::multimap<int, ATBSpecialDays>::const_iterator spec_days_itr;
for (spec_days_itr = cfg->SpecialDays.cbegin(); spec_days_itr != cfg->SpecialDays.cend(); ++spec_days_itr) {
int repeat_every_year = spec_days_itr->second.ped_year;
QDate start = QDate::fromString(spec_days_itr->second.ped_date_start.c_str(), Qt::ISODate);
QDate end = QDate::fromString(spec_days_itr->second.ped_date_end.c_str(), Qt::ISODate);
if (start.isValid() && end.isValid()) {
if ((currentDateTime.date().month() >= start.month()) &&
(currentDateTime.date().month() <= end.month())) {
if ((currentDateTime.date().day() >= start.day()) &&
(currentDateTime.date().day() <= end.day())) {
if (repeat_every_year <= 0) {
if ((currentDateTime.date().year() != start.year()) ||
(currentDateTime.date().year() != end.year())) {
continue;
}
}
qDebug() << "CheckSpecialDay() => SPECIAL DAY";
*specialDayId = spec_days_itr->second.ped_id;
*specialDayPrice = cfg->SpecialDaysWorktime.find(*specialDayId)->second.pedwt_price;
return true;
}
}
}
}
return false;
}
QTime Utilities::SpecialDaysWorkTimeFrom(Configuration const *cfg, int specialDayId) {
return QTime::fromString(cfg->SpecialDaysWorktime.find(specialDayId)->second.pedwt_time_from.c_str(), Qt::ISODate);
}
QTime Utilities::SpecialDaysWorkTimeUntil(Configuration const *cfg, int specialDayId) {
return QTime::fromString(cfg->SpecialDaysWorktime.find(specialDayId)->second.pedwt_time_to.c_str(), Qt::ISODate);
}