Timo Cengiz
Timo Cengiz

Reputation: 3417

Knowing if time has already passed or not python

Im using this code right now to get the difference of two different times

from time import strftime
from datetime import datetime

fmt = '%H:%M'

currentTimeString = strftime(fmt) #get the current time

gameTimeObject = datetime.strptime(time , fmt)  
currentTimeObject = datetime.strptime(currentTimeString, fmt)

diff = currentTimeObject-gameTimeObject

diff_minutes = diff.seconds/60

I have a for loop that loops through different time values, 11:00, 12:00, 13:00, 14:00.

For the purpose of this example lets say the currentTime is 13:23

Im getting the result that i want when the time has passed meaning, 11:00, 12:00, 13:00 but when it comes to 14:00 the diff_minutes shows the value 1403

Why this is a problem is because I'm doing this after that code is executed

if diff_minutes >= 0 and diff_minutes <= 47:
    time  = str(diff_minutes)
elif diff_minutes >= 48 and diff_minutes <= 58:
    time = "HT"
elif diff_minutes >= 59 and diff_minutes <= 103:
    time = str(diff_minutes)
elif diff_minutes >= 104 and diff_minutes <= 107:
    time = "90'"
elif diff_minutes >= 108:  # HERE IS THE PROBLEM <<<<<<-------
    time = "FT"

As you can see the times that has not yet been passed will change to "FT" since I'm using that if statement to know if the time of a games is over. How can i fix this?

--- EDIT more information of what I'm trying to do as asked

So what I'm trying to do here is to set the time variable to a soccer game. The different times as mentioned above 13:00 etc are the times of which when the game starts. So what i want to do is if the currentTime is 13:23, then i would change the time label of that soccer game to 23' since 23minutes has passed. If the total time that has passed is over 108 minutes then it means the game is over that is why i set the time variable to "FT" in the last if statement.

As i said before this is where the problem occurs since the diff_minutes gives me a value of higher than 108 minutes when the games has not yet been started it will change the time variable to "FT" which is wrong

Upvotes: 1

Views: 1643

Answers (1)

Tritium21
Tritium21

Reputation: 2932

So what we really should do is work only with datetime objects here. When you do math on them, the produce timedelta objects. These timedelta objects are comparable. This makes writing your code secretarial. Also - since an if/elif/else tree is short circuiting, you need not check if a time is between two times, just less than the upper bound. I admit my domain knowledge of soccer is limited to knowing it is a 90 minute game, so you may need to adjust this, but it should give you the idea.

import datetime

def gametime(hour, minute):
    now = datetime.datetime.now()
    starttime = datetime.datetime.combine(datetime.date.today(), datetime.time(hour, minute))
    dif = now - starttime
    if dif < datetime.timedelta(seconds=48*60):
        return dif.seconds / 60
    elif dif < datetime.timedelta(seconds=58*60):
        return "Half Time"
    elif dif < datetime.timedelta(seconds=104*60):
        return dif.seconds / 60
    elif dif < datetime.timedelta(seconds=108*60):
        return "90'"
    else:
        return "Final"

PS: Seriously, Guido?

PPS: I should have tested this before posting it. I am sorry that I did not.

Upvotes: 1

Related Questions