首页 / PYTHON / python时间范围验证器
python时间范围验证器
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python时间范围验证器,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4137字,纯文字阅读大概需要6分钟。
内容图文
![python时间范围验证器](/upload/InfoBanner/zyjiaocheng/710/f6c89fc4785f4644a6e1086e47cd5fbc.jpg)
我在DB中有2个参数:启动和停止.它们的价值可以是例如07:00-23:00或23:00-07:00
(从07开始,23之后停止或23之后开始,07之后停止)
在那个时候,状态必须是0或1,让我们说它是LED
如何创建在午夜之后/之前不会搞乱的统一逻辑控制器?
我的糟糕实施(不会工作)如下.实际上,我已经尝试了很多变化,但最终仍然是我目前所处的位置.
if curtime >= vv_time_trig1 and curtime <= vv_time_trig2:
logger.info("turning socket on")
logger.debug("#1")
#check current status
#if current is 0
#turn socket on
if vvstatus == 0:
logger.debug("current status off, turning socket on")
GPIO.output(25, GPIO.HIGH)
#check current status
#if current is already 1
#do nothing
elif vvstatus == 1:
logger.info("skiping. already on")
#unhandeled current status
else:
logger.critical("unhandeled vvstatus!")
logger.critical("turning socket off")
GPIO.output(25, GPIO.LOW)
#if current time is before start
#turn off
elif curtime <= vv_time_trig1 and curtime >= vv_time_trig2:
logger.info("turning socket off")
logger.debug("#2")
#check current status
#if current is 1
#turn socket off
if vvstatus == 1:
logger.debug("current status on, turning socket off")
GPIO.output(25, GPIO.LOW)
#check current status
#if current is already 0
#do nothing
elif vvstatus == 0:
logger.info("skiping. already off")
#unhandeled current status
else:
logger.critical("unhandeled vvstatus!")
logger.critical("turning socket off")
GPIO.output(25, GPIO.LOW)
#if current time is after stop
#turn off
elif curtime >= vv_time_trig2:
logger.info("turning socket off")
logger.debug("#3")
#check current status
#if current is 1
#turn socket off
if vvstatus == 1:
logger.debug("current status: %s, turning socket off", vvstatus)
GPIO.output(25, GPIO.LOW)
#check current status
#if current is already 0
#do nothing
elif vvstatus == 0:
logger.info("skiping. already on")
#unhandeled current status
else:
logger.critical("unhandeled vvstatus!")
logger.critical("turning socket off")
GPIO.output(25, GPIO.LOW)
#if current time is before stop
#turn off
elif curtime <= vv_time_trig2 and curtime <= vv_time_trig1:
logger.info("turning socket on")
logger.debug("#4")
#check current status
#if current is 0
#turn socket on
if vvstatus == 0:
logger.debug("current status off, turning socket on")
GPIO.output(25, GPIO.HIGH)
#check current status
#if current is already 1
#do nothing
elif vvstatus == 1:
logger.info("skiping. already on")
#unhandeled current status
else:
logger.critical("unhandeled vvstatus!")
logger.critical("turning socket off")
GPIO.output(25, GPIO.LOW)
更新后的版本.
确定相对于结束的当前时间位置.如果午夜过去,结束将定在明天
n1 = datetime.now()
startTrig = datetime(n1.year, n1.month, n1.day, 23, 00, 0)
logger.debug("start: %s",startTrig)
n = datetime.now()
endTrig = datetime(n.year, n.month, n.day, 07, 00, 0)
logger.debug("end: %s",endTrig)
if startTrig > endTrig:
logger.debug("start > stop")
endTrig += timedelta(days=1)
logger.debug("new stop trig: %s",endTrig)
if datetime.now() < endTrig:
if curStatus == 1:
logger.debug("socket %s already on. doing nothing.")
elif curStatus == 0:
logger.debug("socket %s sould be on. flipping switch")
flipSocketStatus(bcmNo,bcmDir)
else:
logger.critical("unhandeled socket %s current status %s",socName,curStatus)
if curStatus == 1:
logger.critical("shutting socket %s down",socName)
GPIO.output(bcmNo, GPIO.LOW)
elif curStatus == 0:
logger.warn("socket %s already off",socName)
else:
logger.critical("unhandeled current status for pin: %s",bcmNo)
logger.critical("forcing socket %s down",socName)
GPIO.output(bcmNo, GPIO.LOW)
else:
logger.critical("unhandeled start-stop rules")
解决方法:
有两种情况:当前时间是在给定时间(顺时针)或外部(想象时钟圈)之间:
#!/usr/bin/env python
from datetime import datetime
def in_between(now, start, end):
if start < end: # e.g., "07:00-23:00"
return start <= now < end
elif end < start: # e.g., "23:00-07:00"
return start <= now or now < end
else: # start == end
return True # consider it 24 hour interval
now = datetime.now().time()
for date_range in ["07:00-23:00", "23:00-07:00"]:
start, end = [datetime.strptime(s, "%H:%M").time()
for s in date_range.split("-")]
not_ = '' if in_between(now, start, end) else 'not '
print("{now:%H:%M} is {not_}in between {date_range}".format(**vars()))
产量
02:26 is not in between 07:00-23:00
02:26 is in between 23:00-07:00
内容总结
以上是互联网集市为您收集整理的python时间范围验证器全部内容,希望文章能够帮你解决python时间范围验证器所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。