<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -1,30 +1,28 @@
 module Recurring
 
-  VERSION = '0.5.4'
+  VERSION = '0.5.5'
   
   class &lt;&lt; self
-    # returns a number starting with 1.  Needs to assume that weeks start on sunday
-    # for beginning_of_next to work at the scale of weeks.
-    def week_in_month date
-      # Work out the first date in the month
-      first_of_month = date - ((date.day - 1) * 86400)
-      
-      # If the month starts on a Sunday, we're good.
-      if first_of_month.wday == 0
-        adjusted_day = date.day
-      else
-        # Otherwise, we need to offset by whatever partial week starts this month.
-        adjusted_day = date.day + first_of_month.wday
-      end
-      (((adjusted_day - 1).to_f / 7.0) + 1).floor 
+    
+    # For a given date, returns the cardinality of the day of the week for the specific month.
+    # e.g. for September 2009:
+    #   nth_instance_of_day_in_month(Time.utc(2006,9,7)) #=&gt; 1
+    #   nth_instance_of_day_in_month(Time.utc(2006,9,8)) #=&gt; 2
+    #
+    def nth_instance_of_day_in_month(date)
+      return ((date.day - 1) / 7) + 1
     end
 
-    def negative_week_in_month date
-    	end_of_month = (date.month &lt; 12 ? Time.utc(date.year, date.month+1) : Time.utc(date.year + 1)) - 3600
-      
-      (((end_of_month.day - date.day).to_f / 7.0) + 1).floor * -1
+    # The same as above, but work backward from the end of the month.
+    def nth_negative_instance_of_day_in_month(date)
+      # How many days are there in this month? We'll grab the first of next month, then roll
+      # back a day to see.
+      next_month = Time.utc(date.year, (date.month % 12) + 1)
+      next_month = Time.utc(next_month.year + 1, next_month.month) if next_month &lt; date
+      month_days = (next_month - 86400).day
+      return -1 - ((month_days - date.day) / 7)
     end
-
+    
     # just a wrapper for strftime
     def week_of_year date
     	date.strftime('%U').to_i
@@ -313,13 +311,21 @@ module Recurring
 	      true
       end
 
+      # This method is a bit of a problem.  Most of the time, when people say 'Every month on the 
+      # first Sunday', they really mean every month; not every month in which the first week of the 
+      # month contains a Sunday (which is what this method was actually doing).
+      # 
+      # I've changed this to match what I think a user's expectation would be, which is 'the nth
+      # Sunday of the month, regardless of in what week that falls'.  Note that this only affects 
+      # The second half of the method; the first half is for handling recurrences of the form 'every
+      # n weeks on Sunday'.  --g
       def week_matches? date
 	      if @unit == :weeks 
       	  return true if @frequency == 1
       	  return ((Recurring.week_of_year(date) - Recurring.week_of_year(@anchor)) % @frequency) == 0
       	end
       	if @weeks
-      	  @weeks.include?(Recurring.week_in_month(date)) || @weeks.include?(Recurring.negative_week_in_month(date))
+      	  @weeks.include?(Recurring.nth_instance_of_day_in_month(date)) || @weeks.include?(Recurring.nth_negative_instance_of_day_in_month(date))
       	else
       	  true
       	end</diff>
      <filename>lib/schedule.rb</filename>
    </modified>
    <modified>
      <diff>@@ -2,7 +2,7 @@
 
 Gem::Specification.new do |s|
   s.name = %q{recurring}
-  s.version = &quot;0.5.4&quot;
+  s.version = &quot;0.5.5&quot;
 
   s.required_rubygems_version = Gem::Requirement.new(&quot;&gt;= 0&quot;) if s.respond_to? :required_rubygems_version=
   s.authors = [&quot;Chris Anderson&quot;]</diff>
      <filename>recurring.gemspec</filename>
    </modified>
    <modified>
      <diff>@@ -1,10 +1,7 @@
-require 'rubygems'
-gem 'rspec'
 require File.dirname(__FILE__) + &quot;/../lib/date_language&quot;
-#require 'yaml'
 
 context &quot;Every two days from 2006/11/3&quot; do
-  setup do
+  before do
     @rdl = Recurring::DateLanguage.tell do
       every 2, :days, :anchor =&gt; Time.utc(2006,11,3)
       times '4:45am 3pm'</diff>
      <filename>spec/date_language_spec.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,5 +1,3 @@
-require 'rubygems'
-gem 'rspec'
 require File.dirname(__FILE__) + &quot;/../lib/recurring&quot;
 require 'yaml'
 
@@ -102,7 +100,7 @@ describe &quot;Initializing a Schedule&quot; do
 end
 
 describe &quot;A complex Schedule&quot; do
-  setup do
+  before do
     @rs = Recurring::Schedule.new :unit =&gt; 'months', :frequency =&gt; 2, :anchor =&gt; Time.utc(2006,4,15,10,30), :monthdays =&gt; [3,7], :times =&gt; '5pm 4:45:12am'
   end
   
@@ -131,7 +129,7 @@ end
 
 #YEARS
 describe &quot;A basic yearly schedule&quot; do
-  setup do
+  before do
     @rs = Recurring::Schedule.new :unit =&gt; 'years', :frequency =&gt; 1, :anchor =&gt; Time.utc(2008,3,6)
   end
 
@@ -150,7 +148,7 @@ describe &quot;A basic yearly schedule&quot; do
 end
 
 describe &quot;A basic bi-yearly schedule&quot; do
-  setup do
+  before do
     @rs = Recurring::Schedule.new :unit =&gt; 'years', :frequency =&gt; 2, :anchor =&gt; Time.utc(2008,3,6)
   end
 
@@ -161,7 +159,7 @@ describe &quot;A basic bi-yearly schedule&quot; do
 end
 
 describe &quot;A yearly schedule with month and monthdays&quot; do
-  setup do
+  before do
     @rs = Recurring::Schedule.new :unit =&gt; 'years', :months =&gt; 'feb', :monthdays =&gt; [4]
   end
   
@@ -180,7 +178,7 @@ describe &quot;A yearly schedule with month and monthdays&quot; do
 end
 
 describe &quot;A yearly schedule with months and weekdays&quot; do
-  setup do
+  before do
     @rs = Recurring::Schedule.new :unit =&gt; 'years', :months =&gt; 'feb', :weekdays =&gt; ['tuesday', 'weds']
   end
   it &quot;should match every tuesday and wednesday in febraury&quot; do
@@ -207,7 +205,7 @@ describe &quot;A yearly schedule with months and weekdays&quot; do
 end
 
 describe &quot;A bi-yearly schedule with months, weeks, weekdays, and times&quot; do
-  setup do
+  before do
     @rs = Recurring::Schedule.new :unit =&gt; 'years', :frequency =&gt; 2, :anchor =&gt; Time.utc(2006,6), :months =&gt; ['feb', 'may'], :weeks =&gt; [2], :weekdays =&gt; 'fri', :times =&gt; '5:15'
   end
   
@@ -228,7 +226,7 @@ end
 
 describe &quot;A bi-monthly Schedule without more params&quot; do
 
-  setup do
+  before do
     @rs = Recurring::Schedule.new :unit =&gt; 'months', :frequency =&gt; 2, :anchor =&gt; Time.utc(2006,4,15,10,30)
   end
 
@@ -266,7 +264,7 @@ describe &quot;A bi-monthly Schedule without more params&quot; do
 end
 
 describe &quot;A monthly schedule with monthday params&quot; do
-  setup do
+  before do
     @rs = Recurring::Schedule.new :unit =&gt; 'months', :monthdays =&gt; [1,15]
     #    @rs.anchor = Time.utc(2006,6,18,12,30) #should react appropriately to the presence of a (no longer useless) anchor
   end
@@ -301,7 +299,7 @@ end
 
 describe &quot;A bi-monthly Schedule with monthday params&quot; do
 
-  setup do
+  before do
     @rs = Recurring::Schedule.new :unit =&gt; 'months', :frequency =&gt; 2, :anchor =&gt; Time.utc(2006,4,15,10,30), :monthdays =&gt; [10,18]
   end
 
@@ -337,7 +335,7 @@ end
 
 describe &quot;A third-monthly schedule with week params&quot; do
   
-    setup do
+    before do
       lambda{@rs = Recurring::Schedule.new :unit =&gt; 'months', :week =&gt; 1, :frequency =&gt; 3}.should raise_error(ArgumentError)
     end
 
@@ -349,26 +347,25 @@ end
 
 describe &quot;A monthly schedule with week params and weekday params&quot; do
 
-  setup do
-    @rs = Recurring::Schedule.new :unit =&gt; 'months', :weeks =&gt; 1, :weekdays =&gt; :saturday
+  before do
+    @rs = Recurring::Schedule.new :unit =&gt; 'months', :weeks =&gt; 1, :weekdays =&gt; :sunday
   end
 
   it &quot;should include the beginnings of matching days&quot; do
