为什么我的 SystemTimeToFileTime 返回的结果相差 5 小时?
如果我处于 ±5 小时时区,那么这是有意义的,但我处于 GMT -06:00,所以我不确定时区是否是我的问题,或者是否是其他问题。这是我正在使用的代码:
Private Sub SetFileTimes(file As String, Optional creationTime As Date, Optional accessTime As Date, Optional writeTime As Date)
Dim handle As Long
Dim sysCreationTime As FileTime, sysAccessTime As FileTime, sysWriteTime As FileTime
Dim SECURITY_ATTRIBUTES As SecurityAttributes
SECURITY_ATTRIBUTES.nLength = Len(SECURITY_ATTRIBUTES)
SECURITY_ATTRIBUTES.lpSecurityDescriptor = 0
SECURITY_ATTRIBUTES.bInheritHandle = False
handle = CreateFile(file & Chr$(0), GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, SECURITY_ATTRIBUTES, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0&)
Debug.Assert handle <> -1
GetFileTime handle, sysCreationTime, sysAccessTime, sysWriteTime
If creationTime <> 0 Then
SystemTimeToFileTime GetSystemTime(creationTime), sysCreationTime
End If
If accessTime <> 0 Then
SystemTimeToFileTime GetSystemTime(accessTime), sysAccessTime
End If
If writeTime <> 0 Then
SystemTimeToFileTime GetSystemTime(writeTime), sysWriteTime
End If
SetFileTime handle, sysCreationTime, sysAccessTime, sysWriteTime
CloseHandle handle
End Sub
Private Function GetSystemTime(datetime As Date) As SystemTime
GetSystemTime.Year = Year(datetime)
GetSystemTime.Month = Month(datetime)
GetSystemTime.Day = Day(datetime)
GetSystemTime.Hour = Hour(datetime)
GetSystemTime.Minute = Minute(datetime)
GetSystemTime.Second = Second(datetime)
GetSystemTime.Milliseconds = 0
End Function
该函数可以工作,但我所有的时间都提前 5 小时。 (即,如果我尝试将日期设置为上午 10 点,它会将其设置为上午 5 点。)默认时间(我未指定的时间)不会按预期更改。在调试时,我可以看到 SystemTimeToFileTime 返回的值小于应有的值。我可以做什么来解决这个问题?
It would make sense if I was in a ±5 hour timezone, but I'm in GMT -06:00, so I'm not sure if timezones are my problem or if it's something else. Here is my code I'm using:
Private Sub SetFileTimes(file As String, Optional creationTime As Date, Optional accessTime As Date, Optional writeTime As Date)
Dim handle As Long
Dim sysCreationTime As FileTime, sysAccessTime As FileTime, sysWriteTime As FileTime
Dim SECURITY_ATTRIBUTES As SecurityAttributes
SECURITY_ATTRIBUTES.nLength = Len(SECURITY_ATTRIBUTES)
SECURITY_ATTRIBUTES.lpSecurityDescriptor = 0
SECURITY_ATTRIBUTES.bInheritHandle = False
handle = CreateFile(file & Chr$(0), GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, SECURITY_ATTRIBUTES, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0&)
Debug.Assert handle <> -1
GetFileTime handle, sysCreationTime, sysAccessTime, sysWriteTime
If creationTime <> 0 Then
SystemTimeToFileTime GetSystemTime(creationTime), sysCreationTime
End If
If accessTime <> 0 Then
SystemTimeToFileTime GetSystemTime(accessTime), sysAccessTime
End If
If writeTime <> 0 Then
SystemTimeToFileTime GetSystemTime(writeTime), sysWriteTime
End If
SetFileTime handle, sysCreationTime, sysAccessTime, sysWriteTime
CloseHandle handle
End Sub
Private Function GetSystemTime(datetime As Date) As SystemTime
GetSystemTime.Year = Year(datetime)
GetSystemTime.Month = Month(datetime)
GetSystemTime.Day = Day(datetime)
GetSystemTime.Hour = Hour(datetime)
GetSystemTime.Minute = Minute(datetime)
GetSystemTime.Second = Second(datetime)
GetSystemTime.Milliseconds = 0
End Function
The function works, but all of my times are 5 hours early. (I.E. If I try and set the date to 10am, it will set it to 5am instead.) The default times (ones I do not specify) do not change, as expected. While debugging, I can see that SystemTimeToFileTime
is returning a value less than it should be. What can I do to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的creationTime 和其他输入变量设置为多少?您的 GetSystemTime() 正在根据这些创建时间,并且不清楚返回的时间是否采用 UTC 格式。
SystemTimeToFileTime 的文档声明时间必须采用 UTC 格式
http ://msdn.microsoft.com/en-us/library/ms724948(VS.85).aspx
我怀疑您的代码中的某个地方没有处理此转换。
What is your creationTime and other input variables set to? Your GetSystemTime() is creating a time based on those and it's not clear if the returned time is in UTC format.
The docs for SystemTimeToFileTime state that the time must be in UTC format
http://msdn.microsoft.com/en-us/library/ms724948(VS.85).aspx
I suspect this conversion is not being handled in your code someplace.
在夏令时期间,芝加哥(通常为 GMT-06)为 GMT-05。
编辑:添加了 FileTimeToLocalTime 的链接。
During daylight savings time, Chicago (usually GMT-06) is GMT-05.
Edit: Added link to FileTimeToLocalTime.
这是预期的行为。如前所述,考虑到 DST 时,晚 5 小时是有意义的。文件时间以 UTC 格式存储,因此与您当前的时区会有偏移。
看来您正确使用函数来设置文件时间: http://msdn.microsoft.com/en-us/library/ms724205(VS.85).aspx
您可以使用FileTimeToLocalTime 以根据需要对本地时区进行适当调整。
This is the expected behavior. As was noted, being off by 5 hours makes sense when factoring in DST. File times are stored in UTC format, so there will be an offset from your current timezone.
It appears that you are correctly using the functions to set the file time: http://msdn.microsoft.com/en-us/library/ms724205(VS.85).aspx
You can use FileTimeToLocalTime to make the appropriate adjustment to your local timezone if you desire.