0

I'm building car parking system and I'm having some issues with comparing time. Let's say "John" should pay 5$ for one hour to park his car. John leaves his car for 3:30 hours in parking lot and makes a payment when the time for the parking stops at 3:33. So what I would like to get here is something like this, but the thing is that its non logical to make an if at each hour. What's the way to somehow just count the sum of the payment to calculate. Also what I didn't mention is that 3:33 is more then half hour so we can count another hour. But what I want to do here is that:

  • I don't need to create much if's for parking to calculate the sum, that JOHN has to pay.

  • How to automaticly add an 5$ to the hours, that John stayed in the parking?

My code:

if Parking_Sum > datetime.timedelta(minutes=30):
            print ("You were parking for more then 30MINS You have to pay 5$")
            Price = 5
       elif Parking_Sum > datetime.timedelta(hours=1):
            print ("You were parking for more then hour You have to pay 10$")
            Price = 10
       elif Parking_Sum > datetime.timedelta(days=1):
            print ("You were parking for more then DAY You have to pay 120$")
            Price = 120
DzITC
  • 869
  • 1
  • 9
  • 23
  • 1
    You can [convert the `timedelta` into a numeric value of the minutes passed](https://stackoverflow.com/questions/2119472/convert-a-timedelta-to-days-hours-and-minutes). From there the rest is a bit of arithmetic. Does that solve your problem? If not what exactly is the issue? – walnut Sep 05 '19 at 15:20
  • Hmm, after converting let's say to minutes I can make if statement? I don't understand how could I automatically add 5$ to an each hour. – DzITC Sep 05 '19 at 15:28
  • You don't need any `if`. Just take the number of hours and multiply it by the price per hour. You can get the number of hours as explained in the link I posted. You only need to round it correctly. – walnut Sep 05 '19 at 15:32
  • In the above code you will never reach the ``elif`` conditions. For example, if ``Parking_Sum`` is bigger than 1 h it is also bigger 30 min, so the first ``if`` will already trigger. – bjhend Sep 05 '19 at 15:59

2 Answers2

1

Find the difference between entered time and exit time. Calculate total minutes, vehicle was in parking and then calculate the charges. Here is example -

time_entered = datetime(year=2019, month=9, day=2, hour=10, minute=9)
time_left = datetime(year=2019, month=9, day=2, hour=12, minute=40)
total_minutes = ((time_left - time_entered).total_seconds())//60
total_hours = (total_minutes//60 + 1) if total_minutes%60 > 30 else (total_minutes//60)
total_charges = total_hours*5
Ajay Srivastava
  • 1,151
  • 11
  • 15
  • Hmm, this one is interesting :) – DzITC Sep 05 '19 at 16:13
  • @DzITC: Note that in this code, a parking time of 1:30h will still only come to 5$ due to the floor division in line 3. Is that intended? (btw, as a car driver I'd consider this more fair than the result of my code xD) – FObersteiner Sep 05 '19 at 18:39
  • @MrFuppes how could I fix this? – DzITC Sep 05 '19 at 19:08
  • @DzITC, sorry, I meant line 4 (the one with the ternary statement). You could just replace the 30 with 0: `(total_minutes//60 + 1) if total_minutes%60 > 0 else (total_minutes//60)` or if you're fine with using the `math` package (comes with Python by default), use `math.ceil` as I showed in my answer. Still, I'd prefer the 30 minutes of tolerance if parking my car ;-) – FObersteiner Sep 05 '19 at 20:06
1

you could use

import datetime
import math

def parkingcosts(price, t):
    return math.ceil(t.total_seconds()/3600)*price

price_per_h = 5

parkingtime = datetime.timedelta(minutes=30)
print(parkingcosts(price_per_h, parkingtime))
# 5
parkingtime = datetime.timedelta(hours=1.5)
print(parkingcosts(price_per_h, parkingtime))
# 10
parkingtime = datetime.timedelta(days=1)
print(parkingcosts(price_per_h, parkingtime))
# 120

I think the easiest way to get the hours from a timedelta object is to use .total_seconds(). Otherwise, there's .days, .seconds and .microseconds that you would have to sum up (no hours, minutes though... check dir(datetime.timedelta())).

FObersteiner
  • 22,500
  • 8
  • 42
  • 72