Ubuntu – Convert seconds since epoch to a human readable string

command linedatescripts

Command date +%s produces 1403568422. Is there an easy way to convert the 1403568422 to a human readable UTC timestamp?

python -c "import datetime;print datetime.datetime.utcfromtimestamp(1403568422).isoformat()" kind of works; I can also write a short C program to do the same thing. Is there a better way?

Clarification

I do not want to make date print the current time in UTC. I want date (or other utility) to convert a given number of seconds since UTC – say 1403568422 – to a human readable format.

Background: I have these timestamps in a file and I wanted to know when exactly the things happened.

Best Answer

I think you're looking for this,

date -d @$(date +%s) +"%Y-%m-%d %H:%M:%S"

It changes the seconds since 1970-01-01 00:00:00 UTC to a human-readable string.

Example:

$ date -d @$(date +%s) +"%Y-%m-%d %H:%M:%S"
2014-06-24 06:06:54

source

Related Question