Class Time
In: lib/core/facets/time/change.rb
lib/core/facets/time/dst_adjustment.rb
lib/core/facets/time/elapse.rb
lib/core/facets/time/future.rb
lib/core/facets/time/round_to.rb
lib/core/facets/time/set.rb
lib/core/facets/time/shift.rb
lib/core/facets/time/stamp.rb
lib/core/facets/time/to_time.rb
lib/core/facets/time/trunc.rb
lib/more/facets/date.rb
Parent: Object

Methods

ago   change   dst_adjustment   elapse   future?   hence   in   less   local_time   past?   round_to   set   shift   stamp   stamp   time_with_datetime_fallback   to_date   to_datetime   to_time   trunc   utc_time  

Constants

FORMAT = { :db => "%Y-%m-%d %H:%M:%S", :dbase => "%Y-%m-%d %H:%M:%S", :datbase => "%Y-%m-%d %H:%M:%S", :utc => "%Y-%m-%d %H:%M:%S", :number => "%Y%m%d%H%M%S", :short => "%d %b %H:%M", :time => "%H:%M", :long => "%B %d, %Y %H:%M", :day1st => "%d-%m-%Y %H:%M", :dmYHM => "%d-%m-%Y %H:%M", :rfc822 => "%a, %d %b %Y %H:%M:%S %z", nil => "%a %b %d %H:%M:%S %Z %Y"

Public Class methods

Tracks the elapse time of a code block.

  e = Time.elapse { sleep 1 }

  e.assert > 1

CREDIT: Hal Fulton

Produce time stamp for Time.now. See stamp.

CREDIT: Trans

Public Instance methods

ago(*time_units)

Alias for less

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.

  t1 = Time.at(10000)
  t1.ctime   #=> "Wed Dec 31 21:46:40 1969"

  t2 = t1.change(:hour => 11)
  t2.ctime   #=> "Wed Dec 31 11:00:00 1969"

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.

hence(*time_units)

Alias for shift

in(*time_units)

Alias for shift

Returns a new Time representing the time a number of time-units ago. This is just like shift, but reverses the direction.

  t = Time.utc(2010,10,10,0,0,0)

  t.less(4, :days)             #=>  Time.utc(2010,10,6,0,0,0)

Round time at the nearest range (in seconds).

  t1 = Time.now
  t2 = t1.round_to(60*60)
  t2.min #=> 0
  t2.sec #=> 0

TODO: What about `round(:minute)`?

TODO: Fractional seconds should round the usec.

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".

Returns a new Time representing the time shifted by the time-units given. Positive number shift the time forward, negative number shift the time backward.

  t = Time.utc(2010,10,10,0,0,0)
  t.shift( 4, :days)            #=>  Time.utc(2010,10,14,0,0,0)
  t.shift(-4, :days)            #=>  Time.utc(2010,10,6,0,0,0)

More than one unit of time can be given.

  t.shift(4, :days, 3, :hours)  #=>  Time.utc(2010,10,14,3,0,0)

The shift method can also take a hash.

  t.shift(:days=>4, :hours=>3)  #=>  Time.utc(2010,10,14,3,0,0)

Create a time stamp.

  t = Time.at(10000)
  t.stamp(:short)    #=> "31 Dec 21:46"

Supported formats come from the Time::FORMAT constant.

CREDIT: Trans

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

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

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

Truncate time at give range (in seconds).

  t = Time.now
  t = t.trunc(60*60)
  t.min #=> 0
  t.sec #=> 0

[Validate]