Reputation: 75
I'm attempting to take a start date and end date and iterate through this date range and identify the number of weekends/holidays present
Is it possible to build a table that contains the dates within this range? I could then identify the weekends within this table and build my counter off this. Is this the proper approach approach or is there an easier way to iterate through the range?
function countWeekends(startDate, endDate)
dateRange = {}
for i = startDate, endDate
do dateRange[i] = {}
Upvotes: 2
Views: 455
Reputation: 29523
For the number of weekend days, as long as you can reliably determine which day of the week the start day falls upon, the computation is straightforward. Since you didn't ask how to determine day of week of particular start date, I will assume you already have a way to do that. If you don't, and you can't find a Lua library, consider finding a reliable C or C++ library (like boost::date_time) and export a few of its functions (or even just one of your own functions that calls into those libraries, Lua doesn't need to access the libs except yours).
You'll also need a reliable way of determining which years have a Feb 29th, but again that's not really your question so if you can do that, you can iterate and if your iteration crosses the Feb 28th you know to add one day for those years.
Regarding the holidays, that one will be tricky because many are based on rules like "second Friday of 10th month". The boost::date_time lib can probably handle most of these, again you would create a C/C++ DLL extension to Lua.
Alternately, bypass iteration entirely and use an online calculator like http://www.timeanddate.com/date/workdays.html?y1=2014&m2=1&d2=7&y2=2014&m2=1&d2=7 to submit requests (you'll have to look for proper Lua libs to do this) or check out the javascript used there and convert to Lua.
Upvotes: 1