You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Golang中time.Time转Deciseconds生成错误时间戳求助

Fixing Time to Deciseconds Conversion in Go

Let's break down what's going wrong with your code and how to fix it.

The Core Issue: Incorrect Deciseconds Calculation

First, let's clarify: deciseconds are tenths of a second (1 decisecond = 0.1s = 100ms). Your JavaScript code correctly calculates this by taking milliseconds (new Date().getTime()) and dividing by 100.

But in your Go code, you're doing this:

deciseconds := int32(t.Unix() / 1000)

t.Unix() returns the number of seconds since the epoch. Dividing that by 1000 gives you thousandths of a second—way smaller than the deciseconds value you need. That's why you're getting a timestamp from early 1970: your deciseconds value is tiny.

Correct Deciseconds Calculation

You have a few valid ways to compute deciseconds in Go, depending on your Go version:

Option 1 (Go 1.17+): Use UnixMilli()

This is the cleanest approach since it directly gives you milliseconds, which you can divide by 10 to get deciseconds:

func (b bit) setDateToDeciseconds(startInclusive int, size int, t time.Time) {
    // Convert milliseconds to deciseconds (1 ds = 100ms)
    deciseconds := int32(t.UnixMilli() / 10)
    b.setNumberInt32(startInclusive, size, deciseconds)
}

Option 2 (Older Go Versions): Use UnixNano()

If you're stuck on a Go version before 1.17, calculate using nanoseconds:

func (b bit) setDateToDeciseconds(startInclusive int, size int, t time.Time) {
    // 1 decisecond = 100,000,000 nanoseconds
    deciseconds := int32(t.UnixNano() / 100_000_000)
    b.setNumberInt32(startInclusive, size, deciseconds)
}

Option 3: Combine Unix() and Nanoseconds

You can also compute it by combining full seconds and the remaining nanoseconds:

func (b bit) setDateToDeciseconds(startInclusive int, size int, t time.Time) {
    fullSeconds := t.Unix()
    // Convert remaining nanoseconds to deciseconds
    nanoToDs := int64(t.Nanosecond()) / 100_000_000
    deciseconds := int32(fullSeconds*10 + nanoToDs)
    b.setNumberInt32(startInclusive, size, deciseconds)
}

Verify Alignment with ReadTime

Your ReadTime function assumes ds is the total number of deciseconds since the epoch:

return time.Unix(ds/dsPerS, (ds%dsPerS)*nsPerDs).UTC()

Where dsPerS should be 10 (10 deciseconds per second) and nsPerDs should be 100_000_000 (nanoseconds per decisecond). The corrected deciseconds calculation above matches this logic perfectly, so the round-trip (encode → decode) will now work correctly.

Test the Fix

After updating the calculation, your timestamp should no longer be stuck in 1970. For example, if you pass time.Now() to setDateToDeciseconds, the decoded time from ReadTime should match the original time (with precision rounded to deciseconds, as expected).

内容的提问来源于stack exchange,提问作者reticentroot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:24:56