首页 > 编程语言 > 详细

python实现php中的strtotime

时间:2021-07-19 00:28:23      阅读:23      评论:0      收藏:0      [点我收藏+]

       在python中,处理时间字符串的解析,可以用第三方库dateparser, 在不使用第三方库的情况下,是否可以实现解析呢?

stackoverflow 中找到了一个方法,那就是在本地时间戳的基础上,加上通过正则表达式匹配出来的年月日时分秒的值所对应

的秒数计算出时间字符串的时间戳。在此基础上,我进行了扩展,可以支持更多的时间字符串表达。

import time, re

def strtotime(string):
    unit_to_second = dict(
        minute=60, hour=3600, day=86400, week=604800, year=(365 * 86400) + 86400
    )
    
    accumulator = time.time()

    delta = 0
    plus = True
    string = string.strip()
    if (string == ‘today‘) or (string == ‘yesterday‘) or (‘midnight‘ in string) or (‘this month‘ in string):
        time_struct = time.localtime(accumulator)
        timestr_day = time.strftime(‘%Y:%m:%d %X‘, time_struct)
        pattern = re.compile(r‘\d+‘)
        year, month, day, hour, minute, second = pattern.findall(timestr_day)
        accumulator -= int(second)
        accumulator -= int(minute) * unit_to_second[‘minute‘]
        accumulator -= int(hour) * unit_to_second[‘hour‘]
    if string == ‘this month‘ :
        day = int(day) - 1
        string = ‘-‘ + str(day) + ‘ day‘
    if ‘yesterday‘ in string:
        string = ‘-1 day‘ 
    if string == ‘the day before yesterday‘:
        string = ‘-2 days‘    
    if string.startswith(‘in ‘) > 0 :
        string = string.replace(‘in ‘, ‘‘, 1)
        pass
    if string.startswith(‘+‘) > 0 :
        string = string.strip(‘+‘)
        pass
    if string.startswith(‘-‘):
        plus = False
        string = string.strip(‘-‘)
    if ‘ago‘ in string :
        plus = False
        string = string.replace(‘ ago‘, ‘‘, 1)
    string = string.strip()
    for match in re.finditer(r"(\d+) (minute|hour|day|week|year)", string):
        num, unit = match.groups()
        delta += float(num) * unit_to_second[unit]
    if plus :
        accumulator += delta
    else :
        accumulator -= delta
    return accumulator
    
string = ‘-10 days‘    
timestamp = strtotime(string)    
time_struct = time.localtime(timestamp)
timestr_day = time.strftime(‘%Y%m%d %X‘, time_struct)  
print(timestr_day)    #20210708 22:42:27

string = ‘yesterday‘    
timestamp = strtotime(string)    
time_struct = time.localtime(timestamp)
timestr_day = time.strftime(‘%Y%m%d %X‘, time_struct)  
print(timestr_day)   #20210717 00:00:00
  
string = ‘15 days ago midnight‘    
timestamp = strtotime(string)    
time_struct = time.localtime(timestamp)
timestr_day = time.strftime(‘%Y%m%d %X‘, time_struct)  
print(timestr_day)   #20210703 00:00:00

 

python实现php中的strtotime

原文:https://www.cnblogs.com/wallywl/p/15028130.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!