Skip to content

Commit

Permalink
Move to_time to DateTime compatibility.rb file
Browse files Browse the repository at this point in the history
We are overriding it in `Time` and `ActiveSupport::TimeWithZone` so
there's no point in having it in the `DateAndTime::Compatibility`
module. Also add some docs for the `to_time` implementations.
  • Loading branch information
pixeltrix authored and kmcphillips committed Mar 17, 2017
1 parent 8f37556 commit 7441d76
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
Expand Up @@ -11,9 +11,5 @@ module Compatibility
# this behavior, but new apps will have an initializer that sets
# this to true, because the new behavior is preferred.
mattr_accessor(:preserve_timezone, instance_writer: false) { false }

def to_time
preserve_timezone ? getlocal(utc_offset) : getlocal
end
end
end
Expand Up @@ -2,4 +2,14 @@

class DateTime
include DateAndTime::Compatibility

remove_possible_method :to_time

# Either return an instance of `Time` with the same UTC offset
# as +self+ or an instance of `Time` representing the same time
# in the the local system timezone depending on the setting of
# on the setting of +ActiveSupport.to_time_preserves_timezone+.
def to_time
preserve_timezone ? getlocal(utc_offset) : getlocal
end
end
Expand Up @@ -6,6 +6,8 @@ class Time

remove_possible_method :to_time

# Either return +self+ or the time in the local system timezone depending
# on the setting of +ActiveSupport.to_time_preserves_timezone+.
def to_time
preserve_timezone ? self : getlocal
end
Expand Down
4 changes: 3 additions & 1 deletion activesupport/lib/active_support/time_with_zone.rb
Expand Up @@ -326,7 +326,9 @@ def to_datetime
@to_datetime ||= utc.to_datetime.new_offset(Rational(utc_offset, 86_400))
end

# Returns an instance of <tt>Time</tt>
# Returns an instance of +Time+, either with the same UTC offset
# as +self+ or in the local system timezone depending on the setting
# of +ActiveSupport.to_time_preserves_timezone+.
def to_time
if preserve_timezone
@to_time_with_instance_offset ||= getlocal(utc_offset)
Expand Down

0 comments on commit 7441d76

Please sign in to comment.