Using DateTime:
use strict; use warnings; use feature qw( say ); use DateTime qw( ); # Or using your favourite DateTime::Format parser. my $date1 = DateTime->new( year => 1981, month => 2, day => 1 ); my $date2 = DateTime->new( year => 1982, month => 2, day => 5 );

Part 1:

my $ref = DateTime->new( year => 1980, month => 1, day => 1 ); say $date1->delta_days($ref)->in_units('days'); say $date2->delta_days($ref)->in_units('days');

Part 2:

sub last_day_of_year { ( my $dt = DateTime->new( year => $_[0], month => 1, day => 1 ) ) ->add( years => 1, days => -1 ); return $dt->day_of_year(); } my %in_range; my $year1 = $date1->year(); my $year2 = $date2->year(); if ($year1 == $year2) { $in_range{$year1} = [ $date1->day_of_year()-1 .. $date2->day_of_yea +r()-1 ]; } else { $in_range{$year1} = [ $date1->day_of_year()-1 .. last_day_of_year($ +year1)-1 ]; for my $year ($year1+1 .. $year2-1) { $in_range{$year} = [ 0 .. last_day_of_year($year)-1 ]; } $in_range{$year2} = [ 0 .. $date2->day_of_year()-1 ]; } for my $year (sort { $a <=> $b } keys(%in_range)) { say "$year: @{ $in_range{$year} }"; }

PS - You said 365 where you should have said 364.

Update: Added part 2.


In reply to Re: date ranges as days by ikegami
in thread date ranges as days by punkish

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.