isBetween

- Determine if the last modification date/time on a file is between two specified date/time values. The date/time values required consist of a beginning and an ending date/time, and are expected to be in the following format: YYYY-MM-DD HH:MM:SS



#!/usr/bin/ksh93
################################################################
function usagemsg_isBetween {
  print "
Program: isBetween

The function \"isBetween\" will determine if the modification date/time
of a file is between two specified dates.

Usage: ${1##*/} [-?vVq] [-b 'BeginDate'] [-e 'EndDate'] FileName

  Where:
    -b 'YYYY-MM-DD HH:MM:SS' = Beginning Date/Time (Default:Current)
    -e 'YYYY-MM-DD HH:MM:SS' = Ending Date/Time (Default:Current)
    FileName = File to examine to determine if the modification date/time
               is between the specified beginning and ending date/time's.
    -v = Verbose mode
    -V = Very Verbose Mode
    -q = Quiet mode

Author: Dana French (dfrench@mtxia.com) Copyright 2004
\"AutoContent\" enabled
"
}
################################################################
#### 
#### Description:
#### 
#### This function will determine if the last modification
#### date/time on a file is between two specified date/time
#### values.  The date/time values required consist of a
#### beginning and an ending date/time, and are expected to
#### be in the following format: YYYY-MM-DD HH:MM:SS
#### 
#### Where YYYY represents the four digit year, MM is the 2
#### digit month number (1-12), DD is the 2 digit day of the
#### month (1-31), HH is the 2 digit hour of the day on a 24
#### hour clock (0-23), MM is the 2 digit minute value
#### (0-59), and SS is the 2 digit seconds value (0-59). 
#### 
#### Assumptions:
#### 
#### If the beginning date/time or ending date/time is not
#### specified, the current date/time is used as the default
#### value. 
#### 
#### Dependencies:
#### 
#### This function requires execute permission for the "ls"
#### and "date" commands.
#### 
#### Products:
#### 
#### This function examines one file at a time and produces a
#### return code of zero (0) if the last modification time of
#### the file is between or equal to the specified date/time
#### values.  A return code of one (1) is produced if the
#### last modification time of the file is not between the
#### specified date/time values.  A return code of two (2) is
#### produced if the file to examine is not found.  And a
#### return code of three (3) is produced if the date
#### information obtained from the "ls -l" output is invalid. 
#### 
#### The VERBOSE option causes this function to output the
#### beginning time, ending time, file datestamp, and file
#### name.  The QUIET option causes this function to perform
#### it's work quietly and not produce any output.  If both
#### the VERBOSE and QUIET flags are specified at run time,
#### the VERBOSE flag has precedence.
#### 
#### Configured Usage:
#### 
#### This command is a shell function that can be
#### incorporated into other shell scripts, used as part of a
#### function library, or as a stand-alone command.
#### 
#### Details:
#### 
################################################################
function isBetween {
  typeset TRUE="1"
  typeset FALSE="0"
  typeset EXITCODE="0"
  typeset QUIET="${FALSE}"
  typeset VERBOSE="${FALSE}"
  typeset VERYVERB="${FALSE}"
  typeset TZ="GMT"
  typeset LC_ALL="C"
  export TZ LC_ALL
  typeset CURDT="$( date +'%Y-%m-%d %H:%M:%S' )"
  typeset MIN=$( printf '%(%s)T' "${CURDT}" ) # (GMT)
  typeset MAX=$( printf '%(%s)T' "${CURDT}" ) # (GMT)
  typeset FTYPE
  typeset FNAME
  typeset X1 X2 X3 X4
  typeset D1 D2 D3

  while getopts ":vVqb:e:" OPTION
  do
      case "${OPTION}" in
          'q') QUIET="${TRUE}";;
          'v') VERBOSE="${TRUE}";;
          'V') VERYVERB="${TRUE}";;
          'b') MIN=$( printf '%(%s)T' "${OPTARG}" );;
          'e') MAX=$( printf '%(%s)T' "${OPTARG}" );;
          '?') usagemsg_isBetween "${0}" && exit 1 ;;
      esac
  done
   
  shift $(( ${OPTIND} - 1 ))
  
  typeset TARGFILE="${1}"

  trap "usagemsg_isBetween ${0}" EXIT
  if (( MAX < MIN ))
  then
    (( QUIET == FALSE )) &&
        print -u 2 "ERROR: MIN (${MIN}) is greater than MAX (${MAX})"
    exit 2
  fi
  if [[ "_${TARGFILE}" = "_" ]]
  then
    (( QUIET == FALSE )) &&
        print -u 2 "ERROR: Filename to examine not specified"
    exit 3
  fi
  trap "-" EXIT
  
  (( VERYVERB == TRUE )) && set -x

################################################################

#### Obtain a long listing of the file attributes for the
#### target file, and read them into shell variables. 

 if ! ls -ld "${TARGFILE}" 2>/dev/null | IFS=$' \t\n' read -r FTYPE X1 X2 X3 X4 D1 D2 D3 FNAME
 then
    (( QUIET == FALSE )) &&
        print -u 2 "ERROR: Specified file not found: ${TARGFILE}"
    return 2
 fi

#### Verify each date variable contains a value, if not issue
#### an error message and return with an unsuccessful return
#### code. 

 if [[ "_${D1}" = "_" || "_${D2}" = "_" || "_${D3}" = "_" ]]
 then
   (( QUIET == FALSE )) &&
       print -u 2 "ERROR: Invalid date information for file ${FNAME}"
   return 3
 fi

#### Using the date values read in from the long listing
#### output for the target file, convert the date into
#### seconds from the last epoch using the "printf" built-in
#### command. 

 DATESTAMP=$( printf '%(%s)T' "${D1} ${D2} ${D3}" )

#### Check the file type of the target file and if it is a
#### symbolic link, delete the pointer information from the
#### end of the filename.

 if [[ "${FTYPE}" = l* ]]
 then
   FNAME="${FNAME/ -> */}"
 fi

#### If the VERBOSE flag is TRUE, then output the beginning
#### time, ending time, file timestamp, and the file
#### name, regardless of whether or not it falls between the
#### beginning and ending date/time values.

 if (( VERBOSE == TRUE ))
 then
     print -- "# Begin\t\tEnd\t\tFile Date\tFile Name"
     print -- "${MIN}\t${MAX}\t${DATESTAMP}\t${FNAME}"
 fi

#### Check the datestamp of the target file to see if falls
#### between the specified beginning time and ending time.

 if (( DATESTAMP >= MIN )) && (( DATESTAMP <= MAX ))
 then

#### If the last modification date of the target file is
#### between the specified date/time values, the value of the
#### VERBOSE option is FALSE, and the QUIET option is FALSE,
#### output the filename.  Otherwise, if the QUIET option is
#### TRUE, output nothing. 

   if (( VERBOSE == FALSE ))
   then
       (( QUIET == FALSE )) &&
           print -r -- "${FNAME}"
   fi

#### If the last modification date of the target file is
#### between the specified times, return from this
#### function with a successful return code.

   return 0
 fi

#### If the last modification date of the target file is
#### NOT between the specified times, return from this
#### function with an unsuccessful return code.

 return 1
}
################################################################

isBetween "${@}"