Monday, April 25, 2011

Comparing Dates In Epoch Seconds

I put together this script to check the date in a table, convert it to epoch seconds, and then check todays date minus seven days in epoch seconds, to make it possible to do a comparison (greater than) for the dates. So if a date is within the last week, it is returned.

Not so necessary for just seven dates as they could each be declared individually, but for any greater date range would be useful, as converting to epoch seconds means that the two dates can be compared as to which is greater, with later dates obviously being greater than....



#!/bin/sh

#GET CURRENT DATE IN EPOCH SECONDS#

export todaysdate=`date +%s`

#7DAYS AGO IS -604800 SECONDS#
sevendaysago="$(( $todaysdate - 604800 ))"

#QUERY DATABASE TO GET MERCHANT ID ANF LOOP THROUGH EACH#

merchant=`
mysql -u username --password=password << eof
use databasename;
SELECT merchantid FROM tablename;
eof`


for ARG1 in $merchant
do

#QUERY DATABASE TO GET DATE DATA WAS ALTERED FOR EACH MERCHANT#
date=`
mysql -u username --password=password << eof
use database;
SELECT lastUpdated FROM tablename WHERE merchantid='$ARG1';
eof`

for ARG2 in $date
do

#IGNORE COLUMN NAME IN RETURNED RESULT#
if [ "$ARG2" != "lastUpdated" ]; then
#CONVERT DATE TO EPOCH SECONDS#
export recordeddate=`date -d "$ARG2" +%s`


#COMPARE RECORDED DATE TO DATE SEVEN DAYS AGO#
if [ $recordeddate -gt $sevendaysago ]; then

#DO WHATEVER#
echo $ARG1 data changed in the last seven days on $ARG2

fi
fi
done
done


Linux Questions

No comments: