We have instances where we need to use the epoch time in the scripts and there are at times when we need to understand what an epoch time is actually referring to in terms of human understandable value.
Get current Epoch/UNIX/POSIX TimeYou can get the Current Epoch time using the following command:
Quote:
[root@gagan ~]# date +%s
Output:
Code:
1270968638
Convert a Epoch Time to Human Readable FormatThe following is the syntax which will work:
Code:
date -d @epoch
Continuing the above example the following will be command to convert POSIX/UNIX Time to Human readable format:
Quote:
[root@gagan ~]# date -d @1270968638
Output:
Code:
Sun Apr 11 12:20:38 IST 2010
Note: The above feature of date command (use of @) will work only on the latest version of date command (part of coreutils v5.3.0 and above).
For earlier version we can use the following syntax:
Code:
date -d "1970-01-01 epoch sec GMT"
Please note that GMT is used above to provide the output relative to the current GMT offset for the system. If GMT is not used, the time displayed will be for GMT time zone.
The example for the later possibility is given below.
Quote:
[root@gagan ~]# date -d "1970-01-01 1270968638 sec GMT"
Formatted outputYou can certainly use the formatting options available for the date command to get the output in the format you desire.
So the following can be an example:
Quote:
[root@gagan ~]# date -d @1270968638 +"%Y-%m-%d %H:%M"
Output:
Code:
2010-04-11 12:20