View source code
Display the source code in std/file.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

std.file.timeLastModified - multiple declarations

Function timeLastModified

Returns the time that the given file was last modified.

SysTime timeLastModified(R) (
  R name
)
if (isInputRange!R && !isInfinite!R && isSomeChar!(ElementEncodingType!R) && !isConvertibleToString!R);

SysTime timeLastModified(R) (
  auto ref R name
)
if (isConvertibleToString!R);

Parameters

NameDescription
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.

SysTime timeLastModified(R) (
  R name,
  SysTime returnIfMissing
)
if (isInputRange!R && !isInfinite!R && isSomeChar!(ElementEncodingType!R));

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.min default makes a non-existing target seem infinitely old so the test correctly prompts building it.

Parameters

NameDescription
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);

Authors

Walter Bright, Andrei Alexandrescu, Jonathan M Davis

License

Boost License 1.0.