Skip to content
This repository
Browse code

Remove DateTime#to_time override

Currently if the offset is not zero then to_time returns self which
can lead to errors where a developer assumes that the value is a
Time. To solve this we can use the native implementation of
DateTime#to_time in Ruby 1.9.3 as it handles offsets properly and
is faster than our override.
  • Loading branch information...
commit c685d12c19f953768ee445ab29faeb4a1be54535 1 parent 210cd75
Andrew White authored
14  activesupport/lib/active_support/core_ext/date_time/conversions.rb
@@ -4,10 +4,6 @@
4 4
 require 'active_support/values/time_zone'
5 5
 
6 6
 class DateTime
7  
-  # Ruby 1.9 has DateTime#to_time which internally relies on Time. We define our own #to_time which allows
8  
-  # DateTimes outside the range of what can be created with Time.
9  
-  remove_method :to_time
10  
-
11 7
   # Convert to a formatted string. See Time::DATE_FORMATS for predefined formats.
12 8
   #
13 9
   # This method is aliased to <tt>to_s</tt>.
@@ -57,16 +53,6 @@ def readable_inspect
57 53
   alias_method :default_inspect, :inspect
58 54
   alias_method :inspect, :readable_inspect
59 55
 
60  
-  # Attempts to convert self to a Ruby Time object; returns self if out of range of Ruby Time class.
61  
-  # If self has an offset other than 0, self will just be returned unaltered, since there's no clean way to map it to a Time.
62  
-  def to_time
63  
-    if offset == 0
64  
-      ::Time.utc_time(year, month, day, hour, min, sec, sec_fraction * 1000000)
65  
-    else
66  
-      self
67  
-    end
68  
-  end
69  
-
70 56
   # Returns DateTime with local offset for given year if format is local else offset is zero
71 57
   #
72 58
   #   DateTime.civil_from_format :local, 2012

0 notes on commit c685d12

Please sign in to comment.
Something went wrong with that request. Please try again.