Subject: | %j (num of days) is wildly wrong |
Maybe a better approach to calculating %j would be to convert each
DateTime to epoch() and then subtract and round? It might not be
totally accurate, but it would be a lot more accurate than this. Here's
a test script FWIW. Thanks. --mark--
Subject: | 9_days_j.t |
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl 1.t'
use warnings;
use DateTime;
use DateTime::Duration;
use DateTime::Format::Duration;
#########################
my $num_tests = 50;
use Test::More tests => $num_tests;
srand;
my $dt_fmt_dur_j = DateTime::Format::Duration->new( pattern => '%j' );
# test that random dates are the same with %j format as iteration
for ( 1 .. $num_tests ) {
my $dt_start = DateTime->new(
year => 1900 + int(rand(150)),
month => 1 + int(rand(12)),
day => 1 + int(rand(28)), # good enough
);
my $add_days = int(rand( 365 * 10 ));
my $dt_end = $dt_start->clone->add(days => $add_days);
# make sure %j duration is the same as the number of days we added
my $dur = $dt_end - $dt_start;
my $j = $dt_fmt_dur_j->format_duration( $dur );
# avoid DateTime::Format::Strptime dependency for comments
my $dt_start_fmt = $dt_start->ymd('-');
my $dt_end_fmt = $dt_end->ymd('-');
my $wrong = $add_days - $j;
cmp_ok(
$j, '==', $add_days,
"$dt_end_fmt - $dt_start_fmt is $add_days apart (\%j == $j, diff = $wrong)",
);
}