GetDailyTicketDuration: use QDateTime for timestamps

This commit is contained in:
Siegfried Siegert 2023-05-16 16:43:45 +02:00
parent eef94a3fb3
commit 268d43cdea
Signed by: SiegfriedSiegert
GPG Key ID: 68371E015E8F0B03
3 changed files with 22 additions and 20 deletions

View File

@ -29,5 +29,5 @@ public:
double GetCostFromDuration(Configuration* cfg, uint8_t vehicle_type, const QDateTime start_datetime, QDateTime & end_datetime, double durationMin, bool nextDay = false, bool prepaid = false);
// Daily ticket
QString GetDailyTicketDuration(Configuration* cfg, QString start_datetime, uint8_t payment_option, bool carry_over);
QDateTime GetDailyTicketDuration(Configuration* cfg, const QDateTime start_datetime, uint8_t payment_option, bool carry_over);
};

View File

@ -283,21 +283,18 @@ CalcState CALCULATE_LIBRARY_API compute_duration_for_daily_ticket(parking_tariff
{
CalcState calcState;
if (start_parking_time.isValid()) {
QString cs = start_parking_time.toString(Qt::ISODate);
QString endTime = calculator.GetDailyTicketDuration(tariff,
cs.toLocal8Bit().constData(),
ticketEndTime = calculator.GetDailyTicketDuration(tariff,
start_parking_time,
PaymentOption::Option1,
false); // carry over
ticketEndTime = QDateTime::fromString(endTime,Qt::ISODate);
// DEBUG
qCritical() << "compute_duration_for_daily_ticket(): ";
qCritical() << " endTime: " << endTime;
qCritical() << " ticketEndTime: " << ticketEndTime;
if (!ticketEndTime.isValid()) {
calcState.setDesc(QString("ticketEndTime=%1").arg(endTime));
calcState.setDesc(QString("ticketEndTime=%1").arg(ticketEndTime.toString(Qt::ISODate)));
return calcState.set(CalcState::State::WRONG_ISO_TIME_FORMAT);
}

View File

@ -19,20 +19,25 @@ inline struct tm* localtime_r(const time_t *clock, struct tm* result){
}
#endif
QString Calculator::GetDailyTicketDuration(Configuration* cfg, QString start_datetime, uint8_t payment_option, bool carry_over)
QDateTime Calculator::GetDailyTicketDuration(Configuration* cfg, const QDateTime start_datetime, uint8_t payment_option, bool carry_over)
{
if(start_datetime.isNull() || start_datetime.isEmpty()) return NULL;
if(!start_datetime.isValid()) {
return QDateTime();
}
double day_price = 0.0f;
int current_special_day_id = -1;
bool is_special_day = Utilities::CheckSpecialDay(cfg, start_datetime.toStdString().c_str(), &current_special_day_id, &day_price);
bool is_special_day = Utilities::CheckSpecialDay(cfg, start_datetime.toString(Qt::ISODate).toStdString().c_str(), &current_special_day_id, &day_price);
QDateTime inputDateTime = QDateTime::fromString(start_datetime, Qt::ISODate);
QDateTime inputDateTime = start_datetime;
QTime worktime_from;
QTime worktime_to;
int daily_card_price = cfg->PaymentOption.find(payment_option)->second.pop_daily_card_price;
if(daily_card_price <= 0) return "Daily ticket price zero or less";
if(daily_card_price <= 0) {
LOG_ERROR("Calculator::GetDailyTicketDuration(): Daily ticket price zero or less");
return QDateTime();
}
if(is_special_day)
{
@ -46,21 +51,21 @@ QString Calculator::GetDailyTicketDuration(Configuration* cfg, QString start_dat
{
// Go to next day if outside worktime
inputDateTime = inputDateTime.addSecs(86400);
return GetDailyTicketDuration(cfg,inputDateTime.toString(Qt::ISODate), payment_option,true);
return GetDailyTicketDuration(cfg,inputDateTime, payment_option,true);
}
if(day_price <=0)
{
// Go to next day if special day price is 0
inputDateTime = inputDateTime.addSecs(86400);
return GetDailyTicketDuration(cfg,inputDateTime.toString(Qt::ISODate), payment_option,true);
return GetDailyTicketDuration(cfg,inputDateTime, payment_option,true);
}
int diff = abs(inputDateTime.time().secsTo(worktime_to));
inputDateTime = inputDateTime.addSecs(diff);
//qDebug() << "Ticket is valid until: " << inputDateTime.toString(Qt::ISODate) << "price = " << daily_card_price << ", duration = " << diff / 60;
return inputDateTime.toString(Qt::ISODate) + ", price = " + to_string(daily_card_price).c_str() + ", duration = " + to_string((diff/60)).c_str();
return inputDateTime;
}
else
{
@ -76,7 +81,7 @@ QString Calculator::GetDailyTicketDuration(Configuration* cfg, QString start_dat
if(found <=0)
{
inputDateTime = inputDateTime.addSecs(86400);
return GetDailyTicketDuration(cfg,inputDateTime.toString(Qt::ISODate), payment_option,true);
return GetDailyTicketDuration(cfg,inputDateTime, payment_option,true);
}
else
{
@ -92,18 +97,18 @@ QString Calculator::GetDailyTicketDuration(Configuration* cfg, QString start_dat
{
// Go to next day if outside worktime
inputDateTime = inputDateTime.addSecs(86400);
return GetDailyTicketDuration(cfg,inputDateTime.toString(Qt::ISODate), payment_option,true);
return GetDailyTicketDuration(cfg,inputDateTime, payment_option,true);
}
int diff = abs(inputDateTime.time().secsTo(worktime_to));
inputDateTime = inputDateTime.addSecs(diff);
//qDebug() << "Ticket is valid until: " << inputDateTime.toString(Qt::ISODate) << "price = " << daily_card_price << ", duration = " << diff / 60;
return inputDateTime.toString(Qt::ISODate) + ", price = " + to_string(daily_card_price).c_str() + ", duration = " + to_string((diff/60)).c_str();
//qDebug() << "Ticket is valid until: " << inputDateTime.toString(Qt::ISODate) << "price = " << daily_card_price << ", duration = " << diff / 60;
return inputDateTime;
}
}
return NULL;
return QDateTime();
}
/// <inheritdoc/>
std::string Calculator::GetDurationFromCost(Configuration* cfg,