std.file.timeLastModified
- multiple declarations
Function timeLastModified
Returns the time that the given file was last modified.
Parameters
Name | Description |
---|---|
name | the name of the file to check |
Returns
A SysTime
.
Throws
FileException
if the given file does not exist.
Example
import std .datetime : abs, DateTime, hnsecs, SysTime;
scope(exit) deleteme .remove;
import std .datetime : Clock, seconds;
auto currTime = Clock .currTime();
enum leeway = 5 .seconds;
deleteme .write("bb");
assert(abs(deleteme .timeLastModified - currTime) <= leeway);
Function timeLastModified
Returns the time that the given file was last modified. If the
file does not exist, returns returnIfMissing
.
A frequent usage pattern occurs in build automation tools such as
make or ant. To check whether file target
must be rebuilt from file source
(i.e., target
is
older than source
or does not exist), use the comparison
below. The code throws a FileException
if source
does not
exist (as it should). On the other hand, the SysTime
default
makes a non-existing target
seem infinitely old so the test
correctly prompts building it.
Parameters
Name | Description |
---|---|
name | The name of the file to get the modification time for. |
returnIfMissing | The time to return if the given file does not exist. |
Returns
A SysTime
.
Example
if (source .timeLastModified >= target .timeLastModified(SysTime .min))
{
// must (re)build
}
else
{
// target is up-to-date
}
Example
import std .datetime : SysTime;
writeln("file.does.not.exist" .timeLastModified(SysTime .min)); // SysTime.min
auto source = deleteme ~ "source";
auto target = deleteme ~ "target";
scope(exit) source .remove, target .remove;
source .write(".");
assert(target .timeLastModified(SysTime .min) < source .timeLastModified);
target .write(".");
assert(target .timeLastModified(SysTime .min) >= source .timeLastModified);
Function timeLastModified
This function is POSIX-Only.
Returns the time that the given file was last modified.
Parameters
Name | Description |
---|---|
statbuf | stat_t retrieved from file. |