Pages

Sunday, August 17, 2014

A little date magic

Convert A Unix Timestamp to Human Readable Localtime

Note: the version of date used here is GNU date, that is supplied with most free unices. It may also be supplied with some proprietary unices as an optional, third party or perhaps standard option. GNU date is far more powerful and featureful than most of the proprietary date commands I've experienced, so if you're on a system without GNU date, YMMV.

Unix time is stored in seconds since 00:00:00 1 Jan 1970 (it may actually be 00:00:01 1 Jan 1970, not 100% sure and it doesn't make much difference for most operations).
00:00:00 1 Jan 1970 is referred to as the Unix epoch, the time unix started to exist.
This method of  measuring time is unsurprisingly  referred to as "Unix epoch time" or "Unix time". Less accurately (because there is more than one kind of system) "system time".

Unix Epoch Time to Human Readable System Local

To take seconds since 00:00 1 Jan 1970 (i.e. unix epoch time) and convert it to the current system local time:
date -d @<seconds> since 00:00 1 Jan 1970>

e.g.:
[root@myhost mydir]# date -d @1408329090 
Mon Aug 18 12:01:30 CST 2014


System Local Time as Unix Epoch Time

To print the local system time in Unix epoch time:
date +%s

UMT

[root@ckan-db .ssh]# date
Mon Aug 18 12:44:39 CST 2014
[root@ckan-db .ssh]# date -u
Mon Aug 18 03:14:41 UTC 2014
[root@ckan-db .ssh]#




No comments:

Post a Comment