Date Calculations

Started by Richard Kelly, November 23, 2013, 11:28:42 PM

Previous topic - Next topic

Richard Kelly

Attached is an example of what I have been currently using to handle a range of date and time calculations in my applications. Although POWERTIME can do a lot of things, there were many other requirements I had where it fell short. The main focus I started with was to have a rules based method of calculating dates. I'm thinking it might be a decent idea to study how Jose uses objects and convert the functions in a similar manner. I could simplfy the exposed functions and hide some of the complexity.

Rick Kelly

Paul Squires

Cool! Thanks for sharing Richard. That is a pretty extensive collection. Not sure if you are aware of Egbert Zijlema's work? I think this is the link: http://zijlema.basicguru.eu/  Maybe there is stuff in there that you can use to add to your collection as well.

:)
Paul Squires
PlanetSquires Software

Richard Kelly

#2
I am aware of Egberts work although I had my first port of the routines from the book Calendrical Calculations at:

http://emr.cs.iit.edu/home/reingold/calendar-book/third-edition/

in 1997 with most of the additions of my own design first crafted in 1999. The attachment represents only the real core with a refined date calculator thrown on top. I removed all the exotic "fluffy" stuff I really did just for the fun of knowing I could.

I've yet to download and look at Egberts work and probably should at some point.

Given this core group of functions, it does get progressively easier to layer other, lesser know calculations and calendars into the mix. As an example, lets take one of the calendars I did not include - the Icelandic or Viking calendar.

Iceland converted over to the Gregorian calendar in 1700 so I've chosen to use the Gregorian calendar and ignore the prior, Julian based version. All the most common public or national holidays in Iceland today are based on either the Gregorian calendar or some variant from Easter. The common Gregorian holidays are the First Day of Summer (First Thursday after April 18), The Seaman's Day (First Sunday of June) and Commerce Day (First Monday of August) all of which are easily calculated with the attached DateCalc.inc functions.

If for some reason, you wanted to actually show dates in an Icelandic format then you have to know that the year is divided into two seasons - summer and winter. (Here in Alaska where I live, we simplify it even further to early winter and late winter). The year starts with summer and the seasons are divided into months although they are not followed and are a secondary feature of the calendar and easily calculated. Further investigation is left to the reader curious about the months.

Similiar to the ISO calendar format of year, week, day of week, the Icelandic date is presented in four parts - the Gregorian Year, Season, Week, and Day of the Week with the Season part added.

The calculations for converting the common days format (days since Jan 1, 1 which is day 1) are very straightforward since the Icelandic calendar year always consists of an even number of whole and complete 7 day weeks - either 52 or 53 and months always begin on the same day of the week.



%ICELANDIC_SUMMER           = 1
%ICELANDIC_WINTER           = 2 


FUNCTION DaysFromIcelandic (BYVAL nYear AS LONG, _
                            BYVAL nSeason AS LONG, _
                            BYVAL nWeek AS LONG, _
                            BYVAL nWeekday AS LONG) AS LONG

' Return the Days date from an Icelandic date

    FUNCTION = IIF(nSeason = %ICELANDIC_SUMMER,IcelandicSummer(nYear),IcelandicWinter(nYear)) _
             + 7 * (nWeek - 1) _
             + cmMod(7,(nWeekday - IIF(nSeason = %ICELANDIC_SUMMER,%THURSDAY,%SATURDAY)))

END FUNCTION
SUB IcelandicFromDays (BYVAL nDays AS LONG, _
                       BYREF nYear AS LONG, _
                       BYREF nSeason AS LONG, _
                       BYREF nWeek AS LONG, _
                       BYREF nWeekday AS LONG)

' Return the Icelandic year, season, week and weekday from a days date

' Calculate Year

    nYear = GregorianYearFromDays(nDays)

    IF nDays < IcelandicSummer (nYear) THEN

        DECR nYear

    END IF

' Calculate Season

    nSeason = IIF(nDays < IceLandicWinter(nYear),%ICELANDIC_SUMMER,%ICELANDIC_WINTER)

' Calculate Week

    nWeek = INT((nDays - IIF(nSeason = %ICELANDIC_SUMMER,IcelandicSummer(nYear),IcelandicWinter(nYear))) / 7) + 1

' Calculate Weekday

    nWeekday = cmGregorianWeekDay(nDays)

END SUB
FUNCTION IcelandicWinter (BYVAL nGregorianYear AS LONG) AS LONG

' Icelandic Winter begins 180 days prior to summer of the next Gregorian Year

    FUNCTION = IceLandicSummer(nGregorianYear + 1) - 180

END FUNCTION
FUNCTION IcelandicSummer (BYVAL nGregorianYear AS LONG) AS LONG

' Icelandic Summer begins on the Thursday on or after April 19th in a Gregorian Year

    FUNCTION = cmWeekDayOnOrAfter(%Thursday,DaysFromGregorian(%APRIL,19,nGregorianYear))

END FUNCTION



It should be noted that the Win32API.inc include is needed.

I did struggle for quite some time with the crescent moon observation and the Islamic calendar. There is no universal agreement on the use of astronomical calculations as the Koran dictates that the observation must be made with the naked eye and I chose where, if the UTC time of a new moon if <= Noon, a crescent moon must be visible in the world somewhere on or after the the international date line else it will be visible the next day.

Enjoy!

Rick

Rolf Brandt

Fantastic collection of functions! Thank you very much for this, Richard.

Rolf
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

You are quite welcome Rolf. They have come a long way from my first crack at them in DLL form written with MASM32.

Rick

Eddy Van Esch

Awesome job, Rick.
Thanks for sharing!
Eddy

Rolf Brandt

I can imagine that there is a long evolution in developing such an extensive library. Similar to Eddy's HUGE Math and Encrytion library.

If one would count the hours....

Rolf
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)