无论.NET中的时区如何,如何获取FAT32文件的正确的修改日期时间? [英] How do I get the correct modified datetime of a FAT32 file, regardless of timezone in .NET?

查看:62
本文介绍了无论.NET中的时区如何,如何获取FAT32文件的正确的修改日期时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在回答之前,请仔细仔细阅读此问题.答案并不像看起来的那么简单.

Please read this question fully and carefully before answering. The answer is not as simple as it might appear.

我正在编写一个程序,该程序需要跟踪文件的修改日期时间,其中一些存储在外部FAT32驱动器上.该程序正在各种Windows 7计算机上运行.

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.

问题是当当前UTC偏移量更改时,UTC修改的日期时间也会更改.具体来说,当我们从新西兰标准时间(UTC + 12)转到新西兰夏令时间(UTC + 13)并再次返回时.这不是错字-修改了 UTC 的日期时间.它不应该,这是UTC的重点,但它确实如此.这似乎是FAT32文件系统的限制-NTFS上的文件可以正常工作.

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"));

系统时区为新西兰,系统日期为2012年4月9日,即新西兰标准时间.

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

现在将系统日期设置为2012年3月1日,即新西兰夏令时间.请注意,我已重命名了包含测试文件的目录.这很重要,因为否则 Windows将缓存文件的修改日期时间.我浪费了很多时间才明白这一点.

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

现在将系统日期设置为2012年4月9日,并将时区更改为阿德莱德(UTC + 09:30).

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

那么如何获取正确的修改日期时间?我可以尝试弄清该文件是否在FAT32文件系统上,如果它是夏时制,请进行一个小时的调整,但是即使我可以使它正常工作,这也将是一个可怕的丑陋骇客.使用低级别的系统调用是否可以工作(我怀疑不是因为问题似乎出在OS级别)?我可以更改流程的时区,而不在整个计算机上更改它吗?还有其他办法吗?

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?

推荐答案

问题是,

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.

例如,您需要将实际UTC文件修改时间存储在一个单独的文件中,该文件必须在每台计算机上同步,然后才能卸下外部驱动器.如果同步不会至少执行一次,则不能认为它是正确的.而且没有简单的方法可以将其强制执行给用户.

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.

这篇关于无论.NET中的时区如何,如何获取FAT32文件的正确的修改日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