获取当前时间以后的以30分钟为单位的时间,如11:30,12:00(且只能选取9:00到21:30的数据)

def get_timeslot():
    nowtime_str = time.strftime('%Y_%m_%d %H:%M', time.localtime())

    nowtime_num = time.time()

    get_min = nowtime_str.split(':')[-1]
    nowdate = nowtime_str.split(' ')[0]
    now_hour = nowtime_str.split(' ')[1].split(':')[0]

    get_min = int(get_min)
    goaltime_str = nowdate + ' ' + now_hour + ':30'

    goaltime_num = time.mktime(time.strptime(goaltime_str,'%Y_%m_%d %H:%M'))


    if get_min < 30:
       starttime = goaltime_num
       print starttime
    else:
        starttime = goaltime_num + 1800
        print starttime
    time_slot = [starttime]
    for i in range(10):
        starttime = starttime+1800
        starttime_str = time.strftime('%Y_%m_%d %H:%M',time.localtime(starttime))
        print starttime_str
        hour = int(starttime_str.split(' ')[1].split(':')[0])
        min = int(starttime_str.split(' ')[1].split(':')[0])
        now_total_min = hour*60+min
      #判断时间是否在9:00-21:30之间
        if  now_total_min>=540 and now_total_min <=1290:
            time_slot.append(starttime)
    print time_slot


get_timeslot()

猜你喜欢

转载自blog.csdn.net/qq_35958094/article/details/79501081
今日推荐