How to derive last sunday's date based on Current date

A forum for discussing DataStage<sup>®</sup> basics. If you're not sure where your question goes, start here.

Moderators: chulett, rschirm, roy

Post Reply
DSFreddie
Participant
Posts: 130
Joined: Wed Nov 25, 2009 2:16 pm

How to derive last sunday's date based on Current date

Post by DSFreddie »

Hi All,

I know my question is more of a LINUX command, but i thought i will check in this forum as I have seen lots of guys around with plenty of Scripting knowledge.

Here is the situation:
I have a file coming from MF every Sunday & my Master Sequencer runs any date between Tuesday to Friday of every week (Weekly process). Is there a command by which I can derive the Last Sunday's date based on CURR_BUSINESS_DATE & pass this value to read the files from MF.

Any thoughts will be very helpful.

Thanks in advance
Freddie
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

If you're generating a command line argument from a DataStage sequence an elegant expression is

Code: Select all

Oconv(Date() - Mod(Date(),7), "D-YMD[4,2,2]")
This works because the internal date format (number of days since 1967-12-31) remainder after dividing by 7 is 0 for Sunday.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
qt_ky
Premium Member
Premium Member
Posts: 2895
Joined: Wed Aug 03, 2011 6:16 am
Location: USA

Post by qt_ky »

In Linux/UNIX, date arithmetic is not going to be as easy as using functions already built into DataStage. You can use the date command with format options, but subtracting days from a current date will pretty much force you to build your own calendar logic for going backwards across months and years. Leap years will be annoying too. To give an idea:

Weekday number with Monday being 1 and Sunday being 7:
$ date +%u
7

Weekday number, with Sunday being 0 and Saturday being 6:
$ date +%w
0

Sunday April 1, 2012 is a bad example to start with. If you find some utility or existing program for date arithmetic on Linux/UNIX, then it may become easy. I would stick with DataStage functions for this scenario. Suppose today were not Sunday and you used the weekday number to determine previous Sunday was 3 days ago, so you subtract 3 off the day of month, which is currently day 1 (1 minus 3 = -2):

Today's date formatted YYYY-MM-DD:
$ date +%Y-%m-%d
2012-04-01

Date subtraction from one month to previous month:
$ echo `date +%Y-%m-`$((`date +%d` - 3))
2012-04--2
Choose a job you love, and you will never have to work a day in your life. - Confucius
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

There are also functions in parallel Transformer stage for generating date offset from weekday. I can't research them right now, but you can.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
PaulVL
Premium Member
Premium Member
Posts: 1315
Joined: Fri Dec 17, 2010 4:36 pm

Post by PaulVL »

Why not go simple and have a CRON job echo the date to a text file. Concatinate the date to the file and have it execute first thing every Sunday.

That way you'll leave the OS dealing with any Daylight savings / Time Zone issues. You'll also have a history of dates to look at and potentially reprocess old data.

Your datastage job kickoff script would then read in the text file and pass in the date as a parm to your initial sequencer.
qt_ky
Premium Member
Premium Member
Posts: 2895
Joined: Wed Aug 03, 2011 6:16 am
Location: USA

Post by qt_ky »

That's an excellent idea.
Choose a job you love, and you will never have to work a day in your life. - Confucius
FranklinE
Premium Member
Premium Member
Posts: 739
Joined: Tue Nov 25, 2008 2:19 pm
Location: Malvern, PA

Post by FranklinE »

Having been down this road already, I found the following useful:

Code: Select all

PreviousWeekdayFromDate([yyyy-mm-dd],"friday") -- returns date of named weekday.

WeekdayFromDate([yyyy-mm-dd]) -- returns integer, Sunday is 0.
Franklin Evans
"Shared pain is lessened, shared joy increased. Thus do we refute entropy." -- Spider Robinson

Using mainframe data FAQ: viewtopic.php?t=143596 Using CFF FAQ: viewtopic.php?t=157872
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

They're the ones I meant.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Post Reply