Does File Attribute contain millisecond? Objective-C

给你一囗甜甜゛ 提交于 2019-12-01 10:36:38

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!