简体   繁体   English

如何在Python中找到未来时间的纪元时间

[英]How to find epoch time of future time in Python

I'm currrently writing an alarm clock in Python, However i have some technical difficulties.我目前正在用 Python 编写闹钟,但是我遇到了一些技术困难。 The user has the option for the alarm to repeat (on given days), or to not repeat.用户可以选择让闹钟重复(在给定的日子里)或不重复。 They then provide the minutes and hour at which they want the alarm to trigger.然后他们提供他们希望警报触发的分钟和小时。

For my alarm system to work, i need to know the time as an epoch of when the alarm should trigger.为了让我的警报系统正常工作,我需要知道警报应该触发的时间。

If i am trying to set an alarm, (for example for 19:30, time will always be inputted as 24 hours), i need the alarm to be able to find out the epoch time of the next time it is 19:30, because it could either be on the same day if i set the alarm before 19:30, or it could be for the next day if i set the alarm after 19:30.如果我想设置闹钟(例如 19:30,时间将始终输入为 24 小时),我需要闹钟能够找出下一次是 19:30 的纪元时间,因为如果我在 19:30 之前设置闹钟,它可能是在同一天,如果我在 19:30 之后设置闹钟,它可能是第二天。

Because of this it means i can't simply do time.localtime() , and then take the resulting struct_time object and swap out the hours and minutes to the integers of 19 and 30 (located at indexes 3 and 4 respectively of the object's named tuple), as i would also have to correctly assign the values of the month, day, and day of the year in order to have a valid struct_time object, which, whilst possible, would require a lot of manipulating, when i feel like there is likely a much more reasonable way of doing this.因此,这意味着我不能简单地执行time.localtime() ,然后获取生成的 struct_time 对象并将小时和分钟交换为 19 和 30 的整数(分别位于对象命名的索引 3 和 4元组),因为我还必须正确分配年中的月、日和日的值才能获得有效的 struct_time 对象,尽管可能,当我觉得有可能时,需要进行大量操作可能是一种更合理的方法。

Any help would be much appreciated任何帮助将非常感激

You can simply use the timestamp method on the result.您可以简单地对结果使用timestamp方法。 This will return the epoch time of the datetime instance.这将返回datetime时间实例的纪元时间。 This will work in almost any circumstance, especially since it is a simple alarm clock, but be aware of this wraning from the docs.这几乎适用于任何情况,特别是因为它是一个简单的闹钟,但请注意文档中的这一警告。

Naive datetime instances are assumed to represent local time and this method relies on the platform C mktime() function to perform the conversion.假设朴素的日期时间实例表示本地时间,并且此方法依赖于平台 C mktime() 函数来执行转换。 Since datetime supports wider range of values than mktime() on many platforms, this method may raise OverflowError for times far in the past or far in the future.由于 datetime 在许多平台上支持比 mktime() 更广泛的值,因此此方法可能会在过去或将来很长时间内引发 OverflowError。

Depending on your program architecture you might also consider using the amount of seconds between two times, which can be done using simple subtraction to get a timedelta and the total_seconds function:根据您的程序架构,您还可以考虑使用两次之间的秒数,这可以使用简单的减法来获得timedeltatotal_seconds函数:

import time
import datetime

start = datetime.datetime.now()

time.sleep(2)

end = datetime.datetime.now()

# print total seconds
print((end - start).total_seconds())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM