home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 15 / CD_ASCQ_15_070894.iso / vrac / sk210f.zip / SHDATPK.PAS < prev    next >
Pascal/Delphi Source File  |  1994-05-11  |  16KB  |  523 lines

  1. {$I SHDEFINE.INC}
  2.  
  3. {$I SHUNITSW.INC}
  4.  
  5. {$D-,L-}
  6. unit ShDatPk;
  7. {
  8.                                 ShDatPk
  9.  
  10.                         A Date Manipulation Unit
  11.  
  12.                                    by
  13.  
  14.                               Bill Madison
  15.  
  16.                    W. G. Madison and Associates, Ltd.
  17.                           13819 Shavano Downs
  18.                             P.O. Box 780956
  19.                        San Antonio, TX 78278-0956
  20.                              (512)492-2777
  21.                              CIS 73240,342
  22.                 Internet bill.madison@lchance.sat.tx.us
  23.  
  24.                 Copyright 1990, '94 Madison & Associates
  25.                           All Rights Reserved
  26.  
  27.         This file may  be used and distributed  only in accord-
  28.         ance with the provisions described on the title page of
  29.                   the accompanying documentation file
  30.                               SKYHAWK.DOC
  31. }
  32.  
  33. interface
  34.  
  35. uses
  36.   shUtilPk,
  37.   Dos;
  38.  
  39. const
  40.   Copyr = 'Copyright 1990, 1994 by W.G. Madison';
  41.  
  42. type
  43.   GregType  = record
  44.                 Year  : LongInt;
  45.                 Month,
  46.                 Day   : byte;
  47.                 end;
  48.   TimeType  = record
  49.                 H,
  50.                 M,
  51.                 S   : byte;
  52.                 end;
  53.  
  54. const
  55.   DayStr  : array[0..6] of string[9] =
  56.                         ('Sunday', 'Monday', 'Tuesday', 'Wednesday',
  57.                          'Thursday', 'Friday', 'Saturday');
  58.  
  59.   MonthStr: array[1..12] of string[9] =
  60.                         ('January',   'February', 'March',    'April',
  61.                          'May',       'June',     'July',     'August',
  62.                          'September', 'October',  'November', 'December');
  63.  
  64. function DoW(Greg : GregType) : byte;
  65.              {computes the day of the week (Sunday = 0; Saturday = 6)
  66.              from the Gregorian date.}
  67.  
  68. function Greg2ANSI(G : GregType) : string;
  69. {Returns the date as an ANSI date string (YYYYMMDD)}
  70.  
  71. function Greg2JDate(Greg : GregType) : integer;
  72.              {computes the Julian date from the Gregorian date.}
  73.  
  74. function Greg2JDN(Greg : GregType) : LongInt;
  75.              {computes the Julian Day-Number from the Gregorian date.}
  76.  
  77. procedure JDate2Greg(JDate, Year : Integer;
  78.                   var Greg : GregType);
  79.              {computes the Gregorian date from the Julian date.}
  80.  
  81. function JDN2ANSI(JDN : LongInt) : string;
  82. {Returns the JDN as an ANSI date string (YYYYMMDD)}
  83.  
  84. procedure JDN2Greg(JDN : LongInt;
  85.                   var Greg : GregType);
  86.              {computes the Gregorian date from the Julian Day-Number.}
  87.  
  88. function Greg2Str(G : GregType; Delim : string) : string;
  89. {Returns a Gregorian date record as a string of the form MMdDDdYYYY,
  90.  where the separator, "d", is Delim[1].}
  91.  
  92. function JDN2Str(JDN : LongInt; Delim : string) : string;
  93. {Returns a Julian Day-Number as a MMdDDdYYYY string.}
  94.  
  95. function Now  : LongInt;
  96. {Returns the system time as Seconds-Since-Midnight.}
  97.  
  98. procedure Now2Time(var T : TimeType);
  99. {Returns the system time as a Time record.}
  100.  
  101. function NowStr(Delim : string; T24 : boolean) : string;
  102. {Returns the system time as a string of the form:
  103.           HHdMMdSSss    if Delim is non-empty and T24 (24 hour time) is
  104.                         false. The delimiter used, "d", is Delim[1]. The
  105.                         suffix, "ss", is "am" or "pm" as appropriate.
  106.           HHdMMdSS      if Delim is non-empty and T24 (24 hour time) is
  107.                         true. The delimiter used, "d", is Delim[1]. The
  108.                         time will be expressed in 24-hour form.
  109.           HHMMSSss      if Delim is empty and T24 (24 hour time) is
  110.                         false. The suffix, "ss", is "am" or "pm" as
  111.                         appropriate.
  112.           HHMM          if Delim is empty and T24 (24 hour time) is
  113.                         true. The time will be expressed in 24-hour form.
  114. }
  115.  
  116. procedure SSM2Time(SSM : LongInt; var T : TimeType);
  117. {Converts Seconds-Since-Midnight to a Time record.}
  118.  
  119. function SSM2TimeStr(SSM : LongInt; Delim : string; T24 : boolean) : string;
  120. {Returns Seconds-Since-Midnight as a string of the form:
  121.           HHdMMdSSss    if Delim is non-empty and T24 (24 hour time) is
  122.                         false. The delimiter used, "d", is Delim[1]. The
  123.                         suffix, "ss", is "am" or "pm" as appropriate.
  124.           HHdMMdSS      if Delim is non-empty and T24 (24 hour time) is
  125.                         true. The delimiter used, "d", is Delim[1]. The
  126.                         time will be expressed in 24-hour form.
  127.           HHMMSSss      if Delim is empty and T24 (24 hour time) is
  128.                         false. The suffix, "ss", is "am" or "pm" as
  129.                         appropriate.
  130.           HHMM          if Delim is empty and T24 (24 hour time) is
  131.                         true. The time will be expressed in 24-hour form.
  132. }
  133.  
  134. function Time2SSM(T : TimeType) : LongInt;
  135. {Returns a Time record as Seconds-Since-Midnight.}
  136.  
  137. function Time2TimeStr(T : TimeType; Delim : string; T24 : boolean) : string;
  138. {Returns a Time record as a string of the form:
  139.           HHdMMdSSss    if Delim is non-empty and T24 (24 hour time) is
  140.                         false. The delimiter used, "d", is Delim[1]. The
  141.                         suffix, "ss", is "am" or "pm" as appropriate.
  142.           HHdMMdSS      if Delim is non-empty and T24 (24 hour time) is
  143.                         true. The delimiter used, "d", is Delim[1]. The
  144.                         time will be expressed in 24-hour form.
  145.           HHMMSSss      if Delim is empty and T24 (24 hour time) is
  146.                         false. The suffix, "ss", is "am" or "pm" as
  147.                         appropriate.
  148.           HHMM          if Delim is empty and T24 (24 hour time) is
  149.                         true. The time will be expressed in 24-hour form.
  150. }
  151.  
  152. function Today  : LongInt;
  153. {Returns the system date as a Julian Day-Number}
  154.  
  155. function Today2ANSI : string;
  156. {Returns the system date as an ANSI date string (YYYYMMDD)}
  157.  
  158. procedure Today2Greg(var G : GregType);
  159. {Returns the system date as a Gregorian date record.}
  160.  
  161. function TodayStr(Delim : string) : string;
  162. {Returns the system date as a string of the form MMdDDdYYYY, where the
  163.  separator, "d", is Delim[1].}
  164.  
  165. implementation
  166.  
  167. const
  168.   D0 =    1461;
  169.   D1 =  146097;
  170.   D2 = 1721119;
  171.  
  172. function Greg2JDN(Greg : GregType) : LongInt;
  173. var
  174.   Century,
  175.   XYear    : LongInt;
  176. begin {Greg2JDN}
  177.   with Greg do begin
  178.     If Month <= 2 then begin
  179.       Year := pred(Year);
  180.       Month := Month + 12;
  181.       end;
  182.     Month := Month - 3;
  183.     Century := Year div 100;
  184.     XYear := Year mod 100;
  185.     Century := (Century * D1) shr 2;
  186.     XYear := (XYear * D0) shr 2;
  187.     Greg2JDN := ((((Month * 153) + 2) div 5) + Day) + D2
  188.                                       + XYear + Century;
  189.     end; {with Greg}
  190.   end; {Greg2JDN}
  191.  
  192.  
  193. {**************************************************************}
  194.  
  195. procedure JDN2Greg(JDN : LongInt;
  196.                   var Greg : GregType);
  197. var
  198.   Temp,
  199.   XYear   : LongInt;
  200.   YYear,
  201.   YMonth,
  202.   YDay    : Integer;
  203. begin {JDN2Greg}
  204.   with Greg do begin
  205.     Temp := (((JDN - D2) shl 2) - 1);
  206.     XYear := (Temp mod D1) or 3;
  207.     JDN := Temp div D1;
  208.     YYear := (XYear div D0);
  209.     Temp := ((((XYear mod D0) + 4) shr 2) * 5) - 3;
  210.     YMonth := Temp div 153;
  211.     If YMonth >= 10 then begin
  212.       YYear := YYear + 1;
  213.       YMonth := YMonth - 12;
  214.       end;
  215.     YMonth := YMonth + 3;
  216.     YDay := Temp mod 153;
  217.     YDay := (YDay + 5) div 5;
  218.     Year := YYear + (JDN * 100);
  219.     Month := YMonth;
  220.     Day := YDay;
  221.     end; {with Greg}
  222.   end; {JDN2Greg}
  223.  
  224.  
  225. {**************************************************************}
  226.  
  227. function Greg2JDate(Greg : GregType) : integer;
  228. var
  229.   G     : GregType;
  230. begin {Greg2JDate}
  231.   with G do begin
  232.     Year := Greg.Year;
  233.     Month := 1;
  234.     Day := 1;
  235.     end; {with G}
  236.   Greg2JDate := Greg2JDN(Greg) - Greg2JDN(G) + 1;
  237.   end; {Greg2JDate}
  238.  
  239.  
  240. {**************************************************************}
  241.  
  242. procedure JDate2Greg(JDate, Year : Integer;
  243.                   var Greg : GregType);
  244. var
  245.   G     : GregType;
  246. begin
  247.   with G do begin
  248.     Year := Greg.Year;
  249.     Month := 1;
  250.     Day := 1;
  251.     end; {with G}
  252.   JDN2Greg((Greg2JDN(G) + JDate - 1), Greg);
  253.   end; {JDate2Greg}
  254.  
  255.  
  256. {**************************************************************}
  257.  
  258. function DoW(Greg : GregType) : byte;
  259.              {computes the day of the week (Sunday = 0; Saturday = 6)
  260.              from the Gregorian date.}
  261. begin
  262.   DoW := (Greg2JDN(Greg) + 1) mod 7;
  263.   end; {DayOfWeek}
  264.  
  265. {**************************************************************}
  266.  
  267. procedure Today2Greg(var G : GregType);
  268. {Returns the system date as a Gregorian date record.}
  269.   var
  270.     R : registers;
  271.   begin
  272.     with R do begin
  273.       AH := $2A;
  274.       MsDos( R );
  275.       with G do begin
  276.         Year  := CX;
  277.         Month := DH;
  278.         Day   := DL;
  279.         end; {with G}
  280.       end; {with R}
  281.     end; {Today2Greg}
  282.  
  283. function Today  : LongInt;
  284. {Returns the system date as a Julian Day-Number}
  285.   var
  286.     G : GregType;
  287.   begin
  288.     Today2Greg(G);
  289.     Today := Greg2JDN(G);
  290.     end; {Today}
  291.  
  292. function Greg2Str(G : GregType; Delim : string) : string;
  293. {Returns a Gregorian date record as a string of the form MMdDDdYYYY,
  294.  where the separator, "d", is Delim[1].}
  295.   var
  296.     S1: string[4];
  297.     S2: string;
  298.     D : char;
  299.   begin
  300.     if Length(Delim) = 0 then
  301.       D := #0
  302.     else
  303.       D := Delim[1];
  304.     with G do begin
  305.       str(Month:2, S2); {Month}
  306.       str(Day:2, S1); {Day}
  307.       S2 := S2 + D + S1;
  308.       str(Year:4, S1); {Year}
  309.       S2 := S2 + D + S1;
  310.       end; {with R}
  311.     Greg2Str := RepAllF(DelAllF(S2, #0), ' ', '0');
  312.     end; {Greg2Str}
  313.  
  314. function Greg2ANSI(G : GregType) : string;
  315. {Returns the date as an ANSI date string (YYYYMMDD)}
  316.   var
  317.     S1: string[4];
  318.     S2: string;
  319.   begin
  320.     with G do begin
  321.       str(Year:4, S2);  {Year}
  322.       str(Month:2, S1); {Month}
  323.       S2 := S2 + S1;
  324.       str(Day:2, S1);   {Day}
  325.       S2 := S2 + S1;
  326.       end; {with G}
  327.     Greg2ANSI := RepAllF(S2, ' ', '0');
  328.     end; {Greg2ANSI}
  329.  
  330. function JDN2ANSI(JDN : LongInt) : string;
  331. {Returns the JDN as an ANSI date string (YYYYMMDD)}
  332.   var
  333.     G : GregType;
  334.   begin
  335.     JDN2Greg(JDN, G);
  336.     JDN2ANSI := Greg2ANSI(G);
  337.     end; {JDN2ANSI}
  338.  
  339. function Today2ANSI : string;
  340. {Returns the system date as an ANSI date string (YYYYMMDD)}
  341.   begin
  342.     Today2ANSI := JDN2ANSI(Today);
  343.     end; {Today2ANSI}
  344.  
  345. function JDN2Str(JDN : LongInt; Delim : string) : string;
  346. {Returns a Julian Day-Number as a MMdDDdYYYY string.}
  347.   var
  348.     G : GregType;
  349.   begin
  350.     JDN2Greg(JDN, G);
  351.     JDN2Str := Greg2Str(G, Delim);
  352.     end; {JDN2Str}
  353.  
  354. function TodayStr(Delim : string) : string;
  355. {Returns the system date as a string of the form MMdDDdYYYY, where the
  356.  separator, "d", is Delim[1].}
  357.   var
  358.     G : GregType;
  359.   begin
  360.     Today2Greg(G);
  361.     TodayStr := Greg2Str(G, Delim);
  362.     end; {TodayStr}
  363.  
  364. function Time2SSM(T : TimeType) : LongInt;
  365. {Returns a Time record as Seconds-Since-Midnight.}
  366.   var
  367.     L1,
  368.     L2,
  369.     L3 : LongInt;
  370.   begin
  371.     with T do begin
  372.       L1 := H;
  373.       L2 := M;
  374.       L3 := S;
  375.       Time2SSM := (3600 * L1) + (60 * L2) + L3;
  376.       end; {with T}
  377.     end; {Time2SSM}
  378.  
  379. function Now  : LongInt;
  380. {Returns the system time as Seconds-Since-Midnight.}
  381.   var
  382.     R : registers;
  383.     T : TimeType;
  384.   begin
  385.     with R do begin
  386.       AH := $2C;
  387.       MsDos( R );
  388.       with T do begin
  389.         H := CH;
  390.         M := CL;
  391.         S := DH;
  392.         end; {with T}
  393.       end; {with R}
  394.       Now := Time2SSM(T);
  395.     end; {Now}
  396.  
  397. procedure SSM2Time(SSM : LongInt; var T : TimeType);
  398. {Converts Seconds-Since-Midnight to a Time record.}
  399.   var
  400.     Q : LongInt;
  401.     R : byte;
  402.   begin
  403.     with T do begin
  404.       Q := SSM div 60;
  405.       S := SSM mod 60;  {Get SECONDS}
  406.       H := Q div 60;    {Get HOURS}
  407.       M := Q mod 60;    {Get MINUTES}
  408.       end; {with T}
  409.     end; {SSM2Time}
  410.  
  411. procedure Now2Time(var T : TimeType);
  412. {Returns the system time as a Time record.}
  413.   begin
  414.     SSM2Time(Now, T);
  415.     end; {Now2Time}
  416.  
  417. function Time2TimeStr(T : TimeType; Delim : string; T24 : boolean) : string;
  418. {Returns a Time record as a string of the form:
  419.           HHdMMdSSss    if Delim is non-empty and T24 (24 hour time) is
  420.                         false. The delimiter used, "d", is Delim[1]. The
  421.                         suffix, "ss", is "am" or "pm" as appropriate.
  422.           HHdMMdSS      if Delim is non-empty and T24 (24 hour time) is
  423.                         true. The delimiter used, "d", is Delim[1]. The
  424.                         time will be expressed in 24-hour form.
  425.           HHMMSSss      if Delim is empty and T24 (24 hour time) is
  426.                         false. The suffix, "ss", is "am" or "pm" as
  427.                         appropriate.
  428.           HHMM          if Delim is empty and T24 (24 hour time) is
  429.                         true. The time will be expressed in 24-hour form.
  430. }
  431.  var
  432.     S1: string[2];
  433.     S2: string;
  434.     AP: string[2];
  435.     D : char;
  436.   begin
  437.     if Length(Delim) = 0 then
  438.       D := #0
  439.     else
  440.       D := Delim[1];
  441.     with T do begin
  442.       if not T24 then
  443.         case H of
  444.           0     : begin
  445.                     H := 12;
  446.                     AP := 'am';
  447.                     end;
  448.           1..11 : begin
  449.                     AP := 'am';
  450.                     end;
  451.           12    : begin
  452.                     AP := 'pm';
  453.                     end;
  454.           13..23: begin
  455.                     H := H - 12;
  456.                     AP := 'pm';
  457.                     end;
  458.           end {case}
  459.       else
  460.         AP := '';
  461.       str(H:2, S2);
  462.       str(M:2, S1);
  463.       S2 := S2 + D + S1;
  464.       if (not T24) or (D <> #0) then begin
  465.         str(S:2, S1);
  466.         S2 := S2 + D + S1;
  467.         end;
  468.       end; {with R}
  469.     Time2TimeStr := RepAllF(DelAllF(S2, #0), ' ', '0') + AP;
  470.     end; {Time2TimeStr}
  471.  
  472. function NowStr(Delim : string; T24 : boolean) : string;
  473. {Returns the system time as a string of the form:
  474.           HHdMMdSSss    if Delim is non-empty and T24 (24 hour time) is
  475.                         false. The delimiter used, "d", is Delim[1]. The
  476.                         suffix, "ss", is "am" or "pm" as appropriate.
  477.           HHdMMdSS      if Delim is non-empty and T24 (24 hour time) is
  478.                         true. The delimiter used, "d", is Delim[1]. The
  479.                         time will be expressed in 24-hour form.
  480.           HHMMSSss      if Delim is empty and T24 (24 hour time) is
  481.                         false. The suffix, "ss", is "am" or "pm" as
  482.                         appropriate.
  483.           HHMM          if Delim is empty and T24 (24 hour time) is
  484.                         true. The time will be expressed in 24-hour form.
  485. }
  486.   var
  487.     R : Registers;
  488.     T : TimeType;
  489.   begin
  490.     with R do begin
  491.       AH := $2C;
  492.       MsDos( R );
  493.       with T do begin
  494.         H := CH;
  495.         M := CL;
  496.         S := DH;
  497.         NowStr := Time2TimeStr(T, Delim, T24);
  498.         end; {with T}
  499.       end; {with R}
  500.     end;{NowStr}
  501.  
  502. function SSM2TimeStr(SSM : LongInt; Delim : string; T24 : boolean) : string;
  503. {Returns Seconds-Since-Midnight as a string of the form:
  504.           HHdMMdSSss    if Delim is non-empty and T24 (24 hour time) is
  505.                         false. The delimiter used, "d", is Delim[1]. The
  506.                         suffix, "ss", is "am" or "pm" as appropriate.
  507.           HHdMMdSS      if Delim is non-empty and T24 (24 hour time) is
  508.                         true. The delimiter used, "d", is Delim[1]. The
  509.                         time will be expressed in 24-hour form.
  510.           HHMMSSss      if Delim is empty and T24 (24 hour time) is
  511.                         false. The suffix, "ss", is "am" or "pm" as
  512.                         appropriate.
  513.           HHMM          if Delim is empty and T24 (24 hour time) is
  514.                         true. The time will be expressed in 24-hour form.
  515. }
  516.   var
  517.     T : TimeType;
  518.   begin
  519.     SSM2Time(SSM, T);
  520.     SSM2TimeStr := Time2TimeStr(T, Delim, T24);
  521.     end; {SSM2TimeStr}
  522.   end.
  523.