Python runs a program for a specific period of time

import requests
import time
from datetime import datetime, timedelta
'''
死循环程序在每天8:45~23:45期间,每隔1小时发送信息
代码逻辑:=======
死循环运行:
    判断当前时间 in 今天8:45~今天23:45   
        判断当前时间 before xx:45
            程序休眠至 xx:45, 再运行send_msg()
        else:当前时间 after xx:45
            程序休眠至 下一个xx:45, 再运行send_msg()
            
    else:
        判断当前时间 before 今天8:45
            程序休眠至 今天8:45, 再运行send_msg()
        else:当前时间 after 今天23:45
            程序休眠至 明天8:45, 再运行send_msg()
    
'''

def send_msg():
    print(requests.get('https://www.baidu.com'))
    time.sleep(60)

while True:
    today_now = datetime.now()
    print(today_now)
    today_at_0845 = today_now.replace(hour=8, minute=45, second=0)
    today_at_2345 = today_now.replace(hour=23,minute=45, second=0)
    if today_at_0845 <= today_now <= today_at_2345:
        diff45 = today_now.replace(minute=45, second=0) - today_now
        if 0 <= diff45.days:
            print('before xx:45, sleep....')
            time.sleep(diff45.seconds)
        else:
            print('after xx:45, sleeping...')
            diff45 = today_now.replace(hour=today_now.hour + 1, minute=45, second=0) - today_now
            time.sleep(diff45.seconds)
    else:
        print('no in', '='*30)
        diff = today_at_0845 - today_now
        if 0 <= diff.days:
            print('before today_at_0845, sleep...')
            time.sleep(diff.seconds)
        else:
            print('after today_at_2345, sleep...')
            tomorrow_at_0845 = today_now.replace(hour=8, minute=45, second=0) + timedelta(days=1)
            diff = tomorrow_at_0845 - today_now
            time.sleep(diff.seconds)
    send_msg()


def sleeping():
    '''
    对上面的代码进行封装。
    用于实现每天的定时拨测:从8:45到23:45,每隔一个小时
    :return:
    '''
    begin_hour = 8
    minute_point = 45
    end_hour = 23
    per_hour = 1

    today_now = datetime.now()
    print(today_now)
    today_at_0845 = today_now.replace(hour=begin_hour, minute=minute_point, second=0) # begin time
    today_at_2345 = today_now.replace(hour=end_hour, minute=minute_point, second=0) # end time
    if today_at_0845 <= today_now <= today_at_2345:
        diff45 = today_now.replace(minute=minute_point, second=0) - today_now
        if 0 <= diff45.days:
            print(f'before xx:{minute_point}, sleep....')
            time.sleep(diff45.seconds)
        else:
            print(f'after xx:{minute_point}, sleeping...')
            # diff45 = today_now.replace(hour=today_now.hour + 1, minute=minute_point, second=0) - today_now
            diff45 = today_now.replace(minute=minute_point, second=0) - today_now + timedelta(hours=per_hour)
            time.sleep(diff45.seconds)
    else:
        print('no in', '=' * 30)
        # my_logger.info('no in', '=' * 30)
        diff = today_at_0845 - today_now
        if 0 <= diff.days:
            print(f'before today_at_08{minute_point}, sleep...')
            time.sleep(diff.seconds)
        else:
            print(f'after today_at_23{minute_point}, sleep...')
            tomorrow_at_0845 = today_at_0845 + timedelta(days=1)
            diff = tomorrow_at_0845 - today_now
            time.sleep(diff.seconds)

Guess you like

Origin blog.csdn.net/Cameback_Tang/article/details/120574464