Welcome to the Monastery PerlMonks

### Re: date ranges as days

by punkish (Priest)
 on Feb 23, 2011 at 20:33 UTC ( #889860=note: print w/replies, xml ) Need Help??

in reply to date ranges as days

Thanks wind and ikegami. Once again, my apologies for this very simple problem that I should have figured out myself. I am adding my own solution to your contributions for the benefit of the archives

```use Time::JulianDay;

my @start = (1981, 2, 1);
my @end   = (1982, 2, 5);
my \$days = days('start' => \@start, 'end' => \@end, 'type' => 'yearly'
+);
my \$days = days('start' => \@start, 'end' => \@end, 'type' => 'range')
+;

sub days {
my (%a) = @_;

my (\$start, \$end, \$type) = (\$a{'start'}, \$a{'end'}, \$a{'type'});

if (\$type eq 'range') {
my \$j0 = julian_day(1980, 1, 1);
my \$j1 = julian_day(@\$start);
my \$j2 = julian_day(@\$end);

return ((\$j1 - \$j0), (\$j2 - \$j0));
}
elsif (\$type eq 'yearly') {
my \$start_year = \$start->[0];
my \$end_year = \$end->[0];

my %days;
for my \$year (\$start_year .. \$end_year) {
my \$j0 = julian_day(\$year, 1, 1);
my \$j1; my \$j2;
if (\$year == \$start_year) {
\$j1 = julian_day(\$year, \$start->[1], \$start->[2]);
\$j2 = julian_day(\$year, 12, 31);
}
elsif (\$year == \$end_year) {
\$j1 = julian_day(\$year, 1, 1);
\$j2 = julian_day(\$year, \$end->[1], \$end->[2]);
+
}
else {
\$j1 = julian_day(\$year, 1, 1);
\$j2 = julian_day(\$year, 12, 31);
}

\$days{"\$year"} = [(\$j1 - \$j0) .. (\$j2 - \$j0)];
}

return \%days;
}
}
--

when small people start casting long shadows, it is time to go to bed

Create A New User
Node Status?
node history
Node Type: note [id://889860]
help
Chatterbox?
 [erix]: haha

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2018-06-22 17:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should cpanminus be part of the standard Perl release?

Results (124 votes). Check out past polls.

Notices?