Does File Attribute contain millisecond? Objective-C

后端 未结 1 1229
不思量自难忘°
不思量自难忘° 2021-01-14 11:42

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.

1条回答
  •  别那么骄傲
    2021-01-14 12:35

    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.

    0 讨论(0)
提交回复
热议问题