Skip to content

Commit

Permalink
String #to_time and #to_datetime: handle fractional seconds [#864 sta…
Browse files Browse the repository at this point in the history
…te:resolved]
  • Loading branch information
Fryguy authored and gbuesing committed Jun 8, 2009
1 parent 04eb2b7 commit 1d93464
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
2 changes: 2 additions & 0 deletions activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*Edge*

* String #to_time and #to_datetime: handle fractional seconds #864 [Jason Frey]

* Update bundled TZInfo to v0.3.13 [Geoff Buesing]

* Allow MemCacheStore to be initialized with a MemCache-like object instead of addresses and options [Bryan Helmkamp]
Expand Down
Expand Up @@ -9,14 +9,18 @@ def ord

# Form can be either :utc (default) or :local.
def to_time(form = :utc)
::Time.send("#{form}_time", *::Date._parse(self, false).values_at(:year, :mon, :mday, :hour, :min, :sec).map { |arg| arg || 0 })
d = ::Date._parse(self, false).values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction).map { |arg| arg || 0 }
d[6] *= 1000000
::Time.send("#{form}_time", *d)
end

def to_date
::Date.new(*::Date._parse(self, false).values_at(:year, :mon, :mday))
end

def to_datetime
::DateTime.civil(*::Date._parse(self, false).values_at(:year, :mon, :mday, :hour, :min, :sec).map { |arg| arg || 0 })
d = ::Date._parse(self, false).values_at(:year, :mon, :mday, :hour, :min, :sec, :zone, :sec_fraction).map { |arg| arg || 0 }
d[5] += d.pop
::DateTime.civil(*d)
end
end
3 changes: 3 additions & 0 deletions activesupport/test/core_ext/string_ext_test.rb
Expand Up @@ -112,6 +112,8 @@ def test_ord
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 Time.utc(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time
assert_equal Time.local(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time(:local)
assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time
assert_equal Time.local_time(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time(:local)
end
Expand All @@ -120,6 +122,7 @@ def test_string_to_datetime
assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_datetime
assert_equal 0, "2039-02-27 23:50".to_datetime.offset # use UTC offset
assert_equal ::Date::ITALY, "2039-02-27 23:50".to_datetime.start # use Ruby's default start value
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
end

def test_string_to_date
Expand Down

3 comments on commit 1d93464

@jenseng
Copy link
Contributor

@jenseng jenseng commented on 1d93464 Nov 3, 2009

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@jenseng
Copy link
Contributor

@jenseng jenseng commented on 1d93464 Nov 3, 2009

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@jenseng
Copy link
Contributor

@jenseng jenseng commented on 1d93464 Nov 3, 2009

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.