Hi Monks, I have a perl code which generates quarter dates, based on user given inputs. Below is the code qtr_dates.pl
#!/usr/local/bin/perl use Date::Calc qw (Add_Delta_YMD Date_to_Days); # Parameter 1: start_date; # Parameter 2: end_date; @s_dt = split(/-/,$ARGV[0]); @e_dt = split(/-/,$ARGV[1]); # Calculate the begininning quarter for the loop if ($s_dt[1]/3 == int($s_dt[1]/3)) { $add_mon = 3*($s_dt[1]/3-1); $qtr = $s_dt[1]/3; } else { $add_mon = 3*int($s_dt[1]/3); $qtr = int($s_dt[1]/3)+1; } @x = Add_Delta_YMD($s_dt[0],1,1, 0,$add_mon,0); print "x is @x\n"; # Calculate the ending quarter for the loop if ($e_dt[1]/3 == int($e_dt[1]/3)) { $eadd_mon = 3*($e_dt[1]/3); } else { $eadd_mon = 3*(int($e_dt[1]/3)+1); } @y = Add_Delta_YMD($e_dt[0],1,1, 0,$eadd_mon,-1); print "y is @y\n"; if ($e_dt[1] == $eadd_mon && $e_dt[2] == $y[2]) { @y = Add_Delta_YMD($e_dt[0],1,1, 0,$eadd_mon,-1); } else { @y = ($e_dt[0],$e_dt[1],$e_dt[2]); } print "y1 is @y\n"; # Now loop for beginning qtr and ending qtr while (Date_to_Days(@x) < Date_to_Days(@y)) { printf("%4d-%02d-%02d\t%4d-%02d-%02d\t%sQ%01d\n", @x, Add_Delta_YMD(@x, 0,3,-1), substr($x[0],2,2), $qtr) +; @x = Add_Delta_YMD(@x, 0,3,0); # roll over quarter to 1, beyond 4 $qtr++; $qtr = $qtr > 4 ? 1 : $qtr; }
For example:
qtr_dates.pl 2009-10-01 2010-05-31 output: 2009-10-01 2009-12-31 09Q4 2010-01-01 2010-03-31 10Q1 2010-04-01 2010-06-30 10Q2
Now the problem is I want the end_date to be the qtr_end_date in the last line, i.e.
qtr_dates.pl 2009-10-01 2010-05-31 output: 2009-10-01 2009-12-31 09Q4 2010-01-01 2010-03-31 10Q1 2010-04-01 2010-05-31 10Q2
if it end date is 2010-06-30 then output should be:
2009-10-01 2009-12-31 09Q4 2010-01-01 2010-03-31 10Q1 2010-04-01 2010-06-30 10Q2
TIA

In reply to Quarter Dates by brad_nov

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.