# Python Python: Converting float into a 'time'

#### dacruick

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:
 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

Related Programming and Computer Science News on Phys.org

#### story645

How would I convert those two floats 9.5 and 16.5?
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
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:

#### dacruick

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 *

#### story645

would you suggest calling it like from datetime import time
or from datetime import *
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.

#### dacruick

yeah im a little new to python and the way things work but that makes sense. thanks for your help

#### dacruick

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?

#### story645

is there a way i can just put in a float and have it give the equivalent datetime or whatever?
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.

#### dacruick

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.

#### story645

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.
What's the problem in the first place? Why are you storing time in that sort of decimal format?

#### dacruick

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

### Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving