如何将 JDBC:Oracle Date 转换为 Ruby 的 DateTime?
我终于弄清楚如何通过 JDBC 和 Sequel 将我的 Sinatra 应用程序连接到现有的 Oracle 数据库。
现在我的问题是我想将 Oracle DB 中的日期字段显示为带有时间的日期。
Oracle 的快速日期定义:
有效日期范围从 4712 年 1 月 1 日起 公元前至公元9999年12月31日。这 默认格式已确定 明确由 NLS_DATE_FORMAT 参数或隐式由 NLS_TERRITORY 参数。尺寸为 固定为 7 字节。该数据类型 包含日期时间字段 YEAR, 月、日、小时、分钟和秒。 它没有小数秒或 时区。
在 Ruby 中,我想这样做:
row_added.strftime('%d.%B %Y %H:%M:%S') => 09.May 2011 00:00:00
但是 Ruby Date 没有小时或分钟。
相反,我必须在 SQL 级别执行此操作才能查看时间:
to_char(row_added, 'DD.MM.YY HH24:MI:ss') => 09.05.11 08:33:31
它按预期工作,但不应该是最佳解决方案。
谁能告诉我如何让 Ruby Sequel 将其转换为时间或日期时间值而不是日期?
I finally figured it out how to connect my Sinatra app via JDBC and Sequel to an existing Oracle database.
Now my problem is that I would like to show the Date fields from the Oracle DB as Date with time.
Quick date definition from Oracle:
Valid date range from January 1, 4712
BC to December 31, 9999 AD. The
default format is determined
explicitly by the NLS_DATE_FORMAT
parameter or implicitly by the
NLS_TERRITORY parameter. The size is
fixed at 7 bytes. This datatype
contains the datetime fields YEAR,
MONTH, DAY, HOUR, MINUTE, and SECOND.
It does not have fractional seconds or
a time zone.
In Ruby I would like to do this:
row_added.strftime('%d.%B %Y %H:%M:%S') => 09.May 2011 00:00:00
But a Ruby Date does not have hours or minutes.
Instead I have to do this at the SQL level to see the time:
to_char(row_added, 'DD.MM.YY HH24:MI:ss') => 09.05.11 08:33:31
It is working as expected, but should not be the best solution for this.
Can anyone tell me how I can get Ruby Sequel to convert this to a Time or DateTime value and not Date?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Sequel 将
Java::JavaSQL::Timestamp
和Java::JavaSQL::Time
类转换为 rubyTime/DateTim
和Java ::JavaSQL::Date
类到 rubyDate
(https://github.com/jeremyevans/sequel/blob/master/lib/sequel/adapters/jdbc.rb#L579)。如果Oracle使用Java::JavaSQL::Date
的子类但包含时间信息,那么他们就做错了,他们应该子类化Java::JavaSQL::Timestamp
。无论如何,要解决此问题,您需要将
Dataset#convert_type
方法添加到jdbc/oracle 子适配器
(https://github.com/jeremyevans/sequel/blob/master/lib/sequel/adapters/jdbc /oracle.rb)并处理 Oracle 特定日期类型。如果您可以正常工作,请提交补丁。Sequel converts
Java::JavaSQL::Timestamp
andJava::JavaSQL::Time
classes to rubyTime/DateTim
, andJava::JavaSQL::Date
classes to rubyDate
(https://github.com/jeremyevans/sequel/blob/master/lib/sequel/adapters/jdbc.rb#L579). If Oracle uses subclass ofJava::JavaSQL::Date
but includes time information, they are doing it wrong, they should be subclassingJava::JavaSQL::Timestamp
.Anyway, to fix this, you'd need to add the
Dataset#convert_type
method to thejdbc/oracle subadapter
(https://github.com/jeremyevans/sequel/blob/master/lib/sequel/adapters/jdbc/oracle.rb) and handle whatever the Oracle specific date type is. Please submit a patch if you get it working correctly.尝试这个方法: strptime
Try this method: strptime