aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/string/conversions.rb
diff options
context:
space:
mode:
authorDaniel Neighman <has.sox@gmail.com>2010-04-22 09:43:33 +1000
committerwycats <wycats@gmail.com>2010-04-21 16:54:33 -0700
commit726b5d79845c0ef50db64e2991bfec4e108faf4d (patch)
tree27fc030105d495bb7b401e5993e16893b8023fa1 /activesupport/lib/active_support/core_ext/string/conversions.rb
parentb73177a0954aed11543f2bb829cd07cc12d4f590 (diff)
downloadrails-726b5d79845c0ef50db64e2991bfec4e108faf4d.tar.gz
rails-726b5d79845c0ef50db64e2991bfec4e108faf4d.tar.bz2
rails-726b5d79845c0ef50db64e2991bfec4e108faf4d.zip
updates String#to_(date|date_time|time) to return nil for blank strings
Diffstat (limited to 'activesupport/lib/active_support/core_ext/string/conversions.rb')
-rw-r--r--activesupport/lib/active_support/core_ext/string/conversions.rb3
1 files changed, 3 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/string/conversions.rb b/activesupport/lib/active_support/core_ext/string/conversions.rb
index 4cc36147f8..6a243fe982 100644
--- a/activesupport/lib/active_support/core_ext/string/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/string/conversions.rb
@@ -30,16 +30,19 @@ class String
# Form can be either :utc (default) or :local.
def to_time(form = :utc)
+ return nil if self.blank?
d = ::Date._parse(self, false).values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction).map { |arg| arg || 0 }
d[6] *= 1000000
::Time.send("#{form}_time", *d)
end
def to_date
+ return nil if self.blank?
::Date.new(*::Date._parse(self, false).values_at(:year, :mon, :mday))
end
def to_datetime
+ return nil if self.blank?
d = ::Date._parse(self, false).values_at(:year, :mon, :mday, :hour, :min, :sec, :zone, :sec_fraction).map { |arg| arg || 0 }
d[5] += d.pop
::DateTime.civil(*d)