检查时间和日期的功能?
我想编写一个程序,以检查一周的时间和一天,然后根据该信息为能量管理系统开关操作模式。该计划需要在整天每天连续运行和检查日期和时间。这是我最挣扎的第一部分。根据以前的工作,我尝试了下面的解决方案,但是我不确定这是执行此操作的最有效或正确的方法。我想知道是否有更好的方法来执行此操作。
#SE_v1_algorithm
#this algorithm is meant to modify the times at which the system charges and
#discharges each day
import time
import datetime
import schedule
import os
exec(open("filename").read()) #this line runs another Python file that gives me
#access to APIs to change operation mode to shifting energy mode
curr_day = datetime.today().weekday()
if curr_day == 5 #Saturday
#this section of the code checks if the time is within 9:00 and 13:00; if it
#is, the battery discharges
time_start = datetime.time(9,0,0)
time_end = datetime.time(13,0,0)
def time_in_range(time_start, time_end, x):
if time_start <= time_end:
return time_start <= x <= time_end
else:
return time_start <= x or x <= time_end
#set to discharging (O2)
elif curr_day == 6
time_start = datetime.time(7,0,0)
time_end = datetime.time(13,0,0)
def time_in_range(time_start, time_end, x):
#there will be a function here to set a switch on or off for a connected system
if time_start <= time_end:
return time_start <= x <= time_end
else:
return time_start <= x or x <= time_end
#set to discharging (O2)
else
time_start = datetime.time(9,30,0)
time_end = datetime.time(15,30,0)
def time_in_range(time_start, time_end, x):
#there will be a function here to set a switch on or off for a connected system
if time_start <= time_end:
return time_start <= x <= time_end
else:
return time_start <= x or x <= time_end
#set to discharging (O2)
I want to write a program that checks the time and day of the week and then switches operation modes for an energy management system based on that information. The program needs to be running and checking the day and time continuously every day throughout the whole day. It's the first part that I am struggling with the most. Based on previous work, I have tried the solution below, but I am not entirely sure if this is the most efficient or correct way to do this. I wanted to know if there was a better way to carry out this action.
#SE_v1_algorithm
#this algorithm is meant to modify the times at which the system charges and
#discharges each day
import time
import datetime
import schedule
import os
exec(open("filename").read()) #this line runs another Python file that gives me
#access to APIs to change operation mode to shifting energy mode
curr_day = datetime.today().weekday()
if curr_day == 5 #Saturday
#this section of the code checks if the time is within 9:00 and 13:00; if it
#is, the battery discharges
time_start = datetime.time(9,0,0)
time_end = datetime.time(13,0,0)
def time_in_range(time_start, time_end, x):
if time_start <= time_end:
return time_start <= x <= time_end
else:
return time_start <= x or x <= time_end
#set to discharging (O2)
elif curr_day == 6
time_start = datetime.time(7,0,0)
time_end = datetime.time(13,0,0)
def time_in_range(time_start, time_end, x):
#there will be a function here to set a switch on or off for a connected system
if time_start <= time_end:
return time_start <= x <= time_end
else:
return time_start <= x or x <= time_end
#set to discharging (O2)
else
time_start = datetime.time(9,30,0)
time_end = datetime.time(15,30,0)
def time_in_range(time_start, time_end, x):
#there will be a function here to set a switch on or off for a connected system
if time_start <= time_end:
return time_start <= x <= time_end
else:
return time_start <= x or x <= time_end
#set to discharging (O2)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论