Ruby Soap 日期时间解析错误
我正在尝试转换从肥皂服务返回的日期时间,如下所示:“2011-09-30T11:25:56-05:00”。
我想将其解析为这种格式“2011-09-30 11:25:56”
当我在 ruby 代码中硬编码日期字符串时,它可以工作:
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
当我硬编码日期字符串时,此示例有效。但是,下面的示例不会起作用。它使用的日期字符串是“2011-09-30T11:25:56-05:00”,这与我在上面的示例中进行硬编码完全相同。
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
这样它就会抛出这个错误: [01:29:06 PM 2011-10-09] SourceAdapter 引发查询异常:私有方法 `sub!'呼吁#
任何人都可以告诉我发生了什么事吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我无法复制这个问题。该方法调用和定义的范围是什么?
如果没有更多信息,几乎不可能判断您的问题是什么,但请参阅下面的猜测。
不过,我还是大胆猜测一下。试试这个..
I can't replicate this problem. What is the scope surrounding this method call and definition?
Without more information, it's nearly impossible to tell what your problem is, but see guess below.
I would venture a guess, though. Try this..
我想通了这个问题。我必须调用 to_s 因为我试图解析的日期实际上不是一个字符串。
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.