Problems? Is your data what you think it is? PerlMonks

### Quarter Dates

 on Jun 09, 2010 at 06:05 UTC 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

# 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)) {

\$qtr = \$s_dt[1]/3;

} else {

\$qtr = int(\$s_dt[1]/3)+1;
}

print "x is @x\n";

# Calculate the ending quarter for the loop

if (\$e_dt[1]/3 == int(\$e_dt[1]/3)) {

} else {

}

print "y is @y\n";

if (\$e_dt[1] == \$eadd_mon && \$e_dt[2] == \$y[2]) {

} 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",
+;

#  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 \$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);

#  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

Create A New User
Node Status?
node history
Node Type: perlquestion [id://843812]
Approved by sflitman
Front-paged by tye
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2018-05-22 14:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
World peace can best be achieved by:

Results (163 votes). Check out past polls.

Notices?