(*   Program to test functions in UnixTime UNIT ...  *)
PROGRAM XTime;
USES    Crt, UnixTime;  {v0.2p}
TYPE    Months          = ARRAY [1..12] OF String[3];
CONST   MonthName       : Months = ('Jan','Feb','Mar','Apr','May',
                        'Jun','Jul','Aug','Sep','Oct','Nov','Dec');
VAR Year, Month, Day,Hour, Min, Sec  : WORD;
    TimeUnix        : LONGINT;      { Unix kernel time format }
    Gregorian       : String[12];   { 'YYMMDDhhmmss' }
    i               : INTEGER;
(*-------------------------------------------------------------------*)
FUNCTION ASCIItoUnix( tLocal :String; VAR tUnix :LONGINT) :BOOLEAN;
{ Converts 'YYMMDDhhmmss' local date string to Unix time long integer }
{ globals updated: Year, Month, Day, Hour, Min, Sec }
VAR i, n, j : INTEGER; tError  : BOOLEAN;
BEGIN
    ASCIItoUnix := FALSE;               { if error }
    IF Length( tLocal )<>12 THEN EXIT;  { complete input string? }
    tError := FALSE;                    { assume no error }
    i := 1;                             { string index }
    WHILE (i < 12) AND NOT tError DO    { parse 'YYMMDDhhmmss' }
        BEGIN
        Val(Copy(tLocal, i, 2), n, j);  { convert ASCII -> integer n }
        IF (j <> 0)                     { conversion error? }
            THEN tError := TRUE         { if so }
            ELSE CASE i OF
                1   : IF (n > 38)       { 'YY' }
                        THEN Year := n + 1900   { 1970..1999 }
                        ELSE Year := n + 2000;  { 2000..2038 }
                3   : Month := n;       { 'MM' }
                5   : Day := n;         { 'DD' }
                7   : Hour := n;        { 'hh' }
                9   : Min := n;         { 'mm' }
                11  : Sec := n          { 'ss' }
                ELSE
                    tError := TRUE      { trap the unexpected }
                END; {case i}
        INC(i,2)                        { update string index }
        END; {while}                    { loop 'til parsed }
    IF NOT tError THEN                  { call UnixTime function }
       ASCIItoUnix := GregToUnix( Year,Month,Day,Hour,Min,Sec,tUnix );
END;    {ASCIItoUnix}
(*-------------------------------------------------------------------*)
PROCEDURE WhatAboutDST;  { account for local Daylight Savings Times }
    VAR ch : CHAR; i : INTEGER;
    BEGIN
    LocalZone := '';  i := ZoneHrs;     { a TZ environment var? }
    IF LocalZone='' THEN                { no, so ask }
        BEGIN
        Write('Local Daylight Savings Time (DST) [Y,N] ? N',#8);
        ch:=UpCase(ReadKey);    WriteLn(ch);
        IF ch = 'Y' THEN LocalZone := 'EDT+04' { Eastern Daylight }
                    ELSE LocalZone := 'EST+05' { Eastern Standard }
        END
    END;    {WhatAboutDST}
(*-------------------------------------------------------------------*)
PROCEDURE ShowLocalTime;
    BEGIN
        Write( MonthName[ Month ],' ',Day,',',Year);
        IF DaysInMonth(2,Year)=29 THEN Write('L');   {leapyear flag}
        Write(' at ',Hour,':',Min,':',Sec,' ');
        Write(Copy(LocalZone, 1, 3))
    END; {ShowLocalTime}
(*-------------------------------------------------------------------*)
BEGIN   {Program XTime}
WriteLn(#10,'Gregorian date & time <-> Unix time');
WriteLn('(for Jan.01.1970 to Jan.18.2038 AD)');
IF ParamCount <> 0 THEN Gregorian := ParamStr(1); {can do XTIME [G|U]}
REPEAT
CASE UpCase(Gregorian[1]) OF
'G':    BEGIN   { convert Unix kernel time to Gregorian local }
        WriteLn(#10,'(enter 0 to exit)',#10);
        Write('Enter Unix kernel time: '); ReadLn( TimeUnix );
        IF TimeUnix <> 0 THEN   BEGIN
            WhatAboutDST; { account for local daylight time }
            IF UnixToGreg( TimeUnix,Year,Month,Day,Hour,Min,Sec )
            THEN    BEGIN
                    Write( TimeUnix,' Unix time -> ');
                    ShowLocalTime;  WriteLn;  END
            ELSE    WriteLn('Error in value: ',TimeUnix,#7)
        END END;
'U':    BEGIN   { convert Gregorian local to Unix kernel time }
        WriteLn(#10,'(enter X to exit)',#10);
        Write('Enter Gregorian date & local time: YYMMDDhhmmss');
        FOR i := 1 TO 12 DO Write(#8);  ReadLn( Gregorian );
        IF UpCase(Gregorian[1]) <>'X' THEN  BEGIN
            WhatAboutDST; { account for local daylight time }
            IF ASCIItoUnix( Gregorian, TimeUnix )
            THEN  BEGIN
                  ShowLocalTime;
                  WriteLn(' -> ',TimeUnix,' Unix time' )  END
            ELSE  WriteLn('Error in value/format: ',Gregorian,#7)
        END END
END; {case}
Write(#10,'[G]regorian <-Unix | [U]nix <-Gregorian | Quit [G,U,Q] ? ');
ReadLn( Gregorian );
UNTIL UpCase(Gregorian[1]) = 'Q';
END.    {Program XTime}
(* Greg_    Feb.19.92.Toronto.Canada.   greg.vigneault@canrem.uucp *)
