user1987032
user1987032

Reputation: 75

Another converting hh:mm:ss to seconds

I have seen a few questions that want the same except in my situation the time format isn't always hh:mm:ss, sometimes it's just mm:ss

I understand the idea mentioned here How to convert an H:MM:SS time string to seconds in Python? and here Python Time conversion h:m:s to seconds

What i don't understand is how to deal with situations like hh:mm:ss, h:mm:ss, mm:ss or just m:ss

def getSec(s):
    l = s.split(':')
    return int(l[0]) * 3600 + int(l[1]) * 60 + int(l[2])

Edit: I think my question isn't clear enough. I'm searching for a solution to handle all possible formats with 1 single function.

Upvotes: 4

Views: 4358

Answers (3)

andrew cooke
andrew cooke

Reputation: 46882

the very latest version (just committed; 0.3.0 on pypi) of simple-date extends Python's time formatting with optional expressions, a bit like regular expressions. so ((H:)?M:)?S will match just seconds, or minutes and seconds, or hours, minutes and seconds.

for example:

>>> from simpledate import *
>>> SimpleDate('56', format='((H:)?M:)?S').time
datetime.time(0, 0, 56, tzinfo=<DstTzInfo 'America/Santiago' SMT-1 day, 19:17:00 STD>)
>>> SimpleDate('12:34:56', format='((H:)?M:)?S').time
datetime.time(12, 34, 56, tzinfo=<DstTzInfo 'America/Santiago' SMT-1 day, 19:17:00 STD>)
>>> SimpleDate('34:56', format='((H:)?M:)?S').hour
0
>>> SimpleDate('34:56', format='((H:)?M:)?S').minute
34
>>> SimpleDate('34:56', format='((H:)?M:)?S').second
56

(the America/Santiago part is just because that's my local timezone - you can specify a timezone too if you want, with tz='UTC' or similar)

unfortunately there's no currently support for total seconds, so you still have to do that yourself. i will think how to add it, though.

it's similar to regular expressions (where ? means that something is optional). you can also have alternatives with (...|...|...).

[updated to reflect 0.3 syntax]

Upvotes: 1

TerryA
TerryA

Reputation: 59984

It's pretty much the same for all time formats. Check out the datetime module:

>>> s = "15:34:23"
>>> datetime.datetime.strptime(s, "%H:%M:%S")
datetime.datetime(1900, 1, 1, 15, 34, 23)

>>> s = "34:23"  
>>> datetime.datetime.strptime(s, "%M:%S")
datetime.datetime(1900, 1, 1, 0, 34, 23)

>>> s = "4:34"
>>> datetime.datetime.strptime(s, "%M:%S")
datetime.datetime(1900, 1, 1, 0, 4, 34)

%H is for hours, %M is for minutes, %S is for seconds. A list can be found here


As a function:

>>> def getSec(s):
...     L = s.split(':')
...     if len(L) == 1:
...             return L[0]
...     elif len(L) == 2:
...             datee = datetime.datetime.strptime(s, "%M:%S")
...             return datee.minute * 60 + datee.second
...     elif len(L) == 3:
...             datee = datetime.datetime.strptime(s, "%H:%M:%S")
...             return datee.hour * 3600 + datee.minute * 60 + datee.second
...
>>> getSec("13:25:43")
48343
>>> getSec("25:43")
1543
>>> getSec("43")
43

Upvotes: 3

falsetru
falsetru

Reputation: 369164

def getSec(s):
    l = map(int, s.split(':')) # l = list(map(int, s.split(':'))) in Python 3.x
    return sum(n * sec for n, sec in zip(l[::-1], (1, 60, 3600)))

getSec('20') # 20
getSec('1:20') # 80
getSec('1:30:01') # 5401

Upvotes: 10

Related Questions