The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #3 (permalink)  
Old 04-23-2009
jim mcnamara jim mcnamara is online now Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,802
This script uses Julian Days. This is overkill if you have GNU date (Linux)

Code:
#!/bin/ksh

jd=0
fraction=0

JD() # $1==day $2=month $3=year
{
   
    day=$1
    month=$2
    year=$3
	jd=0
	if [[ $month -lt 3 ]] ; then
	   year=$(( $year - 1 ))
	   month=$(( $month + 12 ))
	fi
	a=$(( $year / 100 ))
	b=$(( $a / 4 ))
	c=$(( 2 - $a + $b ))
	e=$(echo "365.25 * ( 4716 + $year ) " | bc -l)
    f=$(echo "30.6001 * ( $month + 1)" | bc -l )
    if [[ $year -gt 1581 ]] ; then
        jd=$(( $c + $day + $e + $f ))
    	jd=$( echo "$jd  - 1524.5" | bc -l )
    fi
    export jd
}


hr=$(( $(date "+%H") * 3600 ))
min=$(( $(date "+%M") *60   ))
sec=$(date "+%S")
fraction=$( echo "$hr/86400 + $min/86400  + $sec/86400"| bc -l)

JD $( date "+%d %m %Y" )
today=$( echo "$jd + $fraction" | bc -l)

yr=$( date "+%Y" )
JD 25 12 $yr
xmas=$jd    
difference=$(( $xmas - $today ))

if [[ $difference -lt 0 ]] ; then 
	yr=$(( $yr + 1 ))
	JD 25 12 $yr
	xmas=$jd
    difference=$(( $xmas - $today ))
fi
echo "Days to Christmas: $difference"