How to get modified date in UTC
GetFileAttributesEx
returns the date that the filesystem returns, unmodified. NTFS stores dates in UTC, and FAT stores them in the local time zone. Without knowing the time zone, a date is pretty worthless.
What is the best way to reliably get the last modified date of a fil开发者_如何转开发e in UTC? Is the best way really to just check for NTFS vs. FAT? What if you are using a different filesystem? Is there a different API I can use? Is there some elegant code someone can post here?
Use the Win32 API GetFileTime, it returns all its times in UTC. Yes, FAT file system does store these values in local time, but the Win32 API is doing the UTC conversion for you.
Quote from the 'FileTimes' MSDN documentation:
GetFileTime retrieves cached UTC times from the FAT file system.
from http://msdn.microsoft.com/en-us/library/ms724290%28v=vs.85%29.aspx
The FAT file system records times on disk in local time. GetFileTime retrieves cached UTC times from the FAT file system. When it becomes daylight saving time, the time retrieved by GetFileTime is off an hour, because the cache is not updated. When you restart the computer, the cached time that GetFileTime retrieves is correct. FindFirstFile retrieves the local time from the FAT file system and converts it to UTC by using the current settings for the time zone and daylight saving time. Therefore, if it is daylight saving time, FindFirstFile takes daylight saving time into account, even if the file time you are converting is in standard time.
精彩评论