1

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?

dlras2
  • 8,416
  • 7
  • 51
  • 90
  • 2
    Being off by 5 hours could make sense when factoring in DST. – jncraton Jun 21 '11 at 16:21
  • This is a very good point - I knew I wasn't thinking of something. How can I compensate for both? The (rather old) example I'm working off of doesn't do any of this. – dlras2 Jun 21 '11 at 16:24

3 Answers3

3

During daylight savings time, Chicago (usually GMT-06) is GMT-05.

Edit: Added link to FileTimeToLocalTime.

agent-j
  • 27,335
  • 5
  • 52
  • 79
2

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.

jncraton
  • 9,022
  • 3
  • 34
  • 49
0

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.

PeskyGnat
  • 2,454
  • 19
  • 22