文件属性是否包含毫秒?物镜 [英] Does File Attribute contain millisecond? Objective-C

查看:162
本文介绍了文件属性是否包含毫秒?物镜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从文件属性的创建时间获得毫秒数 当我获得文件属性时,我使用NSDateFormatter将文件创建时间(NSDate)转换为NSString.

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->秒
  • SS->应该是毫秒
  • A->日期的毫秒数
  • 我得到SS的00和A的54487000. 我注意到,NSDate的后三位数字始终为零,来自任何文件的文件属性. 但是,当我将相同的格式化程序与来自[NSDate date]的NSDate一起使用时,对于A,最后三位数字不为零,并且SS数字并不总是为零.

    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.

    在Objective-C中检索的文件属性是否包含文件属性?

    Does File Attribute retrieved in Objective-C contain file attribute?

    推荐答案

    这可能取决于您所使用的操作系统以及文件所使用的文件系统类型.我假设您使用的是iOS(在这种情况下,您使用的是iOS碰巧使用的任何文件系统).

    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).

    stat系统调用以称为struct stat的结构返回有关文件的信息,包括多个时间戳.该结构将每个时间戳存储为struct timespec. struct timespec包含一个秒字段tv_sec和一个纳秒字段tv_nsec.因此,从理论上讲,您可以获得文件的纳秒级时间戳.

    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);
    

    在运行iOS 5.0.1的iPhone 4S上,得到以下结果:

    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}
    }
    

    您可以看到所有tv_nsec字段均为0.这似乎不太可能是巧合.

    You can see that all of the tv_nsec fields are 0. That seems unlikely to be a coincidence.

    从历史上看,HFS Plus(Mac OS X本机文件系统,也可能由iOS使用)将每个时间戳存储在一个32位无符号整数中,该整数表示自1904年1月1日午夜以来的秒数. (请参阅技术说明TN1150 .)指出他们将时间戳扩展到64位(或者将在2040年之前完成,那时32位时间戳将环绕),但是显然他们没有添加任何小数位.

    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.

    这篇关于文件属性是否包含毫秒?物镜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