Kurt McKee

lessons learned in production

Archive

Hey there! This article was written in 2008.

It might not have aged well for any number of reasons, so keep that in mind when reading (or clicking outgoing links!).

Python's time module

Posted 12 June 2008 in programming and python

So I've been working like a fiend to import all of my comments from around the internet. It has been a herculean effort because almost everything has to be done manually. One comment here, another there, and no uniform way to extract those comments.

Two sites, however, made comment retrieval easy: Xanga and Slashdot. Both offer fine lists of my old comments. I first scraped Xanga for all of my old comments, but stumbled when converting the dates and times of my comments into a usable format: while Xanga outputs dates and times in the form 1/1/2006 1:22 AM, I need output in the form 2006-01-01 01:22:00 for import into the comment-tracking software I'm writing.

I hacked together some trivial regular expressions and had a solution in about ten minutes. However, I then wondered if there was software that could convert the dates and times for me. Surprise, it's already included in Python! Python's time module includes a function called 'strptime' that does all of the heavy interpretation and standardization of the formats. With that in mind, it'll be no trouble converting Slashdot's comments' dates and times (like Sunday January 01 2006, @01:22AM)!

The moral of the story is, do a little searching before writing your own software; somebody else may have already written a solution for you.

☕ Like my work? I accept tips!