age in months for yyyy-mm-dd format

Hi,

I am trying to write a sh script which will give difference in given time to present time in months.

I get date from a script like

 
infoscript.sh | awk '{ print $3}' 

where infoscript is a custom script and gives date in yyyy-mm-dd format
ex: 2010-04-12

Now, need to do something like this

awk '{ print $3}' | { get age in months so that if its more than 3 months, I will run another script, say backup.sh}

I tried to search for date and age threads but there are too many of them and confusing me. Is there a simple approach to solve this.

Thank you for your time and response.

Determining a difference between dates is a common subject because it's so difficult on so many systems and so easy on a very few.

What's your system?

Centos 6.0

Assuming you have a version of date that supports +%s and -d (GNU date seems to do this), then this should work in either kshell or bash:

# returns success if date passed in is more than three months in the past
# assumes the date is in the form yyyy-mm-dd  mm and dd can be single digits.
function is_old
{
    if [[ -z $_now ]]                # "static" values set on first call
    then
        _dim=( 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31 )        # days in months; yes -- 15 months!!

        _now=$(date +%s )
        _m=$(date +%Y%m )              # current month
        _y=${_m:0:4}


        if (( _y % 4 == 0 ))         # works until 2100, so keep it simple
        then
            _dim[4]=29               # leap year this year
        fi
        (( _m = ${_m:4:2} + 2 ))      # proper index into dim

    fi

    typeset tdate=$(date -d $1 +%s )   # convert date to check into seconds past epoch
    typeset cutoff=$((  _now - (86400 * ( (_dim[_m-1] + _dim[_m-2] + _dim[_m-3]))) ))

    return $(( tdate > cutoff ))
}

# simple loop to read date from stdin
while read d
do
    if is_old $d
    then
        echo "old: $d"
    else
        echo "not: $d"
    fi
done

If you are not as particular about what "three months" means, and could accept calling something old if it is simply more than 90 days old, then you could use the function below --

function is_old
{
    return $((  $(date -d $1 +%s ) > $((  $(date +%s )  - (86400 * 90) ))  ))
}

It has 90 days hard coded, but you could change it to anything.

1 Like

Thank you for your response. I was looking for a solution similar this but my system is not supporting "date + %s"

Centos 6.0 not supporting date '+%s' , that would be a new one. Could you verify this one more time?

I just verified, the dev box I am working on is CentOS6.2 but this script will be run on CentOS6.0.

CentOS6.2 is giving me this

-bash: +%s: command not found

This is not a message one would expect, because date would normally report something, not the shell itself and +%s is not a command in this cases but a parameter. So why does your shell see this a command?

  • What is the exact command that you typed?
  • Are you running date '+%s' locally on your dev box??
  • What is the output of type date ?
  • What is the output of date --version ?

thanks for the responses, I was able to run this script from mac and will figure out what I am missing on Centos.