This is an automated email from the ASF dual-hosted git repository.

Xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opendal.git


The following commit(s) were added to refs/heads/main by this push:
     new 9c6cff0593 Add support for metadata last_modified to Ruby bindings 
(#8500)
9c6cff0593 is described below

commit 9c6cff05930970c33ea5170ea434ee62ff43dce9
Author: Antsiscool <[email protected]>
AuthorDate: Sun Nov 22 08:23:28 2026 +1100

    Add support for metadata last_modified to Ruby bindings (#8500)
    
    * Add support for metadata last_modified to Ruby bindings
    
    * fix(bindings/ruby): return metadata modification times in UTC
    
    ---------
    
    Co-authored-by: Xuanwo <[email protected]>
---
 bindings/ruby/src/metadata.rs       | 24 +++++++++++++++++++++
 bindings/ruby/test/metadata_test.rb | 42 +++++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+)

diff --git a/bindings/ruby/src/metadata.rs b/bindings/ruby/src/metadata.rs
index b9077a810f..c8a4658c0b 100644
--- a/bindings/ruby/src/metadata.rs
+++ b/bindings/ruby/src/metadata.rs
@@ -25,6 +25,7 @@
 use magnus::Error;
 use magnus::RModule;
 use magnus::method;
+use magnus::time::{Offset, Time, Timespec};
 
 use crate::*;
 
@@ -92,6 +93,28 @@ impl Metadata {
     pub fn etag(&self) -> Option<&str> {
         self.0.etag()
     }
+
+    /// @yard
+    /// @def last_modified
+    /// Last modified time of this entry in UTC, or nil if unavailable.
+    /// @return [Time, nil]
+    fn last_modified(ruby: &Ruby, rb_self: &Self) -> Result<Option<Time>, 
Error> {
+        let Some(timestamp) = rb_self.0.last_modified() else {
+            return Ok(None);
+        };
+        let timestamp = timestamp.into_inner();
+        let mut timespec = Timespec {
+            tv_sec: timestamp.as_second(),
+            tv_nsec: i64::from(timestamp.subsec_nanosecond()),
+        };
+        // Jiff uses signed fractional seconds, while a timespec uses 
nonnegative nanoseconds.
+        if timespec.tv_nsec < 0 {
+            timespec.tv_sec -= 1;
+            timespec.tv_nsec += 1_000_000_000;
+        }
+
+        ruby.time_timespec_new(timespec, Offset::utc()).map(Some)
+    }
 }
 
 pub fn include(ruby: &Ruby, gem_module: &RModule) -> Result<(), Error> {
@@ -105,6 +128,7 @@ pub fn include(ruby: &Ruby, gem_module: &RModule) -> 
Result<(), Error> {
     class.define_method("content_md5", method!(Metadata::content_md5, 0))?;
     class.define_method("content_type", method!(Metadata::content_type, 0))?;
     class.define_method("etag", method!(Metadata::etag, 0))?;
+    class.define_method("last_modified", method!(Metadata::last_modified, 0))?;
 
     Ok(())
 }
diff --git a/bindings/ruby/test/metadata_test.rb 
b/bindings/ruby/test/metadata_test.rb
index 59a8c4c29b..6c45dd70d8 100644
--- a/bindings/ruby/test/metadata_test.rb
+++ b/bindings/ruby/test/metadata_test.rb
@@ -18,6 +18,7 @@
 # frozen_string_literal: true
 
 require "test_helper"
+require "tmpdir"
 
 class MetadataTest < ActiveSupport::TestCase
   setup do
@@ -75,4 +76,45 @@ class MetadataTest < ActiveSupport::TestCase
     metadata = @op.stat("file")
     assert_nil metadata.etag
   end
+
+  test "#last_modified returns nil when the service does not provide it" do
+    metadata = @op.stat("file")
+    assert_nil metadata.last_modified
+  end
+
+  test "#last_modified returns a UTC Time with fractional seconds" do
+    Dir.mktmpdir do |root|
+      path = File.join(root, "file")
+      File.write(path, "OpenDAL Ruby is ready.")
+      expected = Time.utc(2024, 1, 2, 3, 4, 5, 125_000)
+      File.utime(expected, expected, path)
+
+      op = OpenDal::Operator.new("fs", {"root" => root})
+      last_modified = op.stat("file").last_modified
+
+      assert_instance_of Time, last_modified
+      assert last_modified.utc?
+      assert_equal expected, last_modified
+    end
+  end
+
+  test "#last_modified preserves fractional seconds before the Unix epoch" do
+    Dir.mktmpdir do |root|
+      path = File.join(root, "file")
+      File.write(path, "OpenDAL Ruby is ready.")
+      expected = Time.at(-0.25).utc
+      begin
+        File.utime(expected, expected, path)
+      rescue Errno::EINVAL, Errno::ERANGE, Errno::EOVERFLOW, Errno::ENOTSUP
+        skip "File.utime rejected the pre-epoch timestamp"
+      end
+      skip "File.mtime did not preserve the pre-epoch fractional timestamp" 
unless File.mtime(path) == expected
+
+      op = OpenDal::Operator.new("fs", {"root" => root})
+      last_modified = op.stat("file").last_modified
+
+      assert last_modified.utc?
+      assert_equal expected, last_modified
+    end
+  end
 end

Reply via email to