3 # A Time-like class that can represent a time in any time zone. Necessary because standard Ruby Time instances are
4 # limited to UTC and the system's <tt>ENV['TZ']</tt> zone.
6 # You shouldn't ever need to create a TimeWithZone instance directly via <tt>new</tt> -- instead, Rails provides the methods
7 # +local+, +parse+, +at+ and +now+ on TimeZone instances, and +in_time_zone+ on Time and DateTime instances, for a more
8 # user-friendly syntax. Examples:
10 # Time.zone = 'Eastern Time (US & Canada)' # => 'Eastern Time (US & Canada)'
11 # Time.zone.local(2007, 2, 10, 15, 30, 45) # => Sat, 10 Feb 2007 15:30:45 EST -05:00
12 # Time.zone.parse('2007-02-01 15:30:45') # => Sat, 10 Feb 2007 15:30:45 EST -05:00
13 # Time.zone.at(1170361845) # => Sat, 10 Feb 2007 15:30:45 EST -05:00
14 # Time.zone.now # => Sun, 18 May 2008 13:07:55 EDT -04:00
15 # Time.utc(2007, 2, 10, 20, 30, 45).in_time_zone # => Sat, 10 Feb 2007 15:30:45 EST -05:00
17 # See TimeZone and ActiveSupport::CoreExtensions::Time::Zones for further documentation for these methods.
19 # TimeWithZone instances implement the same API as Ruby Time instances, so that Time and TimeWithZone instances are interchangable. Examples:
21 # t = Time.zone.now # => Sun, 18 May 2008 13:27:25 EDT -04:00
24 # t.utc_offset # => -14400
26 # t.to_s(:rfc822) # => "Sun, 18 May 2008 13:27:25 -0400"
27 # t + 1.day # => Mon, 19 May 2008 13:27:25 EDT -04:00
28 # t.beginning_of_year # => Tue, 01 Jan 2008 00:00:00 EST -05:00
29 # t > Time.utc(1999) # => true
30 # t.is_a?(Time) # => true
31 # t.is_a?(ActiveSupport::TimeWithZone) # => true
34 attr_reader
:time_zone
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
41 # Returns a Time or DateTime instance that represents the time in +time_zone+.
43 @time ||= period
.to_local(@utc)
46 # Returns a Time or DateTime instance that represents the time in UTC.
48 @utc ||= period
.to_utc(@time)
50 alias_method
:comparable_time, :utc
51 alias_method
:getgm, :utc
52 alias_method
:getutc, :utc
53 alias_method
:gmtime, :utc
55 # Returns the underlying TZInfo::TimezonePeriod.
57 @period ||= time_zone
.period_for_utc(@utc)
60 # Returns the simultaneous time in <tt>Time.zone</tt>, or the specified zone.
61 def in_time_zone(new_zone
= ::Time.zone
)
62 return self if time_zone
== new_zone
63 utc
.in_time_zone(new_zone
)
66 # Returns a <tt>Time.local()</tt> instance of the simultaneous time in your system's <tt>ENV['TZ']</tt> zone
70 alias_method
:getlocal, :localtime
75 alias_method
:isdst, :dst?
78 time_zone
.name
== 'UTC'
80 alias_method
:gmt?, :utc?
83 period
.utc_total_offset
85 alias_method
:gmt_offset, :utc_offset
86 alias_method
:gmtoff, :utc_offset
88 def formatted_offset(colon
= true, alternate_utc_string
= nil)
89 utc
? && alternate_utc_string
|| utc_offset
.to_utc_offset_s(colon
)
92 # Time uses +zone+ to display the time zone abbreviation, so we're duck-typing it.
94 period
.zone_identifier
.to_s
98 "#{time.strftime('%a, %d %b %Y %H:%M:%S')} #{zone} #{formatted_offset}"
102 "#{time.strftime("%Y-
%m-
%dT
%H
:%M
:%S
")}#{formatted_offset(true, 'Z')}"
104 alias_method
:iso8601, :xmlschema
106 # Returns a JSON string representing the TimeWithZone. If ActiveSupport.use_standard_json_time_format is set to
107 # true, the ISO 8601 format is used.
111 # # With ActiveSupport.use_standard_json_time_format = true
112 # Time.utc(2005,2,1,15,15,10).in_time_zone.to_json
113 # # => "2005-02-01T15:15:10Z"
115 # # With ActiveSupport.use_standard_json_time_format = false
116 # Time.utc(2005,2,1,15,15,10).in_time_zone.to_json
117 # # => "2005/02/01 15:15:10 +0000"
118 def to_json(options
= nil)
119 if ActiveSupport
.use_standard_json_time_format
122 %("#{time.strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)}")
126 def to_yaml(options = {})
127 if options.kind_of?(YAML::Emitter)
130 time.to_yaml(options).gsub('Z', formatted_offset(true, 'Z'))
141 alias_method :rfc822, :rfc2822
143 # <tt>:db</tt> format outputs time in UTC; all others output time in local.
144 # Uses TimeWithZone's +strftime+, so <tt>%Z</tt> and <tt>%z</tt> work correctly.
145 def to_s(format = :default)
146 return utc.to_s(format) if format == :db
147 if formatter = ::Time::DATE_FORMATS[format]
148 formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
150 "#{time.strftime("%Y-%m-%d %H:%M:%S")} #{formatted_offset(false, 'UTC')}" # mimicking Ruby 1.9 Time#to_s format
154 # Replaces <tt>%Z</tt> and <tt>%z</tt> directives with +zone+ and +formatted_offset+, respectively, before passing to
155 # Time#strftime, so that zone information is correct
157 format = format.gsub('%Z', zone).gsub('%z', formatted_offset(false))
158 time.strftime(format)
161 # Use the time in UTC for comparisons.
166 def between?(min, max)
167 utc.between?(min, max)
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(:+, other)
192 result = utc.acts_like?(:date) ? utc.since(other) : utc + other rescue utc.since(other)
193 result.in_time_zone(time_zone)
198 # If we're subtracting a Duration of variable length (i.e., years, months, days), move backwards from #time,
199 # otherwise move backwards #utc, for accuracy when moving across DST boundaries
200 if other.acts_like?(:time)
202 elsif duration_of_variable_length?(other)
203 method_missing(:-, other)
205 result = utc.acts_like?(:date) ? utc.ago(other) : utc - other rescue utc.ago(other)
206 result.in_time_zone(time_zone)
211 # If we're adding a Duration of variable length (i.e., years, months, days), move forward from #time,
212 # otherwise move forward from #utc, for accuracy when moving across DST boundaries
213 if duration_of_variable_length?(other)
214 method_missing(:since, other)
216 utc.since(other).in_time_zone(time_zone)
225 # If we're advancing a value of variable length (i.e., years, weeks, months, days), advance from #time,
226 # otherwise advance from #utc, for accuracy when moving across DST boundaries
227 if options.detect {|k,v| [:years, :weeks, :months, :days].include? k}
228 method_missing(:advance, options)
230 utc.advance(options).in_time_zone(time_zone)
234 %w(year mon month day mday wday yday hour min sec to_date).each do |method_name|
243 time
.respond_to
?(:usec) ? time
.usec
: 0
247 [time
.sec
, time
.min
, time
.hour
, time
.day
, time
.mon
, time
.year
, time
.wday
, time
.yday
, dst
?, zone
]
257 alias_method
:hash, :to_i
258 alias_method
:tv_sec, :to_i
260 # A TimeWithZone acts like a Time, so just return +self+.
266 utc
.to_datetime
.new_offset(Rational(utc_offset
, 86_400))
269 # So that +self+ <tt>acts_like?(:time)</tt>.
274 # Say we're a Time to thwart type checking.
276 klass
== ::Time || super
278 alias_method
:kind_of?, :is_a?
281 period
; utc
; time
# preload instance variables before freezing
286 [utc
, time_zone
.name
, time
]
289 def marshal_load(variables
)
290 initialize(variables
[0].utc
, ::Time.__send__(:get_zone, variables
[1]), variables
[2].utc
)
293 # Ensure proxy class responds to all methods that underlying time instance responds to.
294 def respond_to
?(sym
, include_priv
= false)
295 # consistently respond false to acts_like?(:date), regardless of whether #time is a Time or DateTime
296 return false if sym
.to_s
== 'acts_like_date?'
297 super || time
.respond_to
?(sym
, include_priv
)
300 # Send the missing method to +time+ instance, and wrap result in a new TimeWithZone with the existing +time_zone+.
301 def method_missing(sym
, *args
, &block
)
302 result
= time
.__send__(sym
, *args
, &block
)
303 result
.acts_like
?(:time) ? self.class.new(nil, time_zone
, result
) : result
307 def get_period_and_ensure_valid_local_time
308 # we don't want a Time.local instance enforcing its own DST rules as well,
309 # so transfer time values to a utc constructor if necessary
310 @time = transfer_time_values_to_utc_constructor(@time) unless @time.utc
?
312 @time_zone.period_for_local(@time)
313 rescue ::TZInfo::PeriodNotFound
314 # time is in the "spring forward" hour gap, so we're moving the time forward one hour and trying again
320 def transfer_time_values_to_utc_constructor(time
)
321 ::Time.utc_time(time
.year
, time
.month
, time
.day
, time
.hour
, time
.min
, time
.sec
, time
.respond_to
?(:usec) ? time
.usec
: 0)
324 def duration_of_variable_length
?(obj
)
325 ActiveSupport
::Duration === obj
&& obj
.parts
.flatten
.detect
{|p
| [:years, :months, :days].include? p
}