Class: Time
- Inherits:
-
Object
- Object
- Time
- Defined in:
- lib/beginning_of_fortnight.rb
Overview
These methods should work similar to beginning_of_week and friends.
In all these methods, if the optional argument reference_date
is not given then the default reference date is used. See BeginningOfFortnight#reference_date.
reference_date can be a Date or a Time object.
– ActiveRecord 2.x puts these methods in a big long name space, ie ActiveSupport::CoreExtensions::Time::Calculations ActiveSupport 3.x just adds methods directly to Time. Will try the 3.x approach and hopefully it will work okay in 2.x also. ++
Instance Method Summary collapse
-
#beginning_of_fortnight(reference_date = nil) ⇒ Object
The beginning of the fortnight.
-
#end_of_fortnight(reference_date = nil) ⇒ Object
The end of the fortnight.
-
#next_fortnight(reference_date = nil) ⇒ Object
The start of the fortnight after this one.
Instance Method Details
#beginning_of_fortnight(reference_date = nil) ⇒ Object
The beginning of the fortnight.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/beginning_of_fortnight.rb', line 124 def beginning_of_fortnight(reference_date=nil) # Can pass in a reference date, otherwise use the configured default reference_date ||= BeginningOfFortnight.reference_date # The to_time is in case reference_date is passed in as a Date object reference_week = reference_date.to_time.beginning_of_week # How many weeks since reference week? weeks_since_reference = ((self - reference_week) / 1.week).to_i # # If the reference time is later than self, ie in the future, # then we flip the odd/even test here. # # Some explanation: # # In this diagram, '|' is a fortnight boundary, '+' is a week boundary and R is the reference week. # R # | a + b | c + d | # # The value of weeks_since_reference for dates at a, b, c and d is as follows: # When self is earlier than reference_date: # a (in first half) : -1, hence odd weeks_since_reference is in first half # b (in second half): 0, hence even weeks_since_reference is in second half (because -0.5.to_i is 0 for example) # When self is later than reference_date: # c (in first half) : 0, hence even weeks_since_reference is in first half # d (in second half): 1, hence odd weeks_since_reference is in second half # in_first_half = (reference_week > self ? weeks_since_reference.odd? : weeks_since_reference.even?) # The value of in_first_half decides which week to use (in_first_half ? self : self - 1.week).beginning_of_week end |
#end_of_fortnight(reference_date = nil) ⇒ Object
The end of the fortnight.
161 162 163 |
# File 'lib/beginning_of_fortnight.rb', line 161 def end_of_fortnight(reference_date=nil) (beginning_of_fortnight(reference_date) + 1.week).end_of_week end |
#next_fortnight(reference_date = nil) ⇒ Object
The start of the fortnight after this one.
168 169 170 |
# File 'lib/beginning_of_fortnight.rb', line 168 def next_fortnight(reference_date=nil) beginning_of_fortnight(reference_date) + 2.weeks end |