From e443176a7976849ffd935739046151ed2dce9c1c Mon Sep 17 00:00:00 2001 From: Alex Ling Date: Thu, 4 Jun 2020 16:31:49 +0000 Subject: [PATCH] Add `ctime` helper function --- src/util.cr | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/util.cr b/src/util.cr index bc97887..a3fad38 100644 --- a/src/util.cr +++ b/src/util.cr @@ -99,3 +99,16 @@ end def random_str UUID.random.to_s.gsub "-", "" end + +# Works in all Unix systems. Follows https://github.com/crystal-lang/crystal/ +# blob/master/src/crystal/system/unix/file_info.cr#L42-L48 +def ctime(file_path : String) : Time + res = LibC.stat(file_path, out stat) + raise "Unable to get ctime of file #{file_path}" if res != 0 + + {% if flag?(:darwin) %} + Time.new stat.st_ctimespec, Time::Location::UTC + {% else %} + Time.new stat.st_ctim, Time::Location::UTC + {% end %} +end