Converting Epoch Seconds to Dates and Times
Problem
You need to convert Epoch seconds to a human-readable date and time.
Solution
Use the GNU date command with your desired format from Formatting Dates for Display:
EPOCH='1131173989' $ date -d "1970-01-01 UTC $EPOCH seconds" +"%Y-%m-%d %T %z" 2005-11-05 01:59:49 -0500 $ date --utc --date "1970-01-01 $EPOCH seconds" +"%Y-%m-%d %T %z" 2005-11-05 06:59:49 +0000
Discussion
Since Epoch seconds are simply the number of seconds since the Epoch (which is Midnight on January 1, 1970, also known as 1970-01-01T00:00:00), this command starts at the Epoch, adds the Epoch seconds, and displays the date and time as you wish.
If you don’t have GNU date on your system you can try one of these Perl one-liners:
EPOCH='1131173989' $ perl -e "print scalar(gmtime($EPOCH)), qq(\n);" # UTC Sat Nov 5 06:59:49 2005 $ perl -e "print scalar(localtime($EPOCH)), qq(\n);" # Your local time Sat Nov 5 01:59:49 2005 $ perl -e "use POSIX qw(strftime); print strftime('%Y-%m-%d %H:%M:%S', localtime($EPOCH)), qq(\n);" 2005-11-05 01:59:49
See Also
man date
“Date and Time String Formatting with strftime” in Appendix A
Get bash Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.