Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

String.to_datetime doesn't take into account timezone or second fractions

#864

The method ActiveSupport::CoreExtensions::String::Conversions::to_datetime (in file activesupport/core_ext/string/conversions.rb) doesn't take into account timezone or second fractions in some standard date formats.

For example, a standard XML Schema dateTime representation isn't parsed correctly:



>> s='2008-08-04T13:10:42.312+03:00'

=> "2008-08-04T13:10:42.312+03:00"

>> d=s.to_datetime

=> Mon, 04 Aug 2008 13:10:42 +0000

>> d.strftime('%FT%T.%L%:z')

=> "2008-08-04T13:10:42.000+00:00"

I've encountered this problem when using the soap4r library with Rails. soap4r looks for a to_datetime method as a first option to parse dates in SOAP responses (presumably since Ruby 1.9 supports such methods, I'm not very familiar with 1.9). Since Rails provides this for the String class, soap4r uses the Rails implementation, which loses information and provides incorrect dates.

Note that DateTime.parse does perform the correct parsing. I'm not sure why the Rails implementation discards the fraction and timezone information.

Reported by Yair Halevi (Spock) · August 19th, 2008 @ 11:50 PM

State: resolved
Milestone: 2.x
Assigned to: Geoff Buesing Geoff Buesing
Importance: none

Activity

  1. josh
    josh
    • Assigned user set to Geoff Buesing
    • Tag set to activesupport, bug, core_ext

    November 22nd, 2008 @ 06:56 PM

  2. Geoff Buesing
    Geoff Buesing
    • State changed from new to open

    Sounds reasonable to have to_datetime recognize second fractions and numeric zone offsets -- I'd accept a patch for this.

    November 24th, 2008 @ 03:20 PM

  3. Geoff Buesing
    Geoff Buesing
    • State changed from open to stale

    Please reopen if you have a patch for this

    February 8th, 2009 @ 06:24 PM

  4. Jason Frey
    Jason Frey
    • Tag changed from activesupport, bug, core_ext to activesupport, bug, core_ext, patch

    Added timezone and fractional seconds support to String-Date conversions.

    June 5th, 2009 @ 06:58 PM

  5. Repository
  6. bingbing