Sponsored Content
Top Forums Shell Programming and Scripting Generate quarter dates with begin date and end date Post 302426676 by durden_tyler on Wednesday 2nd of June 2010 02:09:35 PM
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
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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

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

7. 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

8. 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

9. 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
DhMakePerl::Config(3pm) 				User Contributed Perl Documentation				   DhMakePerl::Config(3pm)

NAME
DhMakePerl::Config - dh-make-perl configuration class METHODS
parse_command_line_options() Parses command line options and populates object members. parse_config_file() Parse configuration file. config_file member is used for location the file, if not set, dh-make-perl.conf file in home_dir is used. dump_config() Returns a string representation of all configuration options. Suitable for populating configuration file. check_obsolete_entries Checks for presense of deprecated/obsolete entries and warns/dies if any is found. COPYRIGHT &; LICENSE Copyright (C) 2008-2010 Damyan Ivanov <dmn@debian.org> Copyright (C) 2009-2010 Gregor Herrmann <gregoa@debian.org> Copyright (C) 2009 Ryan Niebur <RyanRyan52@gmail.com> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. perl v5.14.2 2012-01-15 DhMakePerl::Config(3pm)
All times are GMT -4. The time now is 11:52 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy