Python Python: Converting float into a 'time'

Click For Summary
The discussion revolves around converting float representations of time, specifically 9.5 for 9:30 and 16.5 for 4:30, into a more standard time format using Python's datetime module. The initial approach involved comparing these floats to the current time, but the user sought a more accurate method. Suggestions included breaking down the float into hours and minutes by using integer casting and simple arithmetic. For example, 16.5 can be converted to 16 hours and 30 minutes. The conversation also touched on importing datetime correctly to use the time constructor. A further question arose about converting a more precise float, like 9.3423599187, into a time format, but it was suggested that this might not be straightforward due to the ambiguity of float representations. Ultimately, the user found a way to convert the float into datetime.time, concluding that while there may be challenges, it was possible to achieve the desired format for their specific application involving sun elevation angles.
dacruick
Messages
1,038
Reaction score
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:
 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 don't 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
 
Technology news on Phys.org
dacruick said:
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
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:
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 *
 
dacruick said:
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.
 
yeah I am a little new to python and the way things work but that makes sense. thanks for your help
 
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?
 
dacruick said:
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.
 
yeah I am starting to think this isn't even worth it. I mean, I don't really see very many benefits to converting to datetimes.
 
dacruick said:
yeah I am 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?
 
  • #10
well what I've 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
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
9K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 0 ·
Replies
0
Views
2K
Replies
4
Views
2K