Permalink
Browse files

Added String#to_time and String#to_date for wrapping ParseDate

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@824 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 7093c9a commit 6d688aa20c1075aa37134c2bfa9d7eae75f90437 @dhh dhh committed Mar 2, 2005
View
@@ -1,3 +1,8 @@
+*SVN*
+
+* Added String#to_time and String#to_date for wrapping ParseDate
+
+
*1.0.0* (24th February, 2005)
* Added TimeZone as the first of a number of value objects that among others Active Record can use rich value objects using composed_of #688 [Jamis Buck]
@@ -1,5 +1,7 @@
require File.dirname(__FILE__) + '/string/inflections'
+require File.dirname(__FILE__) + '/string/conversions'
class String #:nodoc:
include ActiveSupport::CoreExtensions::String::Inflections
+ include ActiveSupport::CoreExtensions::String::Conversions
end
@@ -0,0 +1,19 @@
+require 'parsedate'
+
+module ActiveSupport #:nodoc:
+ module CoreExtensions #:nodoc:
+ module String #:nodoc:
+ # Converting strings to other objects
+ module Conversions
+ # Form can be either :utc (default) or :local.
+ def to_time(form = :utc)
+ ::Time.send(form, *ParseDate.parsedate(self))
+ end
+
+ def to_date
+ ::Date.new(*ParseDate.parsedate(self)[0..2])
+ end
+ end
+ end
+ end
+end
@@ -61,4 +61,10 @@ def test_classify
assert_equal(class_name, table_name.classify)
end
end
+
+ def test_string_to_time
+ assert_equal Time.utc(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time
+ assert_equal Time.local(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time(:local)
+ assert_equal Date.new(2005, 2, 27), "2005-02-27".to_date
+ end
end

0 comments on commit 6d688aa

Please sign in to comment.