First time Powerbasic user here and before I begin my next development cycle I want to port and consolidate any number of common routines I have grown accustomed to into one DLL.
This being my maiden venture, so to speak, and hopefully I will learn and grow familiar with this language.
So the creation of this DLL begins...and the first area of interest covers date and time functions. I have in the past developed a complete port (in assembler) cover to cover the algorithms presented in the books Astronomical Algorithms, Second Edition by Jean Meeus and Calendrical Calculations: The Millennium Edition by Edward M. Reingold and Nachum Dershowitz along with a myriad of other custom functions I have found useful. So far, the port is going smoothly and PB is certainly proving an easy master to work for although a ways to go to approach my enjoyment of assembler (MASM32).
With introductions to the group over with, this upcoming project encapsulates a type of business and I'm going to be in need of some business based date calculations that inevitably involve designation of non-business days both as recurring (week days that are workdays/non-workdays) during a year and special days that typically do not reoccur during the year usually spoken of as holidays.
I am not a fan of storing dates (which require someone to maintain) and have taken a break from my DLL port to add a rules-based function that I really like that is capable of calculating every gregorian based public holiday I have been able to find on the internet when provided the gregorian year and the rules describing the holiday.
Given a base date and some number of days to add/subtract which is very simple to do, if you add which days of the week are business or non-business and a list of the observed holidays (as a group of rule sets) two approaches come to mind for calculating the answer.
1. Loop through, adding one day at a time, and checking each day against the weekday and holiday rules until the requisite number of days have been added
2. Calculate the result directly via an algorithmic approach
In keeping with my own and the PB battle cry of efficiency, I have delved into approach 2. It boils down to this:
1. Calculate the preliminary ending date by just adding the presented days.
2. Extend the ending date by the number of non-business weekdays found in the range calculation in step 1
3. Repeat step 2 until the new ending date is a normal business workday.
4. Examine each holiday and check if it is included in the current range and bump the ending date up when true only if the holiday also falls on a weekday that is workday
5. If step 4 is true, redo the weekday check and restart step 4
6. Done when step 4 finds no applicable holidays
There exists the possibility of an endless loop race that I think is avoided as long as one day of the week is a normal workday and can hope that nobody would present 52 or 53 holidays that all fall on that same day of the week and cover an entire gregorian year.
Of course, I have to keep track of when I extend the range past December 31st and regenerate holidays as needed which dictates that the holidays presented to the function be rules based.
In my long winded way, are there any forum members with knowledge or tips on a better way to do this calculation? If I can this to work, I will never, never again store holiday dates, I promise!
Another goal of mine is that this PB DLL be language neutral. If I am to use a set of holiday rules that contains a variable number of rule groups, is it possible, from PB to call this function and pass an array that the DLL just sees as one contiguous segment of memory? I haven't read up yet on how to take this pointer in my PB DLL and use it to parse the rule groups given the variable nature of the the entire structure size.
TIA
Rick Kelly
This being my maiden venture, so to speak, and hopefully I will learn and grow familiar with this language.
So the creation of this DLL begins...and the first area of interest covers date and time functions. I have in the past developed a complete port (in assembler) cover to cover the algorithms presented in the books Astronomical Algorithms, Second Edition by Jean Meeus and Calendrical Calculations: The Millennium Edition by Edward M. Reingold and Nachum Dershowitz along with a myriad of other custom functions I have found useful. So far, the port is going smoothly and PB is certainly proving an easy master to work for although a ways to go to approach my enjoyment of assembler (MASM32).
With introductions to the group over with, this upcoming project encapsulates a type of business and I'm going to be in need of some business based date calculations that inevitably involve designation of non-business days both as recurring (week days that are workdays/non-workdays) during a year and special days that typically do not reoccur during the year usually spoken of as holidays.
I am not a fan of storing dates (which require someone to maintain) and have taken a break from my DLL port to add a rules-based function that I really like that is capable of calculating every gregorian based public holiday I have been able to find on the internet when provided the gregorian year and the rules describing the holiday.
Given a base date and some number of days to add/subtract which is very simple to do, if you add which days of the week are business or non-business and a list of the observed holidays (as a group of rule sets) two approaches come to mind for calculating the answer.
1. Loop through, adding one day at a time, and checking each day against the weekday and holiday rules until the requisite number of days have been added
2. Calculate the result directly via an algorithmic approach
In keeping with my own and the PB battle cry of efficiency, I have delved into approach 2. It boils down to this:
1. Calculate the preliminary ending date by just adding the presented days.
2. Extend the ending date by the number of non-business weekdays found in the range calculation in step 1
3. Repeat step 2 until the new ending date is a normal business workday.
4. Examine each holiday and check if it is included in the current range and bump the ending date up when true only if the holiday also falls on a weekday that is workday
5. If step 4 is true, redo the weekday check and restart step 4
6. Done when step 4 finds no applicable holidays
There exists the possibility of an endless loop race that I think is avoided as long as one day of the week is a normal workday and can hope that nobody would present 52 or 53 holidays that all fall on that same day of the week and cover an entire gregorian year.
Of course, I have to keep track of when I extend the range past December 31st and regenerate holidays as needed which dictates that the holidays presented to the function be rules based.
In my long winded way, are there any forum members with knowledge or tips on a better way to do this calculation? If I can this to work, I will never, never again store holiday dates, I promise!
Another goal of mine is that this PB DLL be language neutral. If I am to use a set of holiday rules that contains a variable number of rule groups, is it possible, from PB to call this function and pass an array that the DLL just sees as one contiguous segment of memory? I haven't read up yet on how to take this pointer in my PB DLL and use it to parse the rule groups given the variable nature of the the entire structure size.
TIA
Rick Kelly
Comment