Kevin Vance - Since I picked on Objective C earlier, it's only fair that I now…

Entries | Archive | Friends | Friends' Friends | User Info

01:39 pm

Monday, May 18th, 2009
Previous Entry Share Next Entry

Since I picked on Objective C earlier, it's only fair that I now call out python on a serious standard library shortcoming.

Today I noticed that the timestamps were wrong on the twitter posts I pulled from my twitter RSS feed. They appeared to be in UTC instead of local time, so I figured it would be an easy one-liner to convert them.

Not so. While python does have support for time zones throughout its date/time functions, it's up to you to actually build the timezone data yourself, parsing an environment variable or the /etc/localtime file!

Luckily, there is a third party library called dateutil that will create timezone objects for you. That functionality not being in the standard library is just baffling to me.

At least now, I have some semi-sane code to take a UTC time stamp from feedparser and convert it to a local time stamp for mysql.

    # Convert from UTC to local time.
    utc_tzinfo = {'tzinfo': dateutil.tz.tzutc()}
    local = dateutil.tz.gettz('America/New York')
    published = datetime.datetime(*feed_entry.updated_parsed[:7], **utc_tzinfo)
    localdate = published.astimezone(local)

    # Strip the time zone info to appease mysql.
    naivedate = datetime.datetime(*localdate.timetuple()[:7])

Link )Reply )