Log files are sometimes written with epoch timestamps. To analyse the log file entries, we will first need to convert the time to a human readable format. In this article, we will look at a few available methods to convert the epoch time to real time or human readable time.
Epoch Time to Human Readable Time with GNU Date
On Linux or any system with the GNU date installed, just specify the “--date
” option to the date command. If the epoch time is 1414828800
, we covert it to a human readable format like this.
1 2 |
$ date --date='@1414828800' Sat Nov 1 16:00:00 SGT 2014 |
Epoch Time to Human Readable Time with Perl
The Solaris date
command does not support the --date
option. So, we will need to use perl
.
1 2 |
$ perl -e 'print scalar localtime(1414828800), "\n";' Sat Nov 1 16:00:00 2014 |
Epoch Time to Human Readable Time with Python
Python can also be used to convert the epoch time to a human readable format. On Python 2.x, use the following syntax.
1 2 |
$ python2 -c 'import time; print "%s\n" % time.strftime("%c",time.localtime(1414828800));' Sat Nov 1 16:00:00 2014 |
On Python 3.x, use the new syntax below.
1 2 |
$ python3 -c 'import time; print("%s\n" % time.strftime("%c",time.localtime(1414828800)));' Sat Nov 1 16:00:00 2014 |
Epoch Time to Human Readable Time with Awk
The final method is to use the pattern scanning and text processing tool – awk.
1 2 |
$ echo 1414828800 | awk '{print strftime("%c",$1)}' Saturday 01,November,2014 04:00:00 PM SGT |
If awk
complains about strftime
, try using gawk
instead.
1 2 |
$ echo 1414828800 | gawk '{print strftime("%c",$1)}' Saturday 01,November,2014 04:00:00 PM SGT |