I need to serialize a .NET DateTime value in a protocol buffers message.
My plan is to use the DateTime.ToBinary() and then pass the 64-bit returned value in the message. But I’m not sure what to choose as a protocol buffers data type to represent that.
I guess I’m confused about when the fixed64 (or sfixed64) data types should be used.
I’m assuming in this scenario I would use the signed types since the values returned by DateTime.ToBinary() can be negative as well as positive.
Well, you definitely want either
int64orsfixed64to cope with the value being signed.Having just done a quick test,
DateTime.Now.ToBinary()is encoded in 10 bytes usingint64whereassfixed64will always use 8 bytes. Basically, the variable length encoding is great for small numbers, but becomes bigger than the fixed encoding for large numbers. (It’s the same kind of tradeoff as using UTF-8 instead of UTF-16 – ASCII characters can be encoded in UTF-8 in a single byte, but later on code points end up being encoded as 2 and then 3 bytes, whereas UTF-16 always uses 2 bytes for characters in the BMP.)My guess is that
DateTime.ToBinary()values are likely to be quite large (without knowing the details of exactly what it does) sosfixed64is more appropriate.Does that make sense?