Simple calendar

Calendar functionality is a must in applications where working or non-working days have a special meaning. Fixed holidays (solar cycle) and weekends can be determined without any effort. In European tradition there are also floating holidays dependant on Easter – derived from lunar cycle. Determining these is non-trivial but already solved problem described as Computus.

Currently most of programming languages are equipped with utilities (math and date) that make implementation simple. It allows to build an algorithm of everlasting calendar in any system. Why I'm diving into so trivial problem? Because I consider embedding of such algorithm as evil.

Algorithmic version

I would start from version I recognize as wrong – it will be base for further extension. I'm using Open Office Calc as my prototyping language. Current version (3.x) has in-built Easter function which allow you to focus on differences between approaches.

On first spreadsheet tab I enumerated fixed and floating holidays dates (in Poland). Simple formula combines weekday default (Saturday and Sunday are non-working) with these dates. My calendar prototype has friendly interface and appear to be everlasting.

Calendar in Calc spreadsheet (algorithmic version)

Prepared version

Everlasting lasts till something change. If it is true that 2148-05-02 is working day? I would better toss. Second thing is that above is applicable only when business calendar is the same as national one. It is rare situation. Additionally in Poland we have special regulation (odd one) which forces employers to give a free day for any holiday that happen on Saturday. There is no general rule when additional non-working day appear.

Algorithmic version can be extended with configurable exceptions but it is not convenient direction. Much better is to treat every day as exception. Basing on first formulas on second tab i put implementation of calendar with entry for each day. In Calc formula can be copied easily as long as you wish. One year ahead is reasonable – it is our non-it-tradition.

Calendar in Calc spreadsheet (prepared version)

Exceptions can be simply added by replacing formula with WORKING or FREE literal.

Enterprise version

From this point only one another step must be taken to get an advanced module. Enterprise in name can appear when we use relational database. So on third tab I built database inserts basing on second tab results.

INSERT INTO CALENDAR (DAY, PROPERTY, VALUE)
            VALUES ('2010-1-1','EMPLOYEE','FREE');
INSERT INTO CALENDAR (DAY, PROPERTY, VALUE)
            VALUES ('2010-1-2','EMPLOYEE','FREE');
INSERT INTO CALENDAR (DAY, PROPERTY, VALUE)
            VALUES ('2010-1-3','EMPLOYEE','FREE');
INSERT INTO CALENDAR (DAY, PROPERTY, VALUE)
            VALUES ('2010-1-4','EMPLOYEE','WORKING');
INSERT INTO CALENDAR (DAY, PROPERTY, VALUE)
            VALUES ('2010-1-5','EMPLOYEE','WORKING');
INSERT INTO CALENDAR (DAY, PROPERTY, VALUE)
            VALUES ('2010-1-6','EMPLOYEE','WORKING');
INSERT INTO CALENDAR (DAY, PROPERTY, VALUE)
            VALUES ('2010-1-7','EMPLOYEE','WORKING');
...

After creating a table and importing data into we can determine kind of a day using simple database query. No result means that calendar has not been prepared. But it is better than taking wrong decision based on calendar without revision.

This structure is also opened for further extension – any new property can be added and successfully queried. Of course RDBMS can be replaced with any map implementation or best with XML structure.

Summary

Solved problem is not so important as the method. Direct usage of real life experience is usually better than computational. Solving unexpressed requirements (case of year 2148) make solving real problems harder or inefficient.

If you find it interesting check full version of Calc spreadsheet on my files site.

0 komentarze:

Post a Comment