Commercial Calendar Calculations

I’m interested in doing calculations with a commercial calendar. For
example, “Find every 2nd business day of the month for 2008”. I’m
feeling a
little long on how to approach the topic. Does anyone know of a library
I
could leverage for such a thing (the ability to use said library in
Rails is
a must)? If not, does anyone have experience that they could share on
the
matter?

Many thanks,

James H.

On Tue, Jun 3, 2008 at 2:43 PM, James H. [email protected]
wrote:

I’m interested in doing calculations with a commercial calendar. For
example, “Find every 2nd business day of the month for 2008”. I’m feeling a
little long on how to approach the topic. Does anyone know of a library I
could leverage for such a thing (the ability to use said library in Rails is
a must)? If not, does anyone have experience that they could share on the
matter?

The runt gem provides a nice library for working with recurring
events, it’s probably got what you’re looking for.

Personally, I looked at runt for an event calendar I built sometime
last year, but it didn’t end up being useful as I needed to be able to
store the events in a relational database and query for matching
events therein. To use runt, I’d have had to hydrate all of the events
whenever I wanted to run a query. That was too slow for my use case,
so I ended up doing it all in sql. I’ll be happy to share the details
of that implementation if you’re in a similar boat.

  • donald

On Jun 3, 3:43 pm, James H. [email protected] wrote:

I’m interested in doing calculations with a commercial calendar. For
example, “Find every 2nd business day of the month for 2008”. I’m feeling a
little long on how to approach the topic. Does anyone know of a library I
could leverage for such a thing (the ability to use said library in Rails is
a must)? If not, does anyone have experience that they could share on the
matter?

When you say “2nd business day”, do you mean logic like, “the 2nd of
the month if it’s Tuesday-Friday or the first Monday of the month
otherwise”? Or do you want to take into account a list of holidays
that might make certain weekdays not count as business days?

If it’s the former, you could use the Date class (in the ‘date’
library). I don’t know of a library that handles issues such as
holidays; have you’ve combed through rubyforge.org?

Eric

====

LearnRuby.com offers Rails & Ruby HANDS-ON public & ON-SITE
workshops.
Ruby Fundamentals Wkshp June 16-18 Ann Arbor, Mich.
Ready for Rails R. Wkshp June 23-24 Ann Arbor, Mich.
Ruby on Rails Wkshp June 25-27 Ann Arbor, Mich.
Ruby Plus Rails Combo Wkshp June 23-27 Ann Arbor, Mich
Please visit http://LearnRuby.com for all the details.

Eric,
Both. So, if you ask the program to schedule “every first Friday of the
month”, if Friday is a holiday, the program should schedule a Thursday
(likewise, if you say “every last day of the month”, if the last day is
a
holiday, the first business day prior would be scheduled).

It’s quite a complicated situation. I thought Runt (as Donald
mentioned)
might help, but I’m not sure it will.

James

Donald,
I’m definitely interested in hearing about the details. I’ll be having
to
deal with an RDBMS as well.

James

you could try something like this…very rough pseudo code, but the
logic should be there

def checkworkday(datechecked)
day = datechecked
while day == holiday && day == dayoff
day = nextday
end
return day;
end

dayneeded=firstdayofmonth
nomberofworkdays.times {dayneeded = checkworkday(dayneeded)}
xdayofworkthismonth = dayneeded

Oh, I see! So if you wanted to get the Date object corresponding to a
row
you’d just dump those values into Date.civil. Smart!

I’m curious about your occurrences table. I think I understand this to
be
one-time-only events. Is that correct?

James

On Tue, Jun 3, 2008 at 7:59 PM, James H. [email protected]
wrote:

Donald,
I’m definitely interested in hearing about the details. I’ll be having to
deal with an RDBMS as well.

Sure thing, I’ll describe briefly here and you can feel free to email
me if you have further questions. My data model included a table of
events, a table of occurrences for events on specific dates, and a
table of recurrences for events that recur. The recurrences table has
columns for event id, weekday, monthweek, and monthday; an event
occurring on all Mondays would have 1 for weekday and null for the
others, while an event occurring on the last Friday of a month would
have 5 for the weekday and -1 for the monthweek. I created a view
joining these data (events, occurrences, recurrences) against a table
of dates, excluding holidays, and end up with a virtual table
consisting of a row for every valid event-date pair.

Given that the date functions in SQL aren’t standardized, I’d thought
this would have been practically difficult to bundle up as a rails
plugin, but it now occurs to me that I could store the date parts
(e.g. weekday) in the table of dates, eliminating the need for any
date functions in the database. I may give that a whirl this weekend.

  • donald