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.
-
#nth_wday_in_year(n, which_wday, for_time = self) ⇒ Object
e.g.
-
#nth_wday_in_year?(n, which_wday) ⇒ Boolean
A predicate to determine whether or not the receiver falls on a particular weekday of its year.
-
#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)
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ri_cal/core_extensions/time/week_day_predicates.rb', line 53 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, …
72 73 74 75 |
# File 'lib/ri_cal/core_extensions/time/week_day_predicates.rb', line 72 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 |
#nth_wday_in_year(n, which_wday, for_time = self) ⇒ Object
e.g. to obtain the 2nd Monday of the receivers year use
time.nth_wday_in_year(2, 1)
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/ri_cal/core_extensions/time/week_day_predicates.rb', line 20 def nth_wday_in_year(n, which_wday, for_time = self) if n > 0 first_of_year = for_time.to_ri_cal_property_value.change(:month => 1, :day => 1) first_in_year = first_of_year.advance(:days => (which_wday - first_of_year.wday + 7) % 7) first_in_year.advance(:days => (7*(n - 1))) else december25 = for_time.to_ri_cal_property_value.change(:month => 12, :day => 25) last_in_year = december25.advance(:days => (which_wday - december25.wday + 7) % 7) last_in_year.advance(:days => (7 * (n + 1))) end end |
#nth_wday_in_year?(n, which_wday) ⇒ Boolean
A predicate to determine whether or not the receiver falls on a particular weekday of its year.
See #nth_wday_in_year
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_year?(n, which_wday) target = nth_wday_in_year(n, which_wday) [self.year, self.mon, self.day] == [target.year, target.mon, 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
79 80 81 82 83 84 |
# File 'lib/ri_cal/core_extensions/time/week_day_predicates.rb', line 79 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 |