LJ Archive

Listing 3. maildate.sh Script

#!/bin/bash
# maildate.sh - calculate date as integer days
# since 1900
#          Sample input: "Feb 2 1997"
month=$1 # Received as three-character
    # abbreviation
day=$2   # Received as integer 1 through 31
year=$3  # Received as integer 1900 through 2099

# Compute days from 1900 to beginning of year
# excluding leap days
days1900=$[ ( year - 1900 ) * 365 ]

# Compute approximate leap days since 1900 ; if
# current year is divisible by four and date is
# before 1 Mar then this will overstate leap
# days by one
leapdays=$[ ( year - 1900 ) / 4 ]

# If current year is a leap year then adjust if
# before 1 Mar
if [ $[ year % 4 ] -eq 0 ] && \
   ([ $month = "Jan" ] || [ $month = "Feb" ])
then
  leapdays=$[ leapdays - 1]
fi<\n>

#Compute days into year
case $month in
   'Jan')   daysintoyear=0 ;;
   'Feb')   daysintoyear=31 ;;
   'Mar')   daysintoyear=59 ;;
   'Apr')   daysintoyear=90 ;;
   'May')   daysintoyear=120 ;;
   'Jun')   daysintoyear=151 ;;
   'Jul')   daysintoyear=181 ;;
   'Aug')   daysintoyear=212 ;;
   'Sep')   daysintoyear=243 ;;
   'Oct')   daysintoyear=273 ;;
   'Nov')   daysintoyear=304 ;;
   'Dec')   daysintoyear=334 ;;
esac
dayssince1900=$[ daysintoyear + leapdays + \
   days1900 + day ]
echo $dayssince1900
LJ Archive