module Time::Utils

Constants

COMMON_YEAR_DAYS_IN_MONTH
VERSION

Public Instance Methods

advance(time, options) click to toggle source
# File lib/time/utils.rb, line 44
def advance(time, options)
  unless options[:weeks].nil?
    options[:weeks], partial_weeks = options[:weeks].divmod(1)
    options[:days] = (options[:days] || 0) + 7 * partial_weeks
  end

  unless options[:days].nil?
    options[:days], partial_days = options[:days].divmod(1)
    options[:hours] = (options[:hours] || 0) + 24 * partial_days
  end

  d = Date::Utils.advance(to_date(time), options)
  time_advanced_by_date = change(time, :year => d.year, :month => d.month, :day => d.day)
  seconds_to_advance = (options[:seconds] || 0) + (options[:minutes] || 0) * 60 + (options[:hours] || 0) * 3600
  seconds_to_advance == 0 ? time_advanced_by_date : since(time_advanced_by_date, seconds_to_advance)
end
ago(time, seconds) click to toggle source

Returns a new Time representing the time a number of seconds ago, this is basically a wrapper around the Numeric extension

# File lib/time/utils.rb, line 62
def ago(time, seconds)
  since(time, -seconds)
end
at_beginning_of_day(time)
Alias for: beginning_of_day
at_beginning_of_hour(time)
Alias for: beginning_of_hour
at_beginning_of_month(time)
Alias for: beginning_of_month
at_beginning_of_year(time)
Alias for: beginning_of_year
at_end_of_month(time)
Alias for: end_of_month
at_end_of_year(time)
Alias for: end_of_year
at_midnight(time)
Alias for: beginning_of_day
beginning_of_day(time) click to toggle source

Returns a new Time representing the start of the day (0:00)

# File lib/time/utils.rb, line 118
def beginning_of_day(time)
  #(self - seconds_since_midnight).change(:usec => 0)
  change(time, :hour => 0)
end
beginning_of_hour(time) click to toggle source

Returns a new Time representing the start of the hour (x:00)

# File lib/time/utils.rb, line 132
def beginning_of_hour(time)
  change(time, :min => 0)
end
Also aliased as: at_beginning_of_hour
beginning_of_month(time) click to toggle source

Returns a new Time representing the start of the month (1st of the month, 0:00)

# File lib/time/utils.rb, line 148
def beginning_of_month(time)
  #self - ((self.mday-1).days + self.seconds_since_midnight)
  change(time, :day => 1, :hour => 0)
end
Also aliased as: at_beginning_of_month
beginning_of_year(time) click to toggle source

Returns a new Time representing the start of the year (1st of january, 0:00)

# File lib/time/utils.rb, line 163
def beginning_of_year(time)
  change(time, :month => 1, :day => 1, :hour => 0)
end
Also aliased as: at_beginning_of_year
change(time, options) click to toggle source
# File lib/time/utils.rb, line 31
def change(time, options)
  send(
    time.utc? ? :utc_time : :local_time,
    options[:year]  || time.year,
    options[:month] || time.month,
    options[:day]   || time.day,
    options[:hour]  || time.hour,
    options[:min]   || (options[:hour] ? 0 : time.min),
    options[:sec]   || ((options[:hour] || options[:min]) ? 0 : time.sec),
    options[:usec]  || ((options[:hour] || options[:min] || options[:sec]) ? 0 : time.usec)
  )
end
days_in_month(month, year = now.year) click to toggle source
# File lib/time/utils.rb, line 9
def days_in_month(month, year = now.year)
  return 29 if month == 2 && ::Date.gregorian_leap?(year)
  COMMON_YEAR_DAYS_IN_MONTH[month]
end
end_of_day(time) click to toggle source

Returns a new Time representing the end of the day, 23:59:59.999999 (.999999999 in ruby1.9)

# File lib/time/utils.rb, line 127
def end_of_day(time)
  change(time, :hour => 23, :min => 59, :sec => 59, :usec => 999999.999)
end
end_of_hour(time) click to toggle source

Returns a new Time representing the end of the hour, x:59:59.999999 (.999999999 in ruby1.9)

# File lib/time/utils.rb, line 138
def end_of_hour(time)
  change(
    time,
    :min => 59,
    :sec => 59,
    :usec => 999999.999
  )
