Python 计算时差

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3426870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:01:19  来源:igfitidea点击:

Calculating Time Difference

pythontime

提问by Brian

at the start and end of my program, I have

在我的程序开始和结束时,我有

from time import strftime
print int(strftime("%Y-%m-%d %H:%M:%S")



Y1=int(strftime("%Y"))
m1=int(strftime("%m"))
d1=int(strftime("%d"))
H1=int(strftime("%H"))
M1=int(strftime("%M"))
S1=int(strftime("%S"))


Y2=int(strftime("%Y"))
m2=int(strftime("%m"))
d2=int(strftime("%d"))
H2=int(strftime("%H"))
M2=int(strftime("%M"))
S2=int(strftime("%S"))

print "Difference is:"+str(Y2-Y1)+":"+str(m2-m1)+":"+str(d2-d1)\
          +" "+str(H2-H1)+":"+str(M2-M1)+":"+str(S2-S1)

But when I tried to get the difference, I get syntax errors.... I am doing a few things wrong, but I'm not sure what is going on...

但是当我试图找出差异时,我遇到了语法错误......我做错了一些事情,但我不确定发生了什么......

Basically, I just want to store a time in a variable at the start of my program, then store a 2nd time in a second variable near the end, then at the last bit of the program, compute the difference and display it. I am not trying to time a function speed. I am trying to log how long it took for a user to progress through some menus. What is the best way to do this?

基本上,我只想在程序开始时将时间存储在变量中,然后将第二次存储在接近末尾的第二个变量中,然后在程序的最后一位,计算差异并显示它。我不是要计时功能速度。我正在尝试记录用户浏览某些菜单所需的时间。做这个的最好方式是什么?

采纳答案by Tim Pietzcker

The datetimemodule will do all the work for you:

datetime模块将为您完成所有工作:

>>> import datetime
>>> a = datetime.datetime.now()
>>> # ...wait a while...
>>> b = datetime.datetime.now()
>>> print(b-a)
0:03:43.984000

If you don't want to display the microseconds, just use (as gnibbler suggested):

如果您不想显示微秒,只需使用(如 gnibbler 建议的那样):

>>> a = datetime.datetime.now().replace(microsecond=0)
>>> b = datetime.datetime.now().replace(microsecond=0)
>>> print(b-a)
0:03:43

回答by John La Rooy

from time import time

start_time = time()
...

end_time = time()
seconds_elapsed = end_time - start_time

hours, rest = divmod(seconds_elapsed, 3600)
minutes, seconds = divmod(rest, 60)

回答by Johannes Charra

You cannot calculate the differences separately ... what difference would that yield for 7:59 and 8:00 o'clock? Try

您无法单独计算差异...... 7:59 和 8:00 点会产生什么差异?尝试

import time
time.time()

which gives you the seconds since the start of the epoch.

它为您提供了自纪元开始以来的秒数。

You can then get the intermediate time with something like

然后你可以用类似的东西来获得中间时间

timestamp1 = time.time()
# Your code here
timestamp2 = time.time()
print "This took %.2f seconds" % (timestamp2 - timestamp1)

回答by user2394284

time.monotonic()(basically your computer's uptime in seconds) is guarranteed to not misbehave when your computer's clock is adjusted (such as when transitioning to/from daylight saving time).

time.monotonic()(基本上是您计算机的正常运行时间(以秒为单位))在调整计算机时钟时(例如在转换到/从夏令时转换时)不会出现异常行为。

>>> import time
>>>
>>> time.monotonic()
452782.067158593
>>>
>>> a = time.monotonic()
>>> time.sleep(1)
>>> b = time.monotonic()
>>> print(b-a)
1.001658110995777