Function for checking time and date?

79 views Asked by At

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)
0

There are 0 answers