4 Replies Latest reply on Jan 2, 2018 11:58 PM by Jasmine Lee-Oracle

# OPA Logic Puzzle: Days in Month

Here's a simple logic puzzle (much simpler than the one Paul posted last week!)...

You have one input in your policy model: the assessment date

Your goal attribute is: the number of days in the month in which the assessment date falls

In addition to regular months in regular years, your policy model must cater for leap years, including the century leap year rules. For example:

• if the input is 12 Dec 2017, the output is 31
• if the input is 20 Feb 2016, the output is 29
• if the input is 20 Feb 2015, the output is 28
• if the input is 20 Feb 2000, the output is 29
• if the input is 20 Feb 1900, the output is 28 (although 1900 is divisible by 4, it’s not a leap year, see Century Leap Year rules)

This logic was part of a demo I'm working on, and it occurred to me it might be a nice little puzzle. It can be solved in one (fairly undecipherable!) rule, but I broke it out into four rules so the logic was easier to follow.

You don’t need to solve this puzzle in any particular number of rules. Anything that’s logically correct (and ideally efficient) is fine.

Cheers,

Jasmine

• ###### 1. Re: OPA Logic Puzzle: Days in Month

This would be my approach:

the start of the month = MakeDate(ExtractYear(the date), ExtractMonth(the date), 1)

next month = AddMonths(the date, 1)

the start of next month = MakeDate(ExtractYear(next month), ExtractMonth(next month), 1)

number of days = DayDifference(the start of next month, the start of the month);

• ###### 2. Re: OPA Logic Puzzle: Days in Month

These types of questions show up a lot.  I think it is a good puzzle.  OPA has been a great fit for this in my experience in NY.

I hate plugging things, but since the topic was brought up, there is a zip file attached to a blog post from a few months back that demonstrates multiple methods to perform complex date logic in OPA. Advanced Oracle Policy Automation – OPA Access Timeline Pattern

For instance, with the examples in that project, you can count number of weekdays not including holidays for the assessment month, or perhaps whether the assessment date falls on the second Friday of the month due to accounting reasons...   Natural language methods are shown so the business can understand the logic.

• ###### 3. Re: OPA Logic Puzzle: Days in Month

Hi Ian,

Looks good. I've used that approach in the past. Here's another option.

one month after the calculation date = AddMonths(the calculation date, 1)

the start of the month after the calculation date = MakeDate(ExtractYear(one month after the calculation date), ExtractMonth(one month after the calculation date), 1)

the last day of the month in which the calculation date falls = AddDays(the start of the month after the calculation date, -1)

the number of days in the month = ExtractDay(the last day of the month in which the calculation date falls)

Cheers,

Jasmine