- import time
- import os
- # Get the current time
- current_time = time.localtime()
- # Ask the user for the desired shutdown time
- shutdown_time_str = input("Enter the shutdown time (in 12-hour format with am/pm suffix): ")
- shutdown_time = time.strptime(shutdown_time_str, "%I:%M %p")
- # Convert the shutdown time to 24-hour format
- if shutdown_time.tm_hour == 12 and shutdown_time_str[-2:].lower() == "am":
- shutdown_time = shutdown_time.replace(hour=0)
- elif shutdown_time.tm_hour != 12 and shutdown_time_str[-2:].lower() == "pm":
- shutdown_time = shutdown_time.replace(hour=shutdown_time.tm_hour + 12)
- # Calculate the time until shutdown in seconds
- 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))
- time_until_shutdown = time.mktime(shutdown_time) - time.time()
- # Convert time_until_shutdown to seconds if it's negative (meaning the shutdown time is tomorrow)
- if time_until_shutdown < 0:
- time_until_shutdown += 86400
- # Schedule the shutdown using the "shutdown" command
- os.system(f"shutdown /s /t {int(time_until_shutdown)}")
- print(f"Your computer will shut down in {int(time_until_shutdown/3600)} hours and {(int(time_until_shutdown)%3600)//60} minutes.")
Raw Paste