Page 1 of 1

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

Posted: Wed Mar 07, 2012 5:51 pm
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

Posted: Wed Mar 07, 2012 6:36 pm
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.

Posted: Sun Apr 01, 2012 8:50 pm
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

Posted: Mon Apr 02, 2012 12:54 am
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.

Posted: Mon Apr 02, 2012 7:44 am
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.

Posted: Mon Apr 02, 2012 8:13 am
by qt_ky
That's an excellent idea.

Posted: Mon Apr 02, 2012 10:46 am
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.

Posted: Mon Apr 02, 2012 3:01 pm
by ray.wurlod
They're the ones I meant.