Python: Converting float into a 'time'

  • Context: Python 
  • Thread starter Thread starter dacruick
  • Start date Start date
  • Tags Tags
    Float Python Time
Click For Summary

Discussion Overview

The discussion revolves around converting float representations of time into a 'time' format using Python's datetime module. Participants explore methods for handling time in float format, including comparisons to the current time and the conversion process to a more standard time representation.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant seeks to convert float values (e.g., 9.5 and 16.5) into a time format for comparison with the current time.
  • Another participant suggests breaking the float into hours and minutes, using integer casting and basic arithmetic to derive the time components.
  • A participant expresses difficulty with calling constructors in the datetime module and asks for clarification on import statements.
  • There is a question about converting a more precise float (e.g., 9.3423599187) into a full datetime representation, with uncertainty about whether such a conversion is feasible.
  • Some participants express skepticism about the necessity of converting floats to datetime, questioning the benefits of such a conversion.
  • One participant explains their use case involving the calculation of times based on the angle of elevation of the sun, which results in decimal outputs.

Areas of Agreement / Disagreement

Participants generally agree on the method of breaking down floats into hours and minutes, but there is disagreement regarding the necessity and practicality of converting floats to datetime formats. The discussion remains unresolved regarding the conversion of more precise float values into datetime.

Contextual Notes

Participants note limitations in precision when using floats for time representation and the potential ambiguity in the meaning of floats in different contexts.

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 5 ·
Replies
5
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K