10 Aug 2009

UNIX timestamps from strings

If you have a string, let’s say, “2009-02-19 02:18:00”, and you need a unix timestamp, the dateutils library is super useful. You can grab it with

sudo easy_install python-dateutil

and then you can run your strings through it like so:

dateutil.parser.parse('2009-02-19 02:18:00').strftime('%s')

Why isn’t ‘%s’ anywhere to be found in the strftime docs? That would make this somewhat easier to figure out.

Note that this returns a string containing the number, not the number itself.