Skip to content

Core\Time\Duration::parse

Reads a text such as 30s, 1h30m or 7d and returns the duration it describes. The text uses the same syntax as a duration you write in your code.

Use it when a length of time arrives as text while the program runs, for example a timeout in a configuration file or an option on the command line.

The text is one or more pairs of a whole number and a unit. The units are w, d, h, m, s, ms, us and ns, and they must go from the largest to the smallest. There are no spaces, no signs and no fractions. Any other text throws a ParseError that says what is wrong. A duration longer than about 292 years also throws a ParseError.

Parameters

  • $textstringThe text to read, written the same way as a duration in the code.

Return value

Returns Core\Time\Duration. The Duration the text describes.

Errors

  • ParseError$text is not a valid duration. It has an unknown or repeated unit, a missing count or extra text, or it is longer than a Duration can hold. A $text written directly in the code is checked when the program compiles, and an error there is E0769. Only a computed $text can throw this error.

Examples

Read a length of time

<?nvs
// Shows how to turn a text such as "1h30m" into a duration.
string $text = '1h30m';
Core\Time\Duration $length = Core\Time\Duration::parse($text);
// This prints 1h30m and 5400.
echo $length->toString(), "\n";
echo $length->toSeconds(), "\n";
// `toString` writes a duration with the largest units that fit.
// So "90s" prints as 1m30s.
string $short = '90s';
echo Core\Time\Duration::parse($short)->toString(), "\n";
Output
1h30m
5400
1m30s

Text that is not a duration

<?nvs
// Shows what happens when a text is not a valid duration.
array<string> $texts = ['45s', '1.5h', '30min', ''];
// `parse` throws a `ParseError` when the text is not valid. The message says what is wrong.
// Only "45s" is valid here.
foreach ($texts as string $text) {
try {
Core\Time\Duration $length = Core\Time\Duration::parse($text);
echo '"', $text, '" is ', $length->toString(), "\n";
} catch (ParseError $e) {
echo '"', $text, '": ', $e->message, "\n";
}
}
Output
"45s" is 45s
"1.5h": Core\Time\Duration::parse(): `.` has no meaning in a duration
"30min": Core\Time\Duration::parse(): `min` is not a duration unit — the units are `ns`, `us`, `ms`, `s`, `m`, `h`, `d` and `w`
"": Core\Time\Duration::parse(): a duration is empty

Timeouts from settings

<?nvs
// Reads timeouts from settings, and uses a default when a setting is not a valid duration.
array<string> $settings = ['connect' => '5s', 'read' => '2m30s', 'idle' => 'ten minutes'];
Core\Time\Duration $default = 30s;
// "idle" is not a valid duration, so it gets the default of 30 seconds.
// This prints 5000, 150000 and 30000 milliseconds.
foreach ($settings as string $name => string $text) {
Core\Time\Duration $timeout = $default;
try {
$timeout = Core\Time\Duration::parse($text);
} catch (ParseError $e) {
echo 'The setting ', $name, " is not valid, so the default is used.\n";
}
echo $name, ': ', $timeout->toMilliseconds(), " ms\n";
}
Output
connect: 5000 ms
read: 150000 ms
The setting idle is not valid, so the default is used.
idle: 30000 ms