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
Methods
Included Modules
Attributes
[R] time_zone
Public Class methods
new(utc_time, time_zone, local_time = nil, period = nil)
    # File activesupport/lib/active_support/time_with_zone.rb, line 36
36:     def initialize(utc_time, time_zone, local_time = nil, period = nil)
37:       @utc, @time_zone, @time = utc_time, time_zone, local_time
38:       @period = @utc ? period : get_period_and_ensure_valid_local_time
39:     end
Public Instance methods
+(other)
     # File activesupport/lib/active_support/time_with_zone.rb, line 162
162:     def +(other)
163:       # If we're adding a Duration of variable length (i.e., years, months, days), move forward from #time,
164:       # otherwise move forward from #utc, for accuracy when moving across DST boundaries
165:       if duration_of_variable_length?(other)
166:         method_missing(:+, other)
167:       else
168:         result = utc.acts_like?(:date) ? utc.since(other) : utc + other rescue utc.since(other)
169:         result.in_time_zone(time_zone)
170:       end
171:     end
-(other)
     # File activesupport/lib/active_support/time_with_zone.rb, line 173
173:     def -(other)
174:       # If we're subtracting a Duration of variable length (i.e., years, months, days), move backwards from #time,
175:       # otherwise move backwards #utc, for accuracy when moving across DST boundaries
176:       if other.acts_like?(:time)
177:         utc - other
178:       elsif duration_of_variable_length?(other)
179:         method_missing(:-, other)
180:       else
181:         result = utc.acts_like?(:date) ? utc.ago(other) : utc - other rescue utc.ago(other)
182:         result.in_time_zone(time_zone)
183:       end
184:     end
<=>(other)

Use the time in UTC for comparisons.

     # File activesupport/lib/active_support/time_with_zone.rb, line 150
150:     def <=>(other)
151:       utc <=> other
152:     end
acts_like_time?()

So that self acts_like?(:time).

     # File activesupport/lib/active_support/time_with_zone.rb, line 247
247:     def acts_like_time?
248:       true
249:     end
advance(options)
     # File activesupport/lib/active_support/time_with_zone.rb, line 200
200:     def advance(options)
201:       # If we're advancing a value of variable length (i.e., years, weeks, months, days), advance from #time,
202:       # otherwise advance from #utc, for accuracy when moving across DST boundaries
203:       if options.detect {|k,v| [:years, :weeks, :months, :days].include? k}
204:         method_missing(:advance, options)
205:       else
206:         utc.advance(options).in_time_zone(time_zone)
207:       end
208:     end
ago(other)
     # File activesupport/lib/active_support/time_with_zone.rb, line 196
196:     def ago(other)
197:       since(-other)
198:     end
between?(min, max)
     # File activesupport/lib/active_support/time_with_zone.rb, line 154
154:     def between?(min, max)
155:       utc.between?(min, max)
156:     end
comparable_time()

Alias for utc

dst?()
This method is also aliased as isdst
    # File activesupport/lib/active_support/time_with_zone.rb, line 72
72:     def dst?
73:       period.dst?
74:     end
eql?(other)
     # File activesupport/lib/active_support/time_with_zone.rb, line 158
158:     def eql?(other)
159:       utc == other
160:     end
formatted_offset(colon = true, alternate_utc_string = nil)
    # File activesupport/lib/active_support/time_with_zone.rb, line 88
88:     def formatted_offset(colon = true, alternate_utc_string = nil)
89:       utc? && alternate_utc_string || utc_offset.to_utc_offset_s(colon)
90:     end
freeze()

Neuter freeze because freezing can cause problems with lazy loading of attributes.

     # File activesupport/lib/active_support/time_with_zone.rb, line 258
258:     def freeze
259:       self
260:     end
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()
     # File activesupport/lib/active_support/time_with_zone.rb, line 122
122:     def httpdate
123:       utc.httpdate
124:     end
in_time_zone(new_zone = ::Time.zone)

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

    # File activesupport/lib/active_support/time_with_zone.rb, line 61
61:     def in_time_zone(new_zone = ::Time.zone)
62:       return self if time_zone == new_zone
63:       utc.in_time_zone(new_zone)
64:     end
inspect()
    # File activesupport/lib/active_support/time_with_zone.rb, line 97
97:     def inspect
98:       "#{time.strftime('%a, %d %b %Y %H:%M:%S')} #{zone} #{formatted_offset}"
99:     end
is_a?(klass)

Say we‘re a Time to thwart type checking.

This method is also aliased as kind_of?
     # File activesupport/lib/active_support/time_with_zone.rb, line 252
252:     def is_a?(klass)
253:       klass == ::Time || super
254:     end
isdst()

Alias for dst?

iso8601()

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

This method is also aliased as getlocal
    # File activesupport/lib/active_support/time_with_zone.rb, line 67
67:     def localtime
68:       utc.getlocal
69:     end
marshal_dump()
     # File activesupport/lib/active_support/time_with_zone.rb, line 262
262:     def marshal_dump
263:       [utc, time_zone.name, time]
264:     end
marshal_load(variables)
     # File activesupport/lib/active_support/time_with_zone.rb, line 266
266:     def marshal_load(variables)
267:       initialize(variables[0].utc, ::Time.send!(:get_zone, variables[1]), variables[2].utc)
268:     end
method_missing(sym, *args, &block)

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

     # File activesupport/lib/active_support/time_with_zone.rb, line 278
278:     def method_missing(sym, *args, &block)
279:       result = time.__send__(sym, *args, &block)
280:       result.acts_like?(:time) ? self.class.new(nil, time_zone, result) : result
281:     end
period()

Returns the underlying TZInfo::TimezonePeriod.

    # File activesupport/lib/active_support/time_with_zone.rb, line 56
56:     def period
57:       @period ||= time_zone.period_for_utc(@utc)
58:     end
respond_to?(sym, include_priv = false)

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

     # File activesupport/lib/active_support/time_with_zone.rb, line 271
271:     def respond_to?(sym, include_priv = false)
272:       # consistently respond false to acts_like?(:date), regardless of whether #time is a Time or DateTime
273:       return false if sym.to_s == 'acts_like_date?'
274:       super || time.respond_to?(sym, include_priv)
275:     end
rfc2822()
This method is also aliased as rfc822
     # File activesupport/lib/active_support/time_with_zone.rb, line 126
126:     def rfc2822
127:       to_s(:rfc822)
128:     end
rfc822()

Alias for rfc2822

since(other)
     # File activesupport/lib/active_support/time_with_zone.rb, line 186
186:     def since(other)
187:       # If we're adding a Duration of variable length (i.e., years, months, days), move forward from #time,
188:       # otherwise move forward from #utc, for accuracy when moving across DST boundaries
189:       if duration_of_variable_length?(other)
190:         method_missing(:since, other)
191:       else
192:         utc.since(other).in_time_zone(time_zone)
193:       end
194:     end
strftime(format)

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

     # File activesupport/lib/active_support/time_with_zone.rb, line 144
144:     def strftime(format)
145:       format = format.gsub('%Z', zone).gsub('%z', formatted_offset(false))
146:       time.strftime(format)
147:     end
time()

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

    # File activesupport/lib/active_support/time_with_zone.rb, line 42
42:     def time
43:       @time ||= period.to_local(@utc)
44:     end
to_a()
     # File activesupport/lib/active_support/time_with_zone.rb, line 223
223:     def to_a
224:       [time.sec, time.min, time.hour, time.day, time.mon, time.year, time.wday, time.yday, dst?, zone]
225:     end
to_datetime()
     # File activesupport/lib/active_support/time_with_zone.rb, line 242
242:     def to_datetime
243:       utc.to_datetime.new_offset(Rational(utc_offset, 86_400))
244:     end
to_f()
     # File activesupport/lib/active_support/time_with_zone.rb, line 227
227:     def to_f
228:       utc.to_f
229:     end
to_i()
This method is also aliased as hash tv_sec
     # File activesupport/lib/active_support/time_with_zone.rb, line 231
231:     def to_i
232:       utc.to_i
233:     end
to_json(options = nil)
     # File activesupport/lib/active_support/time_with_zone.rb, line 106
106:     def to_json(options = nil)
107:       if ActiveSupport.use_standard_json_time_format
108:         xmlschema.inspect
109:       else
110:         %("#{time.strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)}")
111:       end
112:     end
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.

     # File activesupport/lib/active_support/time_with_zone.rb, line 133
133:     def to_s(format = :default) 
134:       return utc.to_s(format) if format == :db
135:       if formatter = ::Time::DATE_FORMATS[format]
136:         formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
137:       else
138:         "#{time.strftime("%Y-%m-%d %H:%M:%S")} #{formatted_offset(false, 'UTC')}" # mimicking Ruby 1.9 Time#to_s format
139:       end
140:     end
to_time()

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

     # File activesupport/lib/active_support/time_with_zone.rb, line 238
238:     def to_time
239:       self
240:     end
to_yaml(options = {})
     # File activesupport/lib/active_support/time_with_zone.rb, line 114
114:     def to_yaml(options = {})
115:       if options.kind_of?(YAML::Emitter)
116:         utc.to_yaml(options)
117:       else
118:         time.to_yaml(options).gsub('Z', formatted_offset(true, 'Z'))
119:       end
120:     end
tv_sec()

Alias for to_i

usec()
     # File activesupport/lib/active_support/time_with_zone.rb, line 219
219:     def usec
220:       time.respond_to?(:usec) ? time.usec : 0
221:     end
utc()

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

This method is also aliased as comparable_time getgm getutc gmtime
    # File activesupport/lib/active_support/time_with_zone.rb, line 47
47:     def utc
48:       @utc ||= period.to_utc(@time)
49:     end
utc?()
This method is also aliased as gmt?
    # File activesupport/lib/active_support/time_with_zone.rb, line 77
77:     def utc?
78:       time_zone.name == 'UTC'
79:     end
utc_offset()
This method is also aliased as gmt_offset gmtoff
    # File activesupport/lib/active_support/time_with_zone.rb, line 82
82:     def utc_offset
83:       period.utc_total_offset
84:     end
xmlschema()
This method is also aliased as iso8601
     # File activesupport/lib/active_support/time_with_zone.rb, line 101
101:     def xmlschema
102:       "#{time.strftime("%Y-%m-%dT%H:%M:%S")}#{formatted_offset(true, 'Z')}"
103:     end
zone()

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

    # File activesupport/lib/active_support/time_with_zone.rb, line 93
93:     def zone
94:       period.zone_identifier.to_s
95:     end