Time::Duration - rounded or exact English expression of durations
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);
This module provides functions for expressing durations in rounded or exact terms.
In the first example in the Synopsis, using duration($interval_seconds):
If the time() - $start_time
is 3 seconds, this prints "Runtime: 3 seconds.". If it's 0 seconds, it's "Runtime: 0 seconds.". If it's 1 second, it's "Runtime: 1 second.". If it's 125 seconds, you get "Runtime: 2 minutes and 5 seconds.". If it's 3820 seconds (which is exactly 1h, 3m, 40s), you get it rounded to fit within two expressed units: "Runtime: 1 hour and 4 minutes.". Using duration_exact instead would return "Runtime: 1 hour, 3 minutes, and 40 seconds".
In the second example in the Synopsis, using ago($interval_seconds):
If the $age is 3 seconds, this prints "file was modified 3 seconds ago". If it's 0 seconds, it's "file was modified just now", as a special case. If it's 1 second, it's "from 1 second ago". If it's 125 seconds, you get "file was modified 2 minutes and 5 seconds ago". If it's 3820 seconds (which is exactly 1h, 3m, 40s), you get it rounded to fit within two expressed units: "file was modified 1 hour and 4 minutes ago". Using ago_exact instead 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, $age is -3, and you'll get the equally and appropriately surprising "file was modified 3 seconds from now."
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.
This module provides all the following functions, which are all exported by default when you call use Time::Duration;
.
$precision || 2
expressed units. (That is, duration($seconds) is the same as duration($seconds,2).)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".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".ago(4567)
returns "1 hour and 16 minutes ago", but concise(ago(4567))
returns "1h16m ago".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.
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.
This module calls a durational "year" an interval of exactly 365 days of exactly 24 hours each, with no provision for leap years or monkey business with 23/25 hour 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.
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 use Time::Duration.
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.
Current maintainer Avi Finkel, avi@finkel.org
; Original author Sean M. Burke, sburke@cpan.org