views:

48

answers:

4

Is there a quick, one-line way to convert a unix timestamp to a date from the unix command line?

I've searched through the other posts, and most seem to be a way to convert in the context of a particular language, or to convert datestrings into unixtime. I'm looking for a way to do the reverse at the command line.

'Date' might work, except it's rather awkward to specify each element (month, day, year, hour, etc.), and I can't figure out how to get it to work properly. It seems like there might be an easier way - am I missing something?

A: 
awk 'BEGIN { print strftime("%c", 1271603087); }'
Sjoerd
+3  A: 

You can use strftime() to format UNIX timestamps. It's not available directly from the shell, but we can access it via awk. The %c specifier displays the timestamp in a locale-dependent manner.

echo $TIMESTAMP | awk '{print strftime("%c", $0)}'

# echo 0 | awk '{print strftime("%c", $0)}'
Wed 31 Dec 1969 07:00:00 PM EST

Also, date has a simpler but not widely supported syntax:

date -d @$TIMESTAMP

# date -d @0
Wed Dec 31 19:00:00 EST 1969

(From: BASH: Convert Unix Timestamp to a Date)

John Kugelman
`date -d @` is from the GNU date. *NIX' date doesn't support it.
Dummy00001
+3  A: 

date -d @1278999698 +'%Y-%m-%d %H:%M:%S' Where the number behind @ is is the number in seconds

qbi
+1  A: 

If you find the notation awkward, maybe the -R-option does help. It outpouts the date in RFC 2822 format. So you won't need all those identifiers: date -d @1278999698 -R. Another possibility is to output the date in seconds in your locale: date -d @1278999698 +%c. Should be easy to remember. :-)

qbi