Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
class method
File.atime
(::path | ::IO file_name) -> ::Time
File.atime(object) → time
file.c
View on GitHub ↗
static VALUE
rb_file_s_atime(VALUE klass, VALUE fname)
{
struct stat st;
if (rb_stat(fname, &st) < 0) {
int e = errno;
FilePathValue(fname);
rb_syserr_fail_path(e, fname);
}
return stat_time(stat_atimespec(&st));
}
Returns a new Time object containing the time of the most recent access to the given object. See File System Timestamps.
Access time for a file is established when it is created, and may be updated when the file content is read:
Example
filepath = '/tmp/t.tmp' File.exist?(filepath) # => false File.atime(filepath) # Raises Errno::ENOENT: No such file or directory. File.write(filepath, 'foo') # Establishes access time. File.atime(filepath) # => 2026-10-04 14:23:12.421395099 -0500 File.read(filepath) # Updates access time. File.atime(filepath) # => 2026-10-04 14:23:21.848886816 -0500 File.delete(filepath) # Clean up.
Access time for a directory is established when it is created, and may updated when its entries are read:
Example
dirpath = '/tmp/foo' File.exist?(dirpath) # => false File.atime(dirpath) # Raises Errno::ENOENT: No such file or directory. Dir.mkdir(dirpath) # Establishes access time. File.atime(dirpath) # => 2026-10-04 14:35:53.940716253 -0500 Dir.entries(dirpath) # Updates access time. File.atime(dirpath) # => 2026-10-04 14:36:24.316018872 -0500 Dir.rmdir(dirpath) # Clean up.
Argument object may be a string path (as above), a File object, or a Dir object:
Example
File.atime(File.new('/etc/passwd')) # => 2026-10-04 11:25:01.259278928 -0500 File.atime('.') # => 2026-10-04 14:40:42.141637825 -0500