8

I am aware that with the timedelta function you can convert seconds to h:m:s using something like:

>> import datetime
>> str(datetime.timedelta(seconds=666)) 
'0:11:06'

But I need to convert h:m:s to seconds, or minutes.

Do you know a function that can do this?

thejartender
  • 9,339
  • 6
  • 34
  • 51
Geparada
  • 2,898
  • 9
  • 31
  • 43
  • What is your source input? A timestamp string? Some sort of object? – Silas Ray May 24 '12 at 17:28
  • Do you mean seconds or minutes since the epoch? Or do you just want to extract the seconds and minute parts from the timestamp? – Michael May 24 '12 at 17:30
  • possible duplicate: http://stackoverflow.com/questions/10049805/add-values-of-0212-0345-from-a-list-together-to-make-a-format-time-hours – jadkik94 May 24 '12 at 17:49

8 Answers8

14
>>> import time, datetime
>>> a = time.strptime("00:11:06", "%H:%M:%S")
>>> datetime.timedelta(hours=a.tm_hour, minutes=a.tm_min, seconds=a.tm_sec).seconds
666

And here's a cheeky one liner if you're really intent on splitting over ":"

>>> s = "00:11:06"
>>> sum(int(i) * 60**index for index, i in enumerate(s.split(":")[::-1]))
666
Nolen Royalty
  • 18,415
  • 4
  • 40
  • 50
13
def hms_to_seconds(t):
    h, m, s = [int(i) for i in t.split(':')]
    return 3600*h + 60*m + s
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
  • 1
    Nice and simple, though I think you want to return `datetime.timedelta(seconds=3600*h + 60*m + s)`. – Ben Hoyt May 24 '12 at 17:55
2

Unfortunately, it's not as trivial as constructing a datetime object from a string using datetime.strptime. This question has been asked previously on Stack Overflow here: How to construct a timedelta object from a simple string , where the solution involved using python-dateutil.

Alternatively, if you don't want to have to add another module, here is a class you can use to parse a timedelta from a string: http://kbyanc.blogspot.ca/2007/08/python-reconstructing-timedeltas-from.html

Community
  • 1
  • 1
Brendan Wood
  • 6,220
  • 3
  • 30
  • 28
1
>>> def tt(a):
...     b = a.split(':')
...     return int(b[0]) * 3600 + int(b[1]) * 60 + int(b[2])
... 
>>> print tt('0:11:06')

666

Sveatoslav
  • 842
  • 12
  • 21
0

This works in 2.6.4:

hours, minutes, seconds = [int(_) for _ in thestring.split(':')]

If you want to turn it back into a timedelta:

thetimedelta = datetime.timedelta(hours=hours, minutes=minutes, seconds=seconds)
Andrew Buss
  • 1,532
  • 2
  • 14
  • 23
0

I'm not even sure I'd bother with timedelta for this

>>> pseconds = lambda hms:sum(map(lambda a,b: int(a)*b,hms.split(':'),(3600,60,1)))
>>> pseconds('0:11:06')
666
Phil Cooper
  • 5,747
  • 1
  • 25
  • 41
  • 1
    I'd use list comprehension here instead of `map`. I think it would be much clearer... – jadkik94 May 24 '12 at 17:38
  • right, almost always prefer list comprehension to map...here, it wouldn't have been simpler because it would have required a zip whereas map can handle multiple sequences. 6 of one, half-dozen...look's kind of ugly both ways – Phil Cooper May 24 '12 at 18:21
0

You don't need to import anything!

def time(s):
  m = s // 60
  h = (m // 60) % 60
  m %= 60
  s %= 60
  return h,m,s
0

If you want to be able to handle a string like '23:32' or '07', then you can use the following function:

def get_seconds(time_string):
    time_list = time_string.split(':')
    seconds = 0 
    for i in range(len(time_list),0,-1):
        t = int(time_list[i-1])*60**(time_len-(i))
        seconds += t
    return seconds
DonCarleone
  • 544
  • 11
  • 20