Skip to content

Core\IO::modifiedAt

Returns the time a file was last changed.

Core\IO::modifiedAt returns the time the file at the path was last written, as a Core\Time\Instant. An instant is one point in time. It has no time zone, so it means the same moment everywhere. Use ->in($zone) to show it as a date and a time of day.

You can compare two instants with compareTo, and measure the time between them with since. This is how a program checks whether one file is newer than another, or how old a file is.

A folder has a modification time too. When there is nothing at the path, modifiedAt throws an IOError.

A program you start from the command line may read any path. On a server, a program may read only the files in its application folder, except nvs.toml and names that start with .. Any other path needs the fs.read capability. Without it, modifiedAt throws a RuntimeError.

Parameters

  • $pathstringThe file or directory to check. A relative path must be a string written directly in the code. It is joined to the folder of the file that contains it.

Return value

Returns Core\Time\Instant. The modification time as an exact point in time, with no time zone. Call ->in($zone) to get a calendar date and time.

Errors

  • RuntimeErrorThe configuration does not grant fs.read for this path.
  • IOErrorNothing is at the path, or its metadata could not be read. Or the platform does not record a modification time, or the recorded time is outside the range of a Core\Time\Instant.

Examples

How old is a file

<?nvs
// Shows how to find out how long ago a file was changed.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'notes.txt');
Core\IO::write($path, "Buy milk\n");
// `modifiedAt` returns the time the file was written.
// `since` returns the time between that moment and now, as a `Core\Time\Duration`.
Core\Time\Instant $changed = Core\IO::modifiedAt($path);
Core\Time\Duration $age = Core\Time::now()->since($changed);
// The file was written a moment ago, so this prints "changed less than a minute ago".
if ($age->toSeconds() < 60) {
echo 'changed less than a minute ago', "\n";
} else {
echo 'changed ', $age->toString(), ' ago', "\n";
}
Output
changed less than a minute ago

A missing file throws

<?nvs
// Shows that `Core\IO::modifiedAt` works for a folder, and throws an `IOError` for a missing file.
string $dir = Core\IO::createTemporaryDirectory();
// A folder has a modification time too. This prints "the folder has a time".
Core\Time\Instant $folder = Core\IO::modifiedAt($dir);
echo 'the folder has a time', "\n";
// There is no file at this path, so `modifiedAt` throws an error.
// This prints "no file, so no time".
try {
Core\Time\Instant $when = Core\IO::modifiedAt(Core\Path::join($dir, 'missing.txt'));
echo 'changed at ', $when->toIso(), "\n";
} catch (IOError $e) {
echo 'no file, so no time', "\n";
}
Output
the folder has a time
no file, so no time

Rebuild a cache when the source changes

<?nvs
// Shows how a program rebuilds a cached file only when its source file has changed.
class Page
{
// The cache is out of date when it is missing, or when the source was changed after it.
public static function isStale(string $source, string $cache): bool
{
if (!Core\IO::exists($cache)) {
return true;
}
return Core\IO::modifiedAt($source)->compareTo(Core\IO::modifiedAt($cache)) > 0;
}
public static function render(string $source, string $cache): void
{
if (Page::isStale($source, $cache)) {
Core\IO::write($cache, Core\Str::upper(Core\IO::read($source)));
echo 'rebuilt the cache', "\n";
} else {
echo 'used the cache', "\n";
}
}
}
string $dir = Core\IO::createTemporaryDirectory();
string $source = Core\Path::join($dir, 'page.txt');
string $cache = Core\Path::join($dir, 'page.cache');
Core\IO::write($source, "hello\n");
// There is no cache yet, so this prints "rebuilt the cache".
Page::render($source, $cache);
// Nothing has changed, so this prints "used the cache".
Page::render($source, $cache);
// The program waits a moment and then changes the source.
// The source is now newer than the cache, so this prints "rebuilt the cache".
Core\Time::sleep(Core\Time\Duration::milliseconds(50));
Core\IO::write($source, "hello again\n");
Page::render($source, $cache);
Output
rebuilt the cache
used the cache
rebuilt the cache