From d66794c3054567725452f5780b2a076351ab3174 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Mon, 11 Nov 2024 15:00:19 -0500 Subject: [PATCH] Store-bought is fine :) --- lib/nntp/tiny/message.py | 52 ++-------------------------------------- 1 file changed, 2 insertions(+), 50 deletions(-) diff --git a/lib/nntp/tiny/message.py b/lib/nntp/tiny/message.py index 5eea5b4..aff9f59 100644 --- a/lib/nntp/tiny/message.py +++ b/lib/nntp/tiny/message.py @@ -2,6 +2,7 @@ import re import enum import datetime +from email.utils import parsedate_to_datetime from email.header import decode_header from nntp.tiny.db import DatabaseTable @@ -41,60 +42,11 @@ def each_line(text: str): yield text[start:end] break -DATE_MONTHS = { - 'jan': 1, 'january': 1, - 'feb': 2, 'february': 2, - 'mar': 3, 'march': 3, - 'apr': 4, 'april': 4, - 'may': 5, - 'jun': 6, 'june': 6, - 'jul': 7, 'july': 7, - 'aug': 8, 'august': 8, - 'sep': 9, 'september': 9, - 'oct': 10, 'october': 10, - 'nov': 11, 'november': 11, - 'dec': 12, 'december': 12 -} - -DATE_RE = [ - re.compile(r'^(?:[A-Za-z]+),\s+(?P
\d{1,2}) (?P[A-Za-z]+) (?P\d{4}) (?P\d{2}):(?P\d{2}):(?P\d{2}) (?P[+\-])(?P\d{2})(?P\d{2})$'), - re.compile(r'^(?:[A-Za-z]+),\s+(?P
\d{1,2}) (?P[A-Za-z]+) (?P\d{4}) (?P\d{2}):(?P\d{2}):(?P\d{2}) (?P[+\-])(?P\d{2})(?P\d{2}) \((?:[A-Za-z ]+)\)$'), - re.compile(r'^(?:[A-Za-z]+),\s+(?P
\d{1,2}) (?P[A-Za-z]+) (?P\d{4}) (?P\d{2}):(?P\d{2}):(?P\d{2}) (?:GMT|UTC)$'), - re.compile(r'^(?:[A-Za-z]+),\s+(?P
\d{1,2}) (?P[A-Za-z]+) (?P\d{4}) (?P\d{2}):(?P\d{2}) (?P[+\-])(?P\d{2})(?P\d{2})$'), - re.compile(r'^(?:[A-Za-z]+),\s+(?P
\d{1,2}) (?P[A-Za-z]+) (?P\d{4}) (?P\d{2}):(?P\d{2}) (?P[+\-])(?P\d{2})(?P\d{2}) \((?:[A-Z ]+)\)$'), - re.compile(r'^(?:[A-Za-z]+),\s+(?P
\d{1,2}) (?P[A-Za-z]+) (?P\d{4}) (?P\d{2}):(?P\d{2}) (?:GMT|UTC)$'), - re.compile(r'^(?P
\d{1,2}) (?P[A-Za-z]+) (?P\d{4}) (?P\d{2}):(?P\d{2}):(?P\d{2}) (?P[+\-])(?P\d{2})(?P\d{2})$'), - re.compile(r'^(?P
\d{1,2}) (?P[A-Za-z]+) (?P\d{4}) (?P\d{2}):(?P\d{2}):(?P\d{2}) (?:GMT|UTC)$'), -] - def parse_timestamp(timestamp: str): if timestamp is None or timestamp == '': return datetime.datetime.fromtimestamp(0) - for re in DATE_RE: - match = re.match(timestamp) - - if match is not None: - capture = match.groupdict() - mult = -1 if capture.get('offset_sign', '+') == '-' else 1 - tz = datetime.timezone(datetime.timedelta( - hours = mult * int(capture.get('offset_hour', 0)), - minutes = mult * int(capture.get('offset_minute', 0)) - )) - - yyyy = int(capture['yyyy']) - - if 'month' in capture: - mm = DATE_MONTHS[capture['month'].lower()] - else: - mm = int(capture['mm']) - - dd = int(capture['dd']) - hh = int(capture.get('hh', 0)) - MM = int(capture.get('MM', 0)) - ss = int(capture.get('ss', 0)) - - return datetime.datetime(yyyy, mm, dd, hh, MM, ss, 0, tz) + return parsedate_to_datetime(timestamp) class MessageState(enum.Enum): EMPTY = 0