class Time

Public Class Methods

local_time(*args) click to toggle source
# File lib/facets/date.rb, line 356
def self.local_time(*args)
  time_with_datetime_fallback(:local, *args)
end
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/facets/date.rb, line 366
def self.time_with_datetime_fallback(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0, usec=0)
  ::Time.send(utc_or_local, year, month, day, hour, min, sec, usec)
rescue
  offset = utc_or_local.to_sym == :local ? ::DateTime.local_offset : 0
  ::DateTime.civil(year, month, day, hour, min, sec, offset)
end
utc_time(*args) click to toggle source
# File lib/facets/date.rb, line 361
def self.utc_time(*args)
  time_with_datetime_fallback(:utc, *args)
end

Public Instance Methods

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/facets/date.rb, line 384
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/facets/date.rb, line 406
def to_datetime
  ::DateTime.civil(year, month, day, hour, min, sec, Rational(utc_offset, 86400))
end