FILETIME
转换为TDateTime
但不是相反。
答案 0 :(得分:3)
DateTimeToSystemTime
致电System.SysUtils
。SystemTimeToFileTime
致电Winapi.Windows
。如果您的原始日期时间值是当地时间,那么您需要在调用DateTimeToSystemTime
之前将其转换为UTC。
答案 1 :(得分:1)
您可以在ConvertDateTimeToFileTime
中查看System.IOUtils
的实现。
它是TDirectory
记录的私有成员。
但是您可以将其用作参考,以便在一个功能中实现所需的所有功能。
答案 2 :(得分:0)
function FileTimeToDateTime(AFileTime: FILETIME): TDateTime;
var
li: ULARGE_INTEGER;
systemTime: TSystemTime;
localTime: TSystemTime;
const
OA_ZERO_TICKS = UInt64(94353120000000000);
TICKS_PER_DAY = UInt64(864000000000);
begin
// Convert a FILETIME (which is UTC by definition), into a UTC TDateTime.
// Copy FILETIME into LARGE_INTEGER to allow UInt64 access without alignment faults.
li.LowPart := AFileTime.dwLowDateTime;
li.HighPart := AFileTime.dwHighDateTime;
Result := (Real(li.QuadPart) - OA_ZERO_TICKS) / TICKS_PER_DAY;
end;
function DateTimeToFileTime(ADateTimeUTC: TDateTime): FILETIME;
var
li: ULARGE_INTEGER;
systemTime: TSystemTime;
localTime: TSystemTime;
const
OA_ZERO_TICKS = UInt64(94353120000000000);
TICKS_PER_DAY = UInt64(864000000000);
begin
// Convert a UTC TDateTime into a FILETIME (which is UTC by definition).
li.QuadPart := Round(ADateTimeUtc*TICKS_PER_DAY + OA_ZERO_TICKS);
Result.dwLowDateTime := li.LowPart;
Result.dwHighDateTime := li.HighPart;
end;
通过避免SYSTEMTIME
,我们避免了losing precision in milliseconds.
根据定义,SYSTEMTIME的dwMilliseconds限制为1毫秒。
| FILETIME | SYSTEMTIME |
|--------------------------------|-------------------------|
| 100ns resolution | 1,000,000 ns resolution |
| GetSystemTimeAsFileTime | GetSystemTime |
| GetSystemTimePreciseAsFileTime | n/a |
| -- FileTimeToSystemTime --> |
| <-- SystemTimeToFileTime -- |
您应避免使用SYSTEMTIME
。 SYSTEMTIME
的目的是为您将日期/时间解码为其组成部分: