Python Forum
Simple time comparison
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple time comparison
#1
Hi,

I have been struggling with this for 2 days and can't seem to find the recipe.
I simply want to compare the current time with two time boundaries, simple enough right? I don't want to deal with dates, times only.

I would like to run something like this:

t1 = '09:00:00'
t2 = '17:00:00'

timeStart = datetime.strptime(t1, '%H:%M:%S')
timeEnd = datetime.strptime(t2, '%H:%M:%S')

while datetime.now > timeStart and datetime.now < timeEnd:
#do stuff

By the way, I am running Python 3.8 with Visual Studio 2017.
Also I took the following example on this site and remove superfluous stuff but the while loop never stops:
while True:
now_time = datetime.time()
timeon_time = datetime.time(23, 00, 00, 000000)
if now_time<timeon_time: print("ok")
else: print("stop")

Thanks in advance!
Reply
#2
Please use proper code tags while writing a post.
When you say compare, what exactly do you mean? Like, do you want to see how far away is that random time from the current time, or something else?
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#3
Hi,
Thanks for letting me know about the code tags, it really is neater.
To answer your question pyzyx3qwerty, when I say compare, I simply mean that I want to run some code while the current time is between t1 and t2. I want the code to stop when it's going to be 17:00:00, nothing psychedelic. I am converting my VB .Net code to Python. This is bit of VB .Net code I want to convert:

While TimeValue(Now) > TimeValue("09:00:00") And TimeValue(Now) < TimeValue("17:00:00")
'do stuff
End While

Thanks again

import datetime

if __name__ == "__main__" :
    t1 = '09:00:00'
    t2 = '17:00:00'

    timeStart = datetime.strptime(t1, '%H:%M:%S')
    timeEnd = datetime.strptime(t2, '%H:%M:%S')

    while datetime.now > timeStart and datetime.now < timeEnd:
        print("ok")
    print("stop")
Output:
None
Error:
Exeption User-Unhandled AttributeError: module 'datetime' has no attribute 'strptime'
Reply
#4
strptime is in the "time" module. It is also in datetime.datetime.
Reply
#5
import datetime


if __name__ == "__main__" :
    t1 = '09:00:00'
    t2 = '17:00:00'
 
    time_start = datetime.datetime.strptime(t1, '%H:%M:%S')
    time_end = datetime.datetime.strptime(t2, '%H:%M:%S')
 
    while time_start < datetime.datetime.now() < time_end:
        print("ok")
    print("stop")
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
Hi deanhystad and DeaD_EyE,

Thanks for your answers and sorry for answering inside your reputation ratings...newbie here...
I have tried Dead_EyE's suggestion but the only output I get is 'stop'. My loop does not work.

I have divided my code into pieces and found out that the conditions don't work because datetime.datetime.now()
holds milliseconds whereas the other values don't.

I then tried to declare cast the output of datetime.datetime.now()) into a string and assign it to t0 as follows and
changed the formatting of all variables to include milliseconds but no luck.

Any suggestion is more than welcome

    t0 = str(datetime.datetime.now())
    t1 = "18:28:00.000000"
    t2 = "18:30:00.000000"

    timeNow = datetime.datetime.strptime(t0, "%H:%M:%S.%f")
    timeStart = datetime.datetime.strptime(t1, "%H:%M:%S.%f")
    timeEnd = datetime.datetime.strptime(t2, "%H:%M:%S.%f")
Output:
Traceback (most recent call last): File "C:\Users\User\Desktop\stockwatch\stockwatch_py\stockwatch_py\stockwatch_py\module1.py", line 122, in <module> timeNow = datetime.datetime.strptime(t0, "%H:%M:%S.%f") File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\_strptime.py", line 577, in _strptime_datetime tt, fraction, gmtoff_fraction = _strptime(data_string, format) File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\_strptime.py", line 359, in _strptime (data_string, format)) ValueError: time data '2020-04-14 18:35:48.158411' does not match format '%H:%M:%S.%f'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple animation -- time evolution of function schniefen 0 1,037 Nov-20-2022, 08:05 PM
Last Post: schniefen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020