473,395 Members | 1,443 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

time conversion

Is there an easy to convert following hour notation hh:mm
to decimals?
For instance 2 hours and 30 minutes as 2:30 to 2,50
I don't really know where to search for this kind of conversion.

Thanks,
Benedict
Oct 27 '05 #1
4 5795
flupke wrote:
Is there an easy to convert following hour notation hh:mm
to decimals?
For instance 2 hours and 30 minutes as 2:30 to 2,50
I don't really know where to search for this kind of conversion.

Thanks,
Benedict


I found a way. Not sure if it's the best:

time = "2:3"
factor = 100.0/60.0

time_parts = time.split(":")
minutes = int(float(time_parts[1]) * factor)
if ( len(str(minutes)) == 1 ): minutes *= 10 # to get 50 instead of 5
time_new = "%s,%.02s" % (time_parts[0],minutes)
print "%s -> %s " % (time,time_new)
Oct 27 '05 #2
"flupke" wrote:
Is there an easy to convert following hour notation hh:mm
to decimals?
For instance 2 hours and 30 minutes as 2:30 to 2,50
I don't really know where to search for this kind of conversion.


you mean like
timestamp = "2:30"
hour, minute = timestamp.split(":")
print int(hour) + int(minute) / 60.0

2.5

?

</F>

Oct 27 '05 #3
Fredrik Lundh wrote:
"flupke" wrote:

Is there an easy to convert following hour notation hh:mm
to decimals?
For instance 2 hours and 30 minutes as 2:30 to 2,50
I don't really know where to search for this kind of conversion.

you mean like
>>> timestamp = "2:30"
>>> hour, minute = timestamp.split(":")
>>> print int(hour) + int(minute) / 60.0

2.5

?

</F>

yep, your sollution is shorter so i'll use that :)

Thanks
Benedict
Oct 27 '05 #4
On Thu, 27 Oct 2005 14:36:30 +0000, flupke wrote:
Is there an easy to convert following hour notation hh:mm
to decimals?
For instance 2 hours and 30 minutes as 2:30 to 2,50
I don't really know where to search for this kind of conversion.

def str_HMS(s):
"""Convert a string "H:M:S" to a tuple of numbers (H, M, S).
H and M must be integers, S may be a float.
"""
L = s.split(":")
if len(L) < 3:
raise ValueError("Too few fields in H:M:S string.")
elif len(L) > 3:
raise ValueError("Too many fields in H:M:S string.")
H = int(L[0])
M = int(L[1])
S = float(L[2])
return (H, M, S)

def HMS_Seconds(hours, minutes, seconds):
"""Convert hours minutes seconds to seconds."""
return hours*60*60 + minutes*60 + seconds

def HMS_Hours(hours, minutes, seconds):
"""Convert hours minutes seconds to hours."""
return hours + minutes/60.0 + seconds/(60.0**2)

def Hours_HMS(h):
"""Convert time t in hours to hours minutes seconds."""
hours = int(t)
t = (t - hours)*60
minutes = int(t)
seconds = (t - minutes)*60
return (hours, minutes, seconds)

def Seconds_HMS(h):
"""Convert time t in seconds to hours minutes seconds."""
hours, t = divmod(t, 60*60)
minutes, seconds = divmod(t, 60)
return (hours, minutes, seconds)

--
Steven.

Oct 27 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dan glenn | last post by:
Say, I want to set a cookie and have it expire an hour after it's set. It's looking like this is only possible for browsers which are in the same time zone as my server?? In other words, if I...
2
by: learning_C++ | last post by:
I programmed this code with a function "get_current_time" in the begining. When I compiled with the command g++ -Wall -g xxx.xpp -o xxx there are so many errors. please help me and thanks, ...
1
by: heirou | last post by:
I'm a novice in this subject....I've made a database that requires a time conversion. For example, if local time is 1200, determine the time in Korea. I use two fields: a date field, and a time...
6
by: DCSudolcan | last post by:
I know that a program can create and properly initialize an array of pointers to functions at build time, but can something like the following be done at build time? void foo(void); unsigned...
5
by: Paulers | last post by:
Hello, I'm working on an app that requires the functionality to convert the time in Austrailia to the time in New York (EST). I am wondering, what is the bestway to approach this in vb.net? Is...
3
by: Jason S | last post by:
is there any way to use templates to bind integer/floating point constants to a template for compile-time use? e.g. template <double conversion> class meters { const factor = conversion;
3
by: moni | last post by:
Hi, I wanted to convert a time value in the form of time_t into a readable form in C# or vice versa, in order to be able to subtract two time values and give the result in msecs. eg. I...
3
by: Evan Klitzke | last post by:
Although it is not present in ANSI C, the GNU version of stftime supports the conversion character %z, which is a time offset from GMT. The four digit time offset is required in RFC 2822...
5
by: fimarn | last post by:
I am trying to get rid of compile time error that I am getting only in RHEL5 (not in RHEL4) apparently due to the changes in the stl_list.h file. The error that I am getting is coming from the...
5
by: Grey Alien | last post by:
I need to convert timestamps that are given as the number of seconds that have elapsed since midnight UTC of January 1, 1970, (not counting leap seconds). It seems all of the std C functions...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.