Section 7.6. Converting Between Seconds and Larger Units


7.5. Finding the Nth Weekday in a Month

Sometimes for a given month and year, we want to find the date of the third Monday in the month, or the second Tuesday, and so on. The code in Listing 7.1 makes that calculation simple.

If we are looking for the nth occurrence of a certain weekday, we pass n as the first parameter. The second parameter is the number of that weekday (0 meaning Sunday, 1 meaning Monday, and so on). The third and fourth parameters are the month and year, respectively.

Listing 7.1. Finding the Nth Weekday

def nth_wday(n, wday, month, year)   if (!n.between? 1,5) or      (!wday.between? 0,6) or      (!month.between? 1,12)     raise ArgumentError   end   t = Time.local year, month, 1   first = t.wday   if first == wday     fwd = 1   elsif first < wday     fwd = wday - first + 1   elsif first > wday     fwd = (wday+7) - first + 1   end   target = fwd + (n-1)*7   begin     t2 = Time.local year, month, target   rescue ArgumentError     return nil   end   if t2.mday == target     t2   else     nil   end end

The peculiar-looking code near the end of the method is put there to counteract a long-standing tradition in the underlying time-handling routines. You might expect that trying to create a date of November 31 would result in an error of some kind. You would be mistaken. Most systems would happily (and silently) convert this to December 1. If you are an old-time UNIX hacker, you may think this is a feature; otherwise, you may consider it a bug.

We will not venture an opinion here as to what the underlying library code ought to do or whether Ruby ought to change that behavior. But we don't want to have this routine perpetuate the tradition. If you are looking for the date of, say, the fifth Friday in November 2000, you will get a nil value back (rather than December 1, 2000).




The Ruby Way(c) Solutions and Techniques in Ruby Programming
The Ruby Way, Second Edition: Solutions and Techniques in Ruby Programming (2nd Edition)
ISBN: 0672328844
EAN: 2147483647
Year: 2004
Pages: 269
Authors: Hal Fulton

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net