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

查看:26
本文介绍了无论 .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:DevUtcModifiedDatetimeinDebug>UtcModifiedDatetime.exe M:Test1Test.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:DevUtcModifiedDatetimeinDebug>UtcModifiedDatetime.exe M:Test2Test.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:DevUtcModifiedDatetimeinDebug>UtcModifiedDatetime.exe M:Test3Test.txt
2012-04-09 1:27:21 pm Standard
20120409 06 2316

那么我怎样才能得到正确的修改日期时间呢?我可以尝试确定该文件是否在 FAT32 文件系统上,如果是夏令时,请调整一小时,但即使我可以让它工作,这将是一个可怕的丑陋黑客.使用低级系统调用会起作用吗(我怀疑不是因为问题似乎出在操作系统级别)?我可以更改进程的时区,而不在整台机器上更改它吗?有没有其他办法?

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?

推荐答案

问题是,FAT32 文件系统将文件时间存储为本地时间.因此 UTC 时间是计算的时间,它考虑了 DST,这会导致不同的 UTC 时间.一般来说,这个问题非常复杂,无法解决.

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天全站免登陆