class Time

Constants

FORMAT

Public Class Methods

elapse() { || ... } click to toggle source

Tracks the elapse time of a code block.

Time.elapse { sleep 1 }  #=> 0.999188899993896

CREDIT: Hal Fulton

# File lib/core/facets/time/elapse.rb, line 9
def self.elapse
  raise "Need block" unless block_given?
  t0 = now.to_f
  yield
  now.to_f - t0
end
stamp(*args) click to toggle source

Produce time stamp for Time.now. See stamp.

CREDIT: Trans

# File lib/core/facets/time/stamp.rb, line 24
def self.stamp(*args)
  now.stamp(*args)
end

Public Instance Methods

advance(options) click to toggle source

Uses Date to provide precise Time calculations for years, months, and days. The options parameter takes a hash with any of these keys: :years, :months, :weeks, :days, :hours, :minutes, :seconds.

CREDIT: ActiveSupport Team

# File lib/core/facets/time/advance.rb, line 10
def advance(options)
  d = to_date.advance(options)
  time_advanced_by_date = change(: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 : time_advanced_by_date.since(seconds_to_advance)
end
ago(number, units=:seconds) click to toggle source

Returns a new Time representing the time a number of time-units ago.

# File lib/core/facets/time/hence.rb, line 15
def ago(number, units=:seconds)
  return hence(-number, units) if number < 0

  time = (
    case units.to_s.downcase.to_sym
    when :years
      set(:year => (year - number))
    when :months
      new_month = ((month - number - 1) % 12) + 1
      y = (number / 12) + (new_month > month ? 1 : 0)
      set(:year => (year - y), :month => new_month)
    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
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/core/facets/time/change.rb, line 13
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
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 handles all but 4 tests.

# File lib/core/facets/time/hence.rb, line 83
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
hence(number, units=:seconds) click to toggle source

Returns a new Time representing the time a number of time-units hence.

# File lib/core/facets/time/hence.rb, line 46
def hence(number, units=:seconds)
  return ago(-number, units) if number < 0

  time = (
    case units.to_s.downcase.to_sym
    when :years
      set( :year=>(year + number) )
    when :months
      new_month = ((month + number - 1) % 12) + 1
      y = (number / 12) + (new_month < month ? 1 : 0)
      set(:year => (year + y), :month => new_month)
    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
round(amount) click to toggle source

Round time at the nearest range (in seconds).

t = Time.now
=>
t.round(60*60) # 1 hour
=>
# File lib/core/facets/time/round.rb, line 12
def round(amount)
  (self+amount/2.0).trunc(amount)
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/core/facets/time/set.rb, line 8
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
stamp(fmt = nil) click to toggle source

Create a time stamp.

Time.now.stamp(:short)    #=> "01 Dec 15:15"

Supported formats come from the Time::FORMAT constant.

CREDIT: Trans

# File lib/core/facets/time/stamp.rb, line 36
def stamp(fmt = nil)
  unless String === fmt
    fmt = FORMAT[fmt]
  end
  strftime(fmt).strip
end
to_date() click to toggle source

Converts a Time object to a Date, dropping hour, minute, and second precision.

my_time = Time.now  # => Mon Nov 12 22:59:51 -0500 2007
my_time.to_date     # => Mon, 12 Nov 2007

your_time = Time.parse("1/13/2009 1:13:03 P.M.")  # => Tue Jan 13 13:13:03 -0500 2009
your_time.to_date                                 # => Tue, 13 Jan 2009
# File lib/more/facets/date.rb, line 353
def to_date
  ::Date.new(year, month, day)
end
to_datetime() click to toggle source

Converts a Time instance to a Ruby DateTime instance, preserving UTC offset.

my_time = Time.now    # => Mon Nov 12 23:04:21 -0500 2007
my_time.to_datetime   # => Mon, 12 Nov 2007 23:04:21 -0500

your_time = Time.parse("1/13/2009 1:13:03 P.M.")  # => Tue Jan 13 13:13:03 -0500 2009
your_time.to_datetime                             # => Tue, 13 Jan 2009 13:13:03 -0500
# File lib/more/facets/date.rb, line 374
def to_datetime
  ::DateTime.civil(year, month, day, hour, min, sec, Rational(utc_offset, 86400))
end
to_time() click to toggle source

To be able to keep Dates and Times interchangeable on conversions.

# File lib/core/facets/time/to_time.rb, line 8
def to_time
  getlocal 
end
trunc(amount) click to toggle source

Truncate time at give range (in seconds).

t = Time.now
=>
t.trunc(60*60) # 1 hour
=>
# File lib/core/facets/time/trunc.rb, line 10
def trunc(amount)
  self - (self.to_i % amount)
end