2
MSDN FILETIME定義爲表示自1601年1月1,(UTC)的100毫微秒的時間間隔 數如何轉換FILETIME到Ruby時間,反之亦然
64位的值。
如何轉換FILETIME和Ruby Time?
MSDN FILETIME定義爲表示自1601年1月1,(UTC)的100毫微秒的時間間隔 數如何轉換FILETIME到Ruby時間,反之亦然
64位的值。
如何轉換FILETIME和Ruby Time?
class Time
# Convert the time to the FILETIME format, a 64-bit value representing the
# number of 100-nanosecond intervals since January 1, 1601 (UTC).
def wtime
self.to_i * 10000000 + (self.tv_nsec/100.0).round + 116444736000000000
end
# Create a time object from the FILETIME format, a 64-bit value representing
# the number of 100-nanosecond intervals since January 1, 1601 (UTC).
def self.from_wtime(wtime)
Time.at((wtime - 116444736000000000)/10000000,
((wtime - 116444736000000000) % 10000000)/10.0)
end
end
來源:Christopher Sexton,與我修復納秒/ 100的精度。
用法:基於您的代碼
wtime = 131172192735329876
#=> 131172192735329876
t = Time.from_wtime(wtime)
#=> 2016-09-01 12:01:13 -0400
t.wtime
#=> 131172192735329876
t.wtime == wtime
#=> true
Time.now.wtime
#=> 131520319622603520
,但略短:
require 'time'
class Time
def self.from_wtime(wtime)
Time.utc(1601) + wtime.quo(10_000_000)
end
def wtime
((to_r - Time.utc(1601).to_r) * 10_000_000).to_i
end
end
謝謝你的提及,這是一個驚喜,而我一直在尋找通過新的問題。 – csexton
@csexton哈哈..小小的世界!我實際上在這裏搜索你提及你,但搜索你的名字並沒有找到你。謝謝你剪的!我需要更多的精度,所以我想我應該分享我的編輯。乾杯! – Rado