如何在 Windows 上的 Ruby 中获取文件创建时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4009288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the file creation time in Ruby on Windows?
提问by niteria
How can I get the file creation time in Ruby on Windows?
如何在 Windows 上的 Ruby 中获取文件创建时间?
File.ctime
is supposed to return change time.
File.ctime
应该返回更改时间。
dir /tc
in cmd.exe returns creation time with bunch of other stuff.
dir /tc
在 cmd.exe 中返回创建时间和其他一些东西。
Is there a better way than exec'ing it and parsing?
有没有比执行它并解析更好的方法?
回答by maerics
Apparently, the "ctime" ("creation" or "change" time) metadata attribute of a file is system dependentas some systems (e.g. Windows) store the time that a file was created (its "birth date") and others (Posix systems, e.g. Linux) track the time that it was last updated. Windows uses the ctime attribute as the actual creation time, so you can use the various ctime
functions in Ruby.
显然,文件的“ctime”(“创建”或“更改”时间)元数据属性是系统相关的,因为某些系统(例如 Windows)存储文件的创建时间(其“出生日期”)和其他系统(Posix系统,例如 Linux)跟踪它上次更新的时间。Windows 使用 ctime 属性作为实际创建时间,因此您可以使用ctime
Ruby 中的各种函数。
The Fileclass has static and instance methods named ctime
which return the last modified time and File::Stathas an instance method (which differs by not tracking changes as they occur).
该文件类有一个名为静态和实例方法ctime
,其返回的最后修改时间和文件::统计有一个实例方法(该方法由不同所发生不跟踪更改)。
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.