【问题标题】:How do I get the file creation time in Ruby on Windows?如何在 Windows 上的 Ruby 中获取文件创建时间?
【发布时间】:2011-04-29 21:16:17
【问题描述】:

如何在 Windows 上的 Ruby 中获取文件创建时间?

File.ctime应该返回更改时间。

dir /tc 在 cmd.exe 中返回创建时间以及其他一些东西。

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

【问题讨论】:

  • filectime在windows上用php返回创建时间,和ruby的File.ctime一样吗?
  • Windows 存储创建时间。 Unix系统没有。 Mac OS 将其存储在元信息中,但 Ruby 无法使用 File 方法直接访问它。

标签: ruby windows


【解决方案1】:

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

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.

【讨论】:

  • "ctime" 不是 Linux 上的创建时间,而是 inode 或文件更改时间。每当文件被移动、更改、权限或所有者被更改时,它将被更新。
猜你喜欢
  • 2012-11-21
  • 2020-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-15
  • 1970-01-01
相关资源
最近更新 更多