Class

ActiveSupport::TimeWithZone

Inheritance
< Object
Included Modules
Comparable

A Time-like class that can represent a time in any time zone. Necessary because standard Ruby Time instances are limited to UTC and the system‘s ENV[‘TZ’] zone.

You shouldn‘t ever need to create a TimeWithZone instance directly via new — instead, Rails provides the methods local, parse, at and now on TimeZone instances, and in_time_zone on Time and DateTime instances, for a more user-friendly syntax. Examples:

  Time.zone = 'Eastern Time (US & Canada)'        # => 'Eastern Time (US & Canada)'
  Time.zone.local(2007, 2, 10, 15, 30, 45)        # => Sat, 10 Feb 2007 15:30:45 EST -05:00
  Time.zone.parse('2007-02-01 15:30:45')          # => Sat, 10 Feb 2007 15:30:45 EST -05:00
  Time.zone.at(1170361845)                        # => Sat, 10 Feb 2007 15:30:45 EST -05:00
  Time.zone.now                                   # => Sun, 18 May 2008 13:07:55 EDT -04:00
  Time.utc(2007, 2, 10, 20, 30, 45).in_time_zone  # => Sat, 10 Feb 2007 15:30:45 EST -05:00

See TimeZone and ActiveSupport::CoreExtensions::Time::Zones for further documentation for these methods.

TimeWithZone instances implement the same API as Ruby Time instances, so that Time and TimeWithZone instances are interchangable. Examples:

  t = Time.zone.now                     # => Sun, 18 May 2008 13:27:25 EDT -04:00
  t.hour                                # => 13
  t.dst?                                # => true
  t.utc_offset                          # => -14400
  t.zone                                # => "EDT"
  t.to_s(:rfc822)                       # => "Sun, 18 May 2008 13:27:25 -0400"
  t + 1.day                             # => Mon, 19 May 2008 13:27:25 EDT -04:00
  t.beginning_of_year                   # => Tue, 01 Jan 2008 00:00:00 EST -05:00
  t > Time.utc(1999)                    # => true
  t.is_a?(Time)                         # => true
  t.is_a?(ActiveSupport::TimeWithZone)  # => true

Attributes

Name Visibility R/W Description
time_zone public R

Methods

Class

Visibility Signature
public new (utc_time, time_zone, local_time = nil, period = nil)

Instance

Visibility Signature
public + (other)
public - (other)
public <=> (other)
public acts_like_time? ()
public advance (options)
public ago (other)
public between? (min, max)
public comparable_time ()
public dst? ()
public eql? (other)
public formatted_offset (colon = true, alternate_utc_string = nil)
public freeze ()
public future? ()
public getgm ()
public getlocal ()
public getutc ()
public gmt? ()
public gmt_offset ()
public gmtime ()
public gmtoff ()
public hash ()
public httpdate ()
public in_time_zone (new_zone = ::Time.zone)
public inspect ()
public is_a? (klass)
public isdst ()
public iso8601 (fraction_digits = 0)
public kind_of? (klass)
public localtime ()
public marshal_dump ()
public marshal_load (variables)
public method_missing (sym, *args, &block)
public past? ()
public period ()
public respond_to? (sym, include_priv = false)
public rfc2822 ()
public rfc822 ()
public since (other)
public strftime (format)
public time ()
public to_a ()
public to_datetime ()
public to_f ()
public to_formatted_s (format = :default)
public to_i ()
public to_json (options = nil)
public to_s (format = :default)
public to_time ()
public to_yaml (options = {})
public today? ()
public tv_sec ()
public usec ()
public utc ()
public utc? ()
public utc_offset ()
public xmlschema (fraction_digits = 0)
public zone ()

Class Method Detail

new(utc_time, time_zone, local_time = nil, period = nil)

Instance Method Detail

+(other)

-(other)

<=>(other)

Use the time in UTC for comparisons.

acts_like_time?()

So that self acts_like?(:time).

advance(options)

ago(other)

between?(min, max)

comparable_time()

Alias for utc

dst?()

eql?(other)

formatted_offset(colon = true, alternate_utc_string = nil)

freeze()

future?()

getgm()

Alias for utc

getlocal()

Alias for localtime

getutc()

Alias for utc

gmt?()

Alias for utc?

gmt_offset()

Alias for utc_offset

gmtime()

Alias for utc

gmtoff()

Alias for utc_offset

hash()

Alias for to_i

httpdate()

in_time_zone(new_zone = ::Time.zone)

Returns the simultaneous time in Time.zone, or the specified zone.

inspect()

is_a?(klass)

Say we‘re a Time to thwart type checking.

isdst()

Alias for dst?

iso8601(fraction_digits = 0)

Alias for xmlschema

kind_of?(klass)

Alias for is_a?

localtime()

Returns a Time.local() instance of the simultaneous time in your system‘s ENV[‘TZ’] zone

marshal_dump()

marshal_load(variables)

method_missing(sym, *args, &block)

Send the missing method to time instance, and wrap result in a new TimeWithZone with the existing time_zone.

past?()

period()

Returns the underlying TZInfo::TimezonePeriod.

respond_to?(sym, include_priv = false)

Ensure proxy class responds to all methods that underlying time instance responds to.

rfc2822()

rfc822()

Alias for rfc2822

since(other)

strftime(format)

Replaces %Z and %z directives with zone and formatted_offset, respectively, before passing to Time#strftime, so that zone information is correct

time()

Returns a Time or DateTime instance that represents the time in time_zone.

to_a()

to_datetime()

to_f()

to_formatted_s(format = :default)

Alias for to_s

to_i()

to_json(options = nil)

Returns a JSON string representing the TimeWithZone. If ActiveSupport.use_standard_json_time_format is set to true, the ISO 8601 format is used.

Examples

  # With ActiveSupport.use_standard_json_time_format = true
  Time.utc(2005,2,1,15,15,10).in_time_zone.to_json
  # => "2005-02-01T15:15:10Z"

  # With ActiveSupport.use_standard_json_time_format = false
  Time.utc(2005,2,1,15,15,10).in_time_zone.to_json
  # => "2005/02/01 15:15:10 +0000"

to_s(format = :default)

:db format outputs time in UTC; all others output time in local. Uses TimeWithZone‘s strftime, so %Z and %z work correctly.

to_time()

A TimeWithZone acts like a Time, so just return self.

to_yaml(options = {})

today?()

tv_sec()

Alias for to_i

usec()

utc()

Returns a Time or DateTime instance that represents the time in UTC.

utc?()

utc_offset()

xmlschema(fraction_digits = 0)

zone()

Time uses zone to display the time zone abbreviation, so we‘re duck-typing it.