home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / pascal / shdk_1.zip / SHDATPK.PAS < prev    next >
Pascal/Delphi Source File  |  1992-03-24  |  16KB  |  515 lines

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