python自定义时间关机


##### 用户输入时间,到点关机
##### 写入日志信息


def get_network_time():
    """
    获取网络时间
    :return:
    """
    from bs4 import BeautifulSoup
    import requests
    import datetime
    # url = "http://time.tianqi.com/"
    url = "http://time.tianqi.com/%7B:APP_HOST%7D/"
    response = requests.post(url)
    html = response.text
    soup = BeautifulSoup(html, 'html.parser')
    # gmt_time = soup.find_all(id="times")[0].get_text()
    gmt_time = soup.find_all(id="clock")[0].get_text()
    time_list = gmt_time.split("星期")
    new_time = time_list[0] + time_list[1][2:]
    # gmt_type = "%a, %d %b %Y %H:%M:%S GMT"
    new_type = "%Y年%m月%d日 %H:%M:%S"
    time = datetime.datetime.strptime(new_time, new_type)
    return time


def get_system_time():
    """
    获取本机系统时间
    :return:
    """
    import datetime
    now = datetime.datetime.now()
    time = datetime.datetime.strptime(now, "%Y-%m-%d %H:%M:%S")
    print("11", type(time), time)
    return time


def get_use_time():
    try:
        time = get_network_time()
        return time
    except:
        print("local")
        time = get_system_time()
        return time


def input_mode():
    """
    获取用户输入的模式对应的数字
    让用户选择:1--输入日期及时间,2--输入时间,3--输入倒计时时间。
    :return:
    """
    print("---------------SEPARATE---------------")
    print("1--输入日期及时间,2--输入时间,3--输入倒计时时间,可直接使用“>数字”跳转到第二模式")
    mode = input("请选择:")
    change_mode(mode)


def change_mode(mode):
    """
    通过对比数字跳转到指定模式
    :return:
    """
    if mode == "1":
        print("您选择模式一:请输入日期及时间,格式为 2019-10-26 1:44:00")    # 2008-12-12 12:12:12
        get_input_datetime()
    elif mode == "2":
        print("您选择模式二:请输入时间,格式为 12:50:00")
        get_input_time()
    elif mode == "3":
        print("您选择模式三:请输入倒计时时间,格式为 50 (即为五十秒)")
        second = input("请输入:")
        get_input_down(second)
    else:
        print("请正确输入模式")
        input_mode()


def get_input_datetime():
    """
    获取用户输入的日期及时间,如果用户输入的是过去的时间,则提示“时间犹如白驹过隙,转瞬即逝!!!”
    :return:
    """
    str_time = input("请输入:")
    update_mode_change(str_time)
    
    
def get_input_time():
    """
    获取用户输入的时间,只有时间,
    如果系统时间没到用户输入的时间,则到达时关机
    如果系统时间超过了用户输入的时间,则默认明天的那个时间
    :return:
    """
    import datetime
    year = datetime.datetime.now().year
    month = datetime.datetime.now().month
    day = datetime.datetime.now().day
    str_input_time = input("请输入:")
    str_time = str(year)+"-"+str(month)+"-"+str(day)+" "+str_input_time
    print(str_time)
    update_mode_change(str_time)


def update_mode_change(str_time):
    """
    将时间和格式传递过来,转换为datetime的类型,用于比较
    可在里面直接跳到别的模式
    :param str_time: 时间
    :param pattern: 时间的格式
    :return:
    """
    import datetime
    if str_time.startswith(">"):
        mode = str_time.split(">")[1]
        # 跳转到别的模式
        change_mode(mode)
    try:
        time = datetime.datetime.strptime(str_time, "%Y-%m-%d %H:%M:%S")
        # now_time = get_use_time()
        now_time = datetime.datetime.now()
        print("进入比较阶段")
        compare_time(now_time, time)
    except:
        print("很抱歉,你输入的时间格式有问题,请重新选择模式,并按照格式输入正确时间。")
        input_mode()


def compare_time(new_time, input_time):
    import time
    pattern = "%Y-%m-%d %H:%M:%S"
    print("下面将比较时间")
    # 将datetime转换为时间戳,在进行加减
    new_time_stamp = time.mktime(time.strptime(new_time.strftime(pattern), pattern))
    print(new_time_stamp)
    input_time_stamp = time.mktime(time.strptime(input_time.strftime(pattern), pattern))
    print(input_time_stamp)
    if input_time_stamp > new_time_stamp:
        num = input_time_stamp - new_time_stamp
        print("计算机将于", num, "秒后关机。")
        time.sleep(int(num))
        function_shutdown()
    else:
        print("骚年,韶光易逝,请珍惜时间。您输入的已经是过去啦。")
        input_mode()


def get_input_down(num):
    """
    用户输入时间,倒计时此时间,结束则关机
    :return:
    """
    import time
    try:
        if int(num) > 0:
            print("您的电脑将于", num, "秒后关机")
            time.sleep(int(num))
            function_shutdown()
        else:
            print("请输入正数。")
            input_mode()
    except:
        print("您输入的秒数不正确!")
        input_mode()


def function_shutdown():
    """
    写个功能,关机
    :return:
    """
    import os
    print("关机啦")
    write_log()
	os.system('shutdown /s /t 0')

    
def write_log():
    """
    日志,存储关机记录
    :return:
    """
    import datetime
    import os
    now = datetime.datetime.now()
    with open("关机记录.txt", "a", encoding="utf-8") as f:
        f.write(str(now)+os.linesep)


def start():
    print("********************* START *********************")
    input_mode()


if __name__ == "__main__":
    start()



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hao难懂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值