Date Calculations

Started by Richard Kelly, January 17, 2011, 09:16:58 AM

Previous topic - Next topic

Richard Kelly

Recently, I pulled together the Gregorian/Julian/Easter date routines I use most frequently into one include file for my FF projects. Although I have code for other calendars such as Coptic, Ethiopic, ISO, Hebrew, Hindu, Mayan, Balinese Pawukon, Persian, Bahai, French Revolutionary, Chinese (with Vietnamese, Korean, Japanese variants) and Tibetan, I left these out to keep code bloat out of my projects although with PB10 that may not be an issue. The basic calendrical unit is an integer representing the number of days since Jan 1, 1 which is day 1 and a Monday and calculations are done within this unit. There are rules based routines to handle holidays and other non-business days with supporting calculation routines for Gregorian and Christian/Orthodox Easter.

I've worked diligently to validate everything, and if a problem is found, please post back here for everybody's benefit.

Enjoy!

Rick

Richard Kelly

#1
Chinese and Persian calendars added. Now that we have some basic astronomical routines that are required for the Chinese calendar, we also have some derivative functions such as sunrise, sunset and lunar phases and illumination. All calendars added are supported when it comes to performing date calculations related to business days calculations. I think it might be time to move on to some real work.... ;)

The astronomical version of the Persian calendar is what I used. When this calendar was adopted in 1925, the authorities at that time claimed that the year begins on the day when the vernal equinox (Spring..around March 20th) occurs before true Noon (midday) in Tehran and is postponed to the next day if the equinox is on or after true noon. There are purely arithmatical versions and when comparing the two for Gregorian years 1637 - 2417 there are 28 years where there is a disagreement. However, the two are in agreement for the Gregorian years 1865 - 2024. This is mainly due to the Persian calendar using 365.24242424242 as their approximation for the mean tropical year vs 365.2425 for the Gregorian and the author of Astronomical Algorithms Jean Meeus provides 365.2421896698 as a base.

Afghanistan adopted the Persian calendar in 1957 with different month names.

Richard Kelly

Hindu Solar and Hindu Lunar calendars added. For the curious few, we are now in the world where a calendar can have missing months and missing days within months. Expunged is the word I've seen used.

Now the struggle begins with the Islamic calendar. While I have the routines that do a good job of calculating when there is a crescent moon, crescent moon sightings also depend of all kinds of atmospheric conditions that are both unpredictable and virtually impossible to express algorithmically. It means that a date I've calculated will, on occasion, be off by one day from official sightings. I've also read some papers outlining a global Islamic calendar. The major premise is that a new moon calculated on the international date line at UTC time, if it occurs before noon, that a crescent moon must be viewable somewhere in the world during the day. I guess I'll whip up the global version and then check it against the past 20 years and see how I do.

This really is a fun little project for me... 8)

Rick

Any Islamic scholars out there with advice?

Rolf Brandt

Quite an extensive project.

Would you in time also provide a litte example program to show the use of these routines, what you can accomplish with them?
Rolf Brandt
http://www.rbsoft.eu
http://www.taxifreeware.com
I cook with wine, sometimes I even add it to the food.
(W. C. Fields)

Richard Kelly

I'm putting together a FF3 demo project with a SQLITE3 database. The main form is a listview of what definitions are in the database with a details form and a wizard for adding new stuff. It's only time... :o

Rolf Brandt

Great - I am looking forward to it.
Rolf Brandt
http://www.rbsoft.eu
http://www.taxifreeware.com
I cook with wine, sometimes I even add it to the food.
(W. C. Fields)

Richard Kelly

Islamic calendar added. I use the global concept as outlined at:

http://moonsighting.com/calendar.html

The results seem reasonable, at least as good as doing all the extra work to calculate a time for a crescent moon for some locale. Time to move on and whip up a demo to show what you can do with all this. ;D

Richard Kelly

While I was pulling the pieces together for a demo, I noticed that I had all the supporting routines for moonrise/set...so I added that capability as well.

Richard Kelly

#8
Here is a quick FF3 demo for date and time calculations. Functional but not bullet proof and pretty... ::)

P.S. I didn't have time to do a SQLITE3 version Rolf... :P

Rolf Brandt

Rolf Brandt
http://www.rbsoft.eu
http://www.taxifreeware.com
I cook with wine, sometimes I even add it to the food.
(W. C. Fields)

Richard Kelly

Thank you for your kind words Rolf. Some other function/subs that I have yet to build into this are:

Get days remaining in Fiscal Gregorian year

Calculate the Gregorian ordinal day of the fiscal year (1-365 or 366 if a fiscal leap year)

Given a calendar month, return the fiscal month

Given a calendar month, return the fiscal quarter it belongs to

Given a month and year, return the fiscal year

A whole series of function/subs that handle calculations/maintenance for 13 month rolling histories

...and the usual bevy of other calendars. I'm also in the middle of building Olson Timezone support into the middle of things and crafting a date calc holiday wizard that will walk you through the maze of options possible. I know this collection is not an easy one for everyone to follow. I have the advantage of experimenting with date/time things for over 30 years now and I'm sure there is much more to learn. If only one person uses any of this, I will be be satisfied and perhaps some things can be found useful for FF4 and give it and us some competitive advantages.

Rick

Richard Kelly

#11
Fiscal period routines added

Rolling 13 month history summary routines added

Coptic/Ethiopic calendars added

Month/Weekday names from windows locale added

Support for Unix/Excel timestamps added.

Previous message attachments updated.


Rick