GetDateFileModified为夏令时

编程入门 行业动态 更新时间:2024-10-26 00:28:54
本文介绍了GetDateFileModified为夏令时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 function DateTimeToFileTime(FileTime: TDateTime): TFileTime; var LocalFileTime, Ft: TFileTime; SystemTime: TSystemTime; begin Result.dwLowDateTime := 0; Result.dwHighDateTime := 0; DateTimeToSystemTime(FileTime, SystemTime); SystemTimeToFileTime(SystemTime, LocalFileTime); LocalFileTimeToFileTime(LocalFileTime, Ft); Result := Ft; end; function ExtractShortDate(ATimeIn: TDateTime): string; // Convert DateTime to short date string begin Result := FormatDateTime('mm/dd/yyyy', ATimeIn); end; function ExtractTime(ATimeIn: TDateTime): string; // Convert DateTime to am/pm time string begin Result := FormatDateTime('hh:mm AM/PM', ATimeIn); end; function GetDateFileModified(AFileName: string): string; // Return the file modified date as a string in local time var SR: TSearchRec; UTCTime: Windows.TFileTime; GMTST: Windows.TSystemTime; LocalST: Windows.TSystemTime; ModifyDT: TDateTime; TZ: Windows._TIME_ZONE_INFORMATION; begin Result := ''; if FindFirst(AFileName, faAnyFile, SR) = 0 then begin UTCTime := SR.FindData.ftLastWriteTime; if FileTimeToSystemTime(UTCTime, GMTST) then begin // Get Timezone Information if GetTimeZoneInformation(TZ) <> 0 then if SystemTimeToTzSpecificLocalTime(@TZ, GMTST, LocalST) then begin ModifyDT := SystemTimeToDateTime(LocalST); Result := ExtractShortDate(ModifyDT) + ' ' + ExtractTime(ModifyDT); end else begin TaskMessageDlg('Unable To Convert Time', 'Unable to convert SystemTime To LocalTime', mtInformation, [mbOk], 0); Result := ''; exit; end; end else begin TaskMessageDlg('Unable To Convert Time', 'Unable to convert FileTime To SystemTime', mtInformation, [mbOk], 0); Result := ''; exit; end; end else TaskMessageDlg('File Not Found', ExtractFileName(AFileName) + ' does not exist.', mtInformation, [mbOk], 0); FindClose(SR); end;

发布的原始代码未返回正确的时间。原始代码已替换为工作代码,以便其他人可以从中受益。

The original code posted did not return the correct time. The original code was replaced with working code so that others may find this beneficial.

更新:由于所有人,该代码现在提供了正确的时间

Update: The code provides the correct time now thanks to all that assisted.

推荐答案

问题在MSDN文档中针对FileTimeToLocalFileTime突出显示:

The problem is highlighted in the MSDN docs for FileTimeToLocalFileTime:

FileTimeToLocalFileTime使用时区和夏时制的当前设置。因此,如果是夏令时,即使您转换的时间是标准时间,此功能也会考虑夏令时。您可以使用以下函数序列作为替代。

FileTimeToLocalFileTime uses the current settings for the time zone and daylight saving time. Therefore, if it is daylight saving time, this function will take daylight saving time into account, even if the time you are converting is in standard time. You can use the following sequence of functions as an alternative.

FileTimeToSystemTime / SystemTimeToTzSpecificLocalTime / SystemTimeToFileTime

FileTimeToSystemTime / SystemTimeToTzSpecificLocalTime / SystemTimeToFileTime

在更改前创建的夏令时之后,每次查看文件时都需要使用指定的三个功能(但是当您和文件创建都在同一侧时,此方法当然也适用的夏令时变化)。

You need to use the three functions specified whenever you look at a file after a daylight savings change that was created before the change (but this method of course also works when you and the file creation are both on the same side of the daylight savings change).

更多推荐

GetDateFileModified为夏令时

本文发布于:2023-10-28 08:22:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1536079.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:夏令时   GetDateFileModified

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!