Time::Duration(3)
๐ NAME ๐
Time::Duration โ rounded or exact English expression of durations. This is the Time::Duration(3) manual page from User Contributed Perl Documentation, bestie. โจ
๐ SYNOPSIS ๐
Example use in a program that ends by noting its runtime:
my $start_time = time();
use Time::Duration;
# then things that take all that time, and then ends:
print "Runtime ", duration(time() - $start_time), ".\n";
Example use in a program that reports age of a file:
use Time::Duration;
my $file = 'that_file';
my $age = $^T - (stat($file))[9]; # 9 = modtime
print "$file was modified ", ago($age);
๐ธ DESCRIPTION ๐ธ
This module gives you functions for expressing durations in rounded or exact terms. Itโs giving โtime math but make it readable.โ ๐
In the first example in the Synopsis, using duration($interval_seconds):
- If the
"time() - $start_time"is3seconds, this prints"Runtime: 3 seconds.". - If itโs
0seconds, itโs"Runtime: 0 seconds.". - If itโs
1second, itโs"Runtime: 1 second.". - If itโs
125seconds, you get"Runtime: 2 minutes and 5 seconds.". - If itโs
3820seconds (which is exactly1h, 3m, 40s), you get it rounded to fit within two expressed units:"Runtime: 1 hour and 4 minutes.". - Using
duration_exactinstead would return"Runtime: 1 hour, 3 minutes, and 40 seconds".
In the second example in the Synopsis, using ago($interval_seconds):
- If the
$ageis3seconds, this prints"file was modified 3 seconds ago". - If itโs
0seconds, itโs"file was modified just now", as a special case. - If itโs
1second, itโs"from 1 second ago". - If itโs
125seconds, you get"file was modified 2 minutes and 5 seconds ago". - If itโs
3820seconds (which is exactly1h, 3m, 40s), you get it rounded to fit within two expressed units:"file was modified 1 hour and 4 minutes ago". - Using
ago_exactinstead would return"file was modified 1 hour, 3 minutes, and 40 seconds ago". - And if the fileโs modtime is, surprisingly, three seconds into the future,
$ageis-3, and youโll get the equally and appropriately surprising"file was modified 3 seconds from now.".
โฑ๏ธ MILLISECOND MODE โฑ๏ธ
By default, this module assumes input is an integer representing number of seconds and only emits results based on the integer part of any floating-point values passed to it. However, if you set the variable $Time::Duration::MILLISECOND to any true value, then the methods will interpret inputs as floating-point numbers and will emit results containing information about the number of milliseconds in the value.
For example, duration(1.021) will return 1 second and 21 milliseconds in this mode.
Millisecond mode is not enabled by default because this module sees heavy use and existing users of it may be relying on its implicit truncation of non-integer arguments. No cap. ๐ซถ
๐ ๏ธ FUNCTIONS ๐ ๏ธ
This module provides all the following functions, which are all exported by default when you call use Time::Duration;.
duration($seconds) / duration($seconds, $precision)
Returns English text expressing the approximate time duration of abs($seconds), with at most "$precision || 2" expressed units. (That is, duration($seconds) is the same as duration($seconds,2).)
For example, duration(120) or duration(-120) is "2 minutes". And duration(0) is "0 seconds".
The precision figure means that no more than that many units will be used in expressing the time duration. For example, 31,629,659 seconds is a duration of exactly 1 year, 1 day, 2 hours, and 59 seconds (assuming 1 year = exactly 365 days, as we do assume in this module). However, if you wanted an approximation of this to at most two expressed (i.e., nonzero) units, it would round it and truncate it to "1 year and 1 day". Max of 3 expressed units would get you "1 year, 1 day, and 2 hours". Max of 4 expressed units would get you "1 year, 1 day, 2 hours, and 59 seconds", which happens to be exactly true. Max of 5 (or more) expressed units would get you the same, since there are only four nonzero units possible in for that duration.
duration_exact($seconds)
Same as duration($seconds), except that the returned value is an exact (unrounded) expression of $seconds. For example, duration_exact(31629659) returns "1 year, 1 day, 2 hours, and 59 seconds later", which is exactly true. Yes, the page says later there โ weโre keeping it exactly as the doc says. ๐
ago($seconds) / ago($seconds, $precision)
For a positive value of seconds, this prints the same as "duration($seconds, [$precision]) . ' ago'". For example, ago(120) is "2 minutes ago". For a negative value of seconds, this prints the same as "duration($seconds, [$precision]) . ' from now'". For example, ago(-120) is "2 minutes from now". As a special case, ago(0) returns "right now".
ago_exact($seconds)
Same as ago($seconds), except that the returned value is an exact (unrounded) expression of $seconds.
from_now($seconds) / from_now($seconds, $precision) / from_now_exact($seconds)
The same as ago(-$seconds), ago(-$seconds, $precision), ago_exact(-$seconds). For example, from_now(120) is "2 minutes from now".
later($seconds) / later($seconds, $precision)
For a positive value of seconds, this prints the same as "duration($seconds, [$precision]) . ' later'". For example, ago(120) is "2 minutes later". For a negative value of seconds, this prints the same as "duration($seconds, [$precision]) . ' earlier'". For example, later(-120) is "2 minutes earlier". As a special case, later(0) returns "right then".
later_exact($seconds)
Same as later($seconds), except that the returned value is an exact (unrounded) expression of $seconds.
earlier($seconds) / earlier($seconds, $precision) / earlier_exact($seconds)
The same as later(-$seconds), later(-$seconds, $precision), later_exact(-$seconds). For example, earlier(120) is "2 minutes earlier".
concise( function( ... ) )
Concise takes the string output of one of the above functions and makes it more concise. For example, ago(4567) returns "1 hour and 16 minutes ago", but "concise(ago(4567))" returns "1h16m ago".
๐ I18N/L10N NOTES ๐
Little of the internals of this module are English-specific. See source and/or contact me if youโre interested in making a localized version for some other language than English.
๐ BACKSTORY ๐
I wrote the basic ago() function for use in Infobot ("http://www.infobot.org"), because I was tired of this sort of response from the Purl Infobot:
me> Purl, seen Woozle?
<Purl> Woozle was last seen on #perl 20 days, 7 hours, 32 minutes
and 40 seconds ago, saying: Wuzzle!
I figured if it was 20 days ago, I donโt care about the seconds. So once I had written ago(), I abstracted the code a bit and got all the other functions.
โ ๏ธ CAVEAT โ ๏ธ
This module calls a durational"year"an interval of exactly365days of exactly24hours each, with no provision for leap years or monkey business with23/25hour days (much less leap seconds!). But since the main work of this module is approximation, that shouldnโt be a great problem for most purposes.
๐ SEE ALSO ๐
Time::Elapsedโ similarly converts durations to natural language, but in addition to English also supports Danish, German, French, and Turkish.Date::Interval, which is similarly named, but does something rather different.Star Trek: The Next Generation (1987-1994), where the character Data would express time durations like"1 year, 20 days, 22 hours, 59 minutes, and 35 seconds"instead of rounding to"1 year and 21 days". This is because no-one ever told him to useTime::Duration.
ยฉ๏ธ COPYRIGHT AND DISCLAIMER ยฉ๏ธ
Copyright 2013, Sean M. Burke "sburke@cpan.org"; Avi Finkel, "avi@finkel.org", all rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.
๐ AUTHOR ๐
Original author Sean M. Burke, "sburke@cpan.org".
Then maintained by Avi Finkel, "avi@finkel.org".
Currently maintained by Neil Bowers, "neilb@cpan.org".
๐ Thatโs the whole Time::Duration(3) glow-up, bestie. This page is from perl v5.42.0, dated 2019-05-12 โ iconic, no notes. ๐
๐ฆ where this page comes from
- opensuse-leap 16.0: perl-Time-Duration 1.210000-bp160.1.11 ยท Artistic-1.0 OR GPL-1.0-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms