Change interface: use QDateTime

- use QDateTime instead of char*-string
 - GetCostFromDuration: add end_datetime as a return value
This commit is contained in:
Siegfried Siegert 2023-05-16 15:31:53 +02:00
parent 88a0b6ebe2
commit eef94a3fb3
Signed by: SiegfriedSiegert
GPG Key ID: 68371E015E8F0B03
4 changed files with 40 additions and 32 deletions

View File

@ -109,7 +109,7 @@ CalcState CALCULATE_LIBRARY_API init_tariff(parking_tariff_t **tariff,
void CALCULATE_LIBRARY_API free_tariff(parking_tariff_t *tariff); void CALCULATE_LIBRARY_API free_tariff(parking_tariff_t *tariff);
int CALCULATE_LIBRARY_API get_zone_nr(int zone = -1); int CALCULATE_LIBRARY_API get_zone_nr(int zone = -1);
CalcState CALCULATE_LIBRARY_API compute_price_for_parking_ticket( CalcState CALCULATE_LIBRARY_API compute_price_for_parking_ticket( // deprecated
parking_tariff_t *tariff, parking_tariff_t *tariff,
time_t start_parking_time, time_t start_parking_time,
time_t end_parking_time, time_t end_parking_time,
@ -118,10 +118,11 @@ CalcState CALCULATE_LIBRARY_API compute_price_for_parking_ticket(
CalcState CALCULATE_LIBRARY_API compute_price_for_parking_ticket( CalcState CALCULATE_LIBRARY_API compute_price_for_parking_ticket(
parking_tariff_t *tariff, parking_tariff_t *tariff,
QDateTime const &start_parking_time, QDateTime const &start_parking_time,
QDateTime const &end_parking_time, int netto_parking_time,
struct price_t *price); QDateTime &end_parking_time, // return value
struct price_t *price); // return value
CalcState CALCULATE_LIBRARY_API compute_duration_for_parking_ticket( CalcState CALCULATE_LIBRARY_API compute_duration_for_parking_ticket( // deprecated
parking_tariff_t *tariff, parking_tariff_t *tariff,
time_t start_parking_time, time_t start_parking_time,
double cost, double cost,
@ -131,7 +132,7 @@ CalcState CALCULATE_LIBRARY_API compute_duration_for_parking_ticket(
parking_tariff_t *tariff, parking_tariff_t *tariff,
QDateTime const &start_parking_time, QDateTime const &start_parking_time,
double cost, double cost,
QDateTime &ticketEndTime); QDateTime &ticketEndTime); // return value
CalcState CALCULATE_LIBRARY_API compute_duration_for_daily_ticket( CalcState CALCULATE_LIBRARY_API compute_duration_for_daily_ticket(
parking_tariff_t *tariff, parking_tariff_t *tariff,

View File

@ -23,9 +23,10 @@ public:
/// <param name="tariff_cfg">Pointer to configuration</param> /// <param name="tariff_cfg">Pointer to configuration</param>
/// <param name="vehicle_type">Type of vehicle</param> /// <param name="vehicle_type">Type of vehicle</param>
/// <param name="start_datetime">Date/time of payment to be conducted in ISO8601 format (e.g. 2022-12-25T08:00:00Z) </param> /// <param name="start_datetime">Date/time of payment to be conducted in ISO8601 format (e.g. 2022-12-25T08:00:00Z) </param>
/// <param name="end_datetime">Date/time of park end to be conducted in ISO8601 format (e.g. 2022-12-25T08:00:00Z) </param>
/// <param name="durationMin">Duration of parking in minutes</param> /// <param name="durationMin">Duration of parking in minutes</param>
/// <returns>Returns cost (data type: double)</returns> /// <returns>Returns cost (data type: double)</returns>
double GetCostFromDuration(Configuration* cfg, uint8_t vehicle_type, char const* start_datetime, double durationMin, bool nextDay = false, bool prepaid = false); 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 // Daily ticket
QString GetDailyTicketDuration(Configuration* cfg, QString start_datetime, uint8_t payment_option, bool carry_over); QString GetDailyTicketDuration(Configuration* cfg, QString start_datetime, uint8_t payment_option, bool carry_over);

View File

@ -92,10 +92,12 @@ void CALCULATE_LIBRARY_API free_tariff(parking_tariff_t *tariff) {
} }
} }
// this is currently not used
CalcState CALCULATE_LIBRARY_API compute_price_for_parking_ticket( CalcState CALCULATE_LIBRARY_API compute_price_for_parking_ticket(
parking_tariff_t *tariff, parking_tariff_t *tariff,
time_t start_parking_time, // in minutes time_t start_parking_time, // in minutes
time_t end_parking_time, // in minutes time_t end_parking_time, // netto time in minutes
struct price_t *price) { struct price_t *price) {
CalcState calcState; CalcState calcState;
double minMin = tariff->PaymentOption.find(PaymentOption::Option1)->second.pop_min_time; double minMin = tariff->PaymentOption.find(PaymentOption::Option1)->second.pop_min_time;
@ -129,11 +131,12 @@ CalcState CALCULATE_LIBRARY_API compute_price_for_parking_ticket(
QTime const t(0, 0, 0); QTime const t(0, 0, 0);
QDateTime start(d, t, Qt::UTC); QDateTime start(d, t, Qt::UTC);
start = start.toLocalTime().addSecs(start_parking_time * 60); start = start.toLocalTime().addSecs(start_parking_time * 60);
QDateTime end(start);
if (start.isValid()) { if (start.isValid()) {
QString cs = start.toString(Qt::ISODate);
double cost = calculator.GetCostFromDuration( double cost = calculator.GetCostFromDuration(
tariff, PaymentOption::Option1, tariff, PaymentOption::Option1,
cs.toLocal8Bit().constData(), start,
end,
duration, false, true); duration, false, true);
double minCost = tariff->PaymentOption.find(PaymentOption::Option1)->second.pop_min_price; double minCost = tariff->PaymentOption.find(PaymentOption::Option1)->second.pop_min_price;
if (cost < minCost) { if (cost < minCost) {
@ -152,8 +155,10 @@ CalcState CALCULATE_LIBRARY_API compute_price_for_parking_ticket(
CalcState CALCULATE_LIBRARY_API compute_price_for_parking_ticket( CalcState CALCULATE_LIBRARY_API compute_price_for_parking_ticket(
parking_tariff_t *tariff, parking_tariff_t *tariff,
QDateTime const &start_parking_time, QDateTime const &start_parking_time,
QDateTime const &end_parking_time, int netto_parking_time,
struct price_t *price) { QDateTime &end_parking_time,
struct price_t *price)
{
CalcState calcState; CalcState calcState;
double minMin = tariff->PaymentOption.find(PaymentOption::Option1)->second.pop_min_time; double minMin = tariff->PaymentOption.find(PaymentOption::Option1)->second.pop_min_time;
double maxMin = tariff->PaymentOption.find(PaymentOption::Option1)->second.pop_max_time; double maxMin = tariff->PaymentOption.find(PaymentOption::Option1)->second.pop_max_time;
@ -161,39 +166,37 @@ CalcState CALCULATE_LIBRARY_API compute_price_for_parking_ticket(
// DEBUG // DEBUG
qCritical() << "compute_price_for_parking_ticket() " << endl qCritical() << "compute_price_for_parking_ticket() " << endl
<< " start_parking_time: " << start_parking_time << endl << " start_parking_time: " << start_parking_time << endl
<< " end_parking_time: " << end_parking_time << endl << " netto_parking_time: " << netto_parking_time << endl
<< " minMin: " << minMin << endl << " minMin: " << minMin << endl
<< " maxMin: " << maxMin; << " maxMin: " << maxMin;
int const duration = (end_parking_time.toSecsSinceEpoch() - if (netto_parking_time < 0) {
start_parking_time.toSecsSinceEpoch()) / 60;
if (duration < 0) {
calcState.setDesc(QString("end=%1, start=%2") calcState.setDesc(QString("end=%1, start=%2")
.arg(end_parking_time.toString(Qt::ISODate), .arg(end_parking_time.toString(Qt::ISODate),
start_parking_time.toString(Qt::ISODate))); start_parking_time.toString(Qt::ISODate)));
return calcState.set(CalcState::State::NEGATIVE_PARING_TIME); return calcState.set(CalcState::State::NEGATIVE_PARING_TIME);
} }
if (duration > maxMin) { if (netto_parking_time > maxMin) {
calcState.setDesc(QString("duration=%1, maxMin=%2").arg(duration, maxMin)); calcState.setDesc(QString("duration=%1, maxMin=%2").arg(netto_parking_time).arg(maxMin));
return calcState.set(CalcState::State::ABOVE_MAX_PARKING_TIME); return calcState.set(CalcState::State::ABOVE_MAX_PARKING_TIME);
} }
if (duration < minMin) { if (netto_parking_time < minMin) {
calcState.setDesc(QString("duration=%1, minMin=%2").arg(duration, minMin)); calcState.setDesc(QString("duration=%1, minMin=%2").arg(netto_parking_time).arg(minMin));
return calcState.set(CalcState::State::BELOW_MIN_PARKING_TIME); return calcState.set(CalcState::State::BELOW_MIN_PARKING_TIME);
} }
if (duration == 0) { if (netto_parking_time == 0) {
memset(price, 0x00, sizeof(*price)); memset(price, 0x00, sizeof(*price));
return calcState.set(CalcState::State::SUCCESS); return calcState.set(CalcState::State::SUCCESS);
} }
if (start_parking_time.isValid()) { if (start_parking_time.isValid()) {
QString cs = start_parking_time.toString(Qt::ISODate);
double cost = calculator.GetCostFromDuration( double cost = calculator.GetCostFromDuration(
tariff, PaymentOption::Option1, tariff, PaymentOption::Option1,
cs.toLocal8Bit().constData(), // starting time start_parking_time, // starting time
duration, false, true); // duration, minutes, netto end_parking_time, // return value: end time
netto_parking_time, // minutes, netto
false, true);
double minCost = tariff->PaymentOption.find(PaymentOption::Option1)->second.pop_min_price; double minCost = tariff->PaymentOption.find(PaymentOption::Option1)->second.pop_min_price;
if (cost < minCost) { if (cost < minCost) {
calcState.setDesc(QString("minCost=%1, cost=%2").arg(minCost, cost)); calcState.setDesc(QString("minCost=%1, cost=%2").arg(minCost, cost));

View File

@ -318,7 +318,7 @@ std::string Calculator::GetDurationFromCost(Configuration* cfg,
} }
if(price >= min_price && total_duration_min >= minMin) if(price >= min_price && total_duration_min >= minMin)
qDebug() << "Valid until: " << inputDate.toString(Qt::ISODate); qDebug() << "GetDurationFromCost(): Valid until: " << inputDate.toString(Qt::ISODate);
else else
{ {
qDebug() << "Parking not allowed"; qDebug() << "Parking not allowed";
@ -337,10 +337,10 @@ std::string Calculator::GetDurationFromCost(Configuration* cfg,
/////////////////////////////////////// ///////////////////////////////////////
/// <inheritdoc/> /// <inheritdoc/>
double Calculator::GetCostFromDuration(Configuration* cfg, uint8_t payment_option, const char* start_datetime, double durationMin, bool nextDay, bool prepaid) double Calculator::GetCostFromDuration(Configuration* cfg, uint8_t payment_option, const QDateTime start_datetime, QDateTime & end_datetime, double durationMin, bool nextDay, bool prepaid)
{ {
// Get input date // Get input date
QDateTime inputDate = QDateTime::fromString(start_datetime,Qt::ISODate); QDateTime inputDate = start_datetime;
// Get day of week // Get day of week
int weekdayId = 0; int weekdayId = 0;
@ -422,7 +422,7 @@ double Calculator::GetCostFromDuration(Configuration* cfg, uint8_t payment_optio
{ {
LOG_DEBUG("- No workday found, trying to find next available day"); LOG_DEBUG("- No workday found, trying to find next available day");
inputDate = inputDate.addDays(1); inputDate = inputDate.addDays(1);
return floor(GetCostFromDuration(cfg, payment_option, inputDate.toString(Qt::ISODate).toStdString().c_str(), durationMin, true, prepaid)); return floor(GetCostFromDuration(cfg, payment_option, inputDate, end_datetime, durationMin, true, prepaid));
} }
worktime_from = QTime::fromString(cfg->WeekDaysWorktime.find(weekdayId)->second.pwd_time_from.c_str()); worktime_from = QTime::fromString(cfg->WeekDaysWorktime.find(weekdayId)->second.pwd_time_from.c_str());
worktime_to = QTime::fromString(cfg->WeekDaysWorktime.find(weekdayId)->second.pwd_time_to.c_str()); worktime_to = QTime::fromString(cfg->WeekDaysWorktime.find(weekdayId)->second.pwd_time_to.c_str());
@ -435,7 +435,7 @@ double Calculator::GetCostFromDuration(Configuration* cfg, uint8_t payment_optio
{ {
inputDate = inputDate.addDays(1); inputDate = inputDate.addDays(1);
inputDate.setTime(worktime_from); inputDate.setTime(worktime_from);
return GetCostFromDuration(cfg, payment_option, inputDate.toString(Qt::ISODate).toStdString().c_str(), durationMin, true, prepaid); return GetCostFromDuration(cfg, payment_option, inputDate, end_datetime, durationMin, true, prepaid);
} }
// If overtime flag is set // If overtime flag is set
@ -465,7 +465,7 @@ double Calculator::GetCostFromDuration(Configuration* cfg, uint8_t payment_optio
{ {
LOG_DEBUG(" *** PREPAID *** Current time is past the time range end, searching for next available day"); LOG_DEBUG(" *** PREPAID *** Current time is past the time range end, searching for next available day");
inputDate = inputDate.addDays(1); inputDate = inputDate.addDays(1);
return GetCostFromDuration(cfg, payment_option, inputDate.toString(Qt::ISODate).toStdString().c_str(), durationMin, true, prepaid); return GetCostFromDuration(cfg, payment_option, inputDate, end_datetime, durationMin, true, prepaid);
} }
} }
@ -516,7 +516,7 @@ double Calculator::GetCostFromDuration(Configuration* cfg, uint8_t payment_optio
LOG_DEBUG("Reached end of worktime, searching for the next working day"); LOG_DEBUG("Reached end of worktime, searching for the next working day");
inputDate = inputDate.addDays(1); inputDate = inputDate.addDays(1);
overtime = true; overtime = true;
return GetCostFromDuration(cfg, payment_option, inputDate.toString(Qt::ISODate).toStdString().c_str(), total_duration_min); return GetCostFromDuration(cfg, payment_option, inputDate, end_datetime, total_duration_min);
} }
// Increment input date minutes for each monetary unit // Increment input date minutes for each monetary unit
@ -525,7 +525,10 @@ double Calculator::GetCostFromDuration(Configuration* cfg, uint8_t payment_optio
total_cost += price_per_unit; total_cost += price_per_unit;
} }
qDebug() << "Valid until:" << inputDate.toString(Qt::ISODate).toStdString().c_str(); qDebug() << "GetCostFromDuration(): Valid until:" << inputDate.toString(Qt::ISODate).toStdString().c_str();
end_datetime = inputDate;
double ret_val = total_cost; double ret_val = total_cost;
total_cost = 0.0f; total_cost = 0.0f;
return ceil(ret_val); return ceil(ret_val);