Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
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


Comment on Re: date ranges as days
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://889860]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (12)
As of 2015-07-06 15:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (77 votes), past polls