Get the App
SLTechnology News&Howtos  ›  Development  › 

Conversion method of non-standard time in python

Shulou Source: shulou.com Published: 2022-06-01 02:48:09 11月15日 Update

Most people do not understand the knowledge points of this "python non-standard time conversion method" article, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "python non-standard time conversion method" article.

Matched structure:

Today to the day before yesterday, a few days ago, minutes ago, etc. | 2017-1-4 12:10 | 12:10 on 2017-1-4 | 12:12 on April 2, 2018 | April 2, 2018 | 2017-1-4 | 2017-1-4 | 1 amp 4 |

#-*-coding:utf-8-*-from datetime import datetime, timedeltaimport reimport timedef tz_offset (tz): res = (re.search (r "(? P [- +]) (? Pd {2}):? (? Pd {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 offsetdef parse_date (data, fmt, tz): "" time matching module Can be converted to fixed format return time string 0000-00-0000: 00:00 can match the structure | Today to the day before yesterday, a few days ago Wait a minute before | 2017-1-4 12:10 | 12:10 on 2017-1-4 | 12:12 on April 2, 2018 | April 2, 2018 | 2017-1-4 | 2017-1-4 | 1 tz 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) # several / just / today / yesterday / the day before yesterday x = data.strip () x = x.replace (u "several") "0") x = x.replace (u "just [just]", now.strftime ("% Y-%m-%d% H:%M:%S")) x = x.replace (u "today", now.strftime ("% Y-%m-%d")) x = x.replace (u "yesterday") Now_1.strftime ("% Y-%m-%d") x = x.replace (u "the day before yesterday", now_2.strftime ("% Y-%m-%d")) x = re.sub (r "[year and month]", "/", x) x = re.sub (r "[day]", ", x) x = re.sub (r" s {2,} ", r") X) # before XX res = (re.search (r "(? Pd+) s * seconds? Before ", x) or re.search (r" (? Pd+) s * minutes ago ", x) or re.search (r" (? Pd+) s * hours ago ", x) or re.search (r" (? Pd+) s* days ago ", 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) # is not the form else: # XX-XX-XX XX:XX:XX res = (re.search (r "(? Pd+) [/ -] (? Pd+) [/ -] (? Pd+) (s + (? Pd {1 Pd 2})): (? Pd {2}) (: (? Pd {2}))? X) or re.search (",") .groupdict () if res = = dict (): # match when there is no year Format XX-XX XX:XX:XX month-day hours: minutes: seconds or October 10th, 17 years: minutes res = (re.search (r "(? Pd {1) 2}) [/ -] (? Pd+) (s + (? Pd {2}): (? Pd {2}) (: (? Pd {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

Tags: Content time method standard standard time hour article format knowledge article structure value most character string that is year year month form time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Xiaomi Shulou Tech Info Apple Redmi