Generate quarter dates with begin date and end date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Generate quarter dates with begin date and end date
# 1  
Old 06-02-2010
CPU & Memory Generate quarter dates with begin date and end date

Hi All,

I am trying to generate quarter dates with user giving input as begin date and end date. Example: Input by user:

begin_date = "2009-01-01"
end_date = 2010-04-30"

required output:

2009-01-01 2009-03-31 09Q01
2009-04-01 2009-06-30 09Q02
.
.
till
2010-01-01 2010-03-31 10Q01

TIA
# 2  
Old 06-02-2010
Quote:
Originally Posted by sol_nov
...
Example: Input by user:

begin_date = "2009-01-01"
end_date = 2010-04-30"

required output:

2009-01-01 2009-03-31 09Q01
2009-04-01 2009-06-30 09Q02
.
.
till
2010-01-01 2010-03-31 10Q01

...
Here's one way to do it with Perl:

Code:
$
$
$ # display the content of the Perl program
$ cat -n printqtr.pl
     1  #!perl -w
     2  use Date::Calc qw (Add_Delta_YMD Date_to_Days);
     3
     4  # 1st parameter is start date; 2nd is end date
     5  # no input validation done here
     6  @sdate = split(/-/,$ARGV[0]);
     7  @edate = split(/-/,$ARGV[1]);
     8
     9  # determine the beginning quarter for the loop
    10  if ($sdate[1]/3 == int($sdate[1]/3)) {
    11    $add_mon = 3*($sdate[1]/3-1);
    12    $qtr = $sdate[1]/3;
    13  } else {
    14    $add_mon = 3*int($sdate[1]/3);
    15    $qtr = int($sdate[1]/3)+1;
    16  }
    17  @x = Add_Delta_YMD($sdate[0],1,1, 0,$add_mon,0);
    18
    19  # determine the ending quarter for the loop
    20  if ($edate[1]/3 == int($edate[1]/3)) {
    21    $eadd_mon = 3*($edate[1]/3);
    22  } else {
    23    $eadd_mon = 3*(int($edate[1]/3)+1);
    24  }
    25  @y = Add_Delta_YMD($edate[0],1,1, 0,$eadd_mon,-1);
    26
    27  # and now loop from beginning quarter to ending quarter
    28  while (Date_to_Days(@x) < Date_to_Days(@y)) {
    29    printf("%4d-%02d-%02d\t%4d-%02d-%02d\t%sQ%02d\n",
    30           @x, Add_Delta_YMD(@x, 0,3,-1), substr($x[0],2,2), $qtr);
    31    @x = Add_Delta_YMD(@x, 0,3,0);
    32    #  roll over quarter to 1, beyond 4
    33    $qtr++;
    34    $qtr = $qtr > 4 ? 1 : $qtr;
    35  }
$
$ # test it for different cases, boundary conditions etc.
$
$ # both parameters identical
$ perl printqtr.pl 2009-01-01 2009-01-01
2009-01-01      2009-03-31      09Q01
$
$ # dates within a quarter
$ perl printqtr.pl 2009-01-01 2009-01-20
2009-01-01      2009-03-31      09Q01
$
$ # dates spanning multiple quarters of the same year
$ perl printqtr.pl 2009-01-01 2009-04-01
2009-01-01      2009-03-31      09Q01
2009-04-01      2009-06-30      09Q02
$
$ # dates spanning multiple quarters of multiple years
$ perl printqtr.pl 2009-01-01 2011-08-28
2009-01-01      2009-03-31      09Q01
2009-04-01      2009-06-30      09Q02
2009-07-01      2009-09-30      09Q03
2009-10-01      2009-12-31      09Q04
2010-01-01      2010-03-31      10Q01
2010-04-01      2010-06-30      10Q02
2010-07-01      2010-09-30      10Q03
2010-10-01      2010-12-31      10Q04
2011-01-01      2011-03-31      11Q01
2011-04-01      2011-06-30      11Q02
2011-07-01      2011-09-30      11Q03
$
$ # dates in incorrect order
$ perl printqtr.pl 2011-08-01 2001-08-28
$
$

HTH,
tyler_durden
# 3  
Old 06-02-2010
CPU & Memory

Thanks tyler_durden, it is working... thanks a lot...
can it be done using awk? just wanted to know...Smilie
# 4  
Old 06-07-2010
Hi Tyler,

Suppose if Igive start date as 2000-10-01 and end date as 2010-05-31

Can I get output as

2009-10-01 2009-12-31 09Q04
2010-01-01 2010-03-31 10Q01
2010-04-01 2010-05-31 10Q02
# 5  
Old 06-07-2010
Quote:
Originally Posted by sol_nov
...

