Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python: Converting float into a 'time'

  1. Feb 19, 2010 #1
    Hello. Right now I've got two floats that represent times. say 9.5 for 9:30 and 16.5 for 4:30.

    Basically I want to compare these to the current time, which right now I am doing by saying

    Code (Text):
     dt=datetime.now()
    while 9.5 < float(dt.strftime('%H')) + float(dt.strftime('%M')/60) < 16.5:
        run all this stuff down here
     
    So I dont want to use floats to compare those times, i want to put them into 'time' format.
    which is hours:minutes:seconds.milliseconds. How would I convert those two floats 9.5 and 16.5?

    dacruick
     
  2. jcsd
  3. Feb 19, 2010 #2
    Just split up the float and do the math. Your float isn't precise enough for seconds or milliseconds.

    You can get the hours by doing integer casting int(16.5) = 16
    subtract your hours from your decimal to get your minutes: 16.5-16 = .5
    that minutes bit is actually a fraction of an hour, so: .5 (hours) * 60 (minutes/hour) = 30 minutes

    Once you break it into parts, you just call the constructor:
    j = datetime.time(hour = 16, minute=30)
    this should return :
    datetime(16,30)

    It really is all in the docs
     
    Last edited: Feb 19, 2010
  4. Feb 19, 2010 #3
    ill give that a shot again. I was having a lot of issues with calling constructors. It was telling me that the datetime.time didnt exist and so forth. would you suggest calling it like from datetime import time
    or from datetime import *
     
  5. Feb 19, 2010 #4
    The way I did it was:
    import datetime
    j = datetime.time(hour=,minute=)

    if you use:
    from datetime import time
    from datetime import *

    the constructor is:
    j = time(hour=, minute=)

    because you've imported all the modules, so you refer to them on that level.
     
  6. Feb 19, 2010 #5
    yeah im a little new to python and the way things work but that makes sense. thanks for your help
     
  7. Feb 19, 2010 #6
    actually, I have another question to tack on to here. What if i have 9.3423599187 and I want to convert that to the appropriate time as in hours:minutes:seconds.ms. what would i then do? is there a way i can just put in a float and have it give the equivalent datetime or whatever?
     
  8. Feb 19, 2010 #7
    Dunno, never really worked with datetime. Look through the documentation I've linked. My instinct is that you can't ('cause the floats probably mean different things to different people), and that you'll just have to work backward from how you constructed the float in the first place.
     
  9. Feb 19, 2010 #8
    yeah im starting to think this isn't even worth it. I mean, I don't really see very many benefits to converting to datetimes.
     
  10. Feb 19, 2010 #9
    What's the problem in the first place? Why are you storing time in that sort of decimal format?
     
  11. Feb 22, 2010 #10
    well what ive done is created a program where you input an angle of elevation of the sun, and it outputs the two times where that angle of the sun occurs. so the output of that is unavoidably a decimal. But i was able to convert it into a datetime.time so its all good. thanks for your help
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Python: Converting float into a 'time'
  1. Time library in python (Replies: 7)

  2. Python installation (Replies: 10)

Loading...