Home > Net >  Python datetime converter
Python datetime converter

Time:12-21

I'm trying to write a Python function to standardize incoming timestamps to yyyy-mm-ddThh:mm /-tz offset.

Example:

def format_ts(ts):
                
    beg_format = [
        '%H:%M%a, %b %d, %Y %Z',
        '%a, %d %b %Y %H:%M:%S %z',
    ]

    end_format = '%Y-%m-%dT%H:%M %z'

    try:
        for f in beg_format:
            if datetime.strptime(ts, f):
                ts_fmt = datetime.strptime(ts, f)
                ts_fmt = ts_fmt.strftime(end_format)
                return ts_fmt

    except:
        pass


ts = [
    '08:27Sun, Dec 19, 2021 IST',
    'Sun, 19 Dec 2021 02:28:56  0000'
]

for t in ts:
    formatted_ts = format_ts(t)
    print(formatted_ts)

Issue:

  • IIRC, I shouldn't rely a failure (pass), but should rather catch the exception and handle
  • The above function iterates all timestamps through all formats (slow)
  • Unless I'm missing something, IST is not read by %Z
  • For some reason, '%a, %d %b %Y %H:%M:%S %z' is not for the correct format for ts[1]

Question: How are others handling this type of issue in Python?

CodePudding user response:

Your timezone doesn't appear to be supported in the string representation for strptime. You can use dateutil parser with a timezone to overcome this.

from dateutil import parser, tz

ts = [
    '08:27Sun, Dec 19, 2021 IST',
    'Sun, 19 Dec 2021 02:28:56  0000'
]


def format_ts(ts):
    return [parser.parse(t, tzinfos={'IST':tz.gettz('Asia/Calcutta')}) for t in ts]

format_ts(ts)

Output

[datetime.datetime(2021, 12, 19, 8, 27, tzinfo=tzfile('Asia/Calcutta')),
 datetime.datetime(2021, 12, 19, 2, 28, 56, tzinfo=tzutc())]
  • Related