end
end_of_month(time) click to toggle source

Returns a new Time representing the end of the month (end of the last day of the month)

# File lib/time/utils.rb, line 155
def end_of_month(time)
  #self - ((self.mday-1).days + self.seconds_since_midnight)
  last_day = days_in_month(time.month, time.year)
  change(time, :day => last_day, :hour => 23, :min => 59, :sec => 59, :usec => 999999.999)
end
Also aliased as: at_end_of_month
end_of_year(time) click to toggle source

Returns a new Time representing the end of the year (end of the 31st of december)

# File lib/time/utils.rb, line 169
def end_of_year(time)
  change(time, :month => 12, :day => 31, :hour => 23, :min => 59, :sec => 59, :usec => 999999.999)
end
Also aliased as: at_end_of_year
in(time, seconds)
Alias for: since
local_time(*args) click to toggle source

Wraps class method time_with_datetime_fallback with utc_or_local set to :local.

# File lib/time/utils.rb, line 27
def local_time(*args)
  time_with_datetime_fallback(:local, *args)
end
midnight(time)
Alias for: beginning_of_day
months_ago(time, months) click to toggle source

Returns a new Time representing the time a number of specified months ago

# File lib/time/utils.rb, line 78
def months_ago(time, months)
  advance(time, :months => -months)
end
months_since(time, months) click to toggle source

Returns a new Time representing the time a number of specified months in the future

# File lib/time/utils.rb, line 83
def months_since(time, months)
  advance(time, :months => months)
end
next_month(time) click to toggle source

Short-hand for months_since(1)

# File lib/time/utils.rb, line 113
def next_month(time)
  months_since(time, 1)
end
next_year(time) click to toggle source

Short-hand for years_since(1)

# File lib/time/utils.rb, line 103
def next_year(time)
  years_since(time, 1)
end
prev_month(time) click to toggle source

Short-hand for months_ago(1)

# File lib/time/utils.rb, line 108
def prev_month(time)
  months_ago(time, 1)
end
prev_year(time) click to toggle source

Short-hand for years_ago(1)

# File lib/time/utils.rb, line 98
def prev_year(time)
  years_ago(time, 1)
end
since(time, seconds) click to toggle source

Returns a new Time representing the time a number of seconds since the instance time

# File lib/time/utils.rb, line 67
def since(time, seconds)
  time + seconds
end
Also aliased as: in
time_with_datetime_fallback(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0, usec=0) click to toggle source
# File lib/time/utils.rb, line 14
def time_with_datetime_fallback(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0, usec=0)
  time = ::Time.send(utc_or_local, year, month, day, hour, min, sec, usec)
  # This check is needed because Time.utc(y) returns a time object in the 2000s for 0 <= y <= 138.
  time.year == year ? time : ::DateTime.civil_from_format(utc_or_local, year, month, day, hour, min, sec)
  time
end
to_date(time) click to toggle source

Conversions

# File lib/time/utils.rb, line 186
def to_date(time)
  Date.new(time.year, time.month, time.day)
end
tomorrow(time) click to toggle source

Convenience method which returns a new Time representing the time 1 day since the instance time

# File lib/time/utils.rb, line 180
def tomorrow(time)
  advance(time, :days => 1)
end
utc_time(*args) click to toggle source

Wraps class method time_with_datetime_fallback with utc_or_local set to :utc.

# File lib/time/utils.rb, line 22
def utc_time(*args)
  time_with_datetime_fallback(:utc, *args)
end
weeks_ago(time, weeks) click to toggle source

Returns a new Time representing the time a number of specified weeks ago.

# File lib/time/utils.rb, line 73
def weeks_ago(time, weeks)
  advance(time, :weeks => -weeks)
end
years_ago(time, years) click to toggle source

Returns a new Time representing the time a number of specified years ago

# File lib/time/utils.rb, line 88
def years_ago(time, years)
  advance(time, :years => -years)
end
years_since(time, years) click to toggle source

Returns a new Time representing the time a number of specified years in the future

# File lib/time/utils.rb, line 93
def years_since(time, years)
  advance(time, :years => years)
end
yesterday(time) click to toggle source

Convenience method which returns a new Time representing the time 1 day ago

# File lib/time/utils.rb, line 175
def yesterday(time)
  advance(time, :days => -1)
end