""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
   SET A TIMER TO RUN A PROGRAM AT A FUTURE DATE & TIME
   """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
   PRODUCT   :  R:BASE                  VERSION      :  3.1
   CATEGORY  :  PROGRAMMING             SUBCATEGORY  :  TOOLS
   """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
   From Alex Rafferty, Rafferty Software, 6517 High Country Trail,
   Arlington TX 76016. Alex is an R:BASE application developer. You can
   reach him at 817-483-1316.
 
   I developed TIMER.CMD for a client who wanted to load a large ASCII
   file into his database outside of normal office hours. You can use it
   to execute any R:BASE command file at a specified future date and time
   such as overnight or on a weekend.
 
   TIMER.CMD prevents invalid dates or times. You must enter times in the
   24 hour clock format (HH:MM)--13:14 for 1:14 pm. When you activate the
   timer, TIMER.CMD clears the screen but leaves a status message giving
   the date and time settings and the current date and time.
 
 
   Programming Examples
   """"""""""""""""""""
   TIMER.CMD shows developers how to use CHKKEY and GETKEY in a WHILE
   loop to allow a user to interrupt a process with the press of a key.
 
   Developers will also see how to create different colored DISPLAY
   screens by taking snapshots of colored screens.
 
 
   Create Colored SNAP Files
   """""""""""""""""""""""""
   Create two colored SNAP files for TIMER.CMD to use to collect and
   display the date and time. First, Use RBEDIT to create the following
   two files. Leave the top line blank in each one. Name them TIME1 and
   TIME2:
 
 
   Here's TIME1:
 
      +-----------------------------------+
      | Date and time to begin procedure. |
      |                                   |
      |   DATE:            TIME:          |
      |                                   |
      +-----------------------------------+
 
 
   Here's TIME2:
 
      +-----------------------------------+
      | Date and time Procedure begins.   |
      |   DATE:            TIME:          |
      |                                   |
      |                                   |
      +-----------------------------------+
 
 
   Next, run COLOR.CMD to make the colored SNAP files.
 
     *( COLOR.CMD--Colored SNAP files.)
     CLS
     SET COLOR red ON cyan
     DISPLAY time1
     SNAP collect.snp FROM 2 1 TO 7 37
     CLS
     SET COLOR black ON cyan
     DISPLAY time2
     SNAP display.snp FROM 2 1 TO 7 37
     SET COLOR white ON blue
 
 
   Create ERROR.CMD
 
     *( ERROR.CMD--called by TIMER.CMD)
     SET VAR verr1 = .%1
     SET VAR verr2 = 'Press any key to continue'
     WRITE .verr1 AT 23 1 USING +
     [^]_????????????????????????????+
     ????????????????????????????????+
     ??????????????????? cyan ON red
     WRITE .verr2 AT 24 1 USING +
     [^]_????????????????????????????+
     ????????????????????????????????+
     ??????????????????? cyan ON red
     BEEP ; PAUSE ; CLS FROM 23 TO 24
     CLEAR VAR verr1, verr2, %1
     RETURN
 
 
   Create TIMER.CMD
 
     *( TIMER.CMD--Set run date and time.)
     SET BELL OFF ; SET INSERT OFF
     SET MESSAGES OFF
     SET ERROR MESSAGES OFF
     SET ERROR VAR verr
     LABEL begin
     SET DATE FORMAT MM/DD/YY
     SET TIME FORMAT HH:MM
     SET VAR vdttext TEXT = ('Currently:' & CTXT(.#DATE) & CTXT(.#TIME)), +
       vfore TEXT = 'white', vback TEXT = 'blue'
     SET COLOR .vfore ON .vback ; CLS
     DISPLAY collect.snp AT 8, 12
     WRITE '        ' AT 11 22 black ON gray
     WRITE '     ' AT 11 39 black ON gray
     WRITE 'mm/dd/yy' AT 10 22 white ON cyan
     WRITE 'hh:mm' AT 10 39 white ON cyan
     WRITE .vdttext AT 12 16 blue ON cyan
     *( Set date.)
     LABEL expdt1
     CLEAR VAR vdte
     SET VAR vdte DATE
     FILLIN vdte=8 USING '' AT 11 22 black ON gray
     IF verr <> 0 THEN
       RUN error.cmd USING 'not a valid date      MM/DD/YY'
       WRITE '        ' AT 11 22 black ON gray
       GOTO expdt1
     ENDIF
     IF vdte IS NULL THEN
       GOTO endexp
     ENDIF
     IF vdte < .#DATE THEN
       RUN error.cmd USING 'That date has passed.'
       GOTO expdt1
     ENDIF
     *( Set time.)
     LABEL expdt2
     CLEAR VAR vtm2
     SET VAR vtm2 TIME
     SET TIME SEQ HHMM
     FILLIN vtm2=5 USING '' AT 11 39 +
       black ON gray
     IF verr <> 0 THEN
       RUN error.cmd USING 'not a valid time HH:MM'
       WRITE '     ' AT 11 39 black ON gray
       GOTO expdt2
     ENDIF
     IF vtm2 IS NULL THEN
       GOTO expdt1
     ENDIF
     IF vdte <= .#DATE AND vtm2 < .#TIME THEN
       RUN error.cmd USING 'That time has passed.'
       WRITE '     ' AT 11 39 black ON gray
       GOTO expdt2
     ENDIF
     LABEL timeron
     NEW
     SET DATE FORMAT MM/DD/YY
     SET TIME FORMAT HH:MM
     DISPLAY display.snp AT 8, 12
     WRITE .vdte AT 10 24 black ON gray
     WRITE .vtm2 AT 10 39 black ON gray
     WRITE 'Press [F2] to Start timer.' AT 12 18 white ON cyan
     WRITE 'Press [F6] to change the setting, or [Esc] to cancel.' +
       AT 15 4 white ON cyan
     LABEL ft
     FILLIN vstr=0 USING '' AT 12 60
     IF vstr IS NULL THEN
       GOTO ft
     ELSE
       IF vstr = '[Esc]' THEN
         GOTO endexp
       ELSE
         IF vstr = '[F6]' THEN
           GOTO begin
         ENDIF
         IF vstr <> '[F2]' THEN
           GOTO ft
         ENDIF
       ENDIF
     ENDIF
     IF #DATE > .vdte THEN  *( [F2] pressed.)
       GOTO toolate
     ENDIF
     SET VAR vtimer INT = 251, vwhms INT = 1
     SET ESCAPE OFF
     WHILE #DATE < .vdte THEN  *( Date loop.)
       SET COLOR gray ON black
       SET DATE FORMAT 'WWW-MMM-DD'
       SET VAR vset = ('Date set for -' & CTXT(.vdte) +
         & 'Current date is -' & CTXT(#DATE))
       WRITE .vset AT .vwhms 14
       IF vtimer > 250 THEN
         IF vwhms > 22 THEN
           SET VAR vwhms = 1, vtimer = 0
         ELSE
           SET VAR vwhms=(.vwhms + 1), vtimer = 0
         ENDIF
         CLS
         WRITE 'Timer activated, press [F5] to change or [Esc] to cancel.' +
           AT .vwhms 5
         SET VAR vwhms = (.vwhms + 1)
       ENDIF
       SET VAR vtimer = (.vtimer + 1), +
         vchkkey = (CHKKEY(0))
       IF vchkkey = 1 THEN
         SET VAR vgetkey = (GETKEY(0))
         IF vgetkey = '[Esc]' THEN
           GOTO endexp
         ENDIF
         IF vgetkey = '[F5]' THEN
           SET COLOR .vfore ON .vback
           GOTO timeron
         ENDIF
       ENDIF
     ENDWHILE
     IF #TIME > .vtm2 THEN
       *( By the time you pressed [F2],)
       *( the date or time set had passed.)
       LABEL toolate
       SET VAR vdttext = +
         ('Current date' & CTXT(.#DATE) +
         & 'Current time - ' + CTXT(.#TIME))
       WRITE .vdttext AT 12 17 black ON gray
       RUN error.cmd USING 'Date is past enter date & time again'
       GOTO begin
     ENDIF
     WHILE #TIME < .vtm2 THEN  *( Time loop.)
       SET COLOR gray ON black
       SET TIME FORMAT HH:MM:SS
       SET VAR vset = ('Timer set for' & CTXT(.vtm2) & 'Current time is' +
         & CTXT(#TIME))
       WRITE .vset AT .vwhms 14
       IF vtimer > 250 THEN
         IF vwhms > 22 THEN
           SET VAR vwhms = 1, vtimer = 0
         ELSE
           SET VAR vwhms = (.vwhms + 1), vtimer = 0
         ENDIF
         CLS
         WRITE 'Timer activated. Press [F5] to change settings, [Esc] to cancel
.' +
           AT .vwhms 5
         SET VAR vwhms = (.vwhms + 1)
       ENDIF
       SET VAR vtimer = (.vtimer + 1), vchkkey = (CHKKEY(0))
       IF vchkkey = 1 THEN
         SET VAR vgetkey = (GETKEY(0))
         IF vgetkey = '[Esc]' THEN
           GOTO endexp
         ENDIF
         IF vgetkey = '[F5]' THEN
           SET COLOR .vfore ON .vback
           GOTO timeron
         ENDIF
       ENDIF
     ENDWHILE
     BEEP ; CLS  *( Time up! Alarm goes off.)
     SET COLOR .vfore ON .vback
     *( Put RUN command here to run program.)
     RUN your.cmd  *( program. to execute.)
     LABEL endexp
     SET COLOR .vfore ON .vback
     CLEAR VAR vtm2, vdte, vchkkey, vgetkey, vtimer, vwhms, vdttext, +
       vtmtext, vfore, vback, vset, vstr, verr
     *( Reset the environment.)
     SET TIME FORMAT HH:MM:SS
     SET TIME SEQ HHMMSS
     SET DATE FORMAT MM/DD/YYYY
     SET MESSAGES ON
     SET ERROR MESSAGES ON
     SET BELL ON ; SET INSERT ON
     SET ESCAPE ON
     RETURN
 
 
   What TIMER.CMD Does
   """""""""""""""""""
   First, TIMER.CMD asks you for the starting date and time. It uses an
   error variable to test for an invalid date or time. Next, it checks
   the entered date and time against the current date and time to keep
   from inadvertently accepting a past date or time. In the event of an
   error, it passes the error message to ERROR.CMD, which displays the
   error in red.
 
   After setting the date and time, press [F2] to start the timer, [F6]
   to change the settings, or [Esc] to cancel. After starting the timer,
   TIMER.CMD clears the screen, sets the screen colors to gray on black,
   and displays a two-line message that moves approximately every 25
   seconds to prevent screen burn-in. At any time, you can press [F5] to
   change the settings or [Esc] to deactivate the timer.
 
   TIMER.CMD uses two WHILE loops. Both loops use GETKEY and CHKKEY to
   see if the user wishes to interrupt. The first WHILE loop executes as
   long as the date set by the user is greater than the current date
   (#DATE). When the two dates match, the loop breaks and moves on to the
   second WHILE loop, which executes as long as the time set by the user
   is greater than the current time (#TIME). Then a bell chimes to
   indicate the time has expired, and TIMER.CMD executes the delayed
   program.
 
   The code you're running may need to reset some of the environment
   settings set by TIMER.CMD. You may wish to change the environment
   settings at the end of TIMER.CMD to suit your own configuration.