home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / pcmag / vol9n21.zip / DGDATE.PAS < prev    next >
Pascal/Delphi Source File  |  1990-09-25  |  16KB  |  484 lines

  1. {
  2.  ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
  3.  █                                                                         █
  4.  █        TITLE :      DGDATE.TPU                                          █
  5.  █      PURPOSE :      Date/Time functions.                                █
  6.  █       AUTHOR :      David Gerrold, CompuServe ID:  70307,544            █
  7.  █ ______________________________________________________________________  █
  8.  █                                                                         █
  9.  █   Written in Turbo Pascal, Version 5.5,                                 █
  10.  █   with routines from TurboPower, Object Professional.                   █
  11.  █                                                                         █
  12.  █   Turbo Pascal is a product of Borland International.                   █
  13.  █   Object Professional is a product of TurboPower Software.              █
  14.  █ ______________________________________________________________________  █
  15.  █                                                                         █
  16.  █   This is not public domain software.                                   █
  17.  █   This software is copyright 1990, by David Gerrold.                    █
  18.  █   Permission is hereby granted for personal use.                        █
  19.  █                                                                         █
  20.  █        The Brass Cannon Corporation                                     █
  21.  █        9420 Reseda Blvd., #804                                          █
  22.  █        Northridge, CA  91324-2932.                                      █
  23.  █                                                                         █
  24.  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
  25.                                                                             }
  26. { Compiler Directives ===================================================== }
  27.  
  28. {$A-}    {Switch word alignment off, necessary for cloning}
  29. {$R-}    {Range checking off}
  30. {$B-}    {Boolean complete evaluation off}
  31. {$S-}    {Stack checking off}
  32. {$I-}    {I/O checking off}
  33. {$N+,E+} {Simulate numeric coprocessor}
  34. {$M 16384,0,327680} {stack and heap}
  35. {$V-}    {Variable range checking off}
  36.  
  37. { Name ==================================================================== }
  38.  
  39. UNIT DgDate;
  40. {
  41.   The purpose of DgDate is to provide the most commonly needed date and
  42.   time functions.
  43. }
  44.  
  45. { Interface =============================================================== }
  46.  
  47. INTERFACE
  48.  
  49. USES
  50. { Object Professional Units }
  51.   OpColor,
  52.   OpCrt,
  53.   OpString,
  54.   OpDate,
  55.  
  56. { Dg Units }
  57. dgfile,                                          { delete after debugging }
  58.   DgWryte,
  59.   DgSound,
  60.   DgStr;
  61.  
  62. { Declarations ============================================================ }
  63.  
  64. TYPE
  65.   TimeString = string [12];
  66.  
  67. CONST
  68.   ClockFlag : boolean = false;
  69.  
  70.   CkColor   : byte = LtRedOnBlack;               { clock attr color }
  71.   CkMono    : byte = LtGrayOnBlack;              { clock attr mono }
  72.  
  73.   PcTimeStr       = 'HH:mm:ss te';               { '10:36:09 pm' }
  74.   ClockTimeStr    = 'HH:mm te';                  { '10:36 pm' }
  75.   ShortTimeStr    = 'HH:mmt';                    { '10:36p' }
  76.   MilitaryTimeStr = 'hh:mm';                     { '22:36' }
  77.  
  78. VAR
  79.   TimeCheck : Time;                              { for counting }
  80.   TimeStr   : TimeString;                        { clock parameters }
  81.   ClockProc : Procedure;                         { which clock to use }
  82.   NoClock   : Procedure;                         { how to turn it off }
  83.  
  84.   LogOnTime : DateTimeRec;                       { time program began }
  85.  
  86. { ========================================================================= }
  87. { Clock Sounds ============================================================ }
  88.  
  89. PROCEDURE Chimes;
  90.  
  91. PROCEDURE TickTock;
  92.  
  93. { Time Functions ========================================================== }
  94.  
  95. PROCEDURE TimeToggle;                            { flips ClockFlag }
  96.  
  97. FUNCTION PcTime    : TimeString;                 { '10:36:09 pm' }
  98.  
  99. FUNCTION ClockTime : TimeString;                 { '10:36 pm' }
  100.  
  101. FUNCTION ShortTime : TimeString;                 { '10:36p' }
  102.  
  103. FUNCTION MilitaryTime : TimeString;              { '22:36' }
  104.  
  105. { Date Functions ---------------------------------------------------------- }
  106.  
  107. FUNCTION DayOfTheWeek (D : Date)  : TimeString;  { returns 'Sunday' }
  108.  
  109. FUNCTION DayOfTheWeek3 (D : Date) : TimeString;  { returns 'Sun' }
  110.  
  111. FUNCTION PcDate   : TimeString;                  { '01-Apr-90' }
  112.  
  113. FUNCTION StarDate : TimeString;                  { '9004.01' }
  114.  
  115. FUNCTION LogDate  : TimeString;                  { 'Apr 1, 1990' }
  116.  
  117. FUNCTION FormalDate : DateString;                { 'April 5, 1988' }
  118.  
  119. FUNCTION FullDate   : DateString;                { 'Tuesday, March 5, 1988' }
  120.  
  121. FUNCTION TimeStamp  : DateString;                { 'Tue, Mar-05-88, 11:01p' }
  122.  
  123. FUNCTION DateTimeToSortString (D : Date;  T : Time) : DateString;
  124.  
  125. { Time Display Procedures ------------------------------------------------- }
  126.  
  127. PROCEDURE ShowClock;
  128.  
  129. PROCEDURE ShowTimeString (S : DateString);
  130.  
  131. PROCEDURE ShowTime;
  132.  
  133. PROCEDURE ShowTimeStamp;
  134.  
  135. PROCEDURE ShowToday;
  136.  
  137. { Parsing functions ------------------------------------------------------- }
  138.  
  139. FUNCTION ParseDate (S : string) : Date;
  140. { Parses a date out of a string. }
  141.  
  142. FUNCTION ParseBirthday (S : string) : Date;
  143. { Parses a date out of a string. }
  144.  
  145. { Implementation ========================================================== }
  146.  
  147. IMPLEMENTATION
  148.  
  149. { ========================================================================= }
  150. { Declarations ============================================================ }
  151.  
  152. CONST
  153.   ShowTimeLen : byte = 0;                        { len of ShowTime string }
  154.  
  155.   BeepFlag : boolean = true;
  156. {
  157.   The BeepFlag is to insure that the Chimes procedure only beeps once per
  158.   hour.  Otherwise, the routine might be called several times, resulting
  159.   in a one-second burst of sound.
  160. }
  161.  
  162. { Chimes ================================================================== }
  163.  
  164. PROCEDURE Chimes;
  165.  
  166. BEGIN
  167.   if                                             { if }
  168.     (CurrentTime mod 3600 = 0)                   { hour and flag }
  169.       and
  170.     BeepFlag
  171.   then begin                                     { then }
  172.     BeepBeep;                                    { make noise }
  173.     BeepFlag := false;                           { turn flag off }
  174.     end;
  175.   if                                             { if }
  176.     CurrentTime mod 3600 <> 0                    { not hour }
  177.   then
  178.     BeepFlag := true;                            { turn flag on }
  179. END;
  180.  
  181. { TickTock ================================================================ }
  182.  
  183. PROCEDURE TickTock;
  184.   PROCEDURE MakeTick;
  185.   { alternates ticking and tocking }
  186.   CONST
  187.     Tick     = 440;
  188.     Tock     = 880;
  189.     TickFreq : word = Tick;
  190.  
  191.   BEGIN
  192.     if not Sfx (SfxCues) then exit;
  193.     Sound (TickFreq);
  194.     Delay (2);
  195.     NoSound;
  196.     if TickFreq = Tock then
  197.       TickFreq := Tick
  198.     else
  199.       TickFreq := Tock;
  200.   END;
  201.  
  202. BEGIN
  203.   if
  204.     (CurrentTime < TimeCheck)                    { or midnight has passed }
  205.   then
  206.     TimeCheck := CurrentTime;                    { reset time }
  207.   if CurrentTime > TimeCheck then begin          { time to ticktock? }
  208.     MakeTick;
  209.     TimeCheck := CurrentTime;
  210.     end;
  211. END;
  212.  
  213. { TimeToggle ============================================================== }
  214.  
  215. PROCEDURE TimeToggle;
  216. BEGIN
  217.   ClockFlag := not ClockFlag;
  218.   If not ClockFlag then
  219.     NoClock;
  220. END;
  221.  
  222. { PcTime ================================================================== }
  223.  
  224. FUNCTION PcTime : TimeString;                    { '10:36:09 pm' }
  225. BEGIN
  226.   PcTime := CurrentTimeString (PcTimeStr);
  227. END;
  228.  
  229. { ClockTime =============================================================== }
  230.  
  231. FUNCTION ClockTime : TimeString;                 { '10:36 pm' }
  232. BEGIN
  233.   ClockTime := CurrentTimeString (ClockTimeStr);
  234. END;
  235.  
  236. { ShortTime =============================================================== }
  237.  
  238. FUNCTION ShortTime : TimeString;                 { '9:07p' }
  239. BEGIN
  240.   ShortTime := CurrentTimeString (ShortTimeStr);
  241. END;
  242.  
  243. { MilitaryTime ============================================================ }
  244.  
  245. FUNCTION MilitaryTime : TimeString;              { '21:07' }
  246. BEGIN
  247.   MilitaryTime := CurrentTimeString (MilitaryTimeStr);
  248. END;
  249.  
  250. { DayOfTheWeek ============================================================ }
  251.  
  252. FUNCTION DayOfTheWeek (D : Date) : TimeString;   { returns 'Tuesday' }
  253. BEGIN
  254.   DayOfTheWeek := DayString [DayOfWeek (D)];
  255. END;
  256.  
  257. { DayOfTheWeek3 =========================================================== }
  258.  
  259. FUNCTION DayOfTheWeek3 (D : Date) : TimeString;  { returns 'Tue' }
  260. BEGIN
  261.   DayOfTheWeek3 := Copy (DayOfTheWeek (D), 1, 3);
  262. END;
  263.  
  264. { PcDate ================================================================== }
  265.  
  266. FUNCTION PcDate : TimeString;                    { '05-Mar-88' }
  267. BEGIN
  268.   PcDate := DateToDateString ('dd-nnn-yy', Today);
  269. END;
  270.  
  271. { StarDate ================================================================ }
  272.  
  273. FUNCTION StarDate : TimeString;                  { '8803.05' }
  274. BEGIN
  275.   StarDate := DateToDateString ('yymm.dd', Today);
  276. END;
  277.  
  278. { LogDate ================================================================= }
  279.  
  280. FUNCTION LogDate : TimeString;                   { 'Mar 5, 1988' }
  281. BEGIN
  282.   LogDate := DateToDateString ('nnn ', Today) +
  283.              TrimLead (DateToDateString ('DD, yyyy', Today));
  284. END;
  285.  
  286. { FormalDate ============================================================== }
  287.  
  288. FUNCTION FormalDate : DateString;                { 'March 5, 1988' }
  289. BEGIN
  290.   FormalDate := TrimTrail (DateToDateString ('nnnnnnnnn', Today)) + ' ' +
  291.                 TrimLead (DateToDateString ('DD, yyyy', Today));
  292. END;
  293.  
  294. { FullDate ================================================================ }
  295.  
  296. FUNCTION FullDate : DateString;                  { 'Tuesday, March 5, 1988' }
  297. BEGIN
  298.   FullDate := DayOfTheWeek (Today) + ', ' + FormalDate;
  299. END;
  300.  
  301. { TimeStamp =============================================================== }
  302.  
  303. FUNCTION TimeStamp : DateString;                 { 'Tue, Mar-05-88, 11:01p' }
  304. BEGIN
  305.   TimeStamp := DayOfTheWeek3 (Today) + ', ' +
  306.                DateToDateString ('nnn-dd-yy, ', Today) +
  307.                CurrentTimeString ('hh:mmt');
  308. END;
  309.  
  310. { DateTimeToSortString ==================================================== }
  311.  
  312. FUNCTION DateTimeToSortString (D : Date;  T : Time) : DateString;
  313. { for database programs }
  314. BEGIN
  315.   DateTimeToSortString := DateToSortString (D) + TimeToSortString (T);
  316. END;
  317.  
  318. { ShowClock =============================================================== }
  319.  
  320. PROCEDURE ShowClock;
  321. {
  322.   Beeps on the hour, if FxBeep byte is set in FxOptions.
  323.   Calls procedure stored in ClockProc variable to display time on screen.
  324.   User can substitute his own display function by assigning a new
  325.   procedure to ClockProc:
  326.  
  327.     ClockProc := MyProcedure;
  328. }
  329.  
  330. BEGIN
  331.   if not ClockFlag then exit;                    { no clock }
  332.   ClockProc;                                     { show time on screen }
  333.   Chimes;
  334. END;
  335.  
  336. { EraseTimeString ========================================================= }
  337.  
  338. {$F+} PROCEDURE EraseTimeString; {$F-}
  339. { erases time or date from screen }
  340.  
  341. BEGIN
  342.   FastFlushAbs (CharStr (' ', ShowTimeLen), 1, ColorMono (CkColor, CkMono));
  343. END;
  344.  
  345. { ShowTimeString ========================================================== }
  346.  
  347. {$F+} PROCEDURE ShowTimeString (S : DateString); {$F-}
  348. { shows user-formatted time or date }
  349. VAR
  350.   Len : byte absolute S;
  351.  
  352. BEGIN
  353.   ShowTimeLen := Len;
  354.   FastFlushAbs (S, 1, ColorMono (CkColor, CkMono));
  355. END;
  356.  
  357. { ShowTime ================================================================ }
  358.  
  359. {$F+} PROCEDURE ShowTime; {$F-}
  360. { shows PcTime on screen }
  361.  
  362. BEGIN
  363.   ShowTimeString (CurrentTimeString (TimeStr));
  364. END;
  365.  
  366. { ShowTimeStamp =========================================================== }
  367.  
  368. {$F+} PROCEDURE ShowTimeStamp; {$F-}
  369. { Puts time and date in the upper right corner of the screen on 1 line }
  370. BEGIN
  371.   ShowTimeString (DayOfTheWeek3 (Today) + ' ' +
  372.                   DateToDateString ('mm-dd-yy, ', Today) +
  373.                   CurrentTimeString ('HH:mm:sst'));
  374. END;
  375.  
  376. { ShowToday =============================================================== }
  377.  
  378. {$F+} PROCEDURE ShowToday; {$F-}
  379. { Puts time and date in the upper right corner of the screen on 2 lines }
  380. BEGIN
  381.   FastFlushAbs (LogDate, 1, ColorMono (CkColor, CkMono));
  382.   FastFlushAbs (PcTime, 2, ColorMono (CkColor, CkMono));
  383. END;
  384.  
  385. { ParseMonth ============================================================== }
  386.  
  387. FUNCTION ParseMonth (VAR S : DateString) : byte;
  388. { If S contains month name, returns month number, else returns first number }
  389.  
  390. VAR
  391.   Loop : word;
  392.  
  393. BEGIN
  394.   ParseMonth := 0;
  395.   S := StUpCase (S);
  396.   Loop := 1;
  397.   While                                          { look for month }
  398.     (Pos (StUpCase (Copy (MonthString [Loop], 1, 3)), S) = 0)
  399.       and
  400.     (Loop < 13)                                  { as a string }
  401.   do
  402.     inc (Loop);
  403.  
  404.   If Loop > 12 then                              { else }
  405.     Loop := ExtractFirstNumber (S);              { get month as number }
  406.  
  407.   If Loop < 13 then
  408.     ParseMonth := Loop;
  409. END;
  410.  
  411. { ParseDate =============================================================== }
  412.  
  413. FUNCTION ParseDate (S : string) : Date;
  414. { Parses a date out of a string. }
  415.  
  416. VAR
  417.   M, D, Y   : integer;
  418.  
  419. BEGIN
  420.   M := ParseMonth (S);                           { get month }
  421.   D := ExtractFirstNumber (S);                   { get day }
  422.   Y := ExtractFirstNumber (S);                   { get year }
  423.   If ContainsNumber (S) then                     { if still more numbers }
  424.                                                  { then it's invalid }
  425.     ParseDate := BadDate
  426.   else
  427.     ParseDate := DMYtoDate (D, M, Y);
  428. END;
  429.  
  430. { ParseBirthday =========================================================== }
  431.  
  432. FUNCTION ParseBirthday (S : string) : Date;
  433. { Parses a date out of a string. }
  434.  
  435. VAR
  436.   D : Date;
  437.  
  438. BEGIN
  439.   D := ParseDate (S);                            { get date }
  440.   If D > Today then
  441.     D := IncDateTrunc (D, 0, -100);              { check for century }
  442.   ParseBirthday := D;
  443. END;
  444.  
  445. { ========================================================================= }
  446. { Initialization ========================================================== }
  447.  
  448. BEGIN
  449. {
  450.   Initialize the ClockProc variable;  tell it which clock display procedure
  451.   to use.  Log what time the program started.
  452. }
  453.   TimeStr     := PcTimeStr;
  454.   ClockProc   := ShowTime;
  455.   NoClock     := EraseTimeString;
  456.  
  457.   TimeCheck   := CurrentTime;                    { initialize variable }
  458.   LogOnTime.T := CurrentTime;                    { what time did we start? }
  459.   LogOntime.D := Today;                          { what day is today? }
  460. END.
  461.  
  462. { ========================================================================= }
  463. { DgDate History ========================================================== }
  464.  
  465. VERSION HISTORY:
  466.   9005.05
  467.     Completely restructured for consistency with Object Professional.
  468.  
  469.   9005.06
  470.     Added TimeToggle and NoClock procedures.
  471.  
  472. { DgTime Needs ============================================================ }
  473.  
  474. NEED TO ADD:
  475.   Can't think of anything ....
  476.  
  477. { Bug Reports ============================================================= }
  478.  
  479. BUGS:
  480.   Don't be silly.
  481.  
  482. { ========================================================================= }
  483. { ========================================================================= }
  484.