Einzelnen Beitrag anzeigen

Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#1

UnixTime in ein Datum und in eine Zeit umwandeln

  Alt 5. Jul 2003, 06:13
Diese beiden Funktionen wandeln einen UnixTimeStamp in ein Datums- bzw. in einen Zeit-String um ohne Verwendung der Unit SysUtils.

Delphi-Quellcode:
{-----------------------------------------------------------------------------
  Procedure : UnixTimeToDateString - Author : Michael Puff
  Date      : 2003-07-05
  Purpose  : Converts a Unixtimestamp to local systemtime
-----------------------------------------------------------------------------}

function UnixTimeToDateString(i: PDWORD): string;
var
  umt: int64;
  st: TSystemTime;
  ft: TFileTime;
  li: ULARGE_INTEGER;
  buf: array[0..255] of char;
begin
  result := '';
  if i = nil then
    exit;
  umt := i^;
  st.wYear := 1970;
  st.wMonth := 1;
  st.wDayOfWeek := 0;
  st.wDay := 1;
  st.wHour := 0;
  st.wMinute := 0;
  st.wSecond := 0;
  st.wMilliseconds := 0;
  SystemTimeToFileTime(st, ft);
  li.QuadPart := (umt * 10000000) + ULARGE_INTEGER(ft).QuadPart;
  ft.dwLowDateTime := li.LowPart;
  ft.dwHighDateTime := li.HighPart;
  FileTimeToSystemTime(ft, st);
  ZeroMemory(@buf, sizeof(buf));
  if (GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, @st, nil, buf,
    sizeof(buf)) > 0) then
    Result := string(buf);
end;
Delphi-Quellcode:
{-----------------------------------------------------------------------------
  Procedure : UnixTimeToTimeString - Author : Michael Puff
  Date      : 2003-07-05
  Purpose  : Converts a Unixtimestamp to local systemtime
-----------------------------------------------------------------------------}

function UnixTimeToTimeString(i: PDWORD): string;
var
  umt: int64;
  st: SystemTime;
  ft: FileTime;
  li: ULARGE_INTEGER;
  buf: array[0..255] of char;
begin
  result := '';
  if i = nil then
    exit;
  umt := i^;
  st.wYear := 1970;
  st.wMonth := 1;
  st.wDayOfWeek := 0;
  st.wDay := 1;
  st.wHour := 0;
  st.wMinute := 0;
  st.wSecond := 0;
  st.wMilliseconds := 0;
  SystemTimeToFileTime(st, ft);
  li.QuadPart := (umt * 10000000);
  ft.dwLowDateTime := li.LowPart;
  ft.dwHighDateTime := li.HighPart;
  FileTimeToSystemTime(ft, st);
  ZeroMemory(@buf, sizeof(buf));
  if (GetTimeFormat(LOCALE_USER_DEFAULT, TIME_FORCE24HOURFORMAT, @st, nil, buf,
    sizeof(buf)) > 0) then
    Result := string(buf);
end;
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat