Converting NTFS timestamps to FAT Timestamps
I'm writing a file transfer application, and I need to copy files from NTFS to FAT drives. Reading from http://support.microsoft.com/kb/127830, I understand that a time such as #11/29/2004 7:31:06 PM, 250ms# should get translated to #11/29/2004 7:31:08 PM, 0ms# when copying to a FAT hard drive. However, what actually happens is that the file time gets truncated to #11/29/2004 7:31:06 PM, 0ms#.
Am I missing something here? When does the time get truncated, and when does it get rounded?
Edit: Add a code sample:
IO.File.GetLastWriteTimeUtc(Source)
My NTFS->FAT function is:
Function NTFSToFATTime(ByVal NTFSTime As Date) As Date
Return (开发者_StackOverflow社区New Date(NTFSTime.Year, NTFSTime.Month, NTFSTime.Day, NTFSTime.Hour, NTFSTime.Minute, NTFSTime.Second).AddSeconds(If(NTFSTime.Millisecond = 0, NTFSTime.Second Mod 2, 2 - (NTFSTime.Second Mod 2))))
End Function
Technical backgrounder: Basically FAT uses 2 bytes to store the time (hours/minutes/seconds) of the file create in the directory entry. It uses the low 4 bits of this field for the seconds, for which values of 0-29 are valid, and are multiplied by 2 to get the final value. Thus by necessity, seconds will be an even number.
Weird, but my guess is that the docs are either wrong or don't refer to the API you're using. Your timestamp is just getting truncated. Not sure which API you're using to create the FAT file (might be useful to see the docs for it).
In fact, the problem was related to a hard drive interface handling the file times in the wrong way (a d-link storage bay), ie. truncating rather than rounding.
There's therefore no true way to escape this problem, but to allow for a little sloppiness in time checking.
精彩评论