Python: Converting float into a 'time'

In summary, the person is trying to figure out how to convert two floats to a time. They explain that they used floats to represent time and that they don't want to use floats to compare the times anymore. They provide a summary of how to convert floats to a time and end the output with "thanks for your help."
  • #1
dacruick
1,042
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
  • #2
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:
  • #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 *
 
  • #4
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.
 
  • #5
yeah I am a little new to python and the way things work but that makes sense. thanks for your help
 
  • #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?
 
  • #7
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.
 
  • #8
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.
 
  • #9
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
 

1. How do I convert a float into a 'time' in Python?

To convert a float into a 'time' in Python, you can use the timedelta function from the datetime module. First, convert the float into an integer by using the int() function. Then, use the timedelta function to create a time object with the desired value.

2. Can I convert a float representing seconds into a time object in Python?

Yes, you can convert a float representing seconds into a time object in Python by using the timedelta function from the datetime module. First, convert the float into an integer by using the int() function. Then, use the timedelta function to create a time object with the desired value in seconds.

3. Is there a specific format for converting a float into a time object in Python?

No, there is no specific format for converting a float into a time object in Python. However, it is recommended to use the timedelta function from the datetime module to ensure the accuracy and consistency of the time object.

4. How do I handle fractions of a second when converting a float into a time object in Python?

If you need to handle fractions of a second when converting a float into a time object in Python, you can use the timedelta function from the datetime module and specify the desired value in microseconds. Alternatively, you can use the round() function to round the float to the nearest whole number before converting it into a time object.

5. Can I convert a float representing minutes or hours into a time object in Python?

Yes, you can convert a float representing minutes or hours into a time object in Python by using the timedelta function from the datetime module. First, convert the float into an integer by using the int() function. Then, use the timedelta function to create a time object with the desired value in minutes or hours.

Similar threads

  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
8
Views
824
  • Programming and Computer Science
Replies
0
Views
412
  • Programming and Computer Science
Replies
1
Views
166
Replies
4
Views
818
  • Computing and Technology
Replies
4
Views
663
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
11
Views
958
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
Back
Top