home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / sigm / vol291 / clock.lib < prev    next >
Encoding:
Text File  |  1986-12-22  |  10.1 KB  |  287 lines

  1.  program test_clock_lib;
  2.  
  3. (******************************************************************)
  4. (*   CLOCK.LIB  Ver 1.1  Oct. 3, 1985  by  Clarence C. Rudd       *)
  5. (*    Added suport for Kaypro 4/84 and (modified) Kaypro 2/84     *)
  6. (*    set constant KAYPRO := true for Kaypro & false for Z-Timer  *)
  7. (*                                                                *)
  8. (*   CLOCK.LIB  Ver 1.0  June 2, 1985  by  Clarence C. Rudd       *)
  9. (*                                                                *)
  10. (*   Clock.LIB is a file that contains several clock routines     *)
  11. (*   for use with the Z-Timer clock board from Alan Percy at      *)
  12. (*   Kenmore Computer Tech.  Kenmore, NY                          *)
  13. (*                                                                *)
  14. (*   To use this libaray load it into your file and then delete   *)
  15. (*   the routines not needed.                                     *)
  16. (*                                                                *)
  17. (*   NOTE: All of the user routines in this libaray are in the    *)
  18. (*   form of function calls. Example of use would be:             *)
  19. (*                                                                *)
  20. (*        String_variable := Date_1 + '  ' + Time_12;             *)
  21. (*                                                                *)
  22. (*    The String_variable would now contain the date & time       *)
  23. (*        i.e. 06/02/85  1:31:15 PM                               *)
  24. (*                                                                *)
  25. (******************************************************************)
  26.  
  27.  
  28. (******************************************************************)
  29. (*                                                                *)
  30. (*    The following declarations and constants plus the           *)
  31. (*    Procedure Read_Clock must be in your program to use         *)
  32. (*    any of the time & date Functions.                           *)
  33.  
  34. const
  35.   Kaypro = false;  {now set for Z-Timer}
  36.   year = '85';
  37.   clock_base = $E0; {base address of Z-Timer clock board}
  38.  
  39. type
  40.   str8  = string[8];
  41.   str11 = string[11];
  42.   str17 = string[17];
  43.   str28 = string[28];
  44.   str30 = string[30];
  45.  
  46.  
  47. procedure Read_Clock(var month, day, week_day, hour, min, sec: byte);
  48.  
  49. (*  This is the main procedure called by all of the time & date   *)
  50. (*  It reads the info from the clock as two BCD digits per byte   *)
  51. (*  and converts to integer and stores it in the following global *)
  52. (*  variables in the form of:                                     *)
  53. (*                            month in range 1(Jan)..12(Dec)      *)
  54. (*                            day in range 1..length of month     *)
  55. (*                            hour in 0..23 (24-hr clock)         *)
  56. (*                            minute and second in 0..59          *)
  57. (*                            and week_day in 1(Sun)..7(Sat)      *)
  58. (*                                                                *)
  59. (*  NOTE: the variables are of type Byte to save space.           *)
  60.  
  61.  
  62.   var
  63.     temp: byte;
  64.  
  65.   function bcd_to_dec(bcd: byte): byte;
  66.   {Converts 2-digit/byte BCD to decimal}
  67.  
  68.     begin
  69.       bcd_to_dec := (bcd and 15) + 10 * (bcd div 16);
  70.     end;
  71.  
  72.   function Inport(location: byte): byte;
  73.   {Reads data from clock register at port(location)}
  74.     begin
  75.       if Kaypro
  76.         then
  77.           begin {of Inport for Kaypro}
  78.             port[$20] := location;
  79.             Inport := bcd_to_dec(port[$24]);
  80.           end  {of Inport for Kaypro}
  81.         else
  82.           begin {of Inport for Z-Time}
  83.             Inport := bcd_to_dec(port[clock_base + location]);
  84.           end; {of Inport for Z-Time}
  85.     end; {of Inport }
  86.  
  87.   procedure Setup_Clock; {used for Kaypro only}
  88.   {Sets Kaypro internal I/O port to address clock}
  89.  
  90.     var
  91.       junk: byte;
  92.  
  93.     begin
  94.       port[$22] := $CF;
  95.       port[$22] := $E0;
  96.       port[$22] := $03;
  97.       junk := Inport($14);
  98.     end;
  99.  
  100.  
  101.   begin  { of Read_clock }
  102.       if Kaypro then Setup_Clock;
  103.       repeat
  104.         sec        := Inport(2);
  105.         min        := Inport(3);
  106.         hour       := Inport(4);
  107.         week_day   := Inport(5);
  108.         day        := Inport(6);
  109.         month      := Inport(7);
  110.         temp       := Inport(2);
  111.       until temp = sec; {Make sure clock hasn't changed during reading}
  112.   end;
  113. (*                                                                *)
  114. (*               End of procedure Read_Clock                      *)
  115. (******************************************************************)
  116.  
  117. (******************************************************************)
  118. (*   Date_1  returns 8-character srting i.e. (06/02/85)           *)
  119. (*                                                                *)
  120.  
  121. function Date_1: str8;
  122.  
  123.   var
  124.     tempm, tempd : string[2];
  125.     month, day, week_day, hour, min, sec : byte;
  126.  
  127.   begin
  128.     Read_Clock(month, day, week_day, hour, min, sec);
  129.     str(month:1,tempm);
  130.     if length(tempm) = 1 then tempm := '0' + tempm; {add leading 0 ?}
  131.     str(day:1,tempd);
  132.     if length(tempd) = 1 then tempd := '0' + tempd; {add leading 0 ?}
  133.     Date_1 := tempm + '/' + tempd + '/' + year;
  134.   end;
  135.  
  136. (*                                                                *)
  137. (*                 End of Function Date_1                         *)
  138. (******************************************************************)
  139.  
  140. (******************************************************************)
  141. (*   Date_2 returns 17-characater string i.e. (Sun  Jun 02, 1985) *)
  142. (*                                                                *)
  143.  
  144. function Date_2: str17;
  145.  
  146.   const
  147.     week_days:   array [1..7] of string[3] =
  148.                  ('Sun','Mon','Tue','Wen','Thu','Fri','Sat');
  149.     months:      array [1..12] of string[3] =
  150.                  ('Jan','Feb','Mar','Apr','May','Jun',
  151.                   'Jul','Aug','Sep','Oct','Nov','Dec');
  152.  
  153.   var
  154.     tempd : string[2];
  155.     month, day, week_day, hour, min, sec : byte;
  156.  
  157.   begin
  158.     Read_Clock(month, day, week_day, hour, min, sec);
  159.     str(day:1,tempd);
  160.  
  161.   {delete the next line if you don't want leading zero on day of month}
  162.     if length(tempd) = 1 then tempd := '0' + tempd; {add leading 0 ?}
  163.  
  164.     Date_2 := week_days[week_day] + '  ' + months[month]  + ' '
  165.                                    + tempd + ', 19' + year;
  166.   end;
  167.  
  168. (*                                                                *)
  169. (*                  End of Function Date_2                        *)
  170. (******************************************************************)
  171.  
  172. (******************************************************************)
  173. (*   Date_3 returns a string with a max length of 28 characters   *)
  174. (*   i.e. (Sunday  June 02, 1985)                                 *)
  175. (*        (Saturday  December 28, 1985)                           *)
  176. (*                                                                *)
  177.  
  178. function Date_3: str28;
  179.  
  180.   const
  181.     week_days:   array [1..7] of string[9] =
  182.                  ('Sunday','Monday','Tuesday','Wendesday','Thursday',
  183.                   'Friday','Saturday');
  184.     months:      array [1..12] of string[9] =
  185.                  ('Janurary','February','March','April','May','June',
  186.                   'July','August','September','October','November','December');
  187.  
  188.   var
  189.     tempd : string[2];
  190.     month, day, week_day, hour, min, sec : byte;
  191.  
  192.   begin
  193.     Read_Clock(month, day, week_day, hour, min, sec);
  194.     str(day:1,tempd);
  195.  
  196.   {delete the next line if you don't want leading zero on day of month}
  197.     if length(tempd) = 1 then tempd := '0' + tempd; {add leading 0 ?}
  198.  
  199.     Date_3 := week_days[week_day] + '  ' + months[month]  + ' ' +
  200.                                           tempd + ', 19' + year;
  201.   end;
  202.  
  203. (*                                                                *)
  204. (*                     End of Function Date_3                     *)
  205. (******************************************************************)
  206.  
  207. (*                START OF THE TIME FUNCTIONS                     *)
  208. (******************************************************************)
  209. (*   Time_12 returns 11-character string i.e. (12:01:00 AM)       *)
  210. (*                                                                *)
  211.  
  212. function Time_12: str11;
  213.  
  214.   var
  215.     pm : boolean;
  216.     temp : string[11];
  217.     temps, tempm, temph: string[2];
  218.     month, day, week_day, hour, min, sec : byte;
  219.  
  220.   begin
  221.     Read_Clock(month, day, week_day, hour, min, sec);
  222.     str(sec:1,temps);
  223.     str(min:1,tempm);
  224.     if length(temps) = 1 then temps := '0' + temps;
  225.     if length(tempm) = 1 then tempm := '0' + tempm;
  226.  
  227.     if hour >= 12 then begin {if after 12 PM convert from military time}
  228.       pm := true;
  229.       if hour > 12 then hour := hour - 12;
  230.     end
  231.     else begin
  232.       pm := false;
  233.       if hour = 0 then hour := 12;   {if 12 AM}
  234.     end;
  235.  
  236.     str(hour:2,temph);
  237.     temp :=  temph + ':' + tempm + ':' + temps;
  238.  
  239.     if pm then temp := temp + ' PM'
  240.     else temp := temp + ' AM';
  241.     Time_12 := temp;
  242.   end;
  243.  
  244. (*                                                                *)
  245. (*                   End of Function Time_12                      *)
  246. (******************************************************************)
  247.  
  248. (******************************************************************)
  249. (*   Time_24 returns  8-character string i.e. (23:01:00)          *)
  250. (*                                                                *)
  251.  
  252. function Time_24: str8;
  253.  
  254.   var
  255.     temp : string[11];
  256.     temps, tempm, temph: string[2];
  257.     month, day, week_day, hour, min, sec : byte;
  258.  
  259.   begin
  260.     Read_Clock(month, day, week_day, hour, min, sec);
  261.     str(sec:1,temps);
  262.     str(min:1,tempm);
  263.     str(hour:1,temph);
  264.     if length(temps) = 1 then temps := '0' + temps;
  265.     if length(tempm) = 1 then tempm := '0' + tempm;
  266.     if length(temph) = 1 then temph := '0' + temph;
  267.  
  268.     Time_24 :=  temph + ':' + tempm + ':' + temps;
  269.   end;
  270.  
  271. (*                                                                *)
  272. (*                   End of Function Time_24                      *)
  273. (******************************************************************)
  274.  
  275.  
  276.  
  277.   begin {of main program test_clock_lib }
  278.     writeln('This is the output of Date_1 ',Date_1);
  279.     writeln('This is the output of Date_2 ',Date_2);
  280.     writeln('This is the output of Date_3 ',Date_3);
  281.     writeln('This is the output of Time_12 ',Time_12);
  282.     writeln('This is the output of Time_24 ',Time_24);
  283.   end.  {of program test_clock_lib }
  284.  
  285.  
  286.  
  287.