mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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
This commit is contained in:
parent
7093c9aaab
commit
6d688aa20c
4 changed files with 32 additions and 0 deletions
|
@ -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 @@ 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
|
||||
|
|
Loading…
Reference in a new issue