# Commercial Calendar Calculations

**URL:** <https://www.ruby-forum.com/t/commercial-calendar-calculations/139431>\
**Category:** Ruby\
**Created:** [June 3, 2008, 9:47pm UTC](https://www.ruby-forum.com/t/commercial-calendar-calculations/139431 "2008-06-03T21:47:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [June 3, 2008, 9:47pm UTC](https://www.ruby-forum.com/t/commercial-calendar-calculations/139431/1 "2008-06-03T21:47:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [June 3, 2008, 10:37pm UTC](https://www.ruby-forum.com/t/commercial-calendar-calculations/139431/2 "2008-06-03T22:37:31Z")

</div>

On Tue, Jun 3, 2008 at 2:43 PM, James H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [June 4, 2008, 12:40am UTC](https://www.ruby-forum.com/t/commercial-calendar-calculations/139431/3 "2008-06-04T00:40:45Z")

</div>

On Jun 3, 3:43 pm, James H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://rubyforge.org)?

Eric

====

[LearnRuby.com](http://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](http://LearnRuby.com) for all the details.

---

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [June 4, 2008, 3:05am UTC](https://www.ruby-forum.com/t/commercial-calendar-calculations/139431/4 "2008-06-04T03:05:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [June 4, 2008, 3:00am UTC](https://www.ruby-forum.com/t/commercial-calendar-calculations/139431/5 "2008-06-04T03:00:42Z")

</div>

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

James

---

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [June 4, 2008, 6:07am UTC](https://www.ruby-forum.com/t/commercial-calendar-calculations/139431/6 "2008-06-04T06:07:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [June 4, 2008, 6:33pm UTC](https://www.ruby-forum.com/t/commercial-calendar-calculations/139431/7 "2008-06-04T18:33:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [June 4, 2008, 5:08pm UTC](https://www.ruby-forum.com/t/commercial-calendar-calculations/139431/8 "2008-06-04T17:08:46Z")

</div>

On Tue, Jun 3, 2008 at 7:59 PM, James H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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
