http://www.perlmonks.org?node_id=843812

brad_nov has asked for the wisdom of the Perl Monks concerning the following question:

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