Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 24356

Daniel Roy Greenfeld: TIL: Parsing messy datetimes strings

$
0
0

How to convert inconsistent datetime strings into datetime objects.

Recently I've been working on yet another rewrite of my blog, this time to FastHTML. Thanks to the power and ease of that framework, that took about 45 minutes to replicate all the web pages of my blog. Wahoo!

Alas, the atom/rss feeds took quite a bit longer.

For the atom/rss feeds I chose to use the venerable Feedgen library. The challenge there is that Feedgen is rightfully particular about the datetime objects it accepts. And over the years as this site has had 650 posts added the timestamps have become rather inconsistent in their format. On that issue I fully blame the author, who unfortunately is me.

In any case, I wrote a little Python function that handles it in a timezone aware way using the dateutils.parser() functon that I learned.

# Python stdlib
from datetime import datetime
from dateutils import parser

# You'll need to install the pytz dependency
import pytz

def convert_dtstr_to_dt(date_str: str) -> datetime:
    """
    Convert a naive or non-naive date/datetime string
    to a datetime object. Naive datetime strings are
    assumed to be in GMT (UTC) timezone.
    """
    try:
        dt = parser.parse(date_str)
        if dt.tzinfo is None:
            # If the datetime object is naive, set it to GMT (UTC)
            dt = dt.replace(tzinfo=pytz.UTC)
        return dt
    except (ValueError, TypeError) as e:
        Raise Exception(f"Error parsing date string: {e}")

Original source code here.

Note: As of publishing, this article is still on my old blog. The DNS switchover to the FastHTML version of my blog happens later this week.


Viewing all articles
Browse latest Browse all 24356

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>