Skip to content
This repository
Browse code

String #to_time and #to_datetime: handle fractional seconds [#864 sta…

…te:resolved]
  • Loading branch information...
commit 1d9346428b3f12a42f717a7b1313cd4375d23e23 1 parent 04eb2b7
Jason Frey authored June 05, 2009 gbuesing committed June 07, 2009
2  activesupport/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *Edge*
2 2
 
  3
+* String #to_time and #to_datetime: handle fractional seconds #864 [Jason Frey]
  4
+
3 5
 * Update bundled TZInfo to v0.3.13 [Geoff Buesing]
4 6
 
5 7
 * Allow MemCacheStore to be initialized with a MemCache-like object instead of addresses and options [Bryan Helmkamp]
8  activesupport/lib/active_support/core_ext/string/conversions.rb
@@ -9,7 +9,9 @@ def ord
9 9
 
10 10
   # Form can be either :utc (default) or :local.
11 11
   def to_time(form = :utc)
12  
-    ::Time.send("#{form}_time", *::Date._parse(self, false).values_at(:year, :mon, :mday, :hour, :min, :sec).map { |arg| arg || 0 })
  12
+    d = ::Date._parse(self, false).values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction).map { |arg| arg || 0 }
  13
+    d[6] *= 1000000
  14
+    ::Time.send("#{form}_time", *d)
13 15
   end
14 16
 
15 17
   def to_date
@@ -17,6 +19,8 @@ def to_date
17 19
   end
18 20
 
19 21
   def to_datetime
20  
-    ::DateTime.civil(*::Date._parse(self, false).values_at(:year, :mon, :mday, :hour, :min, :sec).map { |arg| arg || 0 })
  22
+    d = ::Date._parse(self, false).values_at(:year, :mon, :mday, :hour, :min, :sec, :zone, :sec_fraction).map { |arg| arg || 0 }
  23
+    d[5] += d.pop
  24
+    ::DateTime.civil(*d)
21 25
   end
22 26
 end
3  activesupport/test/core_ext/string_ext_test.rb
@@ -112,6 +112,8 @@ def test_ord
112 112
   def test_string_to_time
113 113
     assert_equal Time.utc(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time
114 114
     assert_equal Time.local(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time(:local)
  115
+    assert_equal Time.utc(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time
  116
+    assert_equal Time.local(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time(:local)
115 117
     assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time
116 118
     assert_equal Time.local_time(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time(:local)
117 119
   end
@@ -120,6 +122,7 @@ def test_string_to_datetime
120 122
     assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_datetime
121 123
     assert_equal 0, "2039-02-27 23:50".to_datetime.offset # use UTC offset
122 124
     assert_equal ::Date::ITALY, "2039-02-27 23:50".to_datetime.start # use Ruby's default start value
  125
+    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
123 126
   end
124 127
   
125 128
   def test_string_to_date

2 notes on commit 1d93464

Jon Jensen

In ruby 1.8.6 and prior, date.rb has a bug wherein fractional seconds above 59 result in an invalid date. Try a "2005-02-27T23:50:59.275038".to_datetime and you'll see what I mean. Should we hack this to ignore fractional seconds above 59 when on broken ruby versions? It's kind of annoying when a method only works 59 times out of 60.

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/23501

Jon Jensen

In ruby 1.8.6 and prior, date.rb has a bug wherein fractional seconds above 59 result in an invalid date. Try a "2005-02-27T23:50:59.275038".to_datetime and you'll see what I mean. Should we hack this to ignore fractional seconds above 59 when on broken ruby versions? It's kind of annoying when a method only works 59 times out of 60.

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/23501

Jon Jensen

gah, sorry for the double post. /me fails at github

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