package timeid import ( "math" "time" ) /* (1) Generates the current time id * * @wsize Window Size in seconds * * @return id Current time id * @return parity Current time parity * ---------------------------------------------------------*/ func Generate(wsize float64) (uint32, uint32){ /* (1) If wsize is 0 (div by zero possible error) */ if wsize == 0 { return 0, 0 } /* (2) Get current timestamp */ timestamp := float64( time.Now().Unix() ); /* (3) Calculate the time id */ var id = uint32( timestamp / wsize ); /* (4) Calculate parity */ var parity = id % 2; return id, parity; } /* (2) Try to guess a previous time id from its parity * * @wsize Window Size in seconds * @parity Received parity * * @return id The guessed time id * ---------------------------------------------------------*/ func Guess(wsize float64, parity uint32) uint32{ /* (1) Get current time id */ var idNow, parityNow = Generate(wsize); /* (2) Update ID with tidNow parity difference */ return idNow - uint32(math.Abs( float64(parityNow) - float64(parity) )); }