Python - 确定现在时间是否介于两次之间 [英] Python - Working out if time now is between two times

查看:63
本文介绍了Python - 确定现在时间是否介于两次之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到最干净/最pythonic的方式来评估现在"是否在两次之间;然而;开始/结束时间可能会也可能不会跨越一天的边界 - 例如(仅使用简单示例):

I'm trying to find the cleanest/most pythonic way of evaluating if "now" is between two times; However; the Start/End times may, or may not, fall across a day boundary- for example (just using simple examples):

onhour=23
onmin=30
offhour=4
offmin=15
timenow = datetime.datetime.now().time()

如果 START < 直接执行 现在 场景不适用于此!

Doing a straight if START < NOW < END scenario won't work for this!

我目前拥有的是一些代码,用于评估当前是否为NightTime",如下所示:

What I have currently is some code which evaluates if it's currently "NightTime", which looks like this:

def check_time(timenow, onhour, onmin, offhour, offmin, verbose):
    now = datetime.datetime.now()
    now_time = now.time()
    # If we're actually scheduling at night:
    if int(offhour) < int(onhour):
        # Check to see if we're in daylight times (ie. off schedule)
        if datetime.time(int(offhour),int(offmin)) <= now_time <= datetime.time(int(onhour),int(onmin)):
            if verbose == True:
                print("Day Time detected.")
            return False
        else:
            if verbose == True:
                print("Night Time detected.")
            return True
    else:
        if datetime.time(int(onhour),int(onmin)) <= now_time <= datetime.time(int(offhour),int(offmin)):
            if verbose == True:
                print("Night Time detected.")
            return True
        else:
            if verbose == True:
                print("Day Time detected.")
            return False

如果标题听起来不像任何新东西,但已经查看了一些类似问题的现有答案,例如:

Apologies if the title doesn't sound like anything new, but having reviewed a few existing answers for similar problems such as:

我注意到这些似乎没有考虑开始和结束时间发生在一天边界上的情况.

I noticed that these don't seem to account for instances where the Start and End times occur over a day boundary.

除此之外;任何关于添加基于日的日程安排的想法也将非常有用!IE.周一至周五,在 23:00 开启,04:00 关闭" - 但管理两边一天的开启和关闭(否则;某些东西将在周五开启,但不会在周六关闭-- 然而,包括星期六意味着它会在 23 点再次打开!...)

In addition to this; any ideas surrounding adding Day based scheduling would be quite useful too! ie. "for Mon - Fri, turn on at 23:00, off a 04:00" - but managing on and off for a day either side (else; something will be turned on, on Friday, but not be turned off on the Saturday-- and yet, including Saturday means it gets turned back on again at 23!...)

我已经考虑过做一个简单的在 X 时开启,为 Y 睡眠"来解决这个问题……但是如果脚本在开启"周期中启动,它会在下一次启动运行...但这似乎是最简单的选择!:)

I've considered doing a simple "Turn on at X, sleep for Y" to get around this... but if the script is started up during an "On" cycle, it won't be initiated until the next run... But it seems like the simplest option! :)

我希望有某种很棒的模块可以完成所有这些... :D

I'm hoping there's some sort of awesome module that does all this... :D

Python2.7 - 3.2 的兼容性对我来说也很重要!

Compatibility of Python2.7 - 3.2 is pretty important to me too!

推荐答案

你的代码有点乱.我会做这样的事情:

Your code is a bit chaotic. I would do something like this:

import datetime

DAY, NIGHT = 1, 2
def check_time(time_to_check, on_time, off_time):
    if on_time > off_time:
        if time_to_check > on_time or time_to_check < off_time:
            return NIGHT, True
    elif on_time < off_time:
        if time_to_check > on_time and time_to_check < off_time:
            return DAY, True
    elif time_to_check == on_time:
        return None, True
    return None, False


on_time = datetime.time(23,30)
off_time = datetime.time(4,15)
timenow = datetime.datetime.now().time()
current_time = datetime.datetime.now().time()

when, matching = check_time(current_time, on_time, off_time)

if matching:
    if when == NIGHT:
        print("Night Time detected.")
    elif when == DAY:
        print("Day Time detected.")

这篇关于Python - 确定现在时间是否介于两次之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