I have a need to compute a year with fraction between two dates.
Meaning date 1 = 2005-05-04 date 2 = 2015-05-06 then the year I’m after is
10.0054794520548.
My thinking on the above is neither 2005 or 2015 is a leap year so I should use
a devisor of 365 days and the number of days extra between the above two dates
is 2 so 2/365 is 0.0054794520548.
Based on the above thinking I need to determine two things to do the above
generically.
1) what devisor to use 365 or 366 (depends on if either the start or end year
is a leap year)
2) exactly how many days exist between the start date and end date minus all
the extra years.
For item 1 using DateTime this is what I came up with:
my $isMinLargerDayInYear;
unless ($isMinLargerDayInYear=$min->month>$max->month) {
if ($min->month==$max->month) {
$isMinLargerDayInYear=$min->day>$max->day;
}
}
my $isMinBeforeLeapDay=DateTime->new(year=>$min->year-1,month=>12,day=>31)
->delta_days($min)->delta_days<60;
my $isMaxPastLeapDay=DateTime->new(year=>$max->year-1,month=>12,day=>31)
->delta_days($max)->delta_days>59;
# determine if we should use 365 or 366 as divisor
$yeardays=365;
if (($min->is_leap_year && $max->is_leap_year) ||
($min->is_leap_year &&
($isMinBeforeLeapDay && ($isMaxPastLeapDay || $isMinLargerDayInYear))
) ||
($max->is_leap_year && $isMaxPastLeapDay)
) {
$yeardays=366
}
For item 2 using DateTime this is what I came up with:
my $days;
if ($min->is_leap_year) {
$days=$min->delta_days(DateTime->new(year=>$min->year,month=>$max->month,
day=>$max->day))->delta_days;
} else {
if ($isMinLargerDayInYear) {
$days=$max->delta_days(DateTime->new(year=>$max->year-1,month=>$min->month,
day=>$min->day))->delta_days;
} else {
$days=$max->delta_days(DateTime->new(year=>$max->year,month=>$min->month,
day=>$min->day))->delta_days;
}
}
Then for the final value:
my $dur = $max - $min;
my $year = $dur->in_units(‘years’)+$days/$yeardays;
No idea how I would attempt to do the above to account for leap seconds but for
anything I’m planning that level is not necessary.
Just wondering if I totally missed something that would make the above much
easier in DateTime.
Thanks
Matthew