home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / PASCAL / PARADIS1 / UNIXDATE.PAS < prev    next >
Pascal/Delphi Source File  |  1992-02-25  |  16KB  |  425 lines

  1. (10447) Sun 26 Jan 92 12:47
  2. By: Brian Stark
  3. To: All
  4. Re: Here's source code for UNIX-dates...
  5. St:
  6. ---------------------------------------------------------------------------
  7. @EID:de88 298302dd
  8. @MSGID: 1:289/3.8@fidonet.org 298302dd
  9. @CHARSET: PC-8
  10. Unit UnixDate;
  11. (***************************************************************************)
  12. (* UNIX DATE Version 1.00                                                  *)
  13. (* This unit provides access to UNIX date related functions and procedures *)
  14. (* A UNIX date is the number of seconds from January 1, 1970. This unit    *)
  15. (* may be freely used. If you modify the source code, please do not        *)
  16. (* distribute your enhancements.                                           *)
  17. (* (C) 1991 by Brian Stark.                                                *)
  18. (* A programming release from Desert Fox Productions                       *)
  19. (* FidoNet 1:289/3.8 + Columbia, MO - USA                                  *)
  20. (* ----------------------------------------------------------------------- *)
  21. (* bstark@pro-aasgard.cts.com                                              *)
  22. (* brian.stark@p8.f3.n289.z1.fidonet.org                                   *)
  23. (***************************************************************************)
  24. INTERFACE
  25. Uses
  26.    DOS;
  27. Function  GetTimeZone : ShortInt;
  28.   {Returns the value from the enviroment variable "TZ". If not found, UTC is
  29.    assumed, and a value of zero is returned}
  30. Function  IsLeapYear(Source : Word) : Boolean;
  31.   {Determines if the year is a leap year or not}
  32. Function  Norm2Unix(Y, M, D, H, Min, S : Word) : LongInt;
  33.   {Convert a normal date to its UNIX date. If environment variable "TZ" is
  34.    defined, then the input parameters are assumed to be in **LOCAL TIME**}
  35. Procedure Unix2Norm(Date : LongInt; Var Y, M, D, H, Min, S : Word);
  36.   {Convert a UNIX date to its normal date counterpart. If the environment
  37.    variable "TZ" is defined, then the output will be in **LOCAL TIME**}
  38. Const
  39.   DaysPerMonth :
  40.     Array[1..12] of ShortInt = (031,028,031,030,031,030,031,031,030,031,030,
  41. 031);
  42.   DaysPerYear  :
  43.     Array[1..12] of Integer  = (031,059,090,120,151,181,212,243,273,304,334,
  44. 365);
  45.   DaysPerLeapYear :
  46.     Array[1..12] of Integer  = (031,060,091,121,152,182,213,244,274,305,335,
  47. 366);
  48.   SecsPerYear      : LongInt  = 31536000;
  49.   SecsPerLeapYear  : LongInt  = 31622400;
  50.   SecsPerDay       : LongInt  = 86400;
  51.   SecsPerHour      : Integer  = 3600;
  52.   SecsPerMinute    : ShortInt = 60;
  53. IMPLEMENTATION Function GetTimeZone : ShortInt;
  54. {}
  55. Var
  56.   Environment : String;
  57.   Index : Integer;
  58. Begin
  59.   GetTimeZone := 0;                            {Assume UTC}
  60.   Environment := GetEnv('TZ');       {Grab TZ string}
  61.   For Index := 1 To Length(Environment) Do
  62.     Environment[Index] := Upcase(Environment[Index]);
  63.   If Environment =  'EST05'    Then GetTimeZone := -05; {USA EASTERN}
  64.   If Environment =  'EST05EDT' Then GetTimeZone := -06;
  65.   If Environment =  'CST06'    Then GetTimeZone := -06; {USA CENTRAL}
  66.   If Environment =  'CST06CDT' Then GetTimeZone := -07;
  67.   If Environment =  'MST07'    Then GetTimeZone := -07; {USA MOUNTAIN}
  68.   If Environment =  'MST07MDT' Then GetTimeZone := -08;
  69.   If Environment =  'PST08'    Then GetTimeZone := -08;
  70.   If Environment =  'PST08PDT' Then GetTimeZone := -09;
  71.   If Environment =  'YST09'    Then GetTimeZone := -09;
  72.   If Environment =  'AST10'    Then GetTimeZone := -10;
  73.   If Environment =  'BST11'    Then GetTimeZone := -11;
  74.   If Environment =  'CET-1'    Then GetTimeZone :=  01;
  75.   If Environment =  'CET-01'   Then GetTimeZone :=  01;
  76.   If Environment =  'EST-10'   Then GetTimeZone :=  10;
  77.   If Environment =  'WST-8'    Then GetTimeZone :=  08; {Perth, Western
  78. Austrailia}
  79.   If Environment =  'WST-08'   Then GetTimeZone :=  08;
  80. End; Function IsLeapYear(Source : Word) : Boolean;
  81. {}
  82. Begin
  83.   If (Source Mod 4 = 0) Then
  84.     IsLeapYear := True
  85.   Else
  86.     IsLeapYear := False;
  87. End; Function Norm2Unix(Y,M,D,H,Min,S : Word) : LongInt;
  88. {}
  89. Var
  90.   UnixDate : LongInt;
  91.   Index    : Word;
  92. Begin
  93.   UnixDate := 0;                                                 {initialize}
  94.   Inc(UnixDate,S);                                              {add seconds}
  95.   Inc(UnixDate,(SecsPerMinute * Min));                          {add minutes}
  96.   Inc(UnixDate,(SecsPerHour * H));                                {add hours}
  97.   (*************************************************************************)
  98.   (* If UTC = 0, and local time is -06 hours of UTC, then                  *)
  99.   (* UTC := UTC - (-06 * SecsPerHour)                                      *)
  100.   (* Remember that a negative # minus a negative # yields a positive value *)
  101.   (*************************************************************************)
  102.   UnixDate := UnixDate - (GetTimeZone * SecsPerHour);            {UTC offset}
  103.   If D > 1 Then                                 {has one day already passed?}
  104.     Inc(UnixDate,(SecsPerDay * (D-1)));
  105.   If IsLeapYear(Y) Then
  106.     DaysPerMonth[02] := 29
  107.   Else
  108.     DaysPerMonth[02] := 28;                             {Check for Feb. 29th}
  109.   Index := 1;
  110.   If M > 1 Then For Index := 1 To (M-1) Do    {has one month already passed?}
  111.     Inc(UnixDate,(DaysPerMonth[Index] * SecsPerDay));
  112.   While Y > 1970 Do
  113.   Begin
  114.     If IsLeapYear((Y-1)) Then
  115.       Inc(UnixDate,SecsPerLeapYear)
  116.     Else
  117.       Inc(UnixDate,SecsPerYear);
  118.     Dec(Y,1);
  119.   End;
  120.   Norm2Unix := UnixDate;
  121. End; Procedure Unix2Norm(Date : LongInt; Var Y, M, D, H, Min, S : Word);
  122. {}
  123. Var
  124.   LocalDate : LongInt; Done : Boolean; X : ShortInt; TotDays : Integer;
  125. Begin
  126.   Y   := 1970; M := 1; D := 1; H := 0; Min := 0; S := 0;
  127.   LocalDate := Date + (GetTimeZone * SecsPerHour);         {Local time date}
  128.  (*************************************************************************)
  129.  (* Sweep out the years...                                                *)
  130.  (*************************************************************************)
  131.   Done := False;
  132.   While Not Done Do
  133.   Begin
  134.     If LocalDate >= SecsPerYear Then
  135.     Begin
  136.       Inc(Y,1);
  137.       Dec(LocalDate,SecsPerYear);
  138.     End
  139.     Else
  140.       Done := True;
  141.     If (IsLeapYear(Y+1)) And (LocalDate >= SecsPerLeapYear) And
  142.        (Not Done) Then
  143.     Begin
  144.       Inc(Y,1);
  145.       Dec(LocalDate,SecsPerLeapYear);
  146.     End;
  147.   End;
  148.   (*************************************************************************)
  149.   M := 1; D := 1;
  150.   Done := False;
  151.   TotDays := LocalDate Div SecsPerDay;
  152.   If IsLeapYear(Y) Then
  153.   Begin
  154.     DaysPerMonth[02] := 29;
  155.     X := 1;
  156.     Repeat
  157.       If (TotDays <= DaysPerLeapYear[x]) Then
  158.       Begin
  159.         M := X;
  160.         Done := True;
  161.         Dec(LocalDate,(TotDays * SecsPerDay));
  162.         D := DaysPerMonth[M]-(DaysPerLeapYear[M]-TotDays) + 1;
  163.       End
  164.       Else
  165.         Done := False;
  166.       Inc(X);
  167.     Until (Done) or (X > 12);
  168.   End
  169.   Else
  170.   Begin
  171.     DaysPerMonth[02] := 28;
  172.     X := 1;
  173.     Repeat
  174.       If (TotDays <= DaysPerYear[x]) Then
  175.       Begin
  176.         M := X;
  177.         Done := True;
  178.         Dec(LocalDate,(TotDays * SecsPerDay));
  179.         D := DaysPerMonth[M]-(DaysPerYear[M]-TotDays) + 1;
  180.       End
  181.       Else
  182.         Done := False;
  183.       Inc(X);
  184.     Until Done = True or (X > 12);
  185.   End;
  186.   H := LocalDate Div SecsPerHour;
  187.     Dec(LocalDate,(H * SecsPerHour));
  188.   Min := LocalDate Div SecsPerMinute;
  189.     Dec(LocalDate,(Min * SecsPerMinute));
  190.   S := LocalDate;
  191. End; END.
  192.  
  193.  
  194. --- Opus-CBCS 1.73a
  195.  * Origin: Desert Fox Productions - Columbia, MO - USA (1:289/3.8)
  196.  
  197. @PATH: 30847/8 289/3 290/4 627 13/13 396/1 170/400 512/0 1007 
  198.  
  199. ---------------------------------------------------------------------------
  200. (7243)  Thu 20 Feb 92  8:14
  201. By: Brian Stark
  202. To: Josh Lippy
  203. Re: Re: UNIX Date Interpreter
  204. St:
  205. ---------------------------------------------------------------------------
  206. @EID:de88 29a3b7db
  207. @CHARSET: PC-8
  208.  > JL: ^aEID:9a66 18518b80
  209.  > JL: ^aMSGID: 1:109/423.0 29a03720
  210.  > JL:    I read in the FrontDoor echo that someone recently posted
  211.  > JL: source on how to interpret the UNIX date/time stamp.  The board
  212.  > JL: I am reading this on doesn't go back that far in messages so
  213.  > JL: I can't find the actual message itself.  Would whoever posted
  214.  > JL: that be so kind as to re-post it again for us late comers.  Thanks
  215.  > JL: alot!
  216.  > JL:
  217. Unit UnixDate;
  218. (***************************************************************************)
  219. (* UNIX DATE Version 1.00                                                  *)
  220. (* This unit provides access to UNIX date related functions and procedures *)
  221. (* A UNIX date is the number of seconds from January 1, 1970. This unit    *)
  222. (* may be freely used. If you modify the source code, please do not        *)
  223. (* distribute your enhancements.                                           *)
  224. (* (C) 1991 by Brian Stark.                                                *)
  225. (* This is a programming release from The Desert Fox CBCS                  *)
  226. (* FidoNet 1:289/3.8 + Columbia, MO - USA                                  *)
  227. (* ----------------------------------------------------------------------- *)
  228. (* bstark@pro-aasgard.cts.com                                              *)
  229. (* brian.stark@p8.f3.n289.z1.fidonet.org                                   *)
  230. (***************************************************************************)
  231. INTERFACE Uses
  232.    DOS;
  233. Function  GetTimeZone : ShortInt;
  234.   {Returns the value from the enviroment variable "TZ". If not found, UTC is
  235.    assumed, and a value of zero is returned}
  236. Function  IsLeapYear(Source : Word) : Boolean;
  237.   {Determines if the year is a leap year or not}
  238. Function  Norm2Unix(Y, M, D, H, Min, S : Word) : LongInt;
  239.   {Convert a normal date to its UNIX date. If environment variable "TZ" is
  240.    defined, then the input parameters are assumed to be in **LOCAL TIME**}
  241. Procedure Unix2Norm(Date : LongInt; Var Y, M, D, H, Min, S : Word);
  242.   {Convert a UNIX date to its normal date counterpart. If the environment
  243.    variable "TZ" is defined, then the output will be in **LOCAL TIME**}
  244. Const
  245.   DaysPerMonth :
  246.     Array[1..12] of ShortInt = (031,028,031,030,031,030,031,031,030,031,030,
  247. 031);
  248.   DaysPerYear  :
  249.     Array[1..12] of Integer  = (031,059,090,120,151,181,212,243,273,304,334,
  250. 365);
  251.   DaysPerLeapYear :
  252.     Array[1..12] of Integer  = (031,060,091,121,152,182,213,244,274,305,335,
  253. 366);
  254.   SecsPerYear      : LongInt  = 31536000;
  255.   SecsPerLeapYear  : LongInt  = 31622400;
  256.   SecsPerDay       : LongInt  = 86400;
  257.   SecsPerHour      : Integer  = 3600;
  258.   SecsPerMinute    : ShortInt = 60;
  259. IMPLEMENTATION Function GetTimeZone : ShortInt;
  260. {}
  261. Var
  262.   Environment : String;
  263.   Index : Integer;
  264. Begin
  265.   GetTimeZone := 0;                            {Assume UTC}
  266.   Environment := GetEnv('TZ');       {Grab TZ string}
  267.   For Index := 1 To Length(Environment) Do
  268.     Environment[Index] := Upcase(Environment[Index]);
  269.   If Environment =  'EST05'    Then GetTimeZone := -05; {USA EASTERN}
  270.   If Environment =  'EST05EDT' Then GetTimeZone := -06;
  271.   If Environment =  'CST06'    Then GetTimeZone := -06; {USA CENTRAL}
  272.   If Environment =  'CST06CDT' Then GetTimeZone := -07;
  273.   If Environment =  'MST07'    Then GetTimeZone := -07; {USA MOUNTAIN}
  274.   If Environment =  'MST07MDT' Then GetTimeZone := -08;
  275.   If Environment =  'PST08'    Then GetTimeZone := -08;
  276.   If Environment =  'PST08PDT' Then GetTimeZone := -09;
  277.   If Environment =  'YST09'    Then GetTimeZone := -09;
  278.   If Environment =  'AST10'    Then GetTimeZone := -10;
  279.   If Environment =  'BST11'    Then GetTimeZone := -11;
  280.   If Environment =  'CET-1'    Then GetTimeZone :=  01;
  281.   If Environment =  'CET-01'   Then GetTimeZone :=  01;
  282.   If Environment =  'EST-10'   Then GetTimeZone :=  10;
  283.   If Environment =  'WST-8'    Then GetTimeZone :=  08; {Perth, Western
  284. Austrailia}
  285.   If Environment =  'WST-08'   Then GetTimeZone :=  08;
  286. End; Function IsLeapYear(Source : Word) : Boolean;
  287. {}
  288. Begin
  289.   If (Source Mod 4 = 0) Then
  290.     IsLeapYear := True
  291.   Else
  292.     IsLeapYear := False;
  293. End; Function Norm2Unix(Y,M,D,H,Min,S : Word) : LongInt;
  294. {}
  295. Var
  296.   UnixDate : LongInt;
  297.   Index    : Word;
  298. Begin
  299.   UnixDate := 0;                                                 {initialize}
  300.   Inc(UnixDate,S);                                              {add seconds}
  301.   Inc(UnixDate,(SecsPerMinute * Min));                          {add minutes}
  302.   Inc(UnixDate,(SecsPerHour * H));                                {add hours}
  303.   (*************************************************************************)
  304.   (* If UTC = 0, and local time is -06 hours of UTC, then                  *)
  305.   (* UTC := UTC - (-06 * SecsPerHour)                                      *)
  306.   (* Remember that a negative # minus a negative # yields a positive value *)
  307.   (*************************************************************************)
  308.   UnixDate := UnixDate - (GetTimeZone * SecsPerHour);            {UTC offset}
  309.  
  310.   If D > 1 Then                                 {has one day already passed?}
  311.     Inc(UnixDate,(SecsPerDay * (D-1)));
  312.  
  313.   If IsLeapYear(Y) Then
  314.     DaysPerMonth[02] := 29
  315.   Else
  316.     DaysPerMonth[02] := 28;                             {Check for Feb. 29th}
  317.  
  318.   Index := 1;
  319.   If M > 1 Then For Index := 1 To (M-1) Do    {has one month already passed?}
  320.     Inc(UnixDate,(DaysPerMonth[Index] * SecsPerDay));
  321.  
  322.   While Y > 1970 Do
  323.   Begin
  324.     If IsLeapYear((Y-1)) Then
  325.       Inc(UnixDate,SecsPerLeapYear)
  326.     Else
  327.       Inc(UnixDate,SecsPerYear);
  328.     Dec(Y,1);
  329.   End;
  330.  
  331.   Norm2Unix := UnixDate;
  332. End;
  333.  
  334. Procedure Unix2Norm(Date : LongInt; Var Y, M, D, H, Min, S : Word);
  335. {}
  336. Var
  337.   LocalDate : LongInt;
  338.   Done      : Boolean;
  339.   X         : ShortInt;
  340.   TotDays   : Integer;
  341. Begin
  342.   Y   := 1970;
  343.   M   := 1;
  344.   D   := 1;
  345.   H   := 0;
  346.   Min := 0;
  347.   S   := 0;
  348.   LocalDate := Date + (GetTimeZone * SecsPerHour);         {Local time date}
  349.  (*************************************************************************)
  350.  (* Sweep out the years...                                                *)
  351.  (*************************************************************************)
  352.   Done := False;
  353.   While Not Done Do
  354.   Begin
  355.     If LocalDate >= SecsPerYear Then
  356.     Begin
  357.       Inc(Y,1);
  358.       Dec(LocalDate,SecsPerYear);
  359.     End
  360.     Else
  361.       Done := True;
  362.  
  363.     If (IsLeapYear(Y+1)) And (LocalDate >= SecsPerLeapYear) And
  364.        (Not Done) Then
  365.     Begin
  366.       Inc(Y,1);
  367.       Dec(LocalDate,SecsPerLeapYear);
  368.     End;
  369.   End;
  370.   (*************************************************************************)
  371.   M := 1;
  372.   D := 1;
  373.   Done := False;
  374.   TotDays := LocalDate Div SecsPerDay;
  375.   If IsLeapYear(Y) Then
  376.   Begin
  377.     DaysPerMonth[02] := 29;
  378.     X := 1;
  379.     Repeat
  380.       If (TotDays <= DaysPerLeapYear[x]) Then
  381.       Begin
  382.         M := X;
  383.         Done := True;
  384.         Dec(LocalDate,(TotDays * SecsPerDay));
  385.         D := DaysPerMonth[M]-(DaysPerLeapYear[M]-TotDays) + 1;
  386.       End
  387.       Else
  388.         Done := False;
  389.       Inc(X);
  390.     Until (Done) or (X > 12);
  391.   End
  392.   Else
  393.   Begin
  394.     DaysPerMonth[02] := 28;
  395.     X := 1;
  396.     Repeat
  397.       If (TotDays <= DaysPerYear[x]) Then
  398.       Begin
  399.         M := X;
  400.         Done := True;
  401.         Dec(LocalDate,(TotDays * SecsPerDay));
  402.         D := DaysPerMonth[M]-(DaysPerYear[M]-TotDays) + 1;
  403.       End
  404.       Else
  405.         Done := False;
  406.       Inc(X);
  407.     Until Done = True or (X > 12);
  408.   End;
  409.   H := LocalDate Div SecsPerHour;
  410.     Dec(LocalDate,(H * SecsPerHour));
  411.   Min := LocalDate Div SecsPerMinute;
  412.     Dec(LocalDate,(Min * SecsPerMinute));
  413.   S := LocalDate;
  414. End;
  415.  
  416.  
  417.  
  418. END.
  419.  
  420.  
  421. --- Opus-CBCS 1.73a
  422.  * Origin: Desert Fox Productions - Columbia, MO - USA (1:289/3.8)
  423.  
  424. @PATH: 30847/8 289/3 290/4 627 13/13 396/1 170/400 512/0 1007 
  425.