Skip to content

Commit

Permalink
handling edge case and adding documentation for String to Date/Time/D…
Browse files Browse the repository at this point in the history
…ateTime conversion methods
  • Loading branch information
jasondew committed Mar 14, 2012
1 parent 29094c0 commit 80222e1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
28 changes: 20 additions & 8 deletions activesupport/lib/active_support/core_ext/string/conversions.rb
Expand Up @@ -2,23 +2,35 @@
require 'active_support/core_ext/time/calculations'

class String
# Form can be either :utc (default) or :local.
# Attempts to converts itself to a <tt>Time</tt> object and
# returns <tt>nil</tt> if it can't.
# <tt>form</tt> 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, :offset).map { |arg| arg || 0 }
d[6] *= 1000000
::Time.send("#{form}_time", *d[0..6]) - d[7]
parts = ::Date._parse(self, false).values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction, :offset)
return nil unless parts.any?
parts.map! {|part| part || 0 }
parts[6] *= 1000000
::Time.send("#{form}_time", *parts[0..6]) - parts[7]
end

# Attempts to converts itself to a <tt>Date</tt> object and
# returns <tt>nil</tt> if it can't.
def to_date
return nil if self.blank?
::Date.new(*::Date._parse(self, false).values_at(:year, :mon, :mday))
parts = ::Date._parse(self, false).values_at(:year, :mon, :mday)
return nil unless parts.any?
::Date.new(*parts)
end

# Attempts to converts itself to a <tt>DateTime</tt> object and
# returns <tt>nil</tt> if it can't.
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)
parts = ::Date._parse(self, false).values_at(:year, :mon, :mday, :hour, :min, :sec, :zone, :sec_fraction)
return nil unless parts.any?
parts.map! {|part| part || 0 }
parts[5] += parts.pop
::DateTime.civil(*parts)
end
end
3 changes: 3 additions & 0 deletions activesupport/test/core_ext/string_ext_test.rb
Expand Up @@ -168,6 +168,7 @@ def test_string_to_time
assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time
assert_equal Time.local_time(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time(:local)
assert_equal Time.utc(2011, 2, 27, 23, 50), "2011-02-27 22:50 -0100".to_time
assert_nil "foo".to_time
assert_nil "".to_time
end

Expand All @@ -176,11 +177,13 @@ def test_string_to_datetime
assert_equal 0, "2039-02-27 23:50".to_datetime.offset # use UTC offset
assert_equal ::Date::ITALY, "2039-02-27 23:50".to_datetime.start # use Ruby's default start value
assert_equal DateTime.civil(2039, 2, 27, 23, 50, 19 + Rational(275038, 1000000), "-04:00"), "2039-02-27T23:50:19.275038-04:00".to_datetime
assert_nil "foo".to_datetime
assert_nil "".to_datetime
end

def test_string_to_date
assert_equal Date.new(2005, 2, 27), "2005-02-27".to_date
assert_nil "foo".to_date
assert_nil "".to_date
end

Expand Down

0 comments on commit 80222e1

Please sign in to comment.