[英]How to convert time object into total minutes python
I have objects of the form '00:00:00.0000000' from which I simply want to extract a float number of minutes. 我有'00:00:00.0000000'形式的对象,我只想从中提取浮点数(分钟)。 From
import time
I so far have this: 从
import time
现在为止,我有:
>>> reformat = time.strptime('00:05:36.0100000', '%H:%M:%S.%f0')
>>> print reformat
time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=5, tm_sec=36, tm_wday=0, tm_yday=1, tm_isdst=-1)
It seems like this should follow with something like time.getminutes(reformat)
or reformat.minutes()
and voila. 似乎应该跟着
time.getminutes(reformat)
或reformat.minutes()
和time.getminutes(reformat)
东西。 I can't find such an operation. 我找不到这样的操作。
Parse the date string as a datetime.datetime. 将日期字符串解析为datetime.datetime。 Since the date string has no year, month or day, the datetime will assume the date to be
1900-1-1
. 由于日期字符串没有年,月或日,因此datetime将假定日期为
1900-1-1
。 If we subtract datetime.datetime(1900,1,1)
we'll obtain a datetime.timedelta
object which has a total_seconds
method . 如果减去
datetime.datetime(1900,1,1)
我们将获得一个具有total_seconds
方法的datetime.timedelta
对象。 Conversion to minutes is then easy: 然后转换为分钟很容易:
Subtract it from 从中减去
import datetime as DT
t1 = DT.datetime.strptime('00:05:36.0100000', '%H:%M:%S.%f0')
t2 = DT.datetime(1900,1,1)
print((t1-t2).total_seconds() / 60.0)
yields 产量
5.60016666667
if it is a datetime time object ie from datetime import time. 如果它是一个日期时间对象,即从日期时间导入时间开始。 you can calculate it simply as
您可以简单地计算为
timeobject.hour*60+timeobject.minute+timeobject.second/60
Since you want minute resolution, I doubt there is need for second or even microsecond accuracy 由于您需要微小的分辨率,因此我怀疑是否需要秒甚至微秒的精度
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.