Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Library: Sonstiges (https://www.delphipraxis.net/45-library-sonstiges/)
-   -   RFC -> TDateTime -> RFC (https://www.delphipraxis.net/6266-rfc-tdatetime-rfc.html)

sakura 5. Jul 2003 12:03


RFC -> TDateTime -> RFC
 
Folgende Funktionen wandeln einen Datumswert in das international gültige RFC Format um und auch wieder zurück. Dieses Datumsformat ist z.B. im Umgang mit den meisten DB zu empfehlen, um mögliche Probleme mit den lokalen Einstellungen an einem Rechner zu umgehen.

Delphi-Quellcode:
function DateTimeToRFCA(Value, Offset: TDateTime): AnsiString;
const
  cWeekday: array[1..7] of AnsiString = (
    'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
  );
  cMonth: array[1..12] of AnsiString = (
    'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
    'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
  );
var
  TimeDifference: Integer;
  Dummy, Year, Month, Day, Hour, Minute, Second: Word;
  Zone: AnsiString;
begin
  // determine time by difference
  TimeDifference := Round(Abs(Offset * 24 * 60)); //multiply by 24 h and 60 min
  if TimeDifference = 0 then
  begin
    Zone := 'GMT'
  end else
  begin
    // create time difference string
    TimeDifference := (TimeDifference div 60) * 100 + (TimeDifference mod 60);
    if Offset < 0 then
      Zone := Format('-%.4u', [TimeDifference])
    else
      Zone := Format('+%.4u', [TimeDifference]);
  end;
  // get date and time values
  DecodeDate(Value, Year, Month, Day);
  DecodeTime(Value, Hour, Minute, Second, Dummy);
  // paste result
  Result := Format(
    '%s, %.2d %s %4d %.2d:%.2d:%.2d %s', [
      cWeekday[DayOfWeek(Value)], Day, cMonth[Month], Year, Hour, Minute,
      Second, Zone
    ]
  );
end;

function RFCToDateTimeA(RFC: AnsiString): TDateTime;
const
  cWeekDay = 'sun#mon#tue#wed#thu#fri#sat';
  cMonth = 'jan#feb#mar#apr#may#jun#jul#aug#sep#oct#nov#dec';
var
  Str: AnsiString;
  Year, Month, Day, Hour, Minute, Second: Word;
begin
  try
    RFC := Trim(RFC);
    // remove day name
    Str := LowerCase(Copy(RFC, 1, 3));
    if Pos(Str, cWeekDay) > 0 then
      Delete(RFC, 1, AnsiPos(' ', RFC));
    // get day of month
    Str := Trim(Copy(RFC, 1, AnsiPos(' ', RFC)));
    Day := StrToIntDef(Str, 0);
    // remove day of month
    Delete(RFC, 1, Length(Str) + 1);
    // get month
    Str := LowerCase(Copy(RFC, 1, 3));
    Month := (Pos(Str, cMonth) div 4) + 1;
    // remove month name
    Delete(RFC, 1, 4);
    // get year
    Str := Copy(RFC, 1, 4);
    Year := StrToIntDef(Str, 0);
    // remove Year
    Delete(RFC, 1, 5);
    // encode date
    Result := EncodeDate(Year, Month, Day);
    Str := RFC[1] + RFC[2];
    Hour := StrToIntDef(RFC[1] + RFC[2], 0);
    Minute := StrToIntDef(RFC[4] + RFC[5], 0);
    Second := StrToIntDef(RFC[7] + RFC[8], 0);
    Delete(RFC, 1, 9);
    // add time to the Date
    Result := Result + EncodeTime(Hour, Minute, Second, 0);
  except
    Result := 0;
  end;
end;
...:cat:...


Alle Zeitangaben in WEZ +1. Es ist jetzt 14:07 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz