Module: Time::TimeInEnglish
- Included in:
- Time
- Defined in:
- lib/mega/time_in_english.rb
Overview
Plain-English convenience methods module designed for the Time class.
Constant Summary collapse
Instance Method Summary collapse
-
#ago(seconds) ⇒ Object
Returns a new Time representing the time a number of seconds ago.
-
#beginning_of_day ⇒ Object
(also: #midnight, #at_midnight, #at_beginning_of_day, #start_of_day)
Returns a new Time representing the start of the day (0:00).
-
#beginning_of_month ⇒ Object
(also: #at_beginning_of_month)
Returns a new Time representing the start of the month (1st of the month, 0:00).
-
#beginning_of_week ⇒ Object
(also: #monday, #at_beginning_of_week)
Returns a new Time representing the “start” of this week (Monday, 0:00).
-
#beginning_of_year ⇒ Object
(also: #at_beginning_of_year)
Returns a new Time representing the start of the year (1st of january, 0:00).
-
#in_day_range?(stime = ZERO, etime = NEVER) ⇒ Boolean
Returns true only if day of time is included in the range (stime..etime).
-
#last_month ⇒ Object
Short-hand for months_ago(1).
-
#last_year ⇒ Object
Short-hand for months_ago(1).
-
#months_ago(months) ⇒ Object
Returns a new Time representing the time a number of specified months ago.
- #months_since(months) ⇒ Object
-
#next_month ⇒ Object
Short-hand for months_since(1).
-
#next_week(day = :monday) ⇒ Object
Returns a new Time representing the start of the given day in next week (default is Monday).
-
#next_year ⇒ Object
Short-hand for months_since(1).
-
#seconds_since_midnight ⇒ Object
Seconds since midnight: Time.now.seconds_since_midnight.
-
#since(seconds) ⇒ Object
(also: #in)
Returns a new Time representing the time a number of seconds since the instance time.
-
#to_end_of_day ⇒ Object
Rrturns a new time at end of day.
-
#to_start_of_day ⇒ Object
Returns a new time at start of day.
-
#tomorrow ⇒ Object
Convenience method which returns a new Time representing the time 1 day since the instance time.
-
#years_ago(years) ⇒ Object
Returns a new Time representing the time a number of specified years ago.
- #years_since(years) ⇒ Object
-
#yesterday ⇒ Object
Convenience method which returns a new Time representing the time 1 day ago.
Instance Method Details
#ago(seconds) ⇒ Object
Returns a new Time representing the time a number of seconds ago. Do not use this method in combination with x.months, use months_ago instead!
118 119 120 121 122 |
# File 'lib/mega/time_in_english.rb', line 118 def ago(seconds) # This is basically a wrapper around the Numeric extension. #seconds.until(self) self - seconds end |
#beginning_of_day ⇒ Object Also known as: midnight, at_midnight, at_beginning_of_day, start_of_day
Returns a new Time representing the start of the day (0:00)
204 205 206 |
# File 'lib/mega/time_in_english.rb', line 204 def beginning_of_day self - self.seconds_since_midnight end |
#beginning_of_month ⇒ Object Also known as: at_beginning_of_month
Returns a new Time representing the start of the month (1st of the month, 0:00)
214 215 216 217 |
# File 'lib/mega/time_in_english.rb', line 214 def beginning_of_month #self - ((self.mday-1).days + self.seconds_since_midnight) change(:mday => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0) end |
#beginning_of_week ⇒ Object Also known as: monday, at_beginning_of_week
Returns a new Time representing the “start” of this week (Monday, 0:00)
183 184 185 |
# File 'lib/mega/time_in_english.rb', line 183 def beginning_of_week (self - self.wday.days).midnight + 1.day end |
#beginning_of_year ⇒ Object Also known as: at_beginning_of_year
Returns a new Time representing the start of the year (1st of january, 0:00)
221 222 223 |
# File 'lib/mega/time_in_english.rb', line 221 def beginning_of_year change(:month => 1,:mday => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0) end |
#in_day_range?(stime = ZERO, etime = NEVER) ⇒ Boolean
Returns true only if day of time is included in the range (stime..etime). Only year days are checked.
250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/mega/time_in_english.rb', line 250 def in_day_range?(stime=ZERO, etime=NEVER) if (etime <= stime) warn "invalid end time (#{etime} < #{stime})" if $DEBUG etime = NEVER end stime = stime.to_start_of_day etime = etime.to_end_of_day return (stime..etime).include?(time) end |
#last_month ⇒ Object
Short-hand for months_ago(1)
173 174 175 |
# File 'lib/mega/time_in_english.rb', line 173 def last_month months_ago(1) end |
#last_year ⇒ Object
Short-hand for months_ago(1)
163 164 165 |
# File 'lib/mega/time_in_english.rb', line 163 def last_year years_since(1) end |
#months_ago(months) ⇒ Object
Returns a new Time representing the time a number of specified months ago.
136 137 138 139 140 141 142 |
# File 'lib/mega/time_in_english.rb', line 136 def months_ago(months) if months >= self.month change(:year => self.year - 1, :month => 12).months_ago(months - self.month) else change(:year => self.year, :month => self.month - months) end end |
#months_since(months) ⇒ Object
144 145 146 147 148 149 150 |
# File 'lib/mega/time_in_english.rb', line 144 def months_since(months) if months + self.month > 12 change(:year => self.year + 1, :month => 1).months_since(months - (self.month == 1 ? 12 : (self.month + 1))) else change(:year => self.year, :month => self.month + months) end end |
#next_month ⇒ Object
Short-hand for months_since(1)
178 179 180 |
# File 'lib/mega/time_in_english.rb', line 178 def next_month months_since(1) end |
#next_week(day = :monday) ⇒ Object
Returns a new Time representing the start of the given day in next week (default is Monday).
191 192 193 194 195 196 |
# File 'lib/mega/time_in_english.rb', line 191 def next_week(day = :monday) days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6 } since(1.week).beginning_of_week.since(days_into_week[day].day).change(:hour => 0) end |
#next_year ⇒ Object
Short-hand for months_since(1)
168 169 170 |
# File 'lib/mega/time_in_english.rb', line 168 def next_year years_since(1) end |
#seconds_since_midnight ⇒ Object
Seconds since midnight: Time.now.seconds_since_midnight
112 113 114 |
# File 'lib/mega/time_in_english.rb', line 112 def seconds_since_midnight self.hour.hours + self.min.minutes + self.sec + (self.usec/1.0e+6) end |
#since(seconds) ⇒ Object Also known as: in
Returns a new Time representing the time a number of seconds since the instance time. Do not use this method in combination with x.months, use months_since instead!
127 128 129 130 131 |
# File 'lib/mega/time_in_english.rb', line 127 def since(seconds) # This is basically a wrapper around the Numeric extension. #seconds.since(self) self + seconds end |
#to_end_of_day ⇒ Object
Rrturns a new time at end of day
244 245 246 |
# File 'lib/mega/time_in_english.rb', line 244 def to_end_of_day return Time.mktime(year, month, day, 23, 59, 59, 999) end |
#to_start_of_day ⇒ Object
Returns a new time at start of day
239 240 241 |
# File 'lib/mega/time_in_english.rb', line 239 def to_start_of_day return Time.mktime(year, month, day, 0, 0, 0, 0) end |
#tomorrow ⇒ Object
Convenience method which returns a new Time representing the time 1 day since the instance time
234 235 236 |
# File 'lib/mega/time_in_english.rb', line 234 def tomorrow self.since(1.day) end |
#years_ago(years) ⇒ Object
Returns a new Time representing the time a number of specified years ago.
154 155 156 |
# File 'lib/mega/time_in_english.rb', line 154 def years_ago(years) change(:year => self.year - years) end |
#years_since(years) ⇒ Object
158 159 160 |
# File 'lib/mega/time_in_english.rb', line 158 def years_since(years) change(:year => self.year + years) end |
#yesterday ⇒ Object
Convenience method which returns a new Time representing the time 1 day ago
228 229 230 |
# File 'lib/mega/time_in_english.rb', line 228 def yesterday self.ago(1.day) end |