How to convert an H:MM:SS time string to seconds in Python?
Question:
Basically I have the inverse of this problem: Python Time Seconds to h:m:s
I have a string in the format H:MM:SS (always 2 digits for minutes and seconds), and I need the integer number of seconds that it represents. How can I do this in python?
For example:
- “1:23:45” would produce an output of 5025
- “0:04:15” would produce an output of 255
- “0:00:25” would produce an output of 25
etc
Answers:
parts = time_string.split(":")
seconds = int(parts[0])*(60*60) + int(parts[1])*60 + int(parts[2])
def get_sec(time_str):
"""Get seconds from time."""
h, m, s = time_str.split(':')
return int(h) * 3600 + int(m) * 60 + int(s)
print(get_sec('1:23:45'))
print(get_sec('0:04:15'))
print(get_sec('0:00:25'))
ts = '1:23:45'
secs = sum(int(x) * 60 ** i for i, x in enumerate(reversed(ts.split(':'))))
print(secs)
Without many checks, and assuming it’s either "SS" or "MM:SS" or "HH:MM:SS" (although not necessarily two digits per part):
def to_seconds(timestr):
seconds= 0
for part in timestr.split(':'):
seconds= seconds*60 + int(part, 10)
return seconds
>>> to_seconds('09')
9
>>> to_seconds('2:09')
129
>>> to_seconds('1:02:09')
3729
This is a different “spelling” of FMc’s answer 🙂
Using datetime module
import datetime
t = '10:15:30'
h,m,s = t.split(':')
print(int(datetime.timedelta(hours=int(h),minutes=int(m),seconds=int(s)).total_seconds()))
Output: 36930
Another alternative if you have days on string:
def duration2sec(string):
if "days" in string:
days = string.split()[0]
hours = string.split()[2].split(':')
return int(days) * 86400 + int(hours[0]) * 3600 + int(hours[1]) * 60 + int(hours[2])
else:
hours = string.split(':')
return int(hours[0]) * 3600 + int(hours[1]) * 60 + int(hours[2])
You can use lambda and reduce a list and the fact that m=60s and h=60m. (see “Reducing a List” at http://www.python-course.eu/lambda.php)
timestamp = "1:23:45"
seconds = reduce(lambda x, y: x*60+y, [int(i) for i in (timestamp.replace(':',',')).split(',')])
I didn’t really like any of the given answers, so I used the following:
def timestamp_to_seconds(t):
return sum(float(n) * m for n,
m in zip(reversed(time.split(':')), (1, 60, 3600))
)
You can split the time into a list and add each individual time component, multiplying the hours component by 3600 (the number of seconds in an hour) and the minutes component by 60 (number of seconds in a minute), like:
timeInterval ='00:35:01'
list = timeInterval.split(':')
hours = list[0]
minutes = list[1]
seconds = list[2]
total = (int(hours) * 3600 + int(minutes) * 60 + int(seconds))
print("total = ", total)
Expanding on @FMc’s solution which embodies half of Horner’s method. Advantage of Horner’s method: Skip reversing the list, avoid power calculation.
from functools import reduce
timestamp = "1:23:45"
seconds = reduce(lambda s, d: s * 60 + int(d), timestamp.split(":"), 0)
or, if you dislike reduce (as does Guido van Rossum and @0xc0de below):
timestamp = "1:23:45"
seconds = 0
for d in timestamp.split(":"):
seconds = seconds * 60 + int(d)
If you prefer zip (as does @nathan-rice below):
from itertools import accumulate, repeat
from operator import mul
def timestamp_to_seconds(t):
return sum(int(n) * m for n, m in
zip(reversed(t.split(":")), accumulate(repeat(60), func=mul, initial=1)))
Using datetime
module is also posible and more robust
import datetime as dt
def get_total_seconds(stringHMS):
timedeltaObj = dt.datetime.strptime(stringHMS, "%H:%M:%S") - dt.datetime(1900,1,1)
return timedeltaObj.total_seconds()
datetime.strptime
parses the string according to format %H:%M:%S, and it creates a datetime object as year 1900, month1, day 1, hour H, minute M, and second S.
That’s why to get the total of seconds is necessary to subtract the year, month and day.
print(get_total_seconds('1:23:45'))
>>> 5025.0
print(get_total_seconds('0:04:15'))
>>> 255.0
print(get_total_seconds('0:00:25'))
>>>25.0
Just a simple generalization to the great response of taskinoor
In the context of my problem the format is similar, but includes AM or PM.
Format ‘HH:MM:SS AM’ or ‘HH:MM:SS PM’
For this case the function changes to:
def get_sec(time_str):
"""Get Seconds from time."""
if 'AM' in time_str:
time_str = time_str.strip('AM')
h, m, s = time_str.split(':')
seconds = int(h) * 3600 + int(m) * 60 + int(s)
if 'PM' in time_str:
time_str = time_str.strip('PM')
h, m, s = time_str.split(':')
seconds = (12 + int(h)) * 3600 + int(m) * 60 + int(s)
return seconds
In Pandas using @JayRizzo ‘s cool function and a list comprehension:
def get_sec(time_str):
"""Get Seconds from time."""
h, m, s = time_str.split(':')
return int(h) * 3600 + int(m) * 60 + int(s)
df['secs']=[get_sec(x) for x in df['original_time_string']]
For %H:%M:%S.%f
def get_sec(time_str):
h, m, s = time_str.split(':')
return int(h) * 3600 + int(m) * 60 + float(s)
I liked tzot’s answer above the best for my purposes because my H:M:S are human created and the can have float seconds part
Here is my small modification
def hms_to_seconds(timestr:str)->float:
"""Get seconds from time.
:param timestr: hh:mm:ss.xxx string or mm:s.xxx or simply s.xxx where xxx is the fraction of seconds
:returns: time in float seconds
"""
seconds= 0.
for part in timestr.split(':'):
seconds= seconds*60. + float(part)
return seconds
>>> hms_to_seconds('3.00')
3.0
>>> hms_to_seconds('1:3.00')
63.0
>>> hms_to_seconds('1:0:3.5')
3603.5
Basically I have the inverse of this problem: Python Time Seconds to h:m:s
I have a string in the format H:MM:SS (always 2 digits for minutes and seconds), and I need the integer number of seconds that it represents. How can I do this in python?
For example:
- “1:23:45” would produce an output of 5025
- “0:04:15” would produce an output of 255
- “0:00:25” would produce an output of 25
etc
parts = time_string.split(":")
seconds = int(parts[0])*(60*60) + int(parts[1])*60 + int(parts[2])
def get_sec(time_str):
"""Get seconds from time."""
h, m, s = time_str.split(':')
return int(h) * 3600 + int(m) * 60 + int(s)
print(get_sec('1:23:45'))
print(get_sec('0:04:15'))
print(get_sec('0:00:25'))
ts = '1:23:45'
secs = sum(int(x) * 60 ** i for i, x in enumerate(reversed(ts.split(':'))))
print(secs)
Without many checks, and assuming it’s either "SS" or "MM:SS" or "HH:MM:SS" (although not necessarily two digits per part):
def to_seconds(timestr):
seconds= 0
for part in timestr.split(':'):
seconds= seconds*60 + int(part, 10)
return seconds
>>> to_seconds('09')
9
>>> to_seconds('2:09')
129
>>> to_seconds('1:02:09')
3729
This is a different “spelling” of FMc’s answer 🙂
Using datetime module
import datetime
t = '10:15:30'
h,m,s = t.split(':')
print(int(datetime.timedelta(hours=int(h),minutes=int(m),seconds=int(s)).total_seconds()))
Output: 36930
Another alternative if you have days on string:
def duration2sec(string):
if "days" in string:
days = string.split()[0]
hours = string.split()[2].split(':')
return int(days) * 86400 + int(hours[0]) * 3600 + int(hours[1]) * 60 + int(hours[2])
else:
hours = string.split(':')
return int(hours[0]) * 3600 + int(hours[1]) * 60 + int(hours[2])
You can use lambda and reduce a list and the fact that m=60s and h=60m. (see “Reducing a List” at http://www.python-course.eu/lambda.php)
timestamp = "1:23:45"
seconds = reduce(lambda x, y: x*60+y, [int(i) for i in (timestamp.replace(':',',')).split(',')])
I didn’t really like any of the given answers, so I used the following:
def timestamp_to_seconds(t):
return sum(float(n) * m for n,
m in zip(reversed(time.split(':')), (1, 60, 3600))
)
You can split the time into a list and add each individual time component, multiplying the hours component by 3600 (the number of seconds in an hour) and the minutes component by 60 (number of seconds in a minute), like:
timeInterval ='00:35:01'
list = timeInterval.split(':')
hours = list[0]
minutes = list[1]
seconds = list[2]
total = (int(hours) * 3600 + int(minutes) * 60 + int(seconds))
print("total = ", total)
Expanding on @FMc’s solution which embodies half of Horner’s method. Advantage of Horner’s method: Skip reversing the list, avoid power calculation.
from functools import reduce
timestamp = "1:23:45"
seconds = reduce(lambda s, d: s * 60 + int(d), timestamp.split(":"), 0)
or, if you dislike reduce (as does Guido van Rossum and @0xc0de below):
timestamp = "1:23:45"
seconds = 0
for d in timestamp.split(":"):
seconds = seconds * 60 + int(d)
If you prefer zip (as does @nathan-rice below):
from itertools import accumulate, repeat
from operator import mul
def timestamp_to_seconds(t):
return sum(int(n) * m for n, m in
zip(reversed(t.split(":")), accumulate(repeat(60), func=mul, initial=1)))
Using datetime
module is also posible and more robust
import datetime as dt
def get_total_seconds(stringHMS):
timedeltaObj = dt.datetime.strptime(stringHMS, "%H:%M:%S") - dt.datetime(1900,1,1)
return timedeltaObj.total_seconds()
datetime.strptime
parses the string according to format %H:%M:%S, and it creates a datetime object as year 1900, month1, day 1, hour H, minute M, and second S.
That’s why to get the total of seconds is necessary to subtract the year, month and day.
print(get_total_seconds('1:23:45'))
>>> 5025.0
print(get_total_seconds('0:04:15'))
>>> 255.0
print(get_total_seconds('0:00:25'))
>>>25.0
Just a simple generalization to the great response of taskinoor
In the context of my problem the format is similar, but includes AM or PM.
Format ‘HH:MM:SS AM’ or ‘HH:MM:SS PM’
For this case the function changes to:
def get_sec(time_str):
"""Get Seconds from time."""
if 'AM' in time_str:
time_str = time_str.strip('AM')
h, m, s = time_str.split(':')
seconds = int(h) * 3600 + int(m) * 60 + int(s)
if 'PM' in time_str:
time_str = time_str.strip('PM')
h, m, s = time_str.split(':')
seconds = (12 + int(h)) * 3600 + int(m) * 60 + int(s)
return seconds
In Pandas using @JayRizzo ‘s cool function and a list comprehension:
def get_sec(time_str):
"""Get Seconds from time."""
h, m, s = time_str.split(':')
return int(h) * 3600 + int(m) * 60 + int(s)
df['secs']=[get_sec(x) for x in df['original_time_string']]
For %H:%M:%S.%f
def get_sec(time_str):
h, m, s = time_str.split(':')
return int(h) * 3600 + int(m) * 60 + float(s)
I liked tzot’s answer above the best for my purposes because my H:M:S are human created and the can have float seconds part
Here is my small modification
def hms_to_seconds(timestr:str)->float:
"""Get seconds from time.
:param timestr: hh:mm:ss.xxx string or mm:s.xxx or simply s.xxx where xxx is the fraction of seconds
:returns: time in float seconds
"""
seconds= 0.
for part in timestr.split(':'):
seconds= seconds*60. + float(part)
return seconds
>>> hms_to_seconds('3.00')
3.0
>>> hms_to_seconds('1:3.00')
63.0
>>> hms_to_seconds('1:0:3.5')
3603.5