代码之家  ›  专栏  ›  技术社区  ›  niteria

如何在Ruby on Windows中获得文件创建时间?

  •  8
  • niteria  · 技术社区  · 14 年前

    如何在Ruby on Windows中获得文件创建时间?

    File.ctime 应该返回换车时间。

    dir /tc 在cmd.exe中,用一堆其他东西返回创建时间。

    有没有比执行和解析更好的方法?

    1 回复  |  直到 11 年前
        1
  •  11
  •   maerics    11 年前

    显然,文件的“ctime”(“创建”或“更改”时间)元数据属性是 system dependent 由于某些系统(例如Windows)存储文件创建的时间(“出生日期”),而其他系统(POSIX系统,例如Linux)跟踪文件上次更新的时间。Windows使用ctime属性 as the actual creation time ,这样您就可以使用 ctime Ruby中的函数。

    这个 File 类具有名为的静态方法和实例方法 CTIME 返回上次修改的时间和 File::Stat 有一个实例方法(其不同之处在于不跟踪发生的更改)。

    File.ctime("foo.txt") # => Sun Oct 24 10:16:47 -0700 2010 (Time)
    
    f = File.new("foo.txt")
    f.ctime # => Will change if the file is replaced (deleted then created).
    
    fs = File::Stat.new("foo.txt")
    fs.ctime # => Will never change, regardless of any action on the file.