# SccsId[] = "%W% (USL function) %G%"
              GPY_name="GET_PREV_YYYYMMDDHH"
              if [ ".${SECONDS}" = "." ]; then # Bourne function already loaded?
                 [ ."`set|egrep '^$GPY_name\(\)\{$'`" != . ] && GPY_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 '/^'$GPY_name'[=\(]?/'`" != . ] && GPY_loaded=1
                 else # Linux
                    [ ."`typeset -F|awk '/^'$GPY_name'[=\(]?/'`" != . ] && GPY_loaded=1
                 fi
              fi
              if [ 0${GPY_loaded} -eq 0 ]; then
              #----------------------------------------------------------------------#
              GET_PREV_YYYYMMDDHH() # 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 [ .${SHLIB} = . ]; then SHLIB=/usr/local/scripts; export SHLIB; fi
                if [ .${SHBIN} = . ]; then SHBIN=/usr/local/bin;     export SHBIN; fi

                . $SHLIB/email_msg.sh # Calls $SHLIB/exit.sh

                #------------------------------------------------------------#
                # If the following variables are not set, use these defaults.#
                #------------------------------------------------------------#
                : ${script_name:=`basename $0`}
                : ${sp:="                    "}

                GPY_ID="$script_name($GPY_name)"

                #--------------------------------------------------------------------#
                # Must have either dateplus (compiled C executable) or dateplus.awk. #
                #--------------------------------------------------------------------#
                if [ ".$dateplus" = "." ]; then
                   if [ -x $SHBIN/dateplus ]; then
                      dateplus=$SHBIN/dateplus
                   elif [ -f $SHLIB/dateplus.awk ]; then
                      dateplus="$AWK -f $SHLIB/dateplus.awk -- "
                   else
                      EMAIL_MSG "ERROR: $GPY_ID"                          \
                        "Unable to locate $SHBIN/dateplus (C executable)" \
                        "${sp}or $SHLIB/dateplus(.awk)* !"                \
                        "${sp}$GPY_ID terminated."
                      return 1
                   fi
                fi

                GPY_yyyymmddhh=`$AWK -v sp="$sp"             \
                                     -v dateplus="$dateplus" \
                                     -v GPY_name=$GPY_name   \
                  'BEGIN \
                   {
                     if (ARGV[1] == "")
                       exit_usage("Insufficient arguments!")
                     if (length(ARGV[1]) != 10)
                       exit_usage("yyyymmddhh argument not 10 digits!")
                     if (ARGV[1] !~ /^[0-9]+$/)
                       exit_usage("yyyymmddhh argument not numeric!")

                     yyyymmdd = substr(ARGV[1],1,8)
                     yyyy     = substr(ARGV[1],1,4)
                     mm       = substr(ARGV[1],5,2)
                     dd       = substr(ARGV[1],7,2)
                     hh       = substr(ARGV[1],9,2)

                     #-----------------------------------------------#
                     # Exit with fail status on invalid hour.        #
                     #-----------------------------------------------#
                     if (hh < "00" || hh > "23")
                       exit_usage("Invalid hour ("hh")!")

                     #-----------------------------------------------#
                     # Set up month_table array for error reporting. #
                     #-----------------------------------------------#
                     split("January February March"     \
                           "April   May      June "     \
                           "July    August   September" \
                           "October November December", month_table)

                     #-----------------------------------------------#
                     # Rudimentary validation of mm and dd           #
                     #-----------------------------------------------#
                     mm+=0
                     if (mm==1||mm==3||mm==5||mm==7||mm==8||mm==10||mm==12)
                       mm_days=31
                     else if (mm==4||mm==6||mm==9||mm==11)
                       mm_days=30;
                     else if (mm==2)
                       mm_days=((yyyy%4==0&&yyyy%100!=0)||(yyyy%400==0))?29:28
                     else
                       exit_usage(mm" is an invalid month number!")

                     if (dd > mm_days)
                       exit_usage("Day "dd" invalid for "month_table[mm]", "yyyy"!")

                     #-----------------------------------------------#
                     # If backing from 00 to 23, get preceding day.  #
                     #-----------------------------------------------#
                     if (hh == "00")
                     {
                       hh = "23"
                       dateplus" -1 "yyyymmdd | getline yyyymmdd
                       close(dateplus" -1" yyyymmdd)
                     }
                     else
                       hh = sprintf("%02d", hh - 1)

                     print yyyymmdd""hh
                     exit 0
                   } # E.O.BEGIN

                   #-------------------------------------------------------#
                   function exit_usage(MSG)
                   #-------------------------------------------------------#
                   {
                     print sp""MSG"\n"sp"Usage: "GPY_name" yyyymmddhh."
                     exit 1
                   }' "$1"`
                GPY_status=$?

                if [ $GPY_status -eq 0 ]; then
                   echo "$GPY_yyyymmddhh"
                else
                   EMAIL_MSG "ERROR: $GPY_ID" \
                     "$GPY_yyyymmddhh"
                fi

                return $GPY_status
              } # "GPY_" prefix identifies this function's local variables.
              fi

              #======================================================================#
              #                       D O C U M E N T A T I O N                      #
              #======================================================================#
              #                                                                      #
              #      Author: Bob Orlando                                             #
              #                                                                      #
              #        Date: December 14, 1996                                       #
              #                                                                      #
              #  Program ID: get_prev_yyyymmddhh.sh                                  #
              #                                                                      #
              #       Usage: GET_PREV_YYYYMMDDHH yyyymmddhh                          #
              #                                                                      #
              #     Purpose: Get the date and hour preceding the date and hour       #
              #              (yyyymmddhh) passed to us.                              #
              #                                                                      #
              #     Globals: No global variables are assigned in this function.      #
              #              "GPY_" prefix identifies local function variables.      #
              #                                                                      #
              # Exit_status: Failure (1) for severe error (e.g. user supplies        #
              #              invalid arguments).  Otherwise returns success (0)      #
              #              echoing the hour previous to our yyyymmddhh.            #
              #                                                                      #
              #       Calls: EMAIL_MSG and dateplus as needed.                       #
              #                                                                      #
              #       Notes: This function is probably best used like this:          #
              #                                                                      #
              #              prev_yyyymmddhh=`GET_PREV_YYYYMMDDHH 1997012517`        #
              #              status=$?                                               #
              #              if [ $status -eq $success ]; then                       #
              #                 prev_hh=`echo $prev_yyyymmddhh | cut -c9-10`         #
              #                 echo "`date '+%Y-%m-%d %T'`"             \           #
              #                   "The previous hour was ............ |" \           #
              #                   "\b`printf '%20s' $prev_hh\``|"                    #
              #              else                                                    #
              #                 echo "`date '+%Y-%m-%d %T'`" \                       #
              #                   "Problem getting previous hour!  Status=$status"   #
              #              fi                                                      #
              #                                                                      #
              #              Despite the fact that this function attempts to notify  #
              #              the user on failure, used as shown (assigned to a       #
              #              variable via background operation [backquotes]), the    #
              #              notification (during interactive operation at least)    #
              #              is lost and the output less than desired.  For this     #
              #              reason, you are always best to test the function's      #
              #              exit status and proceed accordingly.                    #
              #                                                                      #
              #    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.      #
              #                                                                      #
              #----------------------------------------------------------------------#
            
Artificial Intelligence is no match for natural stupidity.
©Copyright Bob Orlando, 1996-2011
All rights reserved.
http://www.OrlandoKuntao.com
E-mail: Bob@OrlandoKuntao.com
Last update: Jan. 26, 2011
by Bob Orlando