Help with Date logic

Post questions here relative to DataStage Enterprise/PX Edition for such areas as Parallel job design, Parallel datasets, BuildOps, Wrappers, etc.

Moderators: chulett, rschirm, roy

Post Reply
pdntsap
Premium Member
Premium Member
Posts: 107
Joined: Mon Jul 04, 2011 5:38 pm

Help with Date logic

Post by pdntsap »

Hello,

We have two date fields, D1and D2, representing start and end dates. According to business rules, we need to increase the year of the dates by one but keep the same day and month. If the year increment results in an invalid date, we make the date field null or store any default value.

For example, let us assume D1 is 02/29/12. Now incrementing the year by one makes D1 as 02/29/13 which is invalid and hence we need to make the date to be null (or default value). What is the best method of handling this logic?

Thanks.
Kryt0n
Participant
Posts: 584
Joined: Wed Jun 22, 2005 7:28 pm

Post by Kryt0n »

As 29 Feb is the only time you would get an invalid date, just check for that.

However, what's wrong with adding 1 year to a date? Just sounds a strange decision to me
qt_ky
Premium Member
Premium Member
Posts: 2895
Joined: Wed Aug 03, 2011 6:16 am
Location: USA

Post by qt_ky »

There are many date functions to choose from in a parallel job...

See the Parallel Job Developer's Guide Chapter 2. Designing parallel jobs. Look under the "Date and time formats" section for all the possible date and time format tags available. See Appendix B. Parallel Transform functions for date and time functions to choose from.
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 »

DateFromDaysSince() should be the function you need. Offset is 365, or 366 if you're in a leap year.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
pdntsap
Premium Member
Premium Member
Posts: 107
Joined: Mon Jul 04, 2011 5:38 pm

Post by pdntsap »

Thanks for the replies.

I used the DateFromDaysSince() function and made the offset as 366. This works perfect for dates less than 02/29/12 but for dates greater than and equal to 02/29/12 the offset should be 365. One option would be checking each date to be less or greater than 02/29/12 but I believe there might be better options than this. Any help is greatlly appreciated.
Also, how can the logic be made more generic and not just for year 2012?
qt_ky
Premium Member
Premium Member
Posts: 2895
Joined: Wed Aug 03, 2011 6:16 am
Location: USA

Post by qt_ky »

Another option is the DateOffsetByComponents() function. It takes 4 arguments: your date column, year offset, month offset, day offset. Offsets can be positive or negative. Offset a valid Feb 29 date by plus or minus one year will produce a valid result with date of March 1. It's as if your input date were actually March 1. No special leap year handling required, unless you must detect Feb 29 as input dates, then you can add an If Then Else to handle it.

Code: Select all

DateOffsetByComponents(lnk.D1, 1, 0, 0)
Choose a job you love, and you will never have to work a day in your life. - Confucius
kandyshandy
Participant
Posts: 597
Joined: Fri Apr 29, 2005 6:19 am
Location: Singapore

Post by kandyshandy »

got this one from wiki.. may be useful to know.

How to find if a year is a leap or common?

Not evenly divisible by 100 but evenly divisible by 4.
or
Evenly divisible by 100 and evenly divisible by 400.

;)

Any other logic available?
Kandy
_________________
Try and Try again…You will succeed atlast!!
qt_ky
Premium Member
Premium Member
Posts: 2895
Joined: Wed Aug 03, 2011 6:16 am
Location: USA

Post by qt_ky »

kandyshandy wrote:Any other logic available?
Sure, just use the DaysInYear() parallel function on any give date.

(it's in the doc I mentioned above) :wink:
Choose a job you love, and you will never have to work a day in your life. - Confucius
kandyshandy
Participant
Posts: 597
Joined: Fri Apr 29, 2005 6:19 am
Location: Singapore

Post by kandyshandy »

Eric, good one.. i was asking about any other mathematical way ;)
Kandy
_________________
Try and Try again…You will succeed atlast!!
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

That is the long established "mathematical way".
-craig

"You can never have too many knives" -- Logan Nine Fingers
Kryt0n
Participant
Posts: 584
Joined: Wed Jun 22, 2005 7:28 pm

Post by Kryt0n »

I would question if that logic is even true... after all, the year 2000 was a leap year and is divisible by 100... or am I missing something?
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

kandyshandy left out one "not".
1900 was not a leap year.
2000 was a leap year.
2100 will not be a leap year.
2400 will be a leap year.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
kandyshandy
Participant
Posts: 597
Joined: Fri Apr 29, 2005 6:19 am
Location: Singapore

Post by kandyshandy »

ah.. Should have phrased it differently. ;)

Kryt0n, 2000 is evenly divisible by 100 and 400, & so it is a leap year.

My other condition is "Not evenly divisible by 100 but evenly divisible by 4". This is applicable for years that are not centenaries..

If none of the above conditions is satisfied, then it is a common year :idea:
Kandy
_________________
Try and Try again…You will succeed atlast!!
qt_ky
Premium Member
Premium Member
Posts: 2895
Joined: Wed Aug 03, 2011 6:16 am
Location: USA

Post by qt_ky »

Next you can determine leap seconds...
Choose a job you love, and you will never have to work a day in your life. - Confucius
kandyshandy
Participant
Posts: 597
Joined: Fri Apr 29, 2005 6:19 am
Location: Singapore

Post by kandyshandy »

fainted :shock:
Kandy
_________________
Try and Try again…You will succeed atlast!!
Post Reply