Parsing hh: mm in Python

Sometimes I get a line like "02:40" indicating 2 hours 40 minutes. I would like to parse this string in the number of minutes (160 in this case) using Python.

Of course, I can parse the string and multiply the clock by 60, but is there anything in the standard library that does this?

+4
source share
6 answers

Personally, I find that just parsing a string is much easier to read:

>>> s = '02:40' >>> int(s[:-3]) * 60 + int(s[-2:]) 160 

Note that using negative indexing means that it will process rows without a leading zero per hour:

 >>> s = '2:40' >>> int(s[:-3]) * 60 + int(s[-2:]) 160 

You can also use the split() function:

 >>> hours, minutes = s.split(':') >>> int(hours) * 60 + int(minutes) 160 

Or use the map() function to convert shapes to integers:

 >>> hours, minutes = map(int, s.split(':')) >>> hours * 60 + minutes 160 

Speed

Using the timeit module indicates that it is also faster than the other methods suggested here:

 >>> import timeit >>> parsetime = timeit.timeit("mins = int(s[:-3]) * 60 + int(s[-2:])", "s='02:40'", number=100000) / 100000 >>> parsetime 9.018449783325196e-06 

The split() method is a bit slower:

 >>> splittime = timeit.timeit("hours,minutes = s.split(':'); mins=int(hours)*60 + int(minutes)", "s='02:40'", number=100000)/100000 >>> splittime 1.1217889785766602e-05 >>> splittime/parsetime 1.2438822697120402 

And again using map() little slower:

 >>> splitmaptime = timeit.timeit("hours,minutes = map(int, s.split(':')); mins=hours*60 + minutes", "s='02:40'", number=100000)/100000 >>> splitmaptime 1.3971350193023682e-05 >>> splitmaptime/parsetime 1.5491964282881776 

John Machin's card and amount are about 2.4 times slower:

 >>> summaptime = timeit.timeit('mins=sum(map(lambda x, y: x * y, map(int, "2:40".split(":")), [60, 1]))', "s='02:40'", number=100000) / 100000 >>> summaptime 2.1276121139526366e-05 >>> summaptime/parsetime 2.43 

The answer to Chrono Kitsune strptime() is ten times slower:

 >>> strp = timeit.timeit("t=time.strptime(s, '%H:%M');mins=t.tm_hour * 60 + t.tm_min", "import time; s='02:40'", number=100000)/100000 >>> strp 9.0362770557403569e-05 >>> strp/parsetime 10.019767557444432 
+6
source

Also, parsing strings (or if you want to be even slower for something so simple, use the re module), this is the only way I can think of if you rely on a standard library. TimeDelta does not seem to fit this task.

 >>> import time >>> x = "02:40" >>> t = time.strptime(x, "%H:%M") >>> minutes = t.tm_hour * 60 + t.tm_min >>> minutes 160 
+8
source

See http://webcache.googleusercontent.com/search?q=cache:EAuL4vECPBEJ:docs.python.org/library/datetime.html+python+datetime&hl=en&client=firefox-a&gl=us&strip=1 , as in the main Python site is having problems.

The function you want is datetime.strptime or time.strptime , which creates a datetime or time object from a string specifying the time and another string describing the format.

If you do not want to describe the format, use dateutil , http://labix.org/python-dateutil .

 from dateutil.parser import parse >>> d = parse('2009/05/13 19:19:30 -0400') >>> d datetime.datetime(2009, 5, 13, 19, 19, 30, tzinfo=tzoffset(None, -14400)) 

See How to parse dates with -0400 timezone line in python?

+4
source
 >>> sum(map(lambda x, y: x * y, map(int, "2:40".split(":")), [60, 1])) 160 
+1
source

I am sure that you can represent this time as a TimeDelta object. From there, I'm sure there is an easy way to present TimeDelta in minutes.

0
source

Exist:

 from time import strptime from calendar import timegm T = '02:40' t = timegm(strptime('19700101'+T,'%Y%m%d%H:%M')) print t 

But is it really better than rough calculus?

.

An exotic solution that does not require imported functions:

 T = '02:40' exec('x = %s' % T.replace(':','*60+')) print x 

edit: fixed second solution to get minutes, not seconds

.

Simplest solution

 T = '02:40' print int(T[0:2])*60 + int(T[3:]) 
-3
source

All Articles