Tuesday, November 11, 2008

Bash - functions, timestamp, date diff

Bash function - command line calculator
calcfn () { echo "$*" | bc -l; }

Bash timestamp of specific date
date +%s -d 20070103

Date difference in Bash
dateOfPast='20080103'
today=`date +%Y%m%d`
timeStampToday=`date +%s -d $today`
timeStampOfPast=`date +%s -d $dateOfPast`
secondsInDay=86400
dayDiff=`echo \($timeStampToday - $timeStampOfPast\) / $secondsInDay | bc`
echo $dayDiff

Convert Unix timestamp to local date time
echo $timeStamp | perl -n -e 'chop; print localtime( ($_)[0] ) . "\n";'

Convert Unix timestamp to GMT
echo 1234567890 | perl -n -e 'chop; print gmtime( ($_)[0] ) . "\n";'
Unix Timestamp
1234567890 ... Friday The 13th.

No comments: