Understanding Rails TimeZone

Consider this point: https://gist.github.com/752934

When moving from String to Time, everything goes well. He returns as UTC. However, when you convert from time to time, it returns as WEST instead of UTC (or returns self: http://api.rubyonrails.org/classes/Time.html#method-i-to_time )

Any idea on why the timezone is changing from UTC to WEST?

Thanks in advance, DBA

+3
source share
1 answer

I assume that it suffers from the same problem as DateTime.to_time. This was presented as a bug in Ruby, but rejected. More details here:

http://redmine.ruby-lang.org/issues/show/3737

UPDATE: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/time/conversions.rb

, API doc . , , to_time. , , ext:

  # A method to keep Time, Date and DateTime instances interchangeable on conversions.
  # In this case, it simply returns +self+.
  def to_time
    self
  end unless method_defined?(:to_time)

: https://github.com/rails/rails/blob/4817bf94d135c44ddfae1a30acb15de989e3c86c/activesupport/lib/active_support/core_ext/time/conversions.rb

, , :

ruby-1.9.2-p0 > class Time
ruby-1.9.2-p0 ?>  def to_time
ruby-1.9.2-p0 ?>    self
ruby-1.9.2-p0 ?>  end
ruby-1.9.2-p0 ?>end
 => nil 
ruby-1.9.2-p0 > Time.zone = Time.zone_default = "UTC"
 => "UTC" 
ruby-1.9.2-p0 > t = "2008-04-01".to_time
 => 2008-04-01 00:00:00 UTC 
ruby-1.9.2-p0 >  p t.zone, t.to_time.zone
"UTC"
"UTC"
 => ["UTC", "UTC"] 

,

+3

Source: https://habr.com/ru/post/1781830/


All Articles