python非標(biāo)準(zhǔn)時(shí)間的轉(zhuǎn)換
可匹配結(jié)構(gòu):
今天~前天, 幾天前, 分鐘秒前等 | 2017-1-4 12:10 | 2017/1/4 12:10 | 2018年4月2日 12:12 | 2018年4月2日 | 2017-1-4 | 2017/1/4 | 1/4 |
# -*- coding:utf-8 -*- from datetime import datetime, timedelta import re import time def tz_offset(tz): res = (re.search(r'(?P<F>[-+])(?P<H>\d{2}):?(?P<M>\d{2})', tz) or re.search('', '')).groupdict() offset = (1 if res.get('F', '+')=='+' else -1) * timedelta( hours = int(res.get('H', 0)), minutes = int(res.get('M', 0))) return offset def parse_date(data, fmt, tz): """ 時(shí)間匹配模塊,可轉(zhuǎn)化為固定格式 返回時(shí)間字符串 0000-00-00 00:00:00 可匹配結(jié)構(gòu) |今天~前天, 幾天前,分鐘秒前等 | 2017-1-4 12:10 | 2017/1/4 12:10 | 2018年4月2日 12:12 | 2018年4月2日 | 2017-1-4 | 2017/1/4 | 1/4 | """ offset = tz_offset(tz) if fmt == 'auto': now = (datetime.utcnow() + timedelta(hours=8)).replace(microsecond=0) + offset now_1 = now - timedelta(days=1) now_2 = now - timedelta(days=2) # 幾/剛/今天/昨天/前天 x = data.strip() x = x.replace(u'幾', ' 0 ') x = x.replace(u'剛[剛才]', now.strftime(' %Y-%m-%d %H:%M:%S ')) x = x.replace(u'今天', now.strftime(' %Y-%m-%d ')) x = x.replace(u'昨天', now_1.strftime(' %Y-%m-%d ')) x = x.replace(u'前天', now_2.strftime(' %Y-%m-%d ')) x = re.sub(r'[年月]', '/', x) x = re.sub(r'[日]', ' ', x) x = re.sub(r'\s{2,}', r' ', x) # XX前 res = (re.search(r'(?P<S>\d+)\s*秒鐘?前', x) \ or re.search(r'(?P<M>\d+)\s*分鐘前', x) \ or re.search(r'(?P<H>\d+)\s*小時(shí)前', x) \ or re.search(r'(?P<d>\d+)\s*天前', x) \ or re.search('', '')).groupdict() if res: dt = now - timedelta( days=int(res.get('d', 0)), hours=int(res.get('H', 0)), minutes=int(res.get('M', 0)), seconds=int(res.get('S', 0)) ) # 不是幾天前分鐘前的形式 else: # XX-XX-XX XX:XX:XX res = (re.search(r'(?P<Y>\d+)[/-](?P<m>\d+)[/-](?P<d>\d+)(\s+(?P<H>\d{1,2}):(?P<M>\d{2})(:(?P<S>\d{2}))?)?', x) or re.search('', '')).groupdict() if res == dict(): # 匹配沒(méi)有年份的時(shí)候,格式 XX-XX XX:XX:XX 月-日 時(shí):分:秒 或 17年10月10日 時(shí):分:秒 res = (re.search( r'(?P<m>\d{1,2})[/-](?P<d>\d+)(\s+(?P<H>\d{2}):(?P<M>\d{2})(:(?P<S>\d{2}))?)?', x) or re.search('', '')).groupdict() if res: Y = res.get('Y', now.year) Y = "20" + Y if len(str(Y)) == 2 else Y m = res.get('m', now.month) d = res.get('d', now.day) H = res.get('H', now.hour) M = res.get('M', now.minute) S = res.get('S', 0) dt = datetime( year=int(Y) if Y != None and 1987 <= int(Y) <= now.year else now.year, month=int(m) if m != None else now.month, day=int(d) if d != None else now.day, # 如果沒(méi)有時(shí)分秒,則被認(rèn)定為00:00:00 hour=int(H) if H != None else 0, minute=int(M) if M != None else 0, second=int(S) if S != None else 0 ) else: # 1970-01-01 00:00:00 # dt = datetime.utcfromtimestamp(0)+offset return "" # 時(shí)間可能超過(guò)當(dāng)前時(shí)間,若超過(guò)則減去一年 if int(time.mktime((dt - offset).timetuple())) > int(time.time()): # 時(shí)間超過(guò)當(dāng)前時(shí)間,減去一年 delta = timedelta(days=-365) real_time = (dt - offset) + delta real_time = real_time.strftime("%Y-%m-%d %H:%M:%S") else: real_time = (dt - offset).strftime("%Y-%m-%d %H:%M:%S") return real_time if __name__ == '__main__': print(parse_date('2秒前', 'auto', '')) print(parse_date('2分鐘前', 'auto', '')) print(parse_date('2小時(shí)前', 'auto', '')) print(parse_date('昨天 00:30', 'auto', '')) print(parse_date('07-20', 'auto', ''))
到此這篇關(guān)于python非標(biāo)準(zhǔn)時(shí)間的轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)python非標(biāo)準(zhǔn)時(shí)間內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)遍歷目錄的方法【測(cè)試可用】
這篇文章主要介紹了Python實(shí)現(xiàn)遍歷目錄的方法,涉及Python針對(duì)目錄與文件的遍歷、判斷、讀取相關(guān)操作技巧,需要的朋友可以參考下2017-03-03python使用多線程備份數(shù)據(jù)庫(kù)的步驟
在日常服務(wù)器運(yùn)維工作中,備份數(shù)據(jù)庫(kù)是必不可少的,剛工作那會(huì)看到公司都是用shell腳本循環(huán)備份數(shù)據(jù)庫(kù),到現(xiàn)在自己學(xué)習(xí)python語(yǔ)言后,利用多進(jìn)程多線程相關(guān)技術(shù)來(lái)實(shí)現(xiàn)并行備份數(shù)據(jù)庫(kù),充分利用服務(wù)器資源,提高備份速度。2021-05-05詳解使用python爬取抖音app視頻(appium可以操控手機(jī))
這篇文章主要介紹了詳解使用python爬取抖音app視頻(appium可以操控手機(jī)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Python獲取接口請(qǐng)求耗時(shí)的方法詳解
你想知道我們請(qǐng)求一個(gè)url的時(shí)候,握手和請(qǐng)求資源分別占用多長(zhǎng)時(shí)間么?今天我們就來(lái)使用python寫(xiě)個(gè)小案例來(lái)看看,感興趣的可以跟隨小編一起了解一下2023-04-04Python如何獲得百度統(tǒng)計(jì)API的數(shù)據(jù)并發(fā)送郵件示例代碼
這篇文章主要給大家介紹了關(guān)于Python如何獲得百度統(tǒng)計(jì)API的數(shù)據(jù)并發(fā)送郵件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01雙向RNN:bidirectional_dynamic_rnn()函數(shù)的使用詳解
今天小編就為大家分享一篇雙向RNN:bidirectional_dynamic_rnn()函數(shù)的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01使用Python對(duì)mongo數(shù)據(jù)庫(kù)中字符串型正負(fù)數(shù)值比較大小
這篇文章主要介紹了使用Python對(duì)mongo數(shù)據(jù)庫(kù)中字符串型正負(fù)數(shù)值比較大小,2023-04-04