2017-10-09 38 views

回答

2
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 
+0

謝謝你的提及,這是一個驚喜,而我一直在尋找通過新的問題。 – csexton

+1

@csexton哈哈..小小的世界!我實際上在這裏搜索你提及你,但搜索你的名字並沒有找到你。謝謝你剪的!我需要更多的精度,所以我想我應該分享我的編輯。乾杯! – Rado

1

,但略短:

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 
相關問題