Calculate time difference using Python(使用 Python 计算时差)
问题描述
我想知道是否有一种方法或内置库可以找到两个字符串输入的时间差异.
我的意思是,如果我有 2 个输入字符串:
- '2013-10-05T01:21:07Z'
- '2013-10-05T01:21:16Z'
如何计算时间差并将其打印为输出.
我知道这听起来有点傻,但感谢任何帮助.
使用 strptime()
解析字符串:
a = time.strptime('2013-10-05T01:21:07Z', '%Y-%m-%dT%H:%M:%SZ')b = time.strptime('2013-10-05T01:21:16Z', '%Y-%m-%dT%H:%M:%SZ')
这会将给定的时间字符串解析为当地时间(将夏令时 (DST) 设置为自动),结果是时间结构.这些仍然反映 DST 是显式关闭 (0)、打开 (1) 还是隐式自动 (-1).将这些转换为浮点数(自 1970-01-01 以来的秒数):
a = time.mktime(a)b = 时间.mktime(b)
然后计算差异(以秒为单位):
d = b - a
并将它们转换为天/小时/分钟/秒:
days = int(d)/86400小时 = int(d)/3600 % 24分钟 = int(d)/60 % 60秒 = int(d) % 60
最后一个块仅适用于正差异,因此请注意不要交换 a
和 b
;-)
但是@J.F.Sebastian 正确地指出这可能不是您想要的.从符号看来,您的字符串描述的是 UTC 时间,而不是本地时间.如果您的时间跨度在 DST 开关上,则仅对于时差是相关的.在这种情况下,当然会导致时差过大或过小一小时(因为 UTC 始终没有 DST).
为避免这种情况,您可以将 DST 标志从自动 (-1) 设置为固定值(例如,0 表示关闭)并使用以下值:
a = time.mktime(a[:-1] + (0,)) # 关闭夏令时b = time.mktime(b[:-1] + (0,))
或者,也正如@JFSebastian 指出的那样,您可以忘记 time
模块,而使用不知道 DST 方面的 datetime.datetime
:
a = datetime.datetime.strptime('2013-10-05T01:21:07Z', '%Y-%m-%dT%H:%M:%SZ')b = datetime.datetime.strptime('2013-10-05T01:21:16Z', '%Y-%m-%dT%H:%M:%SZ')
然后结果是 datetime
对象,可以直接减去这些对象以获得 timedelta
对象,该对象代表您想要的时间差.打印它会产生类似 0:00:05
的结果,这很可能正是您正在寻找的.p>
I am wondering if there is a way or builtin library available to find the difference in time from two string input.
What I mean is, if I have 2 input strings:
- '2013-10-05T01:21:07Z'
- '2013-10-05T01:21:16Z'
how can I can calculate the difference in time and print it as output.
I know it sounds a bit silly but any help on this is appreciated.
Parsing your strings using strptime()
:
a = time.strptime('2013-10-05T01:21:07Z', '%Y-%m-%dT%H:%M:%SZ')
b = time.strptime('2013-10-05T01:21:16Z', '%Y-%m-%dT%H:%M:%SZ')
This will parse the given time strings as local times (setting daylight savings (DST) to automatic), and the results are time structs. These still reflect whether DST was explicitly off (0), on (1), or implicitly automatic (-1). Convert these to a float (seconds since 1970-01-01):
a = time.mktime(a)
b = time.mktime(b)
Then compute the difference (in seconds):
d = b - a
And convert them to days/hours/minutes/seconds:
days = int(d) / 86400
hours = int(d) / 3600 % 24
minutes = int(d) / 60 % 60
seconds = int(d) % 60
The last block only works properly for positive differences, so be careful not to swap the a
and b
;-)
But @J.F.Sebastian correctly pointed out that this might not be what you intended. It seems from the notation that your strings describe a UTC time, not a local time. For mere time differences this is relevant in case your time spans over a DST switch. In this case it would of course result in a time difference one hour too great or one hour too small (because UTC is always without DST).
To avoid this, you can set the DST flag from automatic (-1) to a fixed value (e. g. 0 for off) and use these values:
a = time.mktime(a[:-1] + (0,)) # switch DST to off
b = time.mktime(b[:-1] + (0,))
Or, also as @J.F.Sebastian pointed out, you could forget about the time
module and instead use datetime.datetime
which is unaware of the DST aspect:
a = datetime.datetime.strptime('2013-10-05T01:21:07Z', '%Y-%m-%dT%H:%M:%SZ')
b = datetime.datetime.strptime('2013-10-05T01:21:16Z', '%Y-%m-%dT%H:%M:%SZ')
Then the results are datetime
objects which can be subtracted directly to get a timedelta
object which represents such a time difference as you want it. Printing it will result in sth like 0:00:05
which might well be exactly what you are looking for.
这篇关于使用 Python 计算时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Python 计算时差
基础教程推荐
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 筛选NumPy数组 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01