{*****************************************************************************
 *
 *  DateSup.pas - Date Routines for e-mails  (27-July-1998)
 *
 *  Copyright (c) 1998-99 Michael Haller
 *
 *  Author:     Michael Haller
 *  E-mail:     michael@discountdrive.com
 *  Homepage:   http://www.discountdrive.com/sunrise
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation;
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
 *
 *----------------------------------------------------------------------------
 *
 *  Revision history:
 *
 *     DATE     REV                 DESCRIPTION
 *  ----------- --- ----------------------------------------------------------
 *   Jun 2002   AH        Added timezoning stuff
 *****************************************************************************}

unit DateSup;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

procedure MailTimeToDateTime(S: string; var D, T: TDateTime);
procedure DateTimeToMailTime(var S: string; D, T: TDateTime);
function GetTheRightZone(var LocalZoneName : string) : string; //AHRJ01

implementation

const
  Months: array[1..12] of string = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
    'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
  Days: array[1..7] of string = ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri',
    'Sat');
  DayCount: array[1..12] of Byte = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
    31);

procedure MakeValidDate(var Year, Month, Day, Hour, Minute, Second: Integer);

  procedure MakeValidMonth;
  begin
    while (Month / 12) <= 0 do begin
      Inc(Month, 12);
      Dec(Year, 1);
    end;
    while (Month / 12) > 1 do begin
      Dec(Month, 12);
      Inc(Year, 1);
    end;
  end;

begin
  while (Second / 60) < 0 do begin
    Inc(Second, 60);
    Dec(Minute, 1);
  end;
  while (Second / 60) >= 1 do begin
    Dec(Second, 60);
    Inc(Minute, 1);
  end;
  while (Minute / 60) < 0 do begin
    Inc(Minute, 60);
    Dec(Hour, 1);
  end;
  while (Minute / 60) >= 1 do begin
    Dec(Minute, 60);
    Inc(Hour, 1);
  end;
  while (Hour / 24) < 0 do begin
    Inc(Hour, 24);
    Dec(Day, 1);
  end;
  while (Hour / 24) >= 1 do begin
    Dec(Hour, 24);
    Inc(Day, 1);
  end;
  while (Day / DayCount[Month]) <= 0 do begin
    Inc(Day, DayCount[Month]);
    Dec(Month, 1);
    MakeValidMonth;
  end;
  while (Day / DayCount[Month]) > 1 do begin
    Dec(Day, DayCount[Month]);
    Inc(Month, 1);
    MakeValidMonth;
  end;
  MakeValidMonth;
end;

// AH18
function GetTheRightZone(var LocalZoneName : string) : string;
var bias, h, m : integer;
    g : string;
    TimeZoneInformation: TTimeZoneInformation;
begin
  // AH19 -- Get timezone + bias..
  // AHRJ01 LocalZoneName currently not used...
  LocalZoneName := '';
  case GetTimeZoneInformation(TimeZoneInformation) of
    TIME_ZONE_ID_STANDARD :
    begin
      Bias := TimeZoneInformation.Bias +
               TimeZoneInformation.StandardBias;
      LocalZoneName := TimeZoneInformation.StandardName; //AHRJ01
    end;
    TIME_ZONE_ID_DAYLIGHT :
    begin
     Bias := TimeZoneInformation.Bias +
             TimeZoneInformation.DayLightBias;
     LocalZoneName := TimeZoneInformation.DaylightName; //AHRJ01
    end;
    else
      Bias := TimeZoneInformation.Bias;
  end;

  // watch out, reversed!
  if Bias <= 0 then
    G := '+'
  else
    G := '-';

  // make absolute
  bias := abs(bias);
  h := bias div 60;
  m := bias mod 60;

  Result := G + Format('%.2d%.2d', [H, M]);
end;
// AH18
// AH18
procedure DateTimeToMailTime(var S: string; D, T: TDateTime);
var
  Year, Month, Day, Hour, Minute, Second, MSec: Word;
  timeZone : string; // AHRJ01
