Module: RiCal::CoreExtensions::Time::WeekDayPredicates
- Defined in:
- lib/ri_cal/core_extensions/time/week_day_predicates.rb
Overview
-
©2009 Rick DeNatale
-
All rights reserved. Refer to the file README.txt for the license
Provide predicate and related methods for use by the RiCal gem This module is included by Time, Date, and DateTime
Instance Method Summary collapse
-
#nth_wday_in_month(n, which_wday, for_time = self) ⇒ Object
e.g.
-
#nth_wday_in_month?(n, which_wday) ⇒ Boolean
A predicate to determine whether or not the receiver falls on a particular weekday of its month.
-
#start_of_week_with_wkst(wkst) ⇒ Object
Return a DateTime which is the beginning of the first day on or before the receiver with the specified wday.
Instance Method Details
#nth_wday_in_month(n, which_wday, for_time = self) ⇒ Object
e.g. to obtain the 3nd Tuesday of the receivers month use
time.nth_wday_in_month(2, 2)
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/ri_cal/core_extensions/time/week_day_predicates.rb', line 20 def nth_wday_in_month(n, which_wday, for_time = self) first_of_month = for_time.to_ri_cal_property_value.change(:day => 1) first_in_month = first_of_month.advance(:days => (which_wday - first_of_month.wday)) first_in_month = first_in_month.advance(:days => 7) if first_in_month.month != first_of_month.month if n > 0 first_in_month.advance(:days => (7*(n - 1))) else possible = first_in_month.advance(:days => 21) possible = possible.advance(:days => 7) while possible.month == first_in_month.month last_in_month = possible.advance(:days => - 7) (last_in_month.advance(:days => - (7*(n.abs - 1)))) end end |
#nth_wday_in_month?(n, which_wday) ⇒ Boolean
A predicate to determine whether or not the receiver falls on a particular weekday of its month.
Parameters
- n
-
the ordinal number being requested
- which_wday
-
the weekday using Ruby time conventions, i.e. 0 => Sunday, 1 => Monday, …
39 40 41 42 |
# File 'lib/ri_cal/core_extensions/time/week_day_predicates.rb', line 39 def nth_wday_in_month?(n, which_wday) target = nth_wday_in_month(n, which_wday) [self.year, self.month, self.day] == [target.year, target.month, target.day] end |
#start_of_week_with_wkst(wkst) ⇒ Object
Return a DateTime which is the beginning of the first day on or before the receiver with the specified wday
46 47 48 49 50 51 |
# File 'lib/ri_cal/core_extensions/time/week_day_predicates.rb', line 46 def start_of_week_with_wkst(wkst) wkst ||= 1 date = ::Date.civil(self.year, self.month, self.day) date -= 1 while date.wday != wkst date end |