检查时间和日期的功能?

发布于 2025-01-19 11:51:35 字数 1783 浏览 1 评论 0原文

我想编写一个程序,以检查一周的时间和一天,然后根据该信息为能量管理系统开关操作模式。该计划需要在整天每天连续运行和检查日期和时间。这是我最挣扎的第一部分。根据以前的工作,我尝试了下面的解决方案,但是我不确定这是执行此操作的最有效或正确的方法。我想知道是否有更好的方法来执行此操作。

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文