add File#mtime
MODIFY
mrbgems/mruby-io/src/file.c
mrbgems/mruby-io/src/file.c
@@ -332,6 +332,20 @@ mrb_file__gethome(mrb_state *mrb, mrb_value klass)
#endif
}
+static mrb_value
+mrb_file_mtime(mrb_state *mrb, mrb_value self)
+{
+ mrb_value obj;
+ struct stat st;
+ int fd;
+
+ obj = mrb_obj_value(mrb_class_get(mrb, "Time"));
+ fd = (int)mrb_fixnum(mrb_io_fileno(mrb, self));
+ if (fstat(fd, &st) == -1)
+ return mrb_false_value();
+ return mrb_funcall(mrb, obj, "at", 1, mrb_float_value(mrb, st.st_mtime));
+}
+
mrb_value
mrb_file_flock(mrb_state *mrb, mrb_value self)
{
@@ -471,6 +485,7 @@ mrb_init_file(mrb_state *mrb)
mrb_define_class_method(mrb, file, "_gethome", mrb_file__gethome, MRB_ARGS_OPT(1));
mrb_define_method(mrb, file, "flock", mrb_file_flock, MRB_ARGS_REQ(1));
+ mrb_define_method(mrb, file, "mtime", mrb_file_mtime, MRB_ARGS_NONE());
cnst = mrb_define_module_under(mrb, file, "Constants");
mrb_define_const(mrb, cnst, "LOCK_SH", mrb_fixnum_value(LOCK_SH));

MODIFY
mrbgems/mruby-io/test/file.rb
mrbgems/mruby-io/test/file.rb
@@ -54,7 +54,7 @@ assert('File.extname') do
assert_equal '', File.extname('.foo')
end
-assert('IO#flock') do
+assert('File#flock') do
f = File.open $mrbtest_io_rfname
begin
assert_equal(f.flock(File::LOCK_SH), 0)
@@ -68,6 +68,22 @@ assert('IO#flock') do
end
end
+assert('File#mtime') do
+ unless Object.const_defined?(:Time)
+ skip "File#mtime require Time"
+ end
+ begin
+ now = Time.now.to_i
+ mt = 0
+ File.open('mtime-test', 'w') do |f|
+ mt = f.mtime.to_i
+ end
+ assert_equal true, mt >= now
+ ensure
+ File.delete('mtime-test')
+ end
+end
+
assert('File.join') do
assert_equal "", File.join()
assert_equal "a", File.join("a")

add File#mtime