module RichUnits::Times::Time
Time
Extensions¶ ↑
Public Instance Methods
Returns a new Time
representing the time a number of time-units ago.
# File lib/richunits/times.rb, line 67 def ago(number, units=:seconds) time =( case units.to_s.downcase.to_sym when :years set(:year => (year - number)) when :months #years = ((month - number) / 12).to_i y = ((month - number) / 12).to_i m = ((month - number - 1) % 12) + 1 set(:year => (year - y), :month => m) when :weeks self - (number * 604800) when :days self - (number * 86400) when :hours self - (number * 3600) when :minutes self - (number * 60) when :seconds, nil self - number else raise ArgumentError, "unrecognized time units -- #{units}" end ) dst_adjustment(time) end
Returns a new Time
representing the start of the day (0:00)
# File lib/richunits/times.rb, line 330 def beginning_of_day self - self.seconds_since_midnight end
Returns a new Time
representing the start of the month (1st of the month, 0:00)
# File lib/richunits/times.rb, line 340 def beginning_of_month #self - ((self.mday-1).days + self.seconds_since_midnight) change(:day => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0) end
Returns a new Time
representing the “start” of this week (Monday, 0:00)
# File lib/richunits/times.rb, line 309 def beginning_of_week (self - self.wday.days).midnight + 1.day end
Returns a new Time
representing the start of the year (1st of january, 0:00)
# File lib/richunits/times.rb, line 347 def beginning_of_year change(:month => 1,:day => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0) end
Returns a new Time
where one or more of the elements have been changed according to the options
parameter. The time options (hour, minute, sec, usec) reset cascadingly, so if only the hour is passed, then minute, sec, and usec is set to 0. If the hour and minute is passed, then sec and usec is set to 0.
t = Time.now #=> Sat Dec 01 14:10:15 -0500 2007 t.change(:hour => 11) #=> Sat Dec 01 11:00:00 -0500 2007
# File lib/richunits/times.rb, line 50 def change(options) opts=options; #{}; options.each_pair{ |k,v| opts[k] = v.to_i } self.class.send( self.utc? ? :utc : :local, opts[:year] || self.year, opts[:month] || self.month, opts[:day] || self.day, opts[:hour] || self.hour, opts[:min] || (opts[:hour] ? 0 : self.min), opts[:sec] || ((opts[:hour] || opts[:min]) ? 0 : self.sec), opts[:usec] || ((opts[:hour] || opts[:min] || opts[:sec]) ? 0 : self.usec) ) end
Returns a new Time
representing the time a number of days ago.
# File lib/richunits/times.rb, line 180 def days_ago(days) self - (days * 86400) end
Returns a new Time
representing the time a number of days hence.
# File lib/richunits/times.rb, line 239 def days_hence(days) self + (days * 86400) end
Adjust DST
TODO: Can’t seem to get this to pass ActiveSupport tests.
Even though it is essentially identical to the ActiveSupport code (see Time#since in time/calculations.rb). It handels all but 4 tests.
# File lib/richunits/times.rb, line 138 def dst_adjustment(time) self_dst = self.dst? ? 1 : 0 time_dst = time.dst? ? 1 : 0 seconds = (self - time).abs if (seconds >= 86400 && self_dst != time_dst) time + ((self_dst - time_dst) * 60 * 60) else time end end
Returns a new time at end of day
# File lib/richunits/times.rb, line 371 def end_of_day Time.mktime(year, month, day, 23, 59, 59, 999) end
Returns a new Time
representing the time a number of time-units hence.
# File lib/richunits/times.rb, line 98 def hence(number, units=:seconds) time =( case units.to_s.downcase.to_sym when :years set( :year=>(year + number) ) when :months y = ((month + number) / 12).to_i m = ((month + number - 1) % 12) + 1 set(:year => (year + y), :month => m) when :weeks self + (number * 604800) when :days self + (number * 86400) when :hours self + (number * 3600) when :minutes self + (number * 60) when :seconds self + number else raise ArgumentError, "unrecognized time units -- #{units}" end ) dst_adjustment(time) end
Returns a new Time
representing the time a number of hours ago.
# File lib/richunits/times.rb, line 173 def hours_ago(hours) self - (hours * 3600) end
Returns a new Time
representing the time a number of hours hence.
# File lib/richunits/times.rb, line 232 def hours_hence(hours) self + (hours * 3600) end
Returns true only if day of time is included in the range (stime..etime). Only year days are checked.
# File lib/richunits/times.rb, line 378 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
Short-hand for months_ago
(1)
# File lib/richunits/times.rb, line 299 def last_month months_ago(1) end
Short-hand for years_ago
(1)
# File lib/richunits/times.rb, line 289 def last_year years_ago(1) end
Returns a new Time
representing the time a number of minutes ago.
# File lib/richunits/times.rb, line 166 def minutes_ago(minutes) self - (minutes * 60) end
Returns a new Time
representing the time a number of minutes hence.
# File lib/richunits/times.rb, line 225 def minutes_hence(minutes) self + (minutes * 60) end
Returns a new Time
representing the time a number of months ago.
# File lib/richunits/times.rb, line 194 def months_ago(months) years = (month - months / 12).to_i set(:year=>(year - years), :month=>(month - months) % 12) end
Returns a new Time
representing the time a number of months hence.
# File lib/richunits/times.rb, line 253 def months_hence(months) years = (month + months / 12).to_i set(:year=>(year + years), :month=>(month + months) % 12) end
Short-hand for months_since
(1)
# File lib/richunits/times.rb, line 304 def next_month months_since(1) end
Returns a new Time
representing the start of the given day in next week (default is Monday).
# File lib/richunits/times.rb, line 317 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
Short-hand for years_since
(1)
# File lib/richunits/times.rb, line 294 def next_year years_since(1) end
Seconds since midnight: Time.now.seconds_since_midnight
# File lib/richunits/times.rb, line 151 def seconds_since_midnight self.hour.hours + self.min.minutes + self.sec + (self.usec/1.0e+6) end
Like change but does not reset earlier times.
NOTE: It would be better, probably if this were called “change”.
and that #change were called "reset".
# File lib/richunits/times.rb, line 27 def set(options) opts={}; options.each_pair{ |k,v| opts[k] = v.to_i } self.class.send( self.utc? ? :utc : :local, opts[:year] || self.year, opts[:month] || self.month, opts[:day] || self.day, opts[:hour] || self.hour, opts[:min] || self.min, opts[:sec] || self.sec, opts[:usec] || self.usec ) end
Convenience method which returns a new Time
representing the time 1 day since the instance time
# File lib/richunits/times.rb, line 360 def tomorrow self.since(1.day) end
Returns a new Time
representing the time a number of weeks ago.
# File lib/richunits/times.rb, line 187 def weeks_ago(weeks) self - (weeks * 604800) end
Returns a new Time
representing the time a number of weeks hence.
# File lib/richunits/times.rb, line 246 def weeks_hence(weeks) self + (weeks * 604800) end
Returns a new Time
representing the time a number of years ago.
# File lib/richunits/times.rb, line 212 def years_ago(years) set(:year=>(year - years)) end
Returns a new Time
representing the time a number of years hence.
# File lib/richunits/times.rb, line 269 def years_hence(years) set(:year=>(year + years)) end
Convenience method which returns a new Time
representing the time 1 day ago
# File lib/richunits/times.rb, line 354 def yesterday self.ago(1.day) end