Posted:

Improving date parsing in blogofile

I was annoyed that (1) blogofile wants me to include a "seconds" on the manual timestamp in blog dates (I'm not writing them that quick), and (2) I'm supposed to use the exact same datetime format on all the posts.

Bah, that itches and with the source I may scratch that itch!

There are two places in post.py that parse the blog header's timestamp (for the 'date' and 'updated' fields), so I changed them to call a new parseDateTime function:

@@ -223,13 +260,11 @@
         except KeyError:
             self.guid = self.permalink
         try:
-            self.date = pytz.timezone(self.__timezone).localize(
-                datetime.datetime.strptime(y['date'], config.date_format))
+            self.date = parseDateTime(self.__timezone, y['date'])
         except KeyError:
             pass
         try:
-            self.updated = pytz.timezone(self.__timezone).localize(
-                datetime.datetime.strptime(y['updated'], config.date_format))
+            self.updated = parseDateTime(self.__timezone, y['updated'])
         except KeyError:
             pass
         try:

That new function just tries a bunch of increasingly lenient time/date parsers until one works:

def parseDateTime_original(tz, source):
    return pytz.timezone(tz).localize(
        datetime.datetime.strptime(source, config.date_format))

def parseDateTime(tz, source):
    for fmt in [config.date_format, "%Y/%m/%d %H:%M", "%Y/%m/%d %H", "%Y/%m/%d", ]:
        try:
            return pytz.timezone(tz).localize(
                datetime.datetime.strptime(source, fmt))
        except ValueError, ve: 
            pass # Ignore, and try another format

    raise PostParseException("Cannot find a value format for time data '%s'"  % source)

Comments or Questions?

Subscribe via RSS | Atom Feed