Question

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?

Was it helpful?

Solution

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 called struct stat. The structure stores each timestamp as a struct timespec. The struct timespec contains a seconds field tv_sec and a nanoseconds field tv_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:

struct stat sb;
stat([NSBundle.mainBundle pathForResource:@"Info" ofType:@"plist"].UTF8String, &sb);

on my iPhone 4S running iOS 5.0.1, and got this result:

(gdb) p sb
$1 = {
  st_dev = 234881033, 
  st_mode = 33188, 
  st_nlink = 1, 
  st_ino = 11265454, 
  st_uid = 501, 
  st_gid = 20, 
  st_rdev = 0, 
  st_atimespec = {
    tv_sec = 1330753666, 
    tv_nsec = 0
  }, 
  st_mtimespec = {
    tv_sec = 1330753664, 
    tv_nsec = 0
  }, 
  st_ctimespec = {
    tv_sec = 1330753664, 
    tv_nsec = 0
  }, 
  st_birthtimespec = {
    tv_sec = 1330417559, 
    tv_nsec = 0
  }, 
  st_size = 830, 
  st_blocks = 8, 
  st_blksize = 4096, 
  st_flags = 0, 
  st_gen = 0, 
  st_lspare = 0, 
  st_qspare =     {0,
    0}
}

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top