home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / ACTS11.ZIP / ACTSCLOK.CMD next >
OS/2 REXX Batch file  |  1992-11-04  |  18KB  |  408 lines

  1. /* REXX */
  2. /********************************************************************************/
  3. /*                                                                              */
  4. /* Program: ACTSCLOK.CMD - set system date and time from Automatic              */
  5. /*              Computer Time Service (ACTS) maintained by the                  */
  6. /*              National Institute of Standards                                 */
  7. /*                                                                              */
  8. /* Version: 1.0 - October, 1992                                                 */
  9. /*                                                                              */
  10. /* Author:  Chuck Giglia, Delaware Valley OS/2 Users Group                      */
  11. /*          1129 Webster Drive, Wilmington, Delaware 19803                      */
  12. /*          Compuserve: 72317,3704                                              */
  13. /*                                                                              */
  14. /* Acknowledgements:                                                            */
  15. /*          Many thanks to Jerry am Ende of the Delaware Valley OS/2            */
  16. /*          Users Group for his enthusiasm toward REXX which piqued             */
  17. /*          my interest in improving on his TIMESET.CMD routine.                */
  18. /*          Also, I appreciate the design ideas I got from John                 */
  19. /*          Deurbrouck in the October 27, 1992 issue of PC Magazine.            */
  20. /*          His program was a bit flashier, but it was written for              */
  21. /*          Windows in C and I didn't think this problem needed a               */
  22. /*          graphical solution.                                                 */
  23. /*                                                                              */
  24. /*          I hope you can use this routine to your advantage, but I            */
  25. /*          provide no warrantees nor do I guarantee that it performs           */
  26. /*          any useful function.                                                */
  27. /*                                                                              */
  28. /*          Your feedback or ideas for improvement are appreciated.             */
  29. /*                                                                              */
  30. /* Product Description                                                          */
  31. /*      ACTSCLOK.CMD is an OS/2 REXX command file which sets the System Date    */
  32. /*      and Time of the invoking computer to the current date and time from     */
  33. /*      the Automated Computer Time Service (ACTS) maintained by the National   */
  34. /*      Institute of Standards. The ACTS returns Greenwich Mean Time (GMT),     */
  35. /*      so ACTSCLOK.CMD provides for handling a positive or negative offset     */
  36. /*      from GMT and allows the user to enable or disable Daylight Saving       */
  37. /*      Time (DST) adjustment.                                                  */
  38. /*                                                                              */
  39. /* Usage is:                                                                    */
  40. /*       ACTSCLOK <offset> <dstIndicator> <comPort> <PhoneNumber> <dialPrefix>  */
  41. /*              <offset>       = Minutes from GMT (i.e. EST = -300).            */
  42. /*                              The default is -300                             */
  43. /*              <dstIndicator> = Adjust for DST? (Y or N). The default is Y     */
  44. /*              <comPort>      = COM port to use (COM1, COM2, etc.). The        */
  45. /*                              default is COM1                                 */
  46. /*              <PhoneNumber>  = Area code and phone number of the ACTS.        */
  47. /*                              The default is 303-494-4774.                    */
  48. /*              <dialPrefix>   = Any dialing prefixes or codes required to dial */
  49. /*                              the ACTS. The default is 1, (the comma causes   */
  50. /*                              the modem to pause briefly before continuing)   */
  51. /*                                                                              */
  52. /*      Individual command line arguments may be forced to use the default by   */
  53. /*      coding an exclamation point (!) in that argument's position. For example*/
  54. /*      to accept all defaults except the COM port and dialling prefix, use:    */
  55. /*                                                                              */
  56. /*                      ACTSCLOK ! ! COM2 ! 9,                                  */
  57. /*                                                                              */
  58. /*      This example will set the system clock to 300 minutes before GMT during */
  59. /*      Standard Time or 240 minutes before GMT during Daylight Saving Time.    */
  60. /*      The system will use COM2 to dial 9,303-494-4774 to connect to the ACTS. */
  61. /*                                                                              */
  62. /********************************************************************************/
  63.  
  64. /* Initialize Defaults and Control Variables and Perform Startup Processing     */
  65.  
  66.  
  67. CALL getDateFormat              /* determine the system date format */
  68.  
  69. dDialPrefix = "1,"              /* default dialing prefix   */
  70.  
  71. dPhoneNumber = "303-494-4774"   /* default telephone number */
  72.  
  73. dOffset = -300                  /* default minutes from GMT.*/
  74.                                 /* This is a signed whole   */
  75.                                 /* number (-300 is 5 hours  */
  76.                                 /* earlier than Greenwich   */
  77.                                 /* Mean Time - i.e. Eastern */
  78.                                 /* Standard Time in the USA)*/
  79.  
  80. dDSTIndicator = "Y"             /* default DST indicator.   */
  81.                                 /* "Y" means adjust for DST */
  82.                                 /* "N" means don't adjust   */
  83.  
  84. dCOMPort = "COM1"               /* defualt COM port         */
  85.  
  86. crLf = X2C("0D0A")
  87.  
  88. busyRetrys = 10                 /* number of times to redial*/
  89.                                 /* if busy or no answer     */
  90.  
  91. attemptNum = 1                  /* dial attempt counter     */
  92.  
  93. argTester = ""                  /* used to determine if     */
  94.                                 /* there are any arguments  */
  95.  
  96. connectLimit = 30               /* number of seconds to wait*/
  97.                                 /* for ACTS to answer       */
  98.  
  99. minutesPerDay = 24 * 60         /* used to determine if we  */
  100.                                 /* have adjusted the time   */
  101.                                 /* over a day boundary      */
  102.  
  103. daysPerMonth.1 = 31             /* load array of number of  */
  104. daysPerMonth.2 = 28             /* days in each month       */
  105. daysPerMonth.3 = 31             
  106. daysPerMonth.4 = 30             /* NOTE: the value for      */
  107. daysPerMonth.5 = 31             /* February will be adjusted*/
  108. daysPerMonth.6 = 30             /* later in the logic if    */
  109. daysPerMonth.7 = 31             /* this is a leap year      */
  110. daysPerMonth.8 = 31
  111. daysPerMonth.9 = 30
  112. daysPerMonth.10 = 31
  113. daysPerMonth.11 = 30
  114. daysPerMonth.12 = 31
  115.  
  116. call RxFuncAdd 'SysSleep', 'RexxUtil', 'SysSleep'
  117.  
  118. /* Get and Process any User-Supplied Arguements                                 */
  119.  
  120. PARSE UPPER ARG uOffset uDSTIndicator uCOMPort uPhoneNumber uDialPrefix
  121.  
  122. argTester = uOffset||uDSTIndicator||uCOMPort||uPhoneNumber||uDialPrefix
  123.  
  124. IF argTester == "?" | argTester == "" then do
  125.  
  126.      SAY " "
  127.      SAY "ACTSCLOK.CMD is an OS/2 REXX command file which sets the System Date "
  128.      SAY "     and Time of the invoking computer to the current date and time from "
  129.      SAY "     the Automated Computer Time Service (ACTS) maintained by the National "
  130.      SAY "     Institute of Standards. The ACTS returns Greenwich Mean Time (GMT), "
  131.      SAY "     so ACTSCLOK.CMD provides for handling a positive or negative offset "
  132.      SAY "     from GMT and allows the user to enable or disable Daylight Saving "
  133.      SAY "     Time (DST) adjustment."
  134.      SAY " "
  135.      "@pause"
  136.      SAY " "
  137.      SAY "Usage is: ACTSCLOK <offset> <dstIndicator> <comPort> <PhoneNumber> <dialPrefix>"
  138.      SAY "     <offset>       = Minutes from GMT (i.e. EST = -300). The default is -300"
  139.      SAY "     <dstIndicator> = Adjust for DST? (Y or N). The default is Y"
  140.      SAY "     <comPort>      = COM port to use (COM1, COM2, etc.). The default is COM1"
  141.      SAY "     <PhoneNumber>  = Area code and phone number of the ACTS. The default"
  142.      SAY "                         is 303-494-4774."
  143.      SAY "     <dialPrefix>   = Any dialing prefixes or codes required to dial"
  144.      SAY "                         the ACTS. The default is 1, (the comma"
  145.      SAY "                         causes the modem to pause briefly before "
  146.      SAY "                         continuing)"
  147.      SAY " "
  148.      "@pause"
  149.      SAY " "
  150.      SAY "      Individual command line arguments may be forced to use the default by "
  151.      SAY "      coding an exclamation point (!) in that argument's position. For example "
  152.      SAY "      to accept all defaults except the COM port and dialling prefix, use: "
  153.      SAY " "
  154.      SAY "                      ACTSCLOK ! ! COM2 ! 9, "
  155.      SAY " "
  156.      SAY "      This example will set the system clock to 300 minutes before GMT during "
  157.      SAY "      Standard Time or 240 minutes before GMT during Daylight Saving Time. "
  158.      SAY "      The system will use COM2 to dial 9,303-494-4774 to connect to the ACTS. "
  159.      SAY " "
  160.      "@pause"
  161.      SAY " "
  162.      exit
  163.      
  164. end  /* Do */
  165.  
  166. IF uOffset == "" | uOffset == "!" then
  167.      offset = dOffset
  168. else
  169.      offset = uOffset
  170.  
  171. IF uDSTIndicator == "" | uDSTIndicator == "!" then
  172.      dstIndicator = dDSTIndicator
  173. else
  174.      dstIndicator = uDSTIndicator
  175.  
  176. IF uCOMPort == "" | uCOMPort == "!" then
  177.      comPort = dCOMPort
  178. else
  179.      comPort = uCOMPort
  180.  
  181. IF uPhoneNumber == "" | uPhoneNumber == "!" then
  182.      phoneNumber = dPhoneNumber
  183. else
  184.      phoneNumber = uPhoneNumber
  185.  
  186. IF uDialPrefix == "" | uDialPrefix == "!" then
  187.      dialPrefix = dDialPrefix
  188. else
  189.      dialPrefix = uDialPrefix
  190.  
  191. dialSequence = dialPrefix||phoneNumber
  192.  
  193. /* Open the COM Port and Set the Communications Parameters                      */
  194.  
  195. state = STREAM(comPort, "C", "OPEN")
  196. "@MODE" comPort":1200,E,7,1 > NUL"
  197.  
  198. needToRetry = "Y"                       /* make sure we enter loop at least once */
  199. DO WHILE attemptNum <= busyRetrys & needToRetry = "Y"
  200.  
  201.         SAY " "
  202.         SAY " "
  203.         SAY "Preparing to dial" dialSequence "on" comPort
  204.         SAY "GMT Offset is" offset "minutes"
  205.         SAY "DST Switch is" dstIndicator
  206.         SAY " "
  207.         SAY "Dialing Attempt Number" attemptNum
  208.         SAY " "
  209.  
  210.         /* Dial the ACTS                                                        */
  211.  
  212.         SAY "Dialing . . ."
  213.         SAY " "
  214.  
  215.         startTimer = time('R')          /* reset the elapsed time clock */
  216.         CALL LINEOUT comPort, "ATDT"dialSequence||crLf
  217.  
  218.         /* Bypass first line of help information by grabbing                    */
  219.         /* the first couple lines of output from the ACTS                       */
  220.         /* and throwing them away                                               */
  221.  
  222.         needToRetry = "N"
  223.         dataBuffer = ""
  224.         DO WHILE POS("*", dataBuffer) == 0 & POS("#", dataBuffer) == 0
  225.  
  226.              dataBuffer = dataBuffer||CHARIN(comPort)
  227.              IF POS("BUSY", dataBuffer) <> 0 then do    /* Handle busy condition */
  228.                 SAY "Busy!"
  229.                 needToRetry = "Y"
  230.              end  /* Do */
  231.  
  232.              if time('E') > connectLimit then do        /* Handle no answer condition */
  233.                 say "No Response"
  234.                 needToRetry = "Y"
  235.              end  /* Do */
  236.  
  237.              if needToRetry = "Y" then do
  238.                 call Hangup
  239.                 SAY " "
  240.                 attemptNum = attemptNum + 1
  241.                 say "Waiting . . ."
  242.                 SAY " "
  243.                 call SysSleep 5
  244.                 LEAVE
  245.              end  /* Do */
  246.  
  247.         END /* DO WHILE POS("*", dataBuffer) == 0 & POS("#", dataBuffer) == 0 */
  248.  
  249. END /* DO WHILE attemptNum <= busyRetrys & needToRetry = "Y" */
  250.  
  251. IF attemptNum > busyRetrys then do
  252.         SAY " "
  253.         SAY "No response from ACTS after" busyRetrys "attempts"
  254.         SAY "Please try again later"
  255.         SAY " "
  256.         EXIT
  257. END
  258.  
  259. dataBuffer = ""
  260. DO WHILE POS("UTC", dataBuffer) == 0
  261.  
  262.      dataBuffer = dataBuffer||CHARIN(comPort)
  263.  
  264. END
  265.  
  266. Call Hangup
  267.  
  268. state = STREAM(comPort, "C", "CLOSE")
  269.  
  270. PARSE VAR dataBuffer . ymdDate timeOfDay dstStatus .
  271.  
  272. /* Interpret the date retrieved from the ACTS */
  273.  
  274. currentYear = LEFT(ymdDate, 2)
  275. currentMonth = SUBSTR(ymdDate, 4, 2)
  276. currentDay = RIGHT(ymdDate, 2)
  277.  
  278. if currentYear // 4 == 0 then             /* check for leap year */
  279.         daysPerMonth.2 = 29
  280.  
  281. /* Determine if we should adjust for Daylight Savings Time (DST)                */
  282. /*   The DST indicator returned in the data will be 00 if we are                */
  283. /*   on Standard Time or 99 to 51 if we are on Standard Time, but               */
  284. /*   approaching DST.                                                           */
  285. /*   This indicator will be 50 if we are on DST or 49 to 01 if we               */
  286. /*   are on DST but approaching Standard Time.                                  */
  287.  
  288. IF dstIndicator == "Y" then
  289.         IF dstStatus <= "50" & dstStatus >= "01" then    /* we're on DST */
  290.                 offset = offset + 60
  291.  
  292. /* Interpret then convert the time to minutes to simplify adjustment            */
  293.  
  294. currentHours = LEFT(timeOfDay, 2)
  295. currentMinutes = SUBSTR(timeOfDay, 4, 2)
  296. currentSeconds = RIGHT(timeOfDay, 2)
  297.  
  298. timeInMinutes = (currentHours * 60) + currentMinutes
  299.  
  300. /* Now, adjust the time and extract the date and time in a form suitable for    */
  301. /* setting the system clock                                                     */
  302.  
  303. adjustedTime = timeInMinutes + offset
  304.  
  305. /* make adjustments if we have crossed a date boundary (i.e. crossed into       */
  306. /* yesterday or into tomorrow)                                                  */
  307.  
  308. select          
  309.  
  310.    when adjustedTime < 0 then do                  /* we backed into yesterday   */
  311.         adjustedTime = minutesPerDay + adjustedTime
  312.         currentDay = currentDay - 1
  313.         IF currentDay < 1 then do
  314.                 currentMonth = currentMonth - 1
  315.                 IF currentMonth < 1 then do
  316.                     currentYear = currentYear - 1
  317.                     currentMonth = 12 + currentMonth
  318.                 end    /* do */
  319.                 currentDay = daysPerMonth.currentMonth
  320.         end    /* do */
  321.    end    /* when do */
  322.  
  323.    when adjustedTime > minutesPerDay then do    /* we crossed into tomorrow */
  324.         adjustedTime = adjustedTime - minutesPerDay
  325.         currentDay = currentDay + 1
  326.         if currentDay > daysPerMonth.currentMonth then do
  327.                 currentDay = currentDay - daysPerMonth.currentMonth
  328.                 currentMonth = currentMonth + 1
  329.         end    /* do */
  330.         if currentMonth > 12 then do
  331.                 currentMonth = currentMonth - 12
  332.                 currentYear = currentYear + 1
  333.         end    /* do */
  334.    end    /* when do */
  335.  
  336.    otherwise                                    /* we're still on today */
  337.         nop
  338.  
  339. end  /* select */
  340.  
  341. currentHours = adjustedTime % 60
  342. currentMinutes = adjustedTime // 60
  343.  
  344. /* Finally, Set the system date and time on this PC                             */
  345.  
  346. "@TIME" currentHours":"currentMinutes":"currentSeconds
  347.  
  348. select
  349.    when monthPosition = 1 then               /* mm-dd-yy format */
  350.      "@DATE" currentMonth"-"currentDay"-"currentYear
  351.    when dayPosition = 1 then                 /* dd-mm-yy format */
  352.      "@DATE" "currentDay"-currentMonth"-"currentYear
  353.    when yearPosition = 1 then                /* yy-mm-dd format */
  354.      "@DATE" currentYear"-"currentMonth"-"currentDay
  355.    otherwise                                 /* default to US format */
  356.      "@DATE" currentMonth"-"currentDay"-"currentYear
  357. end  /* select */
  358.  
  359. SAY " "
  360. SAY " "
  361. SAY "New Time" TIME() "and Date" DATE()
  362. SAY " "
  363. SAY " "
  364.  
  365. exit
  366.  
  367. /********************************************************************************/
  368. /* Subroutines Follow                                                           */
  369. /********************************************************************************/
  370.  
  371. /* Subroutine Hangup - Hang up the telephone line to disconnect from the ACTS   */
  372.  
  373. Hangup:
  374.  
  375.      CALL LINEOUT comPort, crLf
  376.      CALL LINEOUT comPort, crLf
  377.      CALL LINEOUT comPort, "ATH"||crLf
  378.  
  379. Return
  380.  
  381. /* Subroutine getDateFormat - determine which date format has been set via      */
  382. /*   the COUNTRY.SYS setting in the CONFIG.SYS file (i.e. mm-dd-yy, yy-mm-dd or */
  383. /*   dd-mm-yy).                                                                 */
  384.  
  385. getDateFormat:
  386.  
  387.      "@if exist rxenter.dat erase rxenter.dat"
  388.  
  389.      CALL CHAROUT "rxenter.dat",x2c('0d')
  390.      call lineout "rxenter.dat"
  391.  
  392.      "@date < rxenter.dat | RXQUEUE"
  393.  
  394.      DO WHILE QUEUED() > 0
  395.         parse upper pull . '(' dFormat ')' .
  396.      END
  397.  
  398.      say "Date Format is:" dFormat
  399.  
  400.      monthPosition = POS(dFormat, "MM")
  401.      yearPosition  = POS(dFormat, "YY")
  402.      dayPosition   = POS(dFormat, "DD")
  403.  
  404.      "@if exist rxenter.dat erase rxenter.dat"
  405.  
  406. RETURN
  407.  
  408.