Core\Time\Instant::fromIso
Core\Time\Instant::fromIso(string $text): Core\Time\InstantCore\Time\Instant::fromIso( string $text, ): Core\Time\Instant
Reads a date and time written as text, such as 2024-06-01T09:30:00+02:00, and returns an Instant, one exact moment in time. The text must follow ISO 8601, the international standard way to write a date and time.
The text needs a date, T or one space, a time and an offset from UTC. The offset is Z for UTC, or a value such as +02:00. Part of a second, such as .25, is kept. For a time without an offset, use Core\Time\DateTime::fromIso with a time zone.
These texts throw a ParseError:
- a text without an offset
- a lower-case
torz - a
,before the part of a second - the second
60 - an offset without a colon, such as
+0100 - a year that does not have four digits, such as
-009999 - a date that does not exist, such as
2024-02-30 - a time before
-9999-01-02T01:59:59Zor after9999-12-30T22:00:00Z
The error message names the wrong part. A text written in the code is checked when the program compiles.
The examples below show a time with an offset and texts that throw. The last one shows how long a job ran.
Parameters
$textstringThe timestamp text, with a date,Tor one space, a time and aZor±HH:MMoffset.TandZare upper case. A part of a second follows a..
Return value
Returns Core\Time\Instant. The Instant the text names.
Errors
ParseError$textis not such a timestamp. The error names the part of the text where reading stopped. A lower-casetorzthrows, and so does a,before the part of a second. The second60and an offset without a colon, such as+0100, also throw. It is also thrown for a time before-9999-01-02T01:59:59Zor after9999-12-30T22:00:00Z.
Changelog
| Version | Change |
|---|---|
0.0.5 | Core\Time\Instant::fromIso reads a timestamp such as 2024-03-01T12:00:00Z and returns an Instant. Core\Time\Instant::fromEpoch builds an Instant from the number of seconds since 1 January 1970. |
0.0.6 | Instant::fromIso reads an upper-case T and Z, a . before the fraction, and an offset with a colon, such as +02:00. A lower-case t or z, a comma, the second 60 or an offset like +0100 throws a ParseError. So does a year that does not have four digits, such as -009999. |
Examples
A time with an offset
<?nvs// Shows how to read a date and time that is written with an offset from UTC.
// `+02:00` means the clock was two hours ahead of UTC. `toIso` prints the moment in UTC.// This prints 2024-06-01T07:30:00Z.var $local = Core\Time\Instant::fromIso('2024-06-01T09:30:00+02:00');echo $local->toIso(), "\n";
// The same moment written in UTC is an equal instant, so `compareTo` returns 0.var $utc = Core\Time\Instant::fromIso('2024-06-01T07:30:00Z');echo $local->compareTo($utc), "\n";2024-06-01T07:30:00Z0Texts that are not timestamps
<?nvs// Shows what happens when a text is not a complete, valid timestamp.
// The second text has no offset. The third has a day that does not exist.// Both throw a `ParseError`, and its message says which part is wrong.array<string> $inputs = ['2024-06-01T09:30:00Z', '2024-06-01T09:30:00', '2024-02-30T09:30:00Z'];foreach ($inputs as string $text) { try { echo Core\Time\Instant::fromIso($text)->toIso(), "\n"; } catch (ParseError $error) { echo 'not a timestamp: ', $text, "\n"; }}2024-06-01T09:30:00Znot a timestamp: 2024-06-01T09:30:00not a timestamp: 2024-02-30T09:30:00ZHow long a job ran
<?nvs// Shows how to find how long a job ran, from the start and end times a web service returned.
// Web services often send times as ISO 8601 text. These two use different offsets.string $started = '2024-06-01T09:30:00.250+02:00';string $finished = '2024-06-01T07:42:10Z';
// `since` gives the time between two instants. The job ran for 12 minutes and 9.75 seconds.// `toSeconds` counts only the whole seconds, so this prints 729.var $ran = Core\Time\Instant::fromIso($finished)->since(Core\Time\Instant::fromIso($started));echo $ran->toSeconds(), "\n";729