# SccsId[] = "%W% (USL function) %G%"
VT_name="VALIDATE_TIME"
if [ ".${SECONDS}" = "." ]; then # Bourne function already loaded?
[ ."`set|egrep '^$VT_name\(\)\{$'`" != . ] && VT_loaded=1
else # Korn or Bash shell and function already loaded?
if [ `expr "\`uname -s\`" : "[Ll][Ii][Nn][Uu][Xx]"` -eq 0 ]; then
[ ."`typeset +f|awk '/^'$VT_name'[=\(]?/'`" != . ] && VT_loaded=1
else # Linux
[ ."`typeset -F|awk '/^'$VT_name'[=\(]?/'`" != . ] && VT_loaded=1
fi
fi
if [ 0${VT_loaded} -eq 0 ]; then
#----------------------------------------------------------------------#
VALIDATE_TIME() # Function documentation located at bottom. #
#----------------------------------------------------------------------#
{ [ ."${AWK}" = . ] && { { [ -x /usr/bin/nawk ] && AWK=/usr/bin/nawk; } \
|| { [ -x /bin/gawk ] && AWK=/bin/gawk ; } \
|| { [ -x /usr/bin/awk ] && AWK=/usr/bin/awk ; }; }
#------------------------------------------------------------#
# If the following variables are not set, use these defaults.#
#------------------------------------------------------------#
: ${script_name:=`basename $0`}
: ${sp:=" "}
VT_errmsg=`$AWK -v sp="$sp" \
-v VT_name="$VT_name" \
-v script_name="$script_name" \
'BEGIN \
{
if (ARGC < 2) # Must have one argument: time (ARGC == 2).
exit_usage(2,"Insufficient args!")
#-------------------------------------------------------#
# Strip punctuation from time, keep only the digits. #
#-------------------------------------------------------#
match(ARGV[1],/[0-9][0-9]:*[0-9][0-9]:*[0-9]*/)
hhmmss = substr(ARGV[1],RSTART,RLENGTH)
gsub(/:/,"",hhmmss) # Remove colons
hhmmss_len = length(hhmmss)
#-------------------------------------------------------#
# hhmmss will be null if it has any invalid characters. #
#-------------------------------------------------------#
if (! (hhmmss_len == 6 || hhmmss_len == 4))
exit_usage(2,"Non-numeric characters in time ("ARGV[1]")!")
#-------------------------------------------------------#
# Seconds are optional, so append 00 seconds if absent. #
#-------------------------------------------------------#
if (hhmmss_len == 4)
hhmmss = hhmmss"00"
#-------------------------------#
# Parse and validate the time. #
#-------------------------------#
hh = substr(hhmmss,1,2)
mm = substr(hhmmss,3,2)
ss = substr(hhmmss,5,2)
if (hh>=0 && hh<=23 && mm>=0 && mm<=59 && ss>=0 && ss<=59)
exit 0
else
exit_usage(1,"Time variable, " ARGV[1] " is invalid!")
exit_usage(2,"UNKNOWN ERROR " $0"!")
} # E.O.BEGIN
#---------------------------------------------------------#
function exit_usage(STATUS,MSG)
#---------------------------------------------------------#
{ print sp""MSG,"\n",
"\n"sp"Usage: "VT_name" hh[:]mm[[:]ss]"
exit STATUS
}' $*`
VT_status=$?
[ $VT_status -eq 0 ] && return 0 # Ahh, the sweet smell of success
#------------------------------------------#
# Reaching here means something is amiss. #
#------------------------------------------#
if [ .${SHLIB} = . ]; then SHLIB=/usr/local/scripts; export SHLIB; fi
. $SHLIB/email_msg.sh # Function dependency
VT_ID="$script_name($VT_name)"
EMAIL_MSG "ERROR: $VT_ID" \
"$VT_errmsg"
return $VT_status
} # "VT_" prefix identifies this function's variables
fi
#======================================================================#
# D O C U M E N T A T I O N #
#======================================================================#
# #
# Author: Bob Orlando #
# #
# Date: January 14, 1997 #
# #
# Program ID: validate_time.sh #
# #
# Usage: VALIDATE_TIME hh[:]mm[[:ss]] (seconds are optional) #
# #
# Purpose: Validate the given time given according to the #
# template provided along with it. #
# #
# Globals: No global variables are assigned in this function. #
# "VT_" prefix identifies local function variables. #
# #
# Exit_status: Returns status = 2 for severe error (e.g. user #
# supplies an invalid argument). Otherwise returns #
# success or failure (0 or 1) indicating that the time #
# given us is OK or invalid. #
# #
# Calls: EMAIL_MSG library function. #
# #
# Notes: ..................................................... #
# ..................................................... #
# #
# Modified: 2004-04-02 Bob Orlando #
# v1.9 * Expand $AWK testing and assignment. #
# #
# 2004-03-03 Bob Orlando #
# v1.8 * Change set|egrep|awk to just set|egrep. #
# #
#----------------------------------------------------------------------#
|