使用 Python 计算时差

2022-01-22 00:00:00 python time date difference gps

问题描述

我想知道是否有一种方法或内置库可以找到两个字符串输入的时间差异.

我的意思是,如果我有 2 个输入字符串:

  1. '2013-10-05T01:21:07Z'
  2. '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

最后一个块仅适用于正差异,因此请注意不要交换 ab ;-)

但是@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 的结果,这很可能正是您正在寻找的.

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:

  1. '2013-10-05T01:21:07Z'
  2. '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.

相关文章