Here's my attempt in Perl, in case Perl is an option for you.
The following program accepts a date in "yyyymmdd" format and returns the start/end date pairs for the previous quarters and month as per your post.
If no date is passed, it uses today's date.
Only the core modules are used, so no additional module from CPAN is required.
However, these core modules - Time::Piece and Time::Seconds are available from Perl version 5.8 onwards, so that's the minimum version required.
A few test runs:
The start of the fiscal month is set as "07" (July) at line # 15, but it can be changed.
I changed the start of fiscal month to "10" (October) below:
And the script works accordingly:
Thereafter, incorporating this script in a shell script should be easy.
I assume you are connecting to PostgreSQL via the "psql" command-line utility.
If so, the Bash shell script looks like this:
If the script has to be run for some previous date, it can be passed to the shell script.
The shell script, in turn, passes it on to Perl.
The "outfile" name is based on the start/end dates.
Thanks Tyler!. This was Awesome and complete script. But One issue I was facing, see below input for 20180105
Quote:
Originally Posted by durden_tyler
Here's my attempt in Perl, in case Perl is an option for you.
The following program accepts a date in "yyyymmdd" format and returns the start/end date pairs for the previous quarters and month as per your post.
If no date is passed, it uses today's date.
Only the core modules are used, so no additional module from CPAN is required.
However, these core modules - Time::Piece and Time::Seconds are available from Perl version 5.8 onwards, so that's the minimum version required.
A few test runs:
The start of the fiscal month is set as "07" (July) at line # 15, but it can be changed.
I changed the start of fiscal month to "10" (October) below:
And the script works accordingly:
Thereafter, incorporating this script in a shell script should be easy.
I assume you are connecting to PostgreSQL via the "psql" command-line utility.
If so, the Bash shell script looks like this:
If the script has to be run for some previous date, it can be passed to the shell script.
The shell script, in turn, passes it on to Perl.
The "outfile" name is based on the start/end dates.
...
...
But One issue I was facing, see below input for 20180105
Hmm... the output of 20180205 is correct, but the output for 20180105 looks like the one you should've seen for 20170705.
Can't figure out, without further information, why it would work incorrectly for just one value.
The code extracts the month from 20180105 and since that is less than 7, it would:
- go back 1 year to 20170105
- extract the year from it, to get 2017
- add the fiscal month "07" and date "01" to the year, to get 20170701 (start fiscal yr)
- and then jump quarters from start fiscal year. so it should return, for 20180105:
A few questions to get more information:
1) Could you check again if that output is really for 20180105 and not 20170705 ?
2) What output do you get for 20170705 ?
3) Are there any other dates, besides 20180105, for which the output is incorrect ?
4) Could you run the program for all the dates I ran it, in my post ?
5) Did you make any changes in the Perl program I posted ?
6) What is your Perl version ? The following command shows the version:
I tested it on my system that has Perl 5.24 on Debian 9.
7) What are the versions of your Perl modules Time::Piece and Time::Seconds ? The following commands show the version:
1) Could you check again if that output is really for 20180105 and not 20170705 ?
2) What output do you get for 20170705 ?
3) Are there any other dates, besides 20180105, for which the output is incorrect ? Tried all months except for Jan remaining all looks perfect
4) Could you run the program for all the dates I ran it, in my post?
5) Did you make any changes in the Perl program I posted ?
Nope, Just copied as it is
6) What is your Perl version? The following command shows the version:
Thank you for testing it for various inputs and posting the Perl version.
I could not find a working Perl 5.10 version to test it, so am unable to reproduce the bug.
However, after searching on the Internet, it looks like "add_months()" method has a few quirks while working with dates close to the ends of a month.
My best guess is that this line is the culprit:
When you subtract 12 months from "20180105", I think it goes back to some day in December, 2016 instead of "20170105".
Thereafter, it goes takes the year "2016", pads "0701" and arrives at "20160701" as the start fiscal year.
If you print $prev_yr_dt, you should be able to see what date exactly it goes to.
For months 1 through 6 of any year, we only have to jump back a bit enough to reach the previous year. So, instead of 12 a safe jump could be 9.
Change the line # 31 from this:
to this:
That should fix the problem.
A more robust solution would be to extract the year for all dates in months 1 through 6.
Subtract one from the year to get to the previous year.
Then pad month "07" and day "01" to arrive at the start of fiscal year.
That way, the "add_months()" method is avoided altogether.
This User Gave Thanks to durden_tyler For This Post:
Hi All,
I have a date variable say dt="2014-01-06 07:18:38"
Now i need to use this variable to search a log and get the entries which occured after that time. (1 Reply)
Hi Sir/Madam
I have a file data.txt like below
file_name date_of_creation
x 2/10/2012
y 8/11/2010
z 11/3/2013
a 2/10/2013
b 3/10/2013
c ... (4 Replies)
I have to increase the date by one week in an input when script is executed in solaris. I was able to acheive this using ksh script that is working in Linux enivironment, when i execute the same script in Solaris i am getting below error:
/var/tmp\n\r-> ./script.ksh
date: illegal option -- d... (3 Replies)
Hi there,
I have a requirement to append dates going forward to a certain line in a file.
I'm not sure of how to go about this. Any help will be greatly appreciated.
Thanks
Slyesco:wall: (2 Replies)
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)
Hi.,
After retrieving values from DB I have two datestamps in format:
12/01/2010:05:40:00 AM and 12/01/2010:06:00:00 PM.
general time format: MM/DD/YYYY:HH:MM:SS AM or PM
Any quick solution to get the difference of two in the format : 1 day(s) 12:20:00
Thanks., (6 Replies)
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
... (9 Replies)