locked
SetLastWriteTime is off by 1 hour

    Question

  • I've written a console utility to set file date/time stamps to a specified date/time: 

    For Each FoundFile In My.Computer.FileSystem.GetFiles(FileSpecPath, FileIO.SearchOption.SearchTopLevelOnly, WildCards)
        FileInfo =
    My.Computer.FileSystem.GetFileInfo(FoundFile)
        System.IO.File.SetCreationTime(FileInfo.FullName, NewDate)
        System.IO.File.SetLastWriteTime(FileInfo.FullName, NewDate)
        System.IO.File.SetLastAccessTime(FileInfo.FullName, NewDate)
        Console.Write(FileInfo.Name +
    " " + NewDate)
    Next

    When I go to a command prompt to check the results, the new date is correct, but the hour is off by 1 hour. Same when I check the times in Windows Explorer -- all three times are displayed to the same time, but exactly 1 hour earlier.

    The system date/time are correct (Vista) and we are not in DST.
    The files reside on a local drive (not a network share).
    The computer is not on a domain.

    I was about to tinker with the SetUTC functions, but thought I'd ask first.
    Thanks,
     -Jack

    Friday, January 16, 2009 11:53 PM

Answers

  • This seems relevant:

    I have run this on 3 pc's and they all get the same result, always 1 hour is added to the time for files in the month of April or May.  Maybe other months like June and July but I have not tried yet.

    http://social.msdn.microsoft.com/Forums/en-US/netfx64bit/thread/10221b9b-82a8-40f5-bdcd-e52edbe12008/

    What version of the framework are you using? Does it have any service packs applied?

    If that's not it then I'm stuck.

    • Marked as answer by Yichun Feng Thursday, January 22, 2009 12:49 AM
    Saturday, January 17, 2009 3:25 AM
  • Yes, Your problem is DaylightSavingTime, based on your timezone, file system adjusts time to Daylightsaving

    You can check whether daylightsaving occurs to the given date by

     Dim NewDate As Date = DateTime.Now.AddMonths(3)
            If NewDate.IsDaylightSavingTime Then

    and act accordingly, but it goods to have that.

    you can convert to utc this way
     NewDate = NewDate.ToUniversalTime
    and to local
                NewDate = NewDate.ToLocalTime


    you can add or subtract hours this way

      If NewDate.IsDaylightSavingTime Then
                NewDate = NewDate.AddHours(+1)
    or
                NewDate = NewDate.AddHours(-1)

    but you have to decide whats your need and whats the correct way of dealing with daylightsaving



    Arjun Paudel
    • Marked as answer by Yichun Feng Thursday, January 22, 2009 12:49 AM
    Saturday, January 17, 2009 4:13 AM

All replies

  • This seems relevant:

    I have run this on 3 pc's and they all get the same result, always 1 hour is added to the time for files in the month of April or May.  Maybe other months like June and July but I have not tried yet.

    http://social.msdn.microsoft.com/Forums/en-US/netfx64bit/thread/10221b9b-82a8-40f5-bdcd-e52edbe12008/

    What version of the framework are you using? Does it have any service packs applied?

    If that's not it then I'm stuck.

    • Marked as answer by Yichun Feng Thursday, January 22, 2009 12:49 AM
    Saturday, January 17, 2009 3:25 AM
  • Yes, Your problem is DaylightSavingTime, based on your timezone, file system adjusts time to Daylightsaving

    You can check whether daylightsaving occurs to the given date by

     Dim NewDate As Date = DateTime.Now.AddMonths(3)
            If NewDate.IsDaylightSavingTime Then

    and act accordingly, but it goods to have that.

    you can convert to utc this way
     NewDate = NewDate.ToUniversalTime
    and to local
                NewDate = NewDate.ToLocalTime


    you can add or subtract hours this way

      If NewDate.IsDaylightSavingTime Then
                NewDate = NewDate.AddHours(+1)
    or
                NewDate = NewDate.AddHours(-1)

    but you have to decide whats your need and whats the correct way of dealing with daylightsaving



    Arjun Paudel
    • Marked as answer by Yichun Feng Thursday, January 22, 2009 12:49 AM
    Saturday, January 17, 2009 4:13 AM
  • You must adjust the time that you set with SetLastWriteTime() according to DST, using these rules: If the filetime is inside DST and the current time is outside, you must adjust by 1 hour.  And if the filetime is outside DST and the current time is inside, you must adjust by 1 hour.  The same is true with times used with SetLastAccessTime() and SetCreationTime(). 

    In C#, the adjustment looks like this: 
    // If I have a time in the .NET environment, and I want to use it for   
    // SetWastWriteTime() etc, then I need to adjust it for Win32.   
    static DateTime AdjustTime_DotNetToWin32(DateTime time)  
    {  
        DateTime adjusted = time;  
        if (DateTime.Now.IsDaylightSavingTime() && !time.IsDaylightSavingTime())  
            adjusted = time - new System.TimeSpan(1, 0, 0);  
     
        else if (!DateTime.Now.IsDaylightSavingTime() && time.IsDaylightSavingTime())  
            adjusted = time + new System.TimeSpan(1, 0, 0);  
     
        return adjusted;  
    }  
     
    void MySetLastWriteTime(String targetFile, DateTime t)  
    {  
        DateTime adjusted = AdjustTime_DotNetToWin32(t);  
        System.IO.File.SetLastWriteTime(targetFile, adjusted);  
    }  
     
     

    The converse applies when retrieving the time from the NTFS.  You must adjust it the opposite way, if you want the times in .NET to make sense.  Like this:

    // If I read a time from a file with GetLastWriteTime() (etc), I need  
    // to adjust it for display in the .NET environment.    
    static DateTime AdjustTime_Win32ToDotNet(DateTime time)  
    {  
        DateTime adjusted = time;  
        if (DateTime.Now.IsDaylightSavingTime() && !time.IsDaylightSavingTime())  
            adjusted = time + new System.TimeSpan(1, 0, 0);  
     
        else if (!DateTime.Now.IsDaylightSavingTime() && time.IsDaylightSavingTime())  
            adjusted = time - new System.TimeSpan(1, 0, 0);  
     
        return adjusted;  
    }  
     
     

    (All these DateTimes are Kind.Local.)

    The reason for this  is discussed here: 
            http://blogs.msdn.com/oldnewthing/archive/2003/10/24/55413.aspx

    Sunday, March 08, 2009 9:59 PM
  • It works for me in Window 7 without applying this DayLight Saving Adjustment. How ever all the other OS does suffer from the Day Light Saving issue.

    Can any one else confirm it.
    Wednesday, November 25, 2009 6:33 PM