Python - 日期 &使用时间戳、timedelta 进行时间比较 [英] Python - Date & Time Comparison using timestamps, timedelta
问题描述
过去一个小时我一直在挖掘 Python 文档和许多 SO 问题;请原谅我是另一个被 Python 时差之谜困住的 Python 新手.
I've spent the past hour digging around the Python docs and many SO questions; please forgive me for being another Python newbie trapped by the mystery of time difference in Python.
我的目标是确定当前时间和某个日期/时间之间的差异,而不管是过去/未来,并返回一个可行的格式,例如秒.
例如,如果输入是 2:00PM 和 4:00PM(现在),我希望它说-7200",表示事件发生在两小时前.如果输入是周五下午 4:00(现在)和周日下午 5:00,则输出应该是176400"秒,代表从现在起两天零一小时.
For example, if the inputs are 2:00PM and 4:00PM (now), I'd like it to say "-7200", representing the event occurred two hours AGO. If the inputs are Fri 4:00PM (now) and Sun 5:00PM the output should be "176400" seconds, representing two days and 1 hour from now.
以下是我尝试过的东西...
Here are the things I've tried...
我的第一个版本是一个函数,它采用字符串时间戳并将其拼凑成多个变量,然后进行比较.它很笨重,有很多错误,我想如果我把它贴在这里,我会对程序员呕吐负责.
My first version was a function that took a string timestamp and pieced it to multiple variables, and then compared them. It was clunky with many errors and I imagine if I posted it here I would be responsible for a programmers throwing up.
我偶然发现了这个神奇的 timedelta 函数,并研究了文档和 SO,但我认为它不能满足我的需求.
I stumbled upon this magical timedelta function and explored the docs and SO, but I don't think it does what I'm looking for.
我的想法是将两个时间戳转换为自纪元以来的秒数,然后减去,但是如果减去的顺序错误(如果事件发生在未来的情况不同),这就会成为一个问题,我觉得就像添加 if 语句来检查秒的符号会很笨拙并且需要避免.
I had the idea to convert both timestamps into seconds since epoch and then subtract, but this becomes a problem if the subtraction is in the wrong order (different cases if the event is in the future), and I feel like adding if statements to check the sign of the seconds would be clunky and something to avoid.
这是我当前的代码(仍然需要修复以进行双向"比较),来自以前解决的 SO 问题:
This is my current code (still needs to be fixed for 'bi-directional' comparison), from a previously resolved SO question:
now = time.strftime("%a %b %d %H:%M:%S %Y")
then = time.ctime(os.path.getmtime("x.cache"))
tdelta = datetime.strptime(now, '%a %b %d %H:%M:%S %Y') - datetime.strptime(then, '%a %b %d %H:%M:%S %Y')
而且我觉得我应该能够从中抽出几秒钟,就像在这个问题中一样:Python 的 timedelta:我不能在我想要的任何时间单位中获取整个差异的值吗?
And I feel like I should somehow be able to pull seconds from this, like in this question: Python's timedelta: can't I just get in whatever time unit I want the value of the entire difference?
但我不知道如何连接这些点.
But I am at a lost on how to connect these dots.
我怎样才能做到这一点?
How can I accomplish this?
推荐答案
你应该可以使用
tdelta.total_seconds()
获得您正在寻找的价值.这是因为 tdelta
是一个 timedelta
对象,以及 datetime
对象之间的任何差异.
to get the value you are looking for. This is because tdelta
is a timedelta
object, as is any difference between datetime
objects.
一些注意事项:
- 使用
strftime
后跟strptime
是多余的.您应该能够使用datetime.now
获取当前日期时间一>. - 同样,使用
time.ctime
后跟strptime
的工作量超出了需要.您应该能够使用datetime 获取另一个
.datetime
对象.fromtimestamp
- Using
strftime
followed bystrptime
is superfluous. You should be able to get the current datetime withdatetime.now
. - Similarly, using
time.ctime
followed bystrptime
is more work than needed. You should be able to get the otherdatetime
object withdatetime.fromtimestamp
.
所以,你的最终代码可能是
So, your final code could be
now = datetime.now()
then = datetime.fromtimestamp(os.path.getmtime("x.cache"))
tdelta = now - then
seconds = tdelta.total_seconds()
这篇关于Python - 日期 &使用时间戳、timedelta 进行时间比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!