by jminter · 3y ago · 0 comments
To me, "math/big" feels like a big hammer for working on an int64; a nice alternative is "encoding/binary":
func atomSlug(publishedAt time.Time) string {
b := make([]byte, 0, 8)
b = binary.BigEndian.AppendUint64(b, uint64(publishedAt.Unix()))
b = bytes.TrimLeft(b, "\x00")
return lexicographicBase32Encoding.EncodeToString(b)
}Right now the time is converted to 4 bytes which then uses 7 base32 characters to encode. When the time rolls over to 33 bits in 2106, it will increase to 5 bytes encoded as 8 base32 chars, even though 7 base32 chars have enough bits (35) to represent the number.
So perhaps a better way would be something like:
func atomSlug(publishedAt time.Time) string {
b := make([]byte, 0, 8)
b = binary.BigEndian.AppendUint64(b, uint64(publishedAt.Unix()))
s = lexicographicBase32Encoding.EncodeToString(b)
return strings.TrimLeft(s, '2'); // '2' is all zero bits
}
This will stick with 7 base32 chars until you exceed 35 bits in 3058.