-    @rs.should include(Time.utc(2006,12,2))
+    @rs.should include(Time.utc(2006,12,3))
   end
 
   it &quot;should not include the matching days in off weeks&quot; do
-    @rs.should_not include(Time.utc(2006,12,23))
-    @rs.should_not include(Time.utc(2006,12,9))
+    @rs.should_not include(Time.utc(2006,12,24))
+    @rs.should_not include(Time.utc(2006,12,10))
   end
 
   it &quot;should have a next occurrence date no more than 38 days in the future&quot; do
     # August 4, 2009 is a Saturday. So is September 5.  Calling next()
     # in 0.5.2 would return Nov. 7, 2009 as the next occurrence.
-    @rs.find_next(Time.utc(2009,8,2)).should == Time.utc(2009,9,5)
+    puts @rs.find_next(Time.utc(2009,8,3))
+    @rs.find_next(Time.utc(2009,8,3)).should == Time.utc(2009,9,6)
   end
-
-
 end
 
 describe &quot;A monthly schedule with weekday params but no week params&quot; do
@@ -379,9 +376,8 @@ end
 
 describe &quot;A monthly schedule with negative week params and weekday params&quot; do
 
- setup do
-   @rs = Recurring::Schedule.new :unit =&gt; 'months', :weeks =&gt; -1,
-:weekdays =&gt; :monday
+ before do
+   @rs = Recurring::Schedule.new(:unit =&gt; 'months', :weeks =&gt; -1, :weekdays =&gt; :monday)
  end
   it &quot;should include the beginnings of matching days&quot; do
    @rs.should include(Time.utc(2006,12,25))
@@ -398,7 +394,7 @@ end
 #WEEKLY
 
 describe &quot;A bi-weekly schedule with weekdays and a midnight time&quot; do
-  setup do
+  before do
     @rs = Recurring::Schedule.new :unit =&gt; 'weeks', :weekdays =&gt; %w{sat sunday}, :times =&gt; '12am', :frequency =&gt; 2, :anchor =&gt; Time.utc(2001)
     @danger = Time.utc(2006,12,17,18)
   end
@@ -420,7 +416,7 @@ describe &quot;A bi-weekly schedule with weekdays and a midnight time&quot; do
 end
 
 describe &quot;A bi-weekly schedule with weekdays and a noon time&quot; do
-  setup do
+  before do
     @rs = Recurring::Schedule.new :unit =&gt; 'weeks', :weekdays =&gt; %w{sat sunday}, :times =&gt; '12pm', :frequency =&gt; 2, :anchor =&gt; Time.utc(2001)
     @danger = Time.utc(2006,12,17,18)
   end
@@ -442,7 +438,7 @@ end
 
 describe &quot;A weekly schedule with weekday params&quot; do
   
-  setup do
+  before do
     @rs = Recurring::Schedule.new :unit =&gt; 'weeks', :weekdays =&gt; %w{sunday monday weds friday}
   end
 
@@ -466,7 +462,7 @@ describe &quot;A weekly schedule with weekday params&quot; do
 end
 
 describe &quot;A Schedule with uppercase weekdays&quot; do
-  setup do
+  before do
     @rs = Recurring::Schedule.new :unit =&gt; 'weeks', :weekdays =&gt; %w{Friday}, :anchor =&gt; Time.now, :times =&gt; '12'
   end
 
@@ -477,7 +473,7 @@ end
 
 describe &quot;A weekly schedule with only an anchor&quot; do
   
-  setup do
+  before do
     @rs = Recurring::Schedule.new :unit =&gt; 'weeks', :anchor =&gt; Time.utc(2006,12,6,17,30)
   end
 
@@ -503,7 +499,7 @@ end
 
 describe &quot;A third-weekly schedule with weekday and times params&quot; do
 
-  setup do
+  before do
     @rs = Recurring::Schedule.new :unit =&gt; 'weeks', :frequency =&gt; 3, :anchor =&gt; Time.utc(2006,12,1), :weekdays =&gt; %w{mon fri sat}, :times =&gt; '3pm' 
   end
 
@@ -569,7 +565,7 @@ end
 
 describe &quot;A daily schedule with no other params&quot; do
 
-  setup do
+  before do
     @rs = Recurring::Schedule.new :unit =&gt; 'days'
   end
 
@@ -591,7 +587,7 @@ describe &quot;A daily schedule with no other params&quot; do
 end
 
 describe &quot;A daily schedule with an anchor&quot; do
-  setup do
+  before do
     @rs = Recurring::Schedule.new :unit =&gt; 'days', :anchor =&gt; Time.utc(2006,11,1,10,15,22)
   end
 
