GagA// 0n D nEt

It is currently Sun Sep 05, 2010 10:28 am

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Convert Epoch Time to Human Readable Time
PostPosted: Sun Apr 11, 2010 7:04 am 
Offline

Joined: Wed Jul 18, 2007 1:14 am
Posts: 193
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 Time

You 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 Format

The 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 output

You 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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
GagA// 0n D nEt © 2007, 2008 -- Powered by phpBB