Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

c# - How do I get the correct modified datetime of a FAT32 file, regardless of timezone in .NET?

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[0]).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:DevUtcModifiedDatetimeinDebug>UtcModifiedDatetime.exe M:Test1Test.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:DevUtcModifiedDatetimeinDebug>UtcModifiedDatetime.exe M:Test2Test.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:DevUtcModifiedDatetimeinDebug>UtcModifiedDatetime.exe M:Test3Test.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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is, FAT32 filesystem stores file time as a local time. Thus UTC time is a calculated time which takes DST into account, which results in a different UTC time. In general this problem is so complex, it's unsolvable.

For example you will need to store real UTC file modification time in a separate file, which must be synchronized on each machine before external drive is removed. If sync will not be preformed at least once, then it can't be considered correct. And there is no simple way to enforce that to users.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...