TEXT   38
Schedule Shutdown python code
Guest on 10th March 2023 04:09:02 PM

1. import time
2. import os
3.
4. # Get the current time
5. current_time = time.localtime()
6.
7. # Ask the user for the desired shutdown time
8. shutdown_time_str = input("Enter the shutdown time (in 12-hour format with am/pm suffix): ")
9. shutdown_time = time.strptime(shutdown_time_str, "%I:%M %p")
10.
11. # Convert the shutdown time to 24-hour format
12. if shutdown_time.tm_hour == 12 and shutdown_time_str[-2:].lower() == "am":
13.     shutdown_time = shutdown_time.replace(hour=0)
14. elif shutdown_time.tm_hour != 12 and shutdown_time_str[-2:].lower() == "pm":
15.     shutdown_time = shutdown_time.replace(hour=shutdown_time.tm_hour + 12)
16.
17. # Calculate the time until shutdown in seconds
18. shutdown_time = time.struct_time((current_time.tm_year, current_time.tm_mon, current_time.tm_mday, shutdown_time.tm_hour, shutdown_time.tm_min, 0, current_time.tm_wday, current_time.tm_yday, current_time.tm_isdst))
19. time_until_shutdown = time.mktime(shutdown_time) - time.time()
20.
21. # Convert time_until_shutdown to seconds if it's negative (meaning the shutdown time is tomorrow)
22. if time_until_shutdown < 0:
23.     time_until_shutdown += 86400
24.
25. # Schedule the shutdown using the "shutdown" command
26. os.system(f"shutdown /s /t {int(time_until_shutdown)}")
27. print(f"Your computer will shut down in {int(time_until_shutdown/3600)} hours and {(int(time_until_shutdown)%3600)//60} minutes.")

Raw Paste

or to edit or fork this paste. It's free.