home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / NUTUG11.ZIP / DATIM.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1987-11-21  |  5.9 KB  |  218 lines

  1.               {NORTHWESTERN UNIVERSITY TURBO USERS GROUP UTILITIES}
  2.  
  3.                        (** NUtility DATIM PROGRAM **)
  4.  
  5.                           {(C) J. E. Hilliard 1986}
  6.  
  7.          {This is a complete program ready for compiling which illus-
  8.          trates the use of some the NUtility routines.               }
  9.  
  10.  
  11.  
  12. PROGRAM DATIM;                         {For IBM PC and Compatables.         }
  13.  
  14.          {This utility can be run from the AUTOEXEC.BAT file after
  15.          the DOS date and time functions have been set by a command
  16.          to a clock/calendar board. It provides a means of regularly
  17.          checking the proper functioning of the clock/calendar. The
  18.          utility displays a greeting, the full date and the time in
  19.          am/pm style. Eg:
  20.  
  21.          Good Afternoon!
  22.  
  23.          Today is Saturday, December 21st. 1985 and the time is: 4:35 pm.
  24.                                                                      }
  25.  
  26. Uses     Crt,
  27.          Dos;
  28. CONST
  29.  
  30.   ThisYear = 1986;                     {Used to check for BAD DATE.         }
  31.                                        {Update as necessary.                }
  32.  
  33.          {/Note the definition of the following two arrays as typed
  34.          constants. This simplifies the loading and speeds execution./}
  35.  
  36.   MonthName : array [1..12] of string[9] =
  37.  
  38.      ('January', 'February',  'March',   'April',    'May', 'June', 'July',
  39.       'August',  'September', 'October', 'November', 'December');
  40.  
  41.   DayName   : array [0..6] of string[6] =
  42.  
  43.      ('Sun', 'Mon', 'Tues', 'Wednes', 'Thurs', 'Fri', 'Satur');
  44.  
  45. TYPE
  46.  
  47.   String2  = string[2];
  48.   String12 = string[12];
  49.  
  50.   RegType  = Registers;
  51.  
  52.          {/Note that no global variables are used.                 /}
  53.  
  54.  
  55. PROCEDURE GetDosTime (VAR Hour, Min : integer);
  56.  
  57.          {Gets the time with MS-DOS interrupt call.                  }
  58.  
  59. VAR
  60.  
  61.   Reg : RegType;
  62.  
  63. Begin
  64.  
  65.   Reg.AX := $2C00;                     {Function call $2C returns time.     }
  66.   MsDos (Reg);
  67.  
  68.   with Reg do
  69.     begin
  70.       Hour   := CH;
  71.       Min    := CL;
  72.     { Sec    := DH); }              {For illustration only. We do not   }
  73.     { CSec   := DL); }              {need Sec or Sec/100.               }
  74.     end {with}
  75.  
  76. End; {GetDosTime}
  77.  
  78.  
  79. PROCEDURE GetDosDate (VAR Y, M, DofM, DofW : integer);
  80.  
  81.          {Gets date with MS-DOS call.                                       }
  82.  
  83. VAR
  84.  
  85.   Reg : RegType;
  86.  
  87. Begin
  88.  
  89.   Reg.AX := $2A00;                     {Function call $2A returns the date. }
  90.   MsDos (Reg);
  91.  
  92.   with Reg do
  93.     begin
  94.       Y    := CX;
  95.       M    := DH;
  96.       DofM := DL;
  97.       DofW := AL;                      {Undocumented. See GetDosTime.       }
  98.     end {with}                         {Sun = 0, Sat = 6.                   }
  99.  
  100. End; {GetDosDate (VAR Y, M, DofM, DofW : integer}
  101.  
  102.  
  103. FUNCTION NumSuf (N : integer): String2;
  104.  
  105.          {Returns the suffix 'st', 'nd', 'rd' or 'th' appropriate for
  106.          the integer N.                                              }
  107.  
  108. Begin
  109.  
  110.   case (N mod 10) of
  111.  
  112.     1 : NumSuf := 'st';
  113.     2 : NumSuf := 'nd';
  114.     3 : NumSuf := 'rd';
  115.      else
  116.         NumSuf := 'th'
  117.  
  118.   end; {case N of}
  119.  
  120.   if N in [11..13]
  121.     then
  122.       NumSuf := 'th';                  {Allow for the exceptions.           }
  123.  
  124. End; {NumSuf (N : integer): String2}
  125.  
  126.  
  127. FUNCTION TimeString (Hour24, Min : integer) : String12;
  128.  
  129.           {Converts Hour24 and Min to a string and appends 'am' or 'pm'.    }
  130.  
  131. VAR
  132.  
  133.   IntStrH, IntStrM : string[5];        {Strings for hour and minutes.       }
  134.   HoldFunc         : String12;         {Temp. hold while constructing       }
  135.   Hour12           : integer;          {function. Reqd. to avoid recursive  }
  136.                                        {call to TimeString.                 }
  137. Begin
  138.  
  139.   Hour12 := Hour24 mod 12;
  140.   if Hour12 = 0 then
  141.     Hour12 := 12;                      {Follow usual convention.            }
  142.   Str (Hour12, IntStrH);
  143.   Str (Min,    IntStrM);
  144.   if length (IntStrM) = 1 then         {Add leading 0.                      }
  145.     IntStrM := '0' + IntStrM;
  146.   HoldFunc := IntStrH + ':' + IntStrM;
  147.   if Hour24 > 11
  148.     then
  149.       TimeString := HoldFunc + ' pm.'
  150.     else
  151.       TimeString := HoldFunc + ' am.'
  152.  
  153. End; {TimeString (Hour24, Min : integer) : String12}
  154.  
  155.  
  156. PROCEDURE WriteMessage;
  157.  
  158.           {This is the main routine.                                        }
  159.  
  160. VAR
  161.  
  162.   Line1   : string[20];
  163.   Line2   : string[80];
  164.  
  165.   Hour24, Min      : integer;
  166.   Year, Month, Day : integer;
  167.   DofW             : integer;          {Day of week (Sunday = 0).           }
  168.   IntStrD, IntStrY : string[5];        {Strings for Day and Year.           }
  169.  
  170. Begin
  171.  
  172.   GetDosTime (Hour24, Min);
  173.     case Hour24 of
  174.  
  175.        0..11 : Line1 := 'Morning!';
  176.       12..17 : Line1 := 'Afternoon!';
  177.         else
  178.                Line1 := 'Evening!'
  179.  
  180.     end; {case Hour24 of}
  181.  
  182.   Line1 := 'Good ' + Line1;
  183.  
  184.   GetDosDate (Year, Month, Day, DofW);
  185.   Str (Day,  IntStrD);
  186.   Str (Year, IntStrY);
  187.  
  188.   Line2 := 'Today is ' + DayName [DofW] + 'day, ' + MonthName [Month]
  189.              + ' ' + IntStrD + NumSuf (Day) + '. ' + IntStrY +
  190.              ' and the time is: ' + TimeString (Hour24, Min);
  191.  
  192.   LowVideo; ClrScr;
  193.   GoToXY (5, 2); write (Line1);
  194.   GoToXY (5, 4); write (Line2);
  195.   if Year < ThisYear then              {Error in calendar setting.          }
  196.     begin
  197.       GoToXY (33, 6); NormVideo;
  198.       write (' BAD DATE ');
  199.       LowVideo
  200.     end;
  201.   writeln; writeln; writeln;
  202.  
  203. End; {WriteMessage}
  204.  
  205.          {/The reason that 'WriteMessage' was made a procedure and then
  206.          invoked by the following single program line was to eliminate
  207.          the use of global variables. This ensures that there can be no
  208.          possibilty of side effects if additions are made in the future.
  209.          For example, routines to display the users' horoscope or to
  210.          announce that today was the birthday of some historic personage./}
  211.  
  212. BEGIN
  213.  
  214.   WriteMessage;
  215.  
  216. END.
  217.  
  218.