Please read this question fully and carefully before answering. The answer is not as simple as it might appear.
I am writing a program that needs to keep track of the modified datetime of files, some of which are stored on an external FAT32 drive. The program is being run on various Windows 7 machines.
The problem is the UTC modified datetime changes when the current UTC offset changes. Specifically, when we go from New Zealand Standard Time (UTC+12) to New Zealand Daylight Time (UTC+13) and back again. That is not a typo – the UTC modified datetime changes. It shouldn’t, that is kind of the point of UTC, but it does. This appears to be a limitation of the FAT32 file system – files on NTFS work fine.
Console.WriteLine(DateTime.Now.ToString() + (DateTime.Now.IsDaylightSavingTime() ? " Daylight" : " Standard")); Console.WriteLine(new FileInfo(args).LastWriteTimeUtc.ToString("yyyyMMdd HH mmss"));
System timezone is New Zealand and the system date is 9 April 2012 which is New Zealand Standard Time.
C:\Dev\UtcModifiedDatetime\bin\Debug>UtcModifiedDatetime.exe M:\Test1\Test.txt 2012-04-09 3:53:46 pm Standard 20120409 03 5316
Now set the system date to 1 March 2012 which is New Zealand Daylight Time. Note that I have renamed the directory that contains the test file. This is important because otherwise Windows will cache the modified datetime of the file. I wasted a lot of time before I figured that out.
C:\Dev\UtcModifiedDatetime\bin\Debug>UtcModifiedDatetime.exe M:\Test2\Test.txt 2012-03-01 3:54:13 pm Daylight 20120409 02 5316
Now set the system date back to 9 April 2012 and change the timezone to Adelaide (UTC+09:30).
C:\Dev\UtcModifiedDatetime\bin\Debug>UtcModifiedDatetime.exe M:\Test3\Test.txt 2012-04-09 1:27:21 pm Standard 20120409 06 2316
So how can I get the correct modified datetime? I could try and figure out if the file is on a FAT32 file system and if it is daylight saving time make a one hour adjustment, but even if I could get that to work it would be a horrible ugly hack. Will using a low level system call work (I suspect not because the problem seems to be at the OS level)? Can I change the timezone of the process, without changing it on the whole machine? Is there any other way?