Ruby Soap 日期时间解析错误

发布于 2024-12-08 19:32:19 字数 776 浏览 1 评论 0原文

我正在尝试转换从肥皂服务返回的日期时间,如下所示:“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼趣 2024-12-15 19:32:19

我无法复制这个问题。该方法调用和定义的范围是什么?

如果没有更多信息,几乎不可能判断您的问题是什么,但请参阅下面的猜测。

$ irb
ruby-1.9.2-p180 :001 > require 'date'
 => true 

ruby-1.9.2-p180 :002 > def parse_date(datestring)
ruby-1.9.2-p180 :003?>   formattedDateTime = DateTime.strptime("2011-09-30T11:25:56-05:00", "%Y-%m-%dT%I:%M:%S%z")
ruby-1.9.2-p180 :004?>   dt = formattedDateTime.strftime("%Y-%m-%d %H:%M:%S")
ruby-1.9.2-p180 :005?>   
ruby-1.9.2-p180 :006 >   return dt
ruby-1.9.2-p180 :007?> end
 => nil 

ruby-1.9.2-p180 :008 > parse_date nil
 => "2011-09-30 11:25:56" 

ruby-1.9.2-p180 :009 > def parse_date(datestring)
ruby-1.9.2-p180 :010?>   formattedDateTime = DateTime.strptime(datestring, "%Y-%m-%dT%I:%M:%S%z")
ruby-1.9.2-p180 :011?>   dt = formattedDateTime.strftime("%Y-%m-%d %H:%M:%S")
ruby-1.9.2-p180 :012?>   
ruby-1.9.2-p180 :013 >   return dt
ruby-1.9.2-p180 :014?> end
 => nil 

ruby-1.9.2-p180 :015 > parse_date "2011-09-30T11:25:56-05:00"
 => "2011-09-30 11:25:56" 

ruby-1.9.2-p180 :016 > 

不过,我还是大胆猜测一下。试试这个..

def parse_date (datestring)
  DateTime.parse(datestring).strftime("%Y-%m-%d %H:%M:%S")
end

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.

$ irb
ruby-1.9.2-p180 :001 > require 'date'
 => true 

ruby-1.9.2-p180 :002 > def parse_date(datestring)
ruby-1.9.2-p180 :003?>   formattedDateTime = DateTime.strptime("2011-09-30T11:25:56-05:00", "%Y-%m-%dT%I:%M:%S%z")
ruby-1.9.2-p180 :004?>   dt = formattedDateTime.strftime("%Y-%m-%d %H:%M:%S")
ruby-1.9.2-p180 :005?>   
ruby-1.9.2-p180 :006 >   return dt
ruby-1.9.2-p180 :007?> end
 => nil 

ruby-1.9.2-p180 :008 > parse_date nil
 => "2011-09-30 11:25:56" 

ruby-1.9.2-p180 :009 > def parse_date(datestring)
ruby-1.9.2-p180 :010?>   formattedDateTime = DateTime.strptime(datestring, "%Y-%m-%dT%I:%M:%S%z")
ruby-1.9.2-p180 :011?>   dt = formattedDateTime.strftime("%Y-%m-%d %H:%M:%S")
ruby-1.9.2-p180 :012?>   
ruby-1.9.2-p180 :013 >   return dt
ruby-1.9.2-p180 :014?> end
 => nil 

ruby-1.9.2-p180 :015 > parse_date "2011-09-30T11:25:56-05:00"
 => "2011-09-30 11:25:56" 

ruby-1.9.2-p180 :016 > 

I would venture a guess, though. Try this..

def parse_date (datestring)
  DateTime.parse(datestring).strftime("%Y-%m-%d %H:%M:%S")
end
毁梦 2024-12-15 19:32:19

我想通了这个问题。我必须调用 to_s 因为我试图解析的日期实际上不是一个字符串。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文