python 时间对应计算

import re
import time
def parse_time(date):
    if re.match('刚刚', date):
        date = time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time()))
    if re.match('\d+分钟前', date):
        minute = re.match('(\d+)', date).group(1)
        date = time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time() - float(minute) * 60))
    if re.match('\d+小时前', date):
        hour = re.match('(\d+)', date).group(1)
        date = time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time() - float(hour) * 60 * 60))
    if re.match('昨天.*', date):
        date = re.match('昨天(.*)', date).group(1).strip()
        print(date)
        date = time.strftime('%Y-%m-%d', time.localtime(time.time()- 24 * 60 * 60)) + ' ' + date
    if re.match('\d{2}-\d{2}', date):
        date = time.strftime('%Y-', time.localtime()) + date + ' 00:00'
    return date
# print(parse_time('10-05'))
print(time.localtime()) # 当前时间
print(time.strftime('%Y-', time.localtime()))

 

posted @ 2019-10-29 10:27  睡觉了嘛  阅读(236)  评论(0编辑  收藏  举报