begin
  DecodeDate(D, Year, Month, Day);
  DecodeTime(T, Hour, Minute, Second, MSec);
  FmtStr(S, '%.2d:%.2d:%.2d', [Hour, Minute, Second]);
  S := Days[DayOfWeek(D)] + ', ' + IntToStr(Day) + ' ' + Months[Month] + ' ' +
    IntToStr(Year) + ' ' + S + ' ' + GetTheRightZone(timezone); //AHRJ01
end;
// AH18

procedure MailTimeToDateTime(S: string; var D, T: TDateTime);
var
  Index, E, MBias: Integer;
  Dummy: string;
  Day, Month, Year, Hour, Minute, Second, ZoneHour, ZoneMinute: Integer;
  Positiv: Boolean;
  ZoneInfo: TTimeZoneInformation;

  procedure SkipSpaces;
  begin
    while (Index <= Length(S)) and (S[Index] = ' ') do
      Inc(Index);
  end;

  function GetToken: string;
  begin
    Result := '';
    // AHRK05 check on not standard period/dot included. Thanks De Bug.
    while (Index <= Length(S)) and (not (S[Index] in [' ', ':', '.'])) do
    begin
      Result := Result + S[Index];
      Inc(Index);
    end;
    Inc(Index);
  end;

begin
  D := Date;
  T := Time;
  try
    Index := 1;
    // AHRK05... cleaned... DBRK10 Extra condition...
    while (Index <= Length(S)) and not (S[Index] in ['0'..'9'])
      do
      Inc(Index);
    Day := StrToInt(GetToken);
    SkipSpaces;
    Month := 9;
    Dummy := GetToken;
    for E := 1 to 12 do
      if LowerCase(Months[E]) = LowerCase(Dummy) then
        Month := E;
    SkipSpaces;
    Year := StrToInt(GetToken);
    SkipSpaces;
    Hour := StrToInt(GetToken);
    SkipSpaces;
    Minute := StrToInt(GetToken);
    Second := 0;
    // DB11 Extra condition
    if Index <= Length(S)+1 then
    begin
      if S[Index - 1] = ':' then begin
        SkipSpaces;
        try
          Second := StrToInt(GetToken);
        except Second := 0;
        end;
      end;
    end;
    SkipSpaces;
    ZoneMinute := 0;
    ZoneHour := 0;
    if Index < Length(S) then begin
      if not (S[Index] in ['+', '-']) then begin
        Dummy := UpperCase(GetToken);
        if Dummy = 'UT' then
          ZoneHour := 0;
        if Dummy = 'GMT' then
          ZoneHour := 0;
        if Dummy = 'EST' then
          ZoneHour := 5;
        if Dummy = 'EDT' then
          ZoneHour := 4;
        if Dummy = 'CST' then
          ZoneHour := 6;
        if Dummy = 'CDT' then
          ZoneHour := 5;
        if Dummy = 'MST' then
          ZoneHour := 7;
        if Dummy = 'MDT' then
          ZoneHour := 6;
        if Dummy = 'PST' then
          ZoneHour := 8;
        if Dummy = 'PDT' then
          ZoneHour := 7;
      end
      else begin
        Positiv := (S[Index] = '+');
        Inc(Index);
        Dummy := S[Index] + S[Index + 1];
        ZoneHour := StrToInt(Dummy);
        Dummy := S[Index + 2] + S[Index + 3];
        ZoneMinute := StrToInt(Dummy);
        if Positiv then begin
          ZoneHour := ZoneHour * (-1);
          ZoneMinute := ZoneMinute * (-1);
        end;
      end;
    end;
    // AH18
    case GetTimeZoneInformation(ZoneInfo) of
    TIME_ZONE_ID_STANDARD : MBias := ZoneInfo.Bias + ZoneInfo.StandardBias;
    TIME_ZONE_ID_DAYLIGHT : MBias := ZoneInfo.Bias + ZoneInfo.DayLightBias;
    else
       MBias := ZoneInfo.Bias;
    end;

    // Subtract the difference... date/time of receiving or delivery.
    Hour := Hour + (ZoneHour - (MBias div 60));
    Minute := Minute + (ZoneMinute -  (MBias mod 60));
    MakeValidDate(Year, Month, Day, Hour, Minute, Second);
    T := EncodeTime(Hour, Minute, Second, 0);
    D := EncodeDate(Year, Month, Day);
  except
  end;
  // AH18
end;

end.