@@ -617,7 +613,7 @@ describe &quot;A fourth-daily schedule with only the unit and frequency != 1&quot; do
 end
 
 describe &quot;A daily schedule with a sub-second precise anchor&quot; do
-  setup do
+  before do
     @rs = Recurring::Schedule.new :unit =&gt; 'days', :anchor =&gt; Time.utc(2006,11,27,0,0,30,45)
   end
   it &quot;should include times equal up to second accuracy&quot; do
@@ -631,7 +627,7 @@ end
 
 describe &quot;A fourth-daily schedule with no other params&quot; do
 
-  setup do
+  before do
     @rs = Recurring::Schedule.new :unit =&gt; 'days', :frequency =&gt; 4, :anchor =&gt; Time.utc(2006,11,1,9,30)
   end
 
@@ -670,7 +666,7 @@ end
 
 describe &quot;An hourly schedule with no other params&quot; do
 
-  setup do
+  before do
     @rs = Recurring::Schedule.new :unit =&gt; 'hours'
   end
 
@@ -686,7 +682,7 @@ describe &quot;An hourly schedule with no other params&quot; do
 end
 
 describe &quot;An hourly schedule with an anchor&quot; do
-  setup do
+  before do
     @rs = Recurring::Schedule.new :unit =&gt; 'hours', :anchor =&gt; Time.utc(2001,5,15,11,17) 
   end
 
@@ -708,7 +704,7 @@ end
 
 describe &quot;An bi-hourly schedule with time params&quot; do
   
-  setup do
+  before do
     @rs = Recurring::Schedule.new :unit =&gt; 'hours', :frequency =&gt; 2, :times =&gt; '0:22 0:13:14', :anchor =&gt; Time.utc(2006)
   end
   
@@ -735,7 +731,7 @@ end
 
 describe &quot;An hourly schedule with times params&quot; do
 
-  setup do
+  before do
     @rs = Recurring::Schedule.new :unit =&gt; 'hours', :times =&gt; '0:15 4:30 0:45:30'
   end
 
@@ -776,7 +772,7 @@ end
 #MINUTELY
 
 describe &quot;Every 45 minutes from an anchor&quot; do
-  setup do
+  before do
     @rs = Recurring::Schedule.new :unit =&gt; 'minutes', :frequency =&gt; 45, :anchor =&gt; Time.utc(2006, 12, 1, 10, 30)
   end
   it &quot;should include 45 minute multiples of the anchor time&quot; do
@@ -787,7 +783,7 @@ describe &quot;Every 45 minutes from an anchor&quot; do
 end
 
 describe &quot;A 30 minutely schedule&quot; do
-  setup do
+  before do
     @rs = Recurring::Schedule.new :unit =&gt; 'minutes', :frequency =&gt; 30, :anchor =&gt; Time.utc(2006,1,1,1,15)
   end
   it &quot;should match the very next time after the start&quot; do
@@ -797,7 +793,7 @@ end
 
 describe &quot;A 5-minutely schedule with no other params&quot; do
   
-  setup do
+  before do
     @rs = Recurring::Schedule.new :unit =&gt; 'minutes', :frequency =&gt; 5, :anchor =&gt; Time.utc(2006,9,1,10,30)
   end
 
@@ -858,7 +854,7 @@ end
 #ETC
 
 describe &quot;a daily schedule with a time 4:29am&quot; do
-  setup do
+  before do
     @rs = Recurring::Schedule.new :unit =&gt; 'days', :times =&gt; '4:29am'
   end
   it &quot;should include any day at the time specified&quot; do</diff>
      <filename>spec/schedule_spec.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>570d88a6d8d4c349a3bc7370b38e082ec90468ec</id>
    </parent>
  </parents>
  <author>
    <name>Grant Goodale</name>
    <email>ggoodale@gmail.com</email>
  </author>
  <url>http://github.com/ggoodale/recurring/commit/8bfc68e753116128311cca3346c2205c20dead7b</url>
  <id>8bfc68e753116128311cca3346c2205c20dead7b</id>
  <committed-date>2009-11-05T23:32:09-08:00</committed-date>
  <authored-date>2009-11-05T23:32:09-08:00</authored-date>
  <message>Fixed week behavior to behave in a more user-friendly way, so 'every month on the first Sunday' works as expected rather than ignoring months where the first week of the month doesn't contain a Sunday</message>
  <tree>9ee4c8e34152776d7a06c1f482fb6e36f7bee301</tree>
  <committer>
    <name>Grant Goodale</name>
    <email>ggoodale@gmail.com</email>
  </committer>
</commit>
