Uptonian Thoughts

Quickly Convert Unix Timestamps

· ·

Inevitably, when dealing with time-related data, one will come across Unix timestamps. They’re great; there’s no guessing the timezone or trying to parse difficult formats and they’re generally extremely useful.

Except they’re not very readable to humans. I use Epoch Converter a lot when I’m dealing with time-series data, which seems to be fairly often recently. Anything involving a calendar or picking a time range also usually involves timestamps. I thought there must be an easier way to convert these into something that I can read without going to an external site and that doesn’t break down when I’m trying to do work with finnicky or non-existant network connection.

There is:

1
date -j -f "%s" "1381528800" +"%a %b %d %T %Z %Y"

An explanation: -j tells date not to try to set the system date. -f describes the format of the input - a timestamp, of course - and the input itself follows. The string after + is the output format. The output of the above is as follows.

Fri Oct 11 17:00:00 CDT 2013

You can use xargs to pipe input in. Note that the -J option might be different on systems that are not OS X.

1
echo -n "1381528800" | xargs -J {} date -j -f "%s" {} +"%a %b %d %T %Z %Y"

Like I seem to do with a lot of my utility scripts, I added a workflow to Alfred. You can get it here. Simply invoke Alfred, type “ts” followed by a space and the timestamp you want to convert. The readable date will be posted as a notification (Growl or Notification Center, configurable in the Advanced section of the Alfred settings) and copied to the clipboard.