I would like to get milliseconds from creation time of file attribute When I get file attribute, I use NSDateFormatter to convert file creation time (NSDate) to NSString.
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss:SS: A"];
- ss --> seconds
- SS --> should be millisecond
- A --> milliseconds of the date
I get 00 for SS and get 54487000 for A. I notice that the last three digits are always zero for NSDate come from file attribute of any files. But when I use the same formatter with the NSDate come from [NSDate date], the last three digits are not zero for A, and SS digit is not always zero.
Does File Attribute retrieved in Objective-C contain file attribute?
This could depend on what operating system you're on, and what type of filesystem the file is on. I'll assume you're on iOS (in which case you're using whatever filesystem iOS happens to use).
The
stat
system call returns information about a file, including several timestamps, in a structure calledstruct stat
. The structure stores each timestamp as astruct timespec
. Thestruct timespec
contains a seconds fieldtv_sec
and a nanoseconds fieldtv_nsec
. So in theory, you could get nanosecond-resolution timestamps for your files.In practice, it looks like you only get second-resolution timestamps. I tested with this code:
on my iPhone 4S running iOS 5.0.1, and got this result:
You can see that all of the
tv_nsec
fields are 0. That seems unlikely to be a coincidence.Historically, HFS Plus (the Mac OS X native filesystem, probably used by iOS also) stored each timestamp in a 32-bit unsigned integer representing the number of seconds since midnight Jan 1 1904 GMT. (See Technical Note TN1150.) Presumably at some point they expanded the timestamps to 64 bits (or will do so before 2040 when the 32-bit timestamps will wrap around), but apparently they didn't add any fractional bits.