Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Quarter Dates

by brad_nov (Novice)
on Jun 09, 2010 at 06:05 UTC ( [id://843812]=perlquestion: print w/replies, xml ) Need Help??

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

Replies are listed 'Best First'.
Re: Quarter Dates
by sflitman (Hermit) on Jun 09, 2010 at 06:24 UTC
    Here is a solution:
    . . . # Now loop for beginning qtr and ending qtr while (Date_to_Days(@x) < Date_to_Days(@y)) { my @qtr_end_date=Add_Delta_YMD(@x, 0,3,-1); my $diff=Date_to_Days(@qtr_end_date)-Date_to_Days(@e_dt); print "DIFF $diff\n"; if (abs($diff)<60) { @qtr_end_date=@e_dt; } printf("%4d-%02d-%02d\t%4d-%02d-%02d\t%sQ%01d\n", @x, @qtr_end_date, 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; } . . .
    This is tested for your two cases. I am curious why you would want output which truncates the last quarter?

    HTH,
    SSF

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://843812]
Approved by sflitman
Front-paged by tye
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-24 13:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found