Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python time
#1
Imi trying to compare two times, but the result comes back backwards.  Here is my code

import time, datetime
while True:
   now_time = datetime.time()#time.time()
   now_time_string = time.strftime("%H:%M:%S")
   print("now_time")
   print now_time_string

   timeon_time = datetime.time(18, 00, 00, 000000)
   print("timeon_time")
   print timeon_time
   if now_time<timeon_time: print("now is smaller-earlier")
   else: print("now is later, fire!")
it is currently 19:26pm, and if I run the script I get these results.  
now_time=19:26:50 & timeon_time=18:00:00, so now_time > timeon_time.  
This means "now is later, fire!" should print but I get, why?:

Results:
now_time
19:26:50
timeon_time
18:00:00
now is smaller-earlier
now_time
19:26:50
timeon_time
18:00:00
now is smaller-earlier
Reply
#2
In the future, Please use code tags

The following has a bunch of time comparison examples and will be helful

Python 2:
https://pymotw.com/2/time/

Python 3:
https://pymotw.com/3/time/

and (my favorite):
http://pleac.sourceforge.net/pleac_pytho...times.html
Reply
#3
what is that #time.time() expected to do?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
It's a comment, it's not expected to do anything.
Reply
#5
(Oct-17-2016, 05:26 AM)Yoriz Wrote: It's a comment, it's not expected to do anything.

i'm asking what was he thinking.  i know what the code does.

he's doing: 
now_time = datetime.time()
which is equivalent to doing:  
now_time = datetime.time(0, 0)
which is definitely going to be a lower/earlier value.  maybe he should have been doing:
now_time = datetime.datetime.now()
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
I tried this:
#!/usr/bin/env python
import time, datetime
print datetime.datetime.now()
ref_time = datetime.time("18 00", "%H:%M")
if ref_time>datetime.datetime.now(): print("ref_time is greater")
else: print("now is greater")
but I cant get it to work.  Please help
Reply
#7
what are you expecting line 4 to do?  i get TypeError: an integer is required

Output:
lt1/forums /home/forums 7> cat marciokoko.py #!/usr/bin/env python import time, datetime print datetime.datetime.now() ref_time = datetime.time("18 00", "%H:%M") if ref_time>datetime.datetime.now(): print("ref_time is greater") else: print("now is greater") lt1/forums /home/forums 8> python2 marciokoko.py 2016-11-16 22:26:44.928197 Traceback (most recent call last):   File "marciokoko.py", line 4, in <module>     ref_time = datetime.time("18 00", "%H:%M") TypeError: an integer is required lt1/forums /home/forums 9>
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
Even if fix "integer is required" it will not work.
>>> datetime.datetime.now() < datetime.time(18, 00)
Traceback (most recent call last):
 File "<interactive input>", line 1, in <module>
TypeError: can't compare datetime.datetime to datetime.time
It is better to keep all in datetime.datetime object.
Can use combine like this to make datetime.time a datetime object.
import time, datetime

print(datetime.datetime.now())
ref_time = datetime.datetime.combine(datetime.datetime.now(), datetime.time(18, 00))
if ref_time > datetime.datetime.now():
    print("ref_time is greater")
else:
    print("now is greater")
Look at an use Pendulum.
Reply
#9
Thanks! One last doubt. Is there a neat way to take the string received as reference_time, "18:00", and use it directly in datetime.time(18, 00), or do I just parse it as a string by cutting off the last 3 places?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to change UTC time to local time in Python DataFrame? SamKnight 2 1,683 Jul-28-2022, 08:23 AM
Last Post: Pedroski55
  Getting error in finding time.time() value in python Lakshana 1 2,528 Jan-11-2018, 07:07 AM
Last Post: wavic

Forum Jump:

User Panel Messages

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