【实例简介】
【实例截图】
【核心代码】
import os
import time
from datetime import datetime
def close_job1(times):
print(
'现在是',
time.strftime(
'%Y-%m-%d %H:%M:%S',
time.localtime(
time.time())))
now = str(datetime.now()).rpartition(":")[0].split(" ")[1]
now_tosec = (int(now.split(":")[0]) * 60 int(now.split(":")[1])) * 60
t_tosec = (int(times.split(":")[0]) * 60 int(times.split(":")[1])) * 60
diff = int((t_tosec - now_tosec) / 60)
print("定时任务启动,系统将在 {},即{}分钟后 自动关闭" .format(times, diff))
command = "shutdown -s -t {}".format(diff * 60)
os.system(command)
def close_job2(times):
print(
'现在是',
time.strftime(
'%Y-%m-%d %H:%M:%S',
time.localtime(
time.time())))
now = str(datetime.now()).rpartition(":")[0].split(" ")[1]
sum = int(now.split(":")[1]) int(times)
if sum >= 60:
a = sum // 60
b = str(sum % 60)
if len(b) < 2:
b = "0" b
tim = str(int(now.split(":")[0]) a) ":" b
print("定时任务启动,系统将在 {},即{}分钟后 自动关闭" .format(tim, times))
else:
tim = str(int(now.split(":")[0]) a) ":" b
print("定时任务启动,系统将在 {},即{}分钟后 自动关闭".format(tim, times))
else:
tim = str(int(now.split(":")[0])) ":" str(sum)
print("定时任务启动,系统将在 {},即{}分钟后 自动关闭" .format(tim, times))
times = times * 60
command = "shutdown -s -t {}".format(times)
os.system(command)
def cancel_job():
print(
'现在是',
time.strftime(
'%Y-%m-%d %H:%M:%S',
time.localtime(
time.time())))
os.system("shutdown -a")
print("定时关机任务已取消")
if __name__ == '__main__':
mode = int(input('选择模式\n1、指定时间关闭\n2、XX分钟后关闭\n3、取消定时关闭任务\n'))
if mode == 1:
close_time = input("输入关闭系统||时间格式如|21:56|\n\t")
close_job1(close_time)
elif mode == 2:
close_time = int(input("输入分钟数进行关闭系统||时间格式如|60|\n\t"))
close_job2(close_time)
elif mode == 3:
cancel_job()
else:
print("输入有误,默认取消定时关机")
cancel_job()