我想要实现的是当时间在 xx 和 yy 之间时运行指定的操作。
(使用 24 小时格式)
前任。
- 当小时和分钟高于23:30 且低于06:15 时,运行指定的操作。
- 当小时和分钟高于06:15 且低于09:00时,运行指定的操作。
- 当小时和分钟高于09:00 且低于23:30时,运行指定的操作。
我什么时候试过:
self.data = 0
localtime = time.strftime("%H%M", time.localtime())
localtime = int(localtime)
if localtime >= 2330 and localtime < 615 and self.data != 1:
[..] //running certain action
self.data = 1
elif localtime >= 615 and localtime < 900 and self.data != 2:
[..] //running certain action
self.data = 2
elif localtime >= 900 and localtime < 2330 and self.data != 3:
[..] //running certain action
self.data = 3
如您所见,我的代码的唯一问题是localtime
不能同时高于2330 和低于615,依此类推。我得到的唯一另一个想法是创建一个列出所有 24 小时的数组,并以这种方式指定某个操作……但是否有其他方法可以实现我想要的?