Trouble calculating difference in number of days

Hi all,

I have a requirement to calculate the difference of number of days of time stamp of a file and system date and if the difference is greater than 15 days it should prompt as previous month file otherwise current month file.

Below is the code i used and it is working fine till now. (You can try some date instead of $Publish_Time, it actually carries time stamp of a file)

Sys_Time=`date |tr -s " "|cut -d" " -f2,3,4|cut -d":" -f1,2`
Sys_Time=`date +%s -d "$Sys_Time"`
Publish_Time=`date +%s -d "$Time_Stamp_cntrl_file"`
Time_Stamp_Diff=`m_eval $Sys_Time-$Publish_Time`
Time_Stamp_Diff=`m_eval $Time_Stamp_Diff/86400`
if [[ $Time_Stamp_Diff -ge 15 ]]; then
echo "file is as of previous month"
else
echo "current month file"
fi

From today since there is change in year, time stamp difference is going negative value and hence even with Dec 1 time stamp it is prompting as current month file.

Any help plz

Your logic escapes me, and it might be quite locale dependent. Here's a somewhat easier approach:

touch -d"-15days" TMP
[ file -nt TMP ] && echo newer || echo older

Way too many steps. What shell are you using?

Example in bash:

#!/bin/bash
#get some random filename just as an example
filename=/usr
mod_time=`stat -c %Y $filename`
curr_time=`date +%s`
diff_in_sec=$(( $curr_time - $mod_time ))
diff_in_days=$(( $diff_in_sec / 86400 ))
echo Diff in days:  $diff_in_days

And I wonder how reliable the use of a time difference greater then 15 full days is in determining "previous month" given that months range from 28 to 31 days long.