Yes, I had seen that. It looked very similar to Time::Duration except that it's based on DateTime.
Here's a script comparing Time::Duration, DateTime::Format::Human::Duration, and Time::Ago for various durations.
#!/usr/bin/env perl
use strict;
use warnings;
use lib 'lib';
use DateTime;
use DateTime::Format::Human::Duration;
use Time::Ago;
use Time::Duration;
my $hour = 60 * 60;
my $day = $hour * 24;
my $month = $day * 30;
my @durations = (
0,
38,
60 * 4,
$hour * 3.8,
$month * 7,
$month * 23,
$month * 28,
);
my $class = 'Human::Duration';
foreach (@durations) {
my $dt1 = DateTime->from_epoch(epoch => time - $_);
my $dt2 = DateTime->from_epoch(epoch => time);
my $diff = $dt2 - $dt1;
print "Seconds: $_\n";
printf "%-25s %s\n", ' Time::Ago', Time::Ago->in_words($diff);
print "\n";
my $class = ' Human::Duration';
printf "%-25s %s\n", "$class #1",
DateTime::Format::Human::Duration->new->format_duration($diff);
printf "%-25s %s\n", "$class #2",
DateTime::Format::Human::Duration->new->format_duration(
$diff,
units => [qw/ years months /],
);
printf "%-25s %s\n", "$class #3",
DateTime::Format::Human::Duration->new->format_duration(
$diff,
precision => 'years',
);
print "\n";
$class = ' Time::Duration';
printf "%-25s %s\n", "$class #1", Time::Duration::ago($_);
printf "%-25s %s\n", "$class #1", Time::Duration::ago($_, 1);
print "\n\n";
}
Output:
Seconds: 0
Time::Ago less than a minute
Human::Duration #1 no time
Human::Duration #2 no time
Human::Duration #3 no time
Time::Duration #1 right now
Time::Duration #2 right now
Seconds: 38
Time::Ago 1 minute
Human::Duration #1 38 seconds
Human::Duration #2 no time
Human::Duration #3 no time
Time::Duration #1 38 seconds ago
Time::Duration #2 38 seconds ago
Seconds: 240
Time::Ago 4 minutes
Human::Duration #1 4 minutes
Human::Duration #2 no time
Human::Duration #3 no time
Time::Duration #1 4 minutes ago
Time::Duration #2 4 minutes ago
Seconds: 13680
Time::Ago about 4 hours
Human::Duration #1 3 hours and 48 minutes
Human::Duration #2 no time
Human::Duration #3 no time
Time::Duration #1 3 hours and 48 minutes ago
Time::Duration #2 4 hours ago
Seconds: 18144000
Time::Ago 7 months
Human::Duration #1 6 months, 4 weeks, and 1 day
Human::Duration #2 6 months
Human::Duration #3 no time
Time::Duration #1 210 days ago
Time::Duration #2 210 days ago
Seconds: 59616000
Time::Ago almost 2 years
Human::Duration #1 1 year, 10 months, 3 weeks, and 1 day
Human::Duration #2 1 year and 10 months
Human::Duration #3 1 year
Time::Duration #1 1 year and 325 days ago
Time::Duration #2 2 years ago
Seconds: 72576000
Time::Ago over 2 years
Human::Duration #1 2 years, 3 months, 2 weeks, and 5 days
Human::Duration #2 2 years and 3 months
Human::Duration #3 2 years
Time::Duration #1 2 years and 110 days ago
Time::Duration #2 2 years ago
You can see that Time::Ago is much more lax in it's approximation, always returning just a single, rounded unit.
The use-case for this is when precision is not especially important. e.g., for when a comment was made on a website, etc.
The latest version of T::A on github is now using Locale::TextDomain for localization, and support has been added for handling DateTime::Duration objects directly.
|