Skip to content

Core\Ldap\Entry::instant

$entry->instant(string $name): ?Core\Time\Instant$entry->instant(
    string $name,
): ?Core\Time\Instant

Returns the one value of an attribute as a Core\Time\Instant, such as whenCreated or pwdLastSet.

Active Directory writes a time in two ways. whenCreated and whenChanged are text, such as 20240101120000.0Z. pwdLastSet, lastLogon and accountExpires are a large number of 100-nanosecond steps since the year 1601. instant reads both and returns an Instant, a point in time in UTC.

The result is null when the entry has no value for the attribute. It is also null for 0 and for the largest possible number, because Active Directory writes these for "never". For example, an account that never expires has accountExpires with one of these values.

For an attribute with several values, instant throws a LogicError. It also throws a LogicError when the value is not a time, and the message names the attribute.

Parameters

  • $namestringThe attribute's name, such as mail. Upper and lower case are the same.

Return value

Returns ?Core\Time\Instant. The instant, or null when the entry has no value for the attribute. It is also null when the number is 0 or 9223372036854775807. Active Directory uses those two numbers for "never".

Errors

  • LogicErrorThe attribute has more than one value, or its value is not in the form this function reads. The message names the attribute.

Examples

The examples below run with this configuration file.

nvs.toml
# These examples use the LDAP directory server on this computer at port 16636.
# `tls_ca_file` is the certificate that the server's certificate is checked against.
[ldap.corp]
url = "ldaps://127.0.0.1:16636"
base = "DC=example,DC=test"
user = "CN=Reader,OU=Company,DC=example,DC=test"
password = "Novis-reader-1"
tls_ca_file = "../../../../../tests/db/ca.crt"
timeout = "5s"
# The program may open the `corp` directory.
[capabilities.ldap]
connect = ["corp"]

When was the account created

<?nvs
// Shows how to read the time an entry was created.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
var $person = $corp->read('CN=Anna Berg,OU=Company,DC=example,DC=test', { select: ['whenCreated'] });
if ($person != null) {
?Core\Time\Instant $created = $person->instant('whenCreated');
if ($created != null) {
// The time is different in every directory, so this prints only a comparison.
// `compareTo` returns -1 for an earlier time. This prints `-1`.
echo $created->compareTo(Core\Time::now()), "\n";
// `toIso` writes the time as text, such as `2024-01-01T12:00:00Z`.
// This prints only the last letter of that text: `Z`, which means UTC.
echo Core\Str::slice($created->toIso(), -1), "\n";
}
}
Output
-1
Z

Never is null

<?nvs
// Shows that `instant` returns `null` for a time that Active Directory writes as "never".
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
var $person = $corp->read('CN=Carla Diaz,OU=Company,DC=example,DC=test', { select: ['accountExpires', 'pwdLastSet'] });
if ($person != null) {
// The account of Carla Diaz never expires. This prints `never expires`.
?Core\Time\Instant $expires = $person->instant('accountExpires');
echo $expires == null ? 'never expires' : 'expires', "\n";
// Carla Diaz must set a new password, so `pwdLastSet` is 0.
// `instant` returns `null`. This prints `must set a password`.
?Core\Time\Instant $passwordSet = $person->instant('pwdLastSet');
echo $passwordSet == null ? 'must set a password' : 'has a password', "\n";
}
Output
never expires
must set a password

A value that is not a time

<?nvs
// Shows what `instant` does with a value that is not a time.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
var $person = $corp->read('CN=Ben Kohl,OU=Company,DC=example,DC=test', { select: ['department'] });
if ($person != null) {
// `department` is the text `Warehouse`. `instant` throws a `LogicError` that names the attribute.
try {
echo $person->instant('department')?->toIso() ?? 'no time', "\n";
} catch (LogicError $error) {
echo $error->message, "\n";
}
}
Output
Core\Ldap\Entry::instant: a value of `department` is not a time