Tuesday, November 11, 2008

Epoch date/time convert - Python and Bash

With python you can convert epoch time to the format you want. Hint below!
>>> import os,time
>>> time.time()
1226392970.4683549
>>> time.gmtime(time.time()).tm_mday
11
>>> time.gmtime(time.time()).tm_mon
11
>>> time.gmtime(time.time()).tm_year
2008
>>>

WIth bash the same is accomplished as below.
veveo@shreyas:work$ date  +%s"  <-->  "%c
1226393170  <-->  Tuesday 11 November 2008 02:16:10 PM IST
veveo@shreyas:work$ date -d "-1 month" +%s"  <-->  "%c
1223714775  <-->  Saturday 11 October 2008 02:16:15 PM IST

veveo@shreyas:work$ aMonthBefore=`date -d "-1 month" +%s`
veveo@shreyas:work$ echo $aMonthBefore
1223714816
veveo@shreyas:work$ date --date=@$aMonthBefore +%c
Saturday 11 October 2008 02:16:56 PM IST
veveo@shreyas:work$ date --date=@1223714816 +%c
Saturday 11 October 2008 02:16:56 PM IST