Perl DateTime 持续时间格式不显示年份

Perl DateTime duration format not displaying years

我在计算两个日期之间的差异时遇到问题。

my $todayDate = DateTime->now;
my @updateDateFields = split /\//, $proteinObj->{lastUpdate}; #yyyy/mm/dd

my $updateDateTime = DateTime->new(
    year => @updateDateFields[0],
    month=> @updateDateFields[1],
    day=> @updateDateFields[2]
);

my $daysSinceLastUpdate = $todayDate - $updateDateTime;

my $dfd = DateTime::Format::Duration->new(pattern => '%Y years, %m months, %e days');
print "Last update was: ". $dfd->format_duration($daysSinceLastUpdate). " ago.\n";

输出是这样的:

Last update date: 2015/01/13 Last update was: 0 years, 22 months, 0 days ago.

不显示1年10个月0天前

您需要在 DateTime::Format::Duration 对象中启用 normalise 选项,像这样

my $dfd = DateTime::Format::Duration->new(
    pattern   => '%Y years, %m months, %e days',
    normalise => 1,
);