home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / top2src.zip / TOPLINK.ZIP / UNIXDATE.PAS < prev    next >
Pascal/Delphi Source File  |  1996-03-16  |  9KB  |  253 lines

  1.  
  2. (***************************************************************************)
  3. (* UNIX DATE Version 1.01                                                  *)
  4. (* This unit provides access to UNIX date related functions and procedures *)
  5. (* A UNIX date is the number of seconds from January 1, 1970. This unit    *)
  6. (* may be freely used. If you modify the source code, please do not        *)
  7. (* distribute your enhancements.                                           *)
  8. (* (C) 1991-1993 by Brian Stark.                                           *)
  9. (* This is a programming release from Digital Illusions                    *)
  10. (* FidoNet 1:289/27.2 + Columbia, MO - USA                                 *)
  11. (* Revision History                                                        *)
  12. (* ----------------------------------------------------------------------- *)
  13. (* 06-13-1993 1.02 | Minor code cleanup                                    *)
  14. (* 05-23-1993 1.01 | Added a few more routines for use with ViSiON BBS     *)
  15. (* ??-??-1991 1.00 | First release                                         *)
  16. (* ----------------------------------------------------------------------- *)
  17. (***************************************************************************)
  18.  
  19. Unit UnixDate;
  20.  
  21. INTERFACE
  22.  
  23. Uses
  24.    DOS;
  25.  
  26. Function  GetTimeZone : ShortInt;
  27.   {Returns the value from the enviroment variable "TZ". If not found, UTC is
  28.    assumed, and a value of zero is returned}
  29. Function  IsLeapYear(Source : Word) : Boolean;
  30.   {Determines if the year is a leap year or not}
  31. Function  Norm2Unix(Y, M, D, H, Min, S : Word) : LongInt;
  32.   {Convert a normal date to its UNIX date. If environment variable "TZ" is
  33.    defined, then the input parameters are assumed to be in **LOCAL TIME**}
  34. Procedure Unix2Norm(Date : LongInt; Var Y, M, D, H, Min, S : Word);
  35.   {Convert a UNIX date to its normal date counterpart. If the environment
  36.    variable "TZ" is defined, then the output will be in **LOCAL TIME**}
  37.  
  38. Function  TodayInUnix : LongInt;
  39.   {Gets today's date, and calls Norm2Unix}
  40. {
  41.  Following returns a string and requires the TechnoJock totSTR unit.
  42. Function  Unix2Str(N : LongInt) : String;
  43. }
  44. Const
  45.   DaysPerMonth :
  46.     Array[1..12] of ShortInt = (031,028,031,030,031,030,031,031,030,031,030,031);
  47.   DaysPerYear  :
  48.     Array[1..12] of Integer  = (031,059,090,120,151,181,212,243,273,304,334,365);
  49.   DaysPerLeapYear :
  50.     Array[1..12] of Integer  = (031,060,091,121,152,182,213,244,274,305,335,366);
  51.   SecsPerYear      : LongInt  = 31536000;
  52.   SecsPerLeapYear  : LongInt  = 31622400;
  53.   SecsPerDay       : LongInt  = 86400;
  54.   SecsPerHour      : Integer  = 3600;
  55.   SecsPerMinute    : ShortInt = 60;
  56.  
  57. IMPLEMENTATION
  58.  
  59. Function GetTimeZone : ShortInt;
  60. Var
  61.   Environment : String;
  62.   Index : Integer;
  63. Begin
  64.   GetTimeZone := 0;                            {Assume UTC}
  65.   Environment := GetEnv('TZ');       {Grab TZ string}
  66.   For Index := 1 To Length(Environment) Do
  67.     Environment[Index] := Upcase(Environment[Index]);
  68. (*
  69.   NOTE: I have yet to find a complete list of the ISO table of time zone
  70.         abbreviations. The following is excerpted from the Opus-Cbcs
  71.         documentation files.
  72. *)
  73.   If Environment =  'EST05'    Then GetTimeZone := -05; {USA EASTERN}
  74.   If Environment =  'EST05EDT' Then GetTimeZone := -06;
  75.   If Environment =  'CST06'    Then GetTimeZone := -06; {USA CENTRAL}
  76.   If Environment =  'CST06CDT' Then GetTimeZone := -07;
  77.   If Environment =  'MST07'    Then GetTimeZone := -07; {USA MOUNTAIN}
  78.   If Environment =  'MST07MDT' Then GetTimeZone := -08;
  79.   If Environment =  'PST08'    Then GetTimeZone := -08;
  80.   If Environment =  'PST08PDT' Then GetTimeZone := -09;
  81.   If Environment =  'YST09'    Then GetTimeZone := -09;
  82.   If Environment =  'AST10'    Then GetTimeZone := -10;
  83.   If Environment =  'BST11'    Then GetTimeZone := -11;
  84.   If Environment =  'CET-1'    Then GetTimeZone :=  01;
  85.   If Environment =  'CET-01'   Then GetTimeZone :=  01;
  86.   If Environment =  'EST-10'   Then GetTimeZone :=  10;
  87.   If Environment =  'WST-8'    Then GetTimeZone :=  08; {Perth, Western Austrailia}
  88.   If Environment =  'WST-08'   Then GetTimeZone :=  08;
  89. End;
  90.  
  91. Function IsLeapYear(Source : Word) : Boolean;
  92. Begin
  93. (*
  94.   NOTE: This is wrong!
  95. *)
  96.   If (Source Mod 4 = 0) Then
  97.     IsLeapYear := True
  98.   Else
  99.     IsLeapYear := False;
  100. End;
  101.  
  102. Function Norm2Unix(Y,M,D,H,Min,S : Word) : LongInt;
  103. Var
  104.   UnixDate : LongInt;
  105.   Index    : Word;
  106. Begin
  107.   UnixDate := 0;                                                 {initialize}
  108.   Inc(UnixDate,S);                                              {add seconds}
  109.   Inc(UnixDate,(SecsPerMinute * Min));                          {add minutes}
  110.   Inc(UnixDate,(SecsPerHour * H));                                {add hours}
  111.   (*************************************************************************)
  112.   (* If UTC = 0, and local time is -06 hours of UTC, then                  *)
  113.   (* UTC := UTC - (-06 * SecsPerHour)                                      *)
  114.   (* Remember that a negative # minus a negative # yields a positive value *)
  115.   (*************************************************************************)
  116.   UnixDate := UnixDate - (GetTimeZone * SecsPerHour);            {UTC offset}
  117.  
  118.   If D > 1 Then                                 {has one day already passed?}
  119.     Inc(UnixDate,(SecsPerDay * (D-1)));
  120.  
  121.   If IsLeapYear(Y) Then
  122.     DaysPerMonth[02] := 29
  123.   Else
  124.     DaysPerMonth[02] := 28;                             {Check for Feb. 29th}
  125.  
  126.   Index := 1;
  127.   If M > 1 Then For Index := 1 To (M-1) Do    {has one month already passed?}
  128.     Inc(UnixDate,(DaysPerMonth[Index] * SecsPerDay));
  129.  
  130.   While Y > 1970 Do
  131.   Begin
  132.     If IsLeapYear((Y-1)) Then
  133.       Inc(UnixDate,SecsPerLeapYear)
  134.     Else
  135.       Inc(UnixDate,SecsPerYear);
  136.     Dec(Y,1);
  137.   End;
  138.  
  139.   Norm2Unix := UnixDate;
  140. End;
  141.  
  142. Procedure Unix2Norm(Date : LongInt; Var Y, M, D, H, Min, S : Word);
  143. {}
  144. Var
  145.   LocalDate : LongInt;
  146.   Done      : Boolean;
  147.   X         : ShortInt;
  148.   TotDays   : Integer;
  149. Begin
  150.   Y   := 1970;
  151.   M   := 1;
  152.   D   := 1;
  153.   H   := 0;
  154.   Min := 0;
  155.   S   := 0;
  156.   LocalDate := Date + (GetTimeZone * SecsPerHour);         {Local time date}
  157.  (*************************************************************************)
  158.  (* Sweep out the years...                                                *)
  159.  (*************************************************************************)
  160.   Done := False;
  161.   While Not Done Do
  162.   Begin
  163.     If LocalDate >= SecsPerYear Then
  164.     Begin
  165.       Inc(Y,1);
  166.       Dec(LocalDate,SecsPerYear);
  167.     End
  168.     Else
  169.       Done := True;
  170.  
  171.     If (IsLeapYear(Y+1)) And (LocalDate >= SecsPerLeapYear) And
  172.        (Not Done) Then
  173.     Begin
  174.       Inc(Y,1);
  175.       Dec(LocalDate,SecsPerLeapYear);
  176.     End;
  177.   End;
  178.   (*************************************************************************)
  179.   M := 1;
  180.   D := 1;
  181.   Done := False;
  182.   TotDays := LocalDate Div SecsPerDay;
  183.   If IsLeapYear(Y) Then
  184.   Begin
  185.     DaysPerMonth[02] := 29;
  186.     X := 1;
  187.     Repeat
  188.       If (TotDays <= DaysPerLeapYear[x]) Then
  189.       Begin
  190.         M := X;
  191.         Done := True;
  192.         Dec(LocalDate,(TotDays * SecsPerDay));
  193.         D := DaysPerMonth[M]-(DaysPerLeapYear[M]-TotDays) + 1;
  194.       End
  195.       Else
  196.         Done := False;
  197.       Inc(X);
  198.     Until (Done) or (X > 12);
  199.   End
  200.   Else
  201.   Begin
  202.     DaysPerMonth[02] := 28;
  203.     X := 1;
  204.     Repeat
  205.       If (TotDays <= DaysPerYear[x]) Then
  206.       Begin
  207.         M := X;
  208.         Done := True;
  209.         Dec(LocalDate,(TotDays * SecsPerDay));
  210.         D := DaysPerMonth[M]-(DaysPerYear[M]-TotDays) + 1;
  211.       End
  212.       Else
  213.         Done := False;
  214.       Inc(X);
  215.     Until Done = True or (X > 12);
  216.   End;
  217.   H := LocalDate Div SecsPerHour;
  218.     Dec(LocalDate,(H * SecsPerHour));
  219.   Min := LocalDate Div SecsPerMinute;
  220.     Dec(LocalDate,(Min * SecsPerMinute));
  221.   S := LocalDate;
  222. End;
  223.  
  224. Function  TodayInUnix : LongInt;
  225. Var
  226.   Year, Month, Day, DayOfWeek: Word;
  227.   Hour, Minute, Second, Sec100: Word;
  228. Begin
  229.   GetDate(Year, Month, Day, DayOfWeek);
  230.   GetTime(Hour, Minute, Second, Sec100);
  231.   TodayInUnix := Norm2Unix(Year,Month,Day,Hour,Minute,Second);
  232. End;
  233.  
  234. {Function  Unix2Str(N : LongInt) : String;
  235. Var
  236.   Year, Month, Day, DayOfWeek  : Word;
  237.   Hour, Minute, Second, Sec100 : Word;
  238.   T : String;
  239. Begin
  240.   Unix2Norm(N, Year, Month, Day, Hour, Minute, Second);
  241.   T := PadRight(IntToStr(Month),2,'0')+'-'+PadRight(IntToStr(Day),2,'0')+'-'+
  242.        PadRight(IntToStr(Year),2,'0')+' '+ PadRight(IntToStr(Hour),2,'0')+':'+
  243.        PadRight(IntToStr(Minute),2,'0')+':'+PadRight(IntToStr(Second),2,'0');
  244.   If Hour > 12 Then
  245.     T := T + ' PM'
  246.   Else
  247.     T := T + ' AM';
  248.   Unix2Str := T;
  249. End;}
  250.  
  251.  
  252. END.
  253.