converting a time string to seconds in python

2019-01-31 06:30发布

I need to convert time value strings given in the following format to seconds.I am using python2.6

eg:

1.'00:00:00,000'  -> 0 seconds

2.'00:00:10,000'  -> 10 seconds

3.'00:01:04,000' -> 64 seconds

4. '01:01:09,000' -> 3669 seconds

Do I need to use regex to do this ?I tried to use time module,but time.strptime('00:00:00,000','%I:%M:%S') threw

ValueError: time data '00:00:00,000' does not match format '%I:%M:%S'

Can someone tell me how this can be solved?

Edit:

I think

 pt =datetime.datetime.strptime(timestring,'%H:%M:%S,%f')
 total_seconds = pt.second+pt.minute*60+pt.hour*3600

gives the correct value..I was using the wrong module

标签: python time
7条回答
Ridiculous、
2楼-- · 2019-01-31 06:31

To get the timedelta(), you should subtract 1900-01-01:

>>> from datetime import datetime
>>> datetime.strptime('01:01:09,000', '%H:%M:%S,%f')
datetime.datetime(1900, 1, 1, 1, 1, 9)
>>> td = datetime.strptime('01:01:09,000', '%H:%M:%S,%f') - datetime(1900,1,1)
>>> td
datetime.timedelta(0, 3669)
>>> td.total_seconds() # 2.7+
3669.0

%H above implies the input is less than a day, to support the time difference more than a day:

>>> import re
>>> from datetime import timedelta
>>> td = timedelta(**dict(zip("hours minutes seconds milliseconds".split(),
...                           map(int, re.findall('\d+', '31:01:09,000')))))
>>> td
datetime.timedelta(1, 25269)
>>> td.total_seconds()
111669.0

To emulate .total_seconds() on Python 2.6:

>>> from __future__ import division
>>> ((td.days * 86400 + td.seconds) * 10**6 + td.microseconds) / 10**6
111669.0
查看更多
贪生不怕死
3楼-- · 2019-01-31 06:34
import time
from datetime import datetime

t1 = datetime.now().replace(microsecond=0)
time.sleep(3)
now = datetime.now().replace(microsecond=0)
print((now - t1).total_seconds())

result: 3.0

查看更多
男人必须洒脱
4楼-- · 2019-01-31 06:42

A little more pythonic way I think would be:

timestr = '00:04:23'

ftr = [3600,60,1]

sum([a*b for a,b in zip(ftr, map(int,timestr.split(':')))])

Output is 263Sec.

I would be interested to see if anyone could simplify it further.

查看更多
We Are One
5楼-- · 2019-01-31 06:43

It looks like you're willing to strip fractions of a second... the problem is you can't use '00' as the hour with %I

>>> time.strptime('00:00:00,000'.split(',')[0],'%H:%M:%S')
time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=1, tm_isdst=-1)
>>>
查看更多
甜甜的少女心
6楼-- · 2019-01-31 06:44

For Python 2.7:

>>> import datetime
>>> import time
>>> x = time.strptime('00:01:00,000'.split(',')[0],'%H:%M:%S')
>>> datetime.timedelta(hours=x.tm_hour,minutes=x.tm_min,seconds=x.tm_sec).total_seconds()
60.0
查看更多
beautiful°
7楼-- · 2019-01-31 06:46

There is always parsing by hand

>>> import re
>>> ts = ['00:00:00,000', '00:00:10,000', '00:01:04,000', '01:01:09,000']
>>> for t in ts:
...     times = map(int, re.split(r"[:,]", t))
...     print t, times[0]*3600+times[1]*60+times[2]+times[3]/1000.
... 
00:00:00,000 0.0
00:00:10,000 10.0
00:01:04,000 64.0
01:01:09,000 3669.0
>>> 
查看更多
登录 后发表回答