From 6d688aa20c1075aa37134c2bfa9d7eae75f90437 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 2 Mar 2005 13:07:02 +0000 Subject: 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 --- activesupport/CHANGELOG | 5 +++++ activesupport/lib/active_support/core_ext/string.rb | 2 ++ .../lib/active_support/core_ext/string/conversions.rb | 19 +++++++++++++++++++ activesupport/test/core_ext/string_ext_test.rb | 6 ++++++ 4 files changed, 32 insertions(+) create mode 100644 activesupport/lib/active_support/core_ext/string/conversions.rb diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 01f42c9aab..8e6f05884d 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -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] diff --git a/activesupport/lib/active_support/core_ext/string.rb b/activesupport/lib/active_support/core_ext/string.rb index 6d554b483c..3056f74744 100644 --- a/activesupport/lib/active_support/core_ext/string.rb +++ b/activesupport/lib/active_support/core_ext/string.rb @@ -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 diff --git a/activesupport/lib/active_support/core_ext/string/conversions.rb b/activesupport/lib/active_support/core_ext/string/conversions.rb new file mode 100644 index 0000000000..cfb767d1f9 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/string/conversions.rb @@ -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 \ No newline at end of file diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index 74a4010f91..f394509dd1 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -61,4 +61,10 @@ class StringInflectionsTest < Test::Unit::TestCase 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 -- cgit v1.2.3