How to convert array into 64bit Double Mac Absolute Time using Ruby?

╄→гoц情女王★ 提交于 2020-01-15 09:11:28

问题


Is it possible to convert Array having binary or hex values stored as each element into 64bit Double Mac Absolute Time? When I inspect array using p var_bytes console shows following output.

\000\000\000\000\000\000\000\000\000\000\000\234\225x\266A\000\000\000\345\005\230\264

Is it possible to convert above array elements in 64bit Double Mac Absolute Time as a string?

My code is following just a simple do..end

puts "\nClose off the page header#{y.unpack("n")}\n"
            z.scan(/(.{8})(.{8})(.{4})(.{4})(.{4})(.{4})(.{8})(.{8})(.{8})(.*\w)/m).each do |j,k,l,m,n,o,p,q,r,s|
                puts "\nContent1#{j.unpack("n")}\n"
                puts "\nContent2:#{k.unpack("n")}\n"
                puts "\nContent3:#{l.unpack("n")}\n"
                puts "\nContent4:#{m.unpack("n")}\n"
                puts "\nContent5:#{n.unpack("n")}\n"
                puts "\nContent6:#{o.unpack("n")}\n"
                puts "\nContent7:#{p.unpack("n")}\n"
                expdt = Time.at((q.unpack("L"))[0])
                createdt = Time.at((r.unpack("L"))[0])
                puts "Date1:\n#{expdt}\n"
                puts "\nDate2:\n#{createdt}\n"
                puts "\nCookie:\n"
                puts s.split(/\0/m)
            end
        end

what will the simple way to convert this negative values to positive so Time.at wont give error and then convert it according to MAC Epoch time?


回答1:


Try this:

MAC_EPOCH = Time.gm(2001,1,1)

def bin2time(bin)
    return MAC_EPOCH + (bin.unpack "D")[0]
end

where bin is an 8-byte representation of a double precision float.

you may need to change "D" to "E" or "G" depending on where you are getting your data from. check the unpack docs for details.



来源:https://stackoverflow.com/questions/10052298/how-to-convert-array-into-64bit-double-mac-absolute-time-using-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!