将Windows刻度转换为Go中的UNIX时间戳

huangapple go评论98阅读模式
英文:

Converting windows ticks into UNIX timestamp in Go

问题

我正在尝试将Windows Ticks转换为Go的本地time.Time。具体来说,我想将635885625204626270转换为UNIX时间戳。到目前为止,我只能适应一个PHP问题,并且只能得到秒数,但是我现在卡在这里。

ticksInUnix := (635885625204626270 / 10000000) - 60*60*24*365*1970
t := time.Unix(ticksInUnix, 0)
英文:

I am trying to convert Windows Ticks into Go's native time.Time. Specifically, I want to convert 635885625204626270 into an UNIX timestamp. So far I only managed to adapt a PHP question, and could get as far as seconds, however I am now stuck here.

ticksInUnix := (635885625204626270 / 10000000) - 60*60*24*365*1970
t := time.Unix(ticksInUnix, 0)

答案1

得分: 1

尝试了很多方法来适应其他编程语言的答案后,我发现这是最准确的解决方案:

t := time.Unix(0, ((635885625204626270)-60*60*24*365*1970*10000000)*100)

这应该是最精确的解决方案,因为它不对原始的计时数进行除法运算,并且可以达到微秒级的精度(前提是Windows的计时数可以达到那么精确)。

英文:

After trying a lot to adapt answers in other programming languages, I found this to be the most accurate:

t := time.Unix(0, ((635885625204626270)-60*60*24*365*1970*10000000)*100)

It should be the most precise solution, as it does not perform divisions on the original tick count, and get to as accurate as microseconds (only if Windows can get that accurate on tick count, that is.)

huangapple
  • 本文由 发表于 2016年3月21日 06:31:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/36120459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定