1

I'm having trouble converting time.Time to Deciseconds. I basically have to port the below to Golang

Deciseconds fits into 36 bits with enough precision to record a user's consent action timing. Javascript: Math.round((new Date()).getTime()/100)

I've tried using Go / golang time.Now().UnixNano() convert to milliseconds?

to convert to milliseconds then dividing by 100. That produces the wrong time stamp. I bet this is a silly mistake on my part, but I just wanted to get some feed back

This is the method that reads the timestamp i'm trying to submit

// ReadTime reads the next 36 bits representing the epoch time in deciseconds
// and converts it to a time.Time.
func (r *ConsentReader) ReadTime() time.Time {
    var ds = int64(r.ReadBits(36))
    return time.Unix(ds/dsPerS, (ds%dsPerS)*nsPerDs).UTC()
}

https://github.com/LiveRamp/iabconsent/blob/328e761006ce2652bc8c0daee614381d7565c05e/parse.go#L34

I'm setting the date with

func (b bit) setDateToDeciseconds(startInclusive int, size int, t time.Time) {
    deciseconds := int32(t.Unix() / 1000)
    b.setNumberInt32(startInclusive, size, deciseconds)
}


func (b bit) setNumberInt32(startInclusive int, size int, to int32) {
    for i := size - 1; i >= 0; i-- {
        index := startInclusive + i
        byteIndex := index / 8
        shift := uint((byteIndex+1)*8 - index - 1)
        b[byteIndex] |= byte((to % 2) << shift)
        to /= 2
    }
}

I keep getting date times that look like

time.Now() -> 1970-01-02 18:26:11.4 +0000 UTC

reticentroot
  • 3,612
  • 2
  • 22
  • 39
  • 2
    Why deci-seconds? also, this should work: `deciSeconds := int32(time.Now().Unix() / 10)` – Derek Pollard May 30 '18 at 20:55
  • @Derek I'm following a standard https://github.com/InteractiveAdvertisingBureau/GDPR-Transparency-and-Consent-Framework/blob/master/Consent%20string%20and%20vendor%20list%20formats%20v1.1%20Final.md#vendor-consent-string-format – reticentroot May 30 '18 at 20:57
  • 1
    Ahhhh, I see. I edited my comment to include converting to `int32` – Derek Pollard May 30 '18 at 21:01
  • 1
    What did you try from the linked question? UnixNano() / 1e8 should work. – Charlie Tumahai May 30 '18 at 21:15
  • @Derek I've updated my post with more information. Setting time time with `int32(time.Now().Unix() / 10)` is yielding the same results.. maybe i'm doing something else wrong – reticentroot May 30 '18 at 21:15
  • @ThunderCat amended my post Using UnixNano() / 1e8 returns `created 2023-10-20 18:54:04.7 +0000 UTC` – reticentroot May 30 '18 at 21:16
  • So deciseconds := int64(t.UnixNano() / 1e8) works, you have make sure it's an int64 though. Thanks guys. Someone should post the answer so I mark the correct answer and so pass out points – reticentroot May 30 '18 at 21:22

1 Answers1

1

Use these functions to convert a time.Time to and from deciseconds since the epoch.

func toDeci(t time.Time) int64 {
    return t.UnixNano() / 1e8
}

func fromDeci(t int64) time.Time {
    return time.Unix(0, t*1e8)
}

playground example

You can replace 1e8 with int64(time.Second/10) for a possible improvement in readability.

Charlie Tumahai
  • 113,709
  • 12
  • 249
  • 242