Skip to content
This repository
Browse code

DateTime#to_time gives hour/minute/second resolution. Closes #5747.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4718 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 52c9ad4c9828504ff7859a9dc7ee33d3bdd532c4 1 parent c3cdd3b
Jeremy Kemper jeremy authored
2  activesupport/CHANGELOG
... ... @@ -1,5 +1,7 @@
1 1 *SVN*
2 2
  3 +* DateTime#to_time gives hour/minute/second resolution. #5747 [jon.evans@pobox.com]
  4 +
3 5 * attr_internal to support namespacing and deprecation. Like attr_* except backed by internally-named instance variable. Set attr_internal_naming_format to change the format from the default '@_%s'. [Jeremy Kemper]
4 6 # def foo() @foo__rofl end
5 7 # def foo=(v) @foo__rofl = v end
12 activesupport/lib/active_support/core_ext/date/conversions.rb
@@ -7,14 +7,14 @@ module Conversions
7 7 :short => "%e %b",
8 8 :long => "%B %e, %Y"
9 9 }
10   -
  10 +
11 11 def self.included(klass) #:nodoc:
12 12 klass.send(:alias_method, :to_default_s, :to_s)
13 13 klass.send(:alias_method, :to_s, :to_formatted_s)
14 14 end
15   -
  15 +
16 16 def to_formatted_s(format = :default)
17   - DATE_FORMATS[format] ? strftime(DATE_FORMATS[format]).strip : to_default_s
  17 + DATE_FORMATS[format] ? strftime(DATE_FORMATS[format]).strip : to_default_s
18 18 end
19 19
20 20 # To be able to keep Dates and Times interchangeable on conversions
@@ -23,7 +23,11 @@ def to_date
23 23 end
24 24
25 25 def to_time(form = :local)
26   - ::Time.send(form, year, month, day)
  26 + if respond_to?(:hour)
  27 + ::Time.send(form, year, month, day, hour, min, sec)
  28 + else
  29 + ::Time.send(form, year, month, day)
  30 + end
27 31 end
28 32
29 33 def xmlschema
4 activesupport/test/core_ext/date_ext_test.rb
@@ -10,6 +10,10 @@ def test_to_time
10 10 assert_equal Time.local(2005, 2, 21), Date.new(2005, 2, 21).to_time
11 11 end
12 12
  13 + def test_to_time_on_datetime
  14 + assert_equal Time.local(2005, 2, 21, 10, 11, 12), DateTime.new(2005, 2, 21, 10, 11, 12).to_time
  15 + end
  16 +
13 17 def test_to_date
14 18 assert_equal Date.new(2005, 2, 21), Date.new(2005, 2, 21).to_date
15 19 end

0 comments on commit 52c9ad4

Please sign in to comment.
Something went wrong with that request. Please try again.