Suppose if Igive start date as 2000-10-01 and end date as 2010-05-31

Can I get output as

2009-10-01 2009-12-31 09Q04
2010-01-01 2010-03-31 10Q01
2010-04-01 2010-05-31 10Q02
Well, what do you see when you pass those parameters to the program ?

tyler_durden
# 6  
Old 06-07-2010
Quote:
Originally Posted by durden_tyler
Well, what do you see when you pass those parameters to the program ?

tyler_durden
2009-10-01 2009-12-31 09Q04
2010-01-01 2010-03-31 10Q01
2010-04-01 2010-06-30 10Q02

Instead of 2010-06-30 can I get the end provided as output...
# 7  
Old 06-07-2010
Quote:
Originally Posted by sol_nov
2009-10-01 2009-12-31 09Q04
2010-01-01 2010-03-31 10Q01
2010-04-01 2010-06-30 10Q02

Instead of 2010-06-30 can I get the end provided as output...
I guess you are passing "2009-10-01" as the first parameter and not "2000-10-01" as mentioned in your earlier post:

Quote:
Originally Posted by sol_nov
Suppose if Igive start date as 2000-10-01 and end date as 2010-05-31
Set the value of @y to the lesser of @y and @edate at line no. 26 of the script.

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Splitting week start date and end date based on custom period start dates

Below are my custom period start and end dates based on a calender, these dates are placed in a file, for each period i need to split into three weeks for each period row, example is given below. Could you please help out to achieve solution through shell script.. File content: ... (2 Replies)
Discussion started by: nani2019
2 Replies

2. Linux

How to calculate the quarter end date according to the current date in shell script?

Hi, My question is how to calculate the quarter end date according to the current date in shell script? (2 Replies)
Discussion started by: Divya_1234
2 Replies

3. UNIX for Dummies Questions & Answers

Print start date to end date, given $1 & $2 in ksh

Dear all, I have an user passing 2 parameter 31/03/2015 and 02/04/2015 to a ksh script. How to print the start date to end date. Expected output is : 31/03/2015 01/04/2015 02/04/2015 Note : 1. Im using aix and ksh 2. I have tried to convert the given input into a date, didnt... (0 Replies)
Discussion started by: mr.rajaravi
0 Replies

4. Shell Programming and Scripting

Get 1st date of month, week , quarter

Hi , I need to trigger few jobs based on the system date ( in case user is not passing any date to the script. In case passing then need to take the user date). Here is the requirement 1. Everyday call daily script 2. On 1st day of week call weekly script 3. On 1st day of month call... (4 Replies)
Discussion started by: Anupam_Halder
4 Replies

5. UNIX for Dummies Questions & Answers

Find Quarter Start and End Dates

Dear Members, Depending on the current date i should find out the start and end dates of the quarter. ex: Today date is 14-Nov-2011 then Quarter start date should be Oct 1 2011 and Quarter End date should be Dec 31 2011. How can i do this? Thanks Sandeep (1 Reply)
Discussion started by: sandeep_1105
1 Replies

6. Shell Programming and Scripting

ksh compare dates INSIDE a file (ie date A is > date B)

In KSH, I am pasting 2 almost identical files together and each one has a date and time on each line. I need to determine if the first instance of the date/time is greater than the 2nd instance of the date/time. If the first instance is greater, I just need to echo that line. I thought I would... (4 Replies)
Discussion started by: right_coaster
4 Replies

7. Shell Programming and Scripting

Need to capture dates between start date and end date Using perl.

Hi All, Want to get all dates and Julian week number for that date between the start date and end date. How can I achive this using perl? (To achive above functionality, I was connecting to the database from DB server. Need to execute the same script in application server, since databse... (6 Replies)
Discussion started by: Nagaraja Akkiva
6 Replies

8. Shell Programming and Scripting

Need to capture all dates between start date and End date.

Hi All, I enter Start date and end date as parameters. I need to capture dates between start date and end date. Please let me know if you have any idea the same. Thanks in advance. Nagaraja Akkivalli. (5 Replies)
Discussion started by: Nagaraja Akkiva
5 Replies

9. UNIX for Dummies Questions & Answers

Get Quarter Date

Hi I need to produce a report that the start date must be the first day of the current quarter. Therefore a repor that is run today the start date would be 01/10/2010 (ddmmyyyy). Obvioulsy I could hard code this date in but what happens when the report is run in January the start of the... (6 Replies)
Discussion started by: theref
6 Replies
Login or Register to Ask a Question