Search code examples
rubyparsingdatetimestrptime

Ruby Soap datetime parsing error


I am trying to convert a datetime that is returning back from a soap service which looks like this: "2011-09-30T11:25:56-05:00".

I want to parse it to this format "2011-09-30 11:25:56"

When I hard code the datestring in my ruby code, it works:

def parse_date(datestring)
    formattedDateTime = DateTime.strptime("2011-09-30T11:25:56-05:00", "%Y-%m-%dT%I:%M:%S%z")
    dt = formattedDateTime.strftime("%Y-%m-%d %H:%M:%S")

    return dt
end

This example works when I hard code the datestring in. However, the below example will not work. The datestring that it is using is "2011-09-30T11:25:56-05:00", which is the exact same as I am hardcoding in the above example.

def parse_date(datestring)
    formattedDateTime = DateTime.strptime(datestring, "%Y-%m-%dT%I:%M:%S%z")
    dt = formattedDateTime.strftime("%Y-%m-%d %H:%M:%S")

    return dt
end

This way it throws this error: [01:29:06 PM 2011-10-09] SourceAdapter raised query exception: private method `sub!' called for #

Can anyone please let me know what is going on?


Solution

  • I figured out the issue. I had to call to_s because the date that I was trying to parse was really not a string.

    def parse_date(dateobject)
    
        tempdatestring = dateobject.to_s
    
        formattedDateTime = DateTime.strptime(tempdatestring, "%Y-%m-%dT%H:%M:%S%z")
        dt = formattedDateTime.strftime("%Y-%m-%d %H:%M:%S")
    
        return dt
    end