Python 给定起始值&;停止时间,检查服务是否应打开
我需要弄清楚一个服务是应该停止还是应该在给定的启动和停止时间内启动 e、 gPython 给定起始值&;停止时间,检查服务是否应打开,python,Python,我需要弄清楚一个服务是应该停止还是应该在给定的启动和停止时间内启动 e、 g start_time='16:00' 停止时间='7:50' 如果现在的时间是16:50,服务应该运行,如果现在的时间是14:00,服务应该停止 开始时间='7:00' 停止时间='20:00' 如果现在的时间是7:05,服务应该运行,如果时间是21:00,服务应该停止。。。你明白了吗 到目前为止,我有这个,但无法理解逻辑 导入日期时间 def测试时间(开始时间、停止时间): now=datetime.dateti
start_time='16:00'
停止时间='7:50'
如果现在的时间是16:50,服务应该运行,如果现在的时间是14:00,服务应该停止
开始时间='7:00'
停止时间='20:00'
如果现在的时间是7:05,服务应该运行,如果时间是21:00,服务应该停止。。。你明白了吗
到目前为止,我有这个,但无法理解逻辑
导入日期时间
def测试时间(开始时间、停止时间):
now=datetime.datetime.now()
当前时数=整数(现在时数)
当前分钟=整数(现在.分钟)
开始时间,开始分钟=开始时间。拆分(“:”)
停止小时,停止分钟=停止时间。拆分(“:”)
打印(f“小时:{current_hour},分钟:{current_minute}”)
使用datetime
将文本时间转换为time
s。这为您提供了一个间隔,在此期间服务应处于打开状态。如果这段时间大约是午夜,那么将它分成两段时间
然后,只需检查当前时间是否在打开
期间或关闭
期间内;还要检查服务的状态。如果两者不匹配,则启动/停止服务
def sync_service(start_time, stop_time):
# start_time and stop_time are "datetime" items.
service_on = # Check status of service; return boolean
now = datetime.datetime.now()
# If interval wraps around midnight, then switch times
# to check when service is *off*
wrap_interval = stop_time < start_time
if wrap_interval:
start_time, stop_time = stop_time, start_time
# Should the service be on now?
# Check whether we're within the daily interval,
# and what type of interval we have (on or off)
want_service_on = wrap_interval != (start_time < now < stop_time)
# Adjust service status, if necessary
if want_service_on and not service_on:
service.start()
if not want_service_on and service_on:
service.stop()
def同步服务(开始时间、停止时间):
#开始时间和停止时间是“日期时间”项。
service_on=#检查服务状态;返回布尔值
now=datetime.datetime.now()
#如果时间间隔在午夜左右,则切换时间
#检查服务何时关闭*
换行间隔=停止时间<开始时间
如果换行间隔:
开始时间,停止时间=停止时间,开始时间
#现在应该开始服务吗?
#检查我们是否在每日间隔内,
#我们有什么样的时间间隔(开或关)
想要服务打开=换行间隔!=(开始时间<现在<停止时间)
#如有必要,调整维修状态
如果希望服务打开而不是服务打开:
service.start()
如果不需要“服务”和“服务”打开:
服务。停止()
这能让您继续吗?您是否尝试过使用
调度程序类
?