module RichUnits::Times::Time

Time Extensions

Public Instance Methods

ago(number, units=:seconds) click to toggle source

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
at_beginning_of_day()
Alias for: beginning_of_day
at_beginning_of_month()
Alias for: beginning_of_month
at_beginning_of_week()
Alias for: beginning_of_week
at_beginning_of_year()
Alias for: beginning_of_year
at_midnight()
Alias for: beginning_of_day
beginning_of_day() click to toggle source

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
beginning_of_month() click to toggle source

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
Also aliased as: at_beginning_of_month
beginning_of_week() click to toggle source

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
Also aliased as: monday, at_beginning_of_week
beginning_of_year() click to toggle source

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
Also aliased as: at_beginning_of_year
change(options) click to toggle source

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
days_ago(days) click to toggle source

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
days_hence(days) click to toggle source

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
Also aliased as: days_since
days_since(days)
Alias for: days_hence
dst_adjustment(time) click to toggle source

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
end_of_day() click to toggle source

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
Also aliased as: to_end_of_day
from_now(number, units=:seconds)

This is a Railism.

Alias for: hence
hence(number, units=:seconds) click to toggle source

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
Also aliased as: in, since, from_now, since
hours_ago(hours) click to toggle source

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
hours_hence(hours) click to toggle source

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
in(number, units=:seconds)
Alias for: hence
in_day_range?(stime=ZERO, etime=NEVER) click to toggle source

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
last_month() click to toggle source

Short-hand for months_ago(1)

# File lib/richunits/times.rb, line 299
def last_month
  months_ago(1)
end
last_year() click to toggle source

Short-hand for years_ago(1)

# File lib/richunits/times.rb, line 289
def last_year
  years_ago(1)
end
midnight()
Alias for: beginning_of_day
minutes_ago(minutes) click to toggle source

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
minutes_hence(minutes) click to toggle source

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
Also aliased as: minutes_since
minutes_since(minutes)
Alias for: minutes_hence
monday()
Alias for: beginning_of_week
months_ago(months) click to toggle source

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
months_hence(months) click to toggle source

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
Also aliased as: months_since
months_since(months)
Alias for: months_hence
next_month() click to toggle source

Short-hand for months_since(1)

# File lib/richunits/times.rb, line 304
def next_month
  months_since(1)
end
next_week(day = :monday) click to toggle source

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
next_year() click to toggle source

Short-hand for years_since(1)

# File lib/richunits/times.rb, line 294
def next_year
  years_since(1)
end
seconds_since_midnight() click to toggle source

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
set(options) click to toggle source

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
since(number, units=:seconds)

This is a Railism.

Alias for: hence
start_of_day()
Also aliased as: to_start_of_day
Alias for: beginning_of_day
to_end_of_day()
Alias for: end_of_day
to_start_of_day()
Alias for: start_of_day
tomorrow() click to toggle source

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
weeks_ago(weeks) click to toggle source

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
weeks_hence(weeks) click to toggle source

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
Also aliased as: weeks_since
weeks_since(weeks)
Alias for: weeks_hence
years_ago(years) click to toggle source

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
years_hence(years) click to toggle source

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
Also aliased as: years_since
years_since(years)
Alias for: years_hence
yesterday() click to toggle source

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