I'd like to know if there are any libraries (preferably DateTime-esque) that can take a normal date time and create an appropriate relative human readable date. Essentially the exact opposite of the more common question: How can I parse relative dates with Perl?.
Obviously, the exact wording/interpretation is up to the actual implementation, but I'm looking to provide a consistent way to specify dates in the future. Knowing an apporximation like "due in 2 weeks
" is (to me) more helpful in getting a grasp of how much time I have remaining than something "due on 2009-07-30
".
Examples:
2009-07-06 => "in 1 year"
2009-07-30 => "in 2 weeks"
2009-07-09 => "tomorrow"
2009-07-09 12:32 => "tomorrow at 12:32"
2009-07-12 05:43 => "monday morning"
2009-07-03 05:74 => "6 days ago"
Update: It looks like this functionality is implemented in a Template Toolkit Plugin. I am leaving the rest of my answer here for reference, but Template::Plugin::DtFormatter might be the best place to look.
Looking at the source code of that module, I was lead to DateTime::Format::Natural which seems related to what you want.
Previous Answer:
Look into Date::Calc to give you deltas using Delta_DHMS
. You should be able to use those deltas to choose how you are going to phrase the date.
Here is a very rudimentary starting point. It is buggy but illustrates the basic idea. Add logic to taste.
#!/usr/bin/perl
use strict;
use warnings;
use Date::Calc qw(:all);
use Lingua::EN::Inflect qw( NO );
my @dates = (
[ 2009, 7, 6 ],
[ 2009, 7, 30 ],
[ 2009, 7, 9 ],
[ 2009, 7, 9, 12, 32 ],
[ 2009, 7, 12, 5, 43 ],
[ 2009, 7, 3, 5, 14 ],
[ 2010, 8, 9 ],
[ 2012, 8, 9 ],
[ 2013, 8, 9 ],
);
for my $date ( @dates ) {
print "@$date: ", relative_when( $date ), "\n";
}
sub relative_when {
my ($year, $month, $day, $hour, $min, $sec) = @{ $_[0] };
my ($Dyear, $Dmon, $Dday, $Dhr, $Dmin, $Dsec) = Delta_YMDHMS(
Today_and_Now(),
$year, $month, $day, $hour || 0, $min || 0, $sec || 0
);
return NO('year', $Dyear ) if $Dyear > 0;
return NO('month', $Dmon ) if $Dmon > 0;
return NO('week', int($Dday/7) if $Dday > 6;
return NO('day', $Dday) if $Dday > 1;
return 'tomorrow' if $Dday == 1;
return 'today' if $Dday == 0;
return "";
}
__END__
Output:
C:\Temp> dfg
2009 7 6:
2009 7 30: 2 weeks
2009 7 9: today
2009 7 9 12 32: today
2009 7 12 5 43: 2 days
2009 7 3 5 14:
2010 8 9: 1 year
2012 8 9: 3 years
2013 8 9: 4 years