Type:
Class

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 use methods local, parse, at and now on TimeZone instances, and in_time_zone on Time and DateTime instances.

1
2
3
4
5
6
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-10 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 Time and TimeZone for further documentation of these methods.

TimeWithZone instances implement the same API as Ruby Time instances, so that Time and TimeWithZone instances are interchangeable.

1
2
3
4
5
6
7
8
9
10
11
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
<=>
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::TimeWithZone

<=>(other) Instance Public methods Use the time in UTC for comparisons

2025-01-10 15:47:30
freeze
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::TimeWithZone

freeze() Instance Public methods

2025-01-10 15:47:30
comparable_time
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::TimeWithZone

comparable_time() Instance Public methods Alias for:

2025-01-10 15:47:30
formatted_offset
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::TimeWithZone

formatted_offset(colon = true, alternate_utc_string = nil) Instance Public methods Returns

2025-01-10 15:47:30
gmt?
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::TimeWithZone

gmt?() Instance Public methods Alias for:

2025-01-10 15:47:30
kind_of?
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::TimeWithZone

kind_of?(klass) Instance Public methods Alias for:

2025-01-10 15:47:30
advance
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::TimeWithZone

advance(options) Instance Public methods

2025-01-10 15:47:30
tv_sec
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::TimeWithZone

tv_sec() Instance Public methods Alias for:

2025-01-10 15:47:30
in_time_zone
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::TimeWithZone

in_time_zone(new_zone = ::Time.zone) Instance Public methods Returns the simultaneous

2025-01-10 15:47:30
acts_like_time?
  • References/Ruby on Rails/Rails/Classes/ActiveSupport/ActiveSupport::TimeWithZone

acts_like_time?() Instance Public methods So that self acts_like

2025-01-10 15:47:30