1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00

use lru_cache for significant performance boost to timestamp

This commit is contained in:
Kelly Brazil
2022-02-07 08:30:11 -08:00
parent f331f53a53
commit f0a37ccf30

View File

@ -5,6 +5,7 @@ import locale
import shutil import shutil
from datetime import datetime, timezone from datetime import datetime, timezone
from textwrap import TextWrapper from textwrap import TextWrapper
from functools import lru_cache
from typing import List, Union, Optional from typing import List, Union, Optional
@ -252,7 +253,7 @@ class timestamp:
detected in datetime string. None if conversion fails. detected in datetime string. None if conversion fails.
""" """
self.string = datetime_string self.string = datetime_string
dt = self._parse() dt = self._parse_dt(self.string)
self.format = dt['format'] self.format = dt['format']
self.naive = dt['timestamp_naive'] self.naive = dt['timestamp_naive']
self.utc = dt['timestamp_utc'] self.utc = dt['timestamp_utc']
@ -260,14 +261,16 @@ class timestamp:
def __repr__(self): def __repr__(self):
return f'timestamp(string={self.string!r}, format={self.format}, naive={self.naive}, utc={self.utc})' return f'timestamp(string={self.string!r}, format={self.format}, naive={self.naive}, utc={self.utc})'
def _parse(self): @staticmethod
@lru_cache
def _parse_dt(dt_string):
""" """
Input a date-time text string of several formats and convert to Input a date-time text string of several formats and convert to
a naive or timezone-aware epoch timestamp in UTC. a naive or timezone-aware epoch timestamp in UTC.
Parameters: Parameters:
data: (string) a string representation of a date-time dt_string: (string) a string representation of a date-time
in several supported formats in several supported formats
Returns: Returns:
@ -301,7 +304,7 @@ class timestamp:
If the conversion completely fails, all fields will be None. If the conversion completely fails, all fields will be None.
""" """
data = self.string or '' data = dt_string or ''
normalized_datetime = '' normalized_datetime = ''
utc_tz = False utc_tz = False
dt = None dt = None