diff --git a/library/include/mobilisis/utilities.h b/library/include/mobilisis/utilities.h index 09de817..72192b7 100644 --- a/library/include/mobilisis/utilities.h +++ b/library/include/mobilisis/utilities.h @@ -9,46 +9,46 @@ #include "configuration.h" #include "time_range.h" +#include + using namespace std; -class Utilities { -public: - +namespace Utilities { /// /// Get day of week from current date (Zeller's Algorithm), starting day is Sunday /// /// /// - static DayOfWeek GetDayOfWeek(struct tm* tm); + DayOfWeek GetDayOfWeek(struct tm* tm); /// /// Date and time parse helper function /// /// Returns time (tm) structure - static struct tm DateTimeToStructTm(const char* dateTimeStr); + struct tm DateTimeToStructTm(const char* dateTimeStr); /// /// Date parse helper function /// /// Returns time (tm) structure - static struct tm DateToStructTm(const char* dateStr); + struct tm DateToStructTm(const char* dateStr); /// /// Time parse helper function /// /// Returns time (tm) structure - 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); /// /// Get current local time /// /// Returns time_t structure - static time_t GetCurrentLocalTime(); + time_t GetCurrentLocalTime(); /// /// Zeller's algorithm for determining day of week /// - static int ZellersAlgorithm(int day, int month, int year); + int ZellersAlgorithm(int day, int month, int year); /// /// Checks if current datetime is in range between start and end month of parking worktime @@ -56,18 +56,23 @@ public: /// /// /// - static bool IsYearPeriodActive(Configuration* cfg, struct tm* currentDateTime); + bool IsYearPeriodActive(Configuration* cfg, struct tm* currentDateTime); /// /// Check permissions /// - 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 ¤tDateTimeS, + int* specialDayId, uint32_t *specialDayPrice); /// /// Calculates price per unit /// /// /// - 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); +} diff --git a/library/src/utilities.cpp b/library/src/utilities.cpp index cb5d896..3550d6c 100644 --- a/library/src/utilities.cpp +++ b/library/src/utilities.cpp @@ -1,6 +1,8 @@ #include "utilities.h" #include "tariff_log.h" +#include + static int protection_counter = 0; /// @@ -271,3 +273,47 @@ bool Utilities::CheckSpecialDay(Configuration* cfg, const char* currentDateTimeS return false; } } + +bool Utilities::CheckSpecialDay(Configuration const *cfg, + QDateTime const ¤tDateTime, + int* specialDayId, + uint32_t *specialDayPrice) { + *specialDayId = -1; + *specialDayPrice = 0; + + std::multimap::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); +}