计算python中两次之间的差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15067710/
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
Calculate the difference between two times in python
提问by Fedrik
I am using python, and want to calculate the difference between two times.
我正在使用 python,并想计算两次之间的差异。
Actually i had scenario to calculate the difference between login and logout times,
for example in organizations there is some particular limit for working hours, so
if a user login at 9:00 AMin the morning and if he logs out at 6:00 PMin the evening,
we need to calculate how much duration he stayed in the office(which is 9 hoursin the present scenario ), but i want to do this in python, so can anyone please let me know how to achieve the above concept of calculating the difference between login and logout times ?
实际上我有一个场景来计算登录和注销时间之间的差异,例如在组织中有一些特定的工作时间限制,所以如果用户9:00 AM在早上登录,如果他6:00 PM在晚上注销,我们需要计算他在办公室呆了多长时间(9 hours在目前的情况下),但我想用 python 做这个,所以有人能告诉我如何实现上述计算登录和注销时间之间差异的概念吗?
回答by Fabian
>>> start = datetime.datetime(year=2012, month=2, day=25, hour=9)
>>> end = datetime.datetime(year=2012, month=2, day=25, hour=18)
>>> diff = end - start
>>> diff
datetime.timedelta(0, 32400)
>>> diff.total_seconds()
32400
>>> diff.total_seconds() / 60 / 60
9
>>>
回答by Has QUIT--Anony-Mousse
You are using the wrong classes to represent time in the first place.
您首先使用错误的类来表示时间。
> import datetime
> print datetime.datetime.now() - datetime.datetime(2013, 1, 1)
55 days, 14:11:06.749378
The returnedobject is a timedelta. But the difference is of course computed between datetimeobjects.
在返回的对象是一个timedelta。但差异当然是在datetime对象之间计算的。
回答by rich tier
回答by Benjamin Schollnick
In my opinion @Fabian's answer is probably the best. But are we making this more difficult than it has to be?
在我看来@Fabian 的答案可能是最好的。但是我们是否让这变得比它必须的更困难?
Do you need to calculate it by day, month, year?
需要按日、月、年计算吗?
If you are generating this, wouldn't it be easier to just use a timesec representation?
如果你正在生成这个,使用时间秒表示不是更容易吗?
When a user logins in:
当用户登录时:
user_login_time = time.time()
When the user revisits:
当用户再次访问时:
time_difference = time.time() - user_login_time
Then check to see if the time_difference is above XXXX seconds? now I am assumingthat you are just going to check if the user hasn't logged in XX minutes, or xx hours?
然后查看time_difference是否在XXXX秒以上?现在我假设您只是要检查用户是否未登录 XX 分钟或 xx 小时?
Otherwise, I would stress that @Fabian's answer would be the best, if you are looking at parsing time date strings, or needing to do other time/date functions with the data.
否则,我会强调@Fabian 的答案将是最好的,如果您正在查看解析时间日期字符串,或者需要对数据执行其他时间/日期函数。
I would also stress, that if you use this method, to make constants for the times, or make sure to comment them, to make it more readable.
我还要强调的是,如果您使用这种方法,请为时间创建常量,或者确保对它们进行注释,以使其更具可读性。
But if your simply just trying to find out if the user has been on within the last 30 minutes, this might be easier.
但是,如果您只是想知道用户是否在过去 30 分钟内一直在线,这可能会更容易。

