使用Ruby或MySql进行CAST(0x00009F0900000000 AS DateTime)

时间:2012-02-29 11:31:04

标签: mysql ruby casting

我正在尝试通过MySql或Rails将此字符串“0x00009F0900000000”转换为日期,因为我正在进行迁移。

无法找到它的格式。看起来没有办法通过rails或mysql将十六进制值转换为日期。

此处的解决方案:how to cast the hexadecimal to varchar(datetime)?

SELECT 
      CAST(
          '1900-01-01 00:00:00' + 
          INTERVAL CAST(CONV(substr(HEX(BinaryData),1,8), 16, 10)  AS SIGNED) DAY +
          INTERVAL CAST(CONV(substr(HEX(BinaryData),9,8), 16, 10)  AS SIGNED)* 10000/3 MICROSECOND
      AS DATETIME) AS converted_datetime
FROM
(
SELECT 0x00009F0900000000 AS BinaryData
) d

2 个答案:

答案 0 :(得分:0)

这是我发现的最接近您问题的内容:

> "0x00009F0900000000".to_i(16)
 => 174861003522048 
> time = "0x00009F0900000000".to_i(16)
 => 174861003522048 


> Time.at(time/1000)
 => 7511-02-16 05:58:42 +0100 


> Time.at(time/1000000)
 => 1975-07-17 21:30:03 +0100 

尝试几个除数,直到得到准确的

答案 1 :(得分:0)

在红宝石中(并忽略时间)

require 'date'
str =  "0x00009F0900000000"
p Date.new(1900,1,1) + str[0..9].hex
#<Date: 2011-06-21 ((2455734j,0s,0n),+0s,2299161j)>