In protobuf-net, I use a graduated scale approach (and indeed, it handles all this for you if you simply use DateTime
) - the equivalent .proto is something like this:
message DateTime {
optional sint64 value = 1; // the offset (in units of the selected scale)
// from 1970/01/01
optional TimeSpanScale scale = 2 [default = DAYS]; // the scale of the
// timespan
enum TimeSpanScale {
DAYS = 0;
HOURS = 1;
MINUTES = 2;
SECONDS = 3;
MILLISECONDS = 4;
MINMAX = 15; // dubious
}
}
i.e. if the DateTime
can be expressed in whole days, I just send the number of days since 1970, etc - plus a small marker to the scale. This means that dates can be sent a bit more efficiently, but it doesn't really cost much more for other scales.
Personally, I wouldn't use ToBinary()
- I would explicitly use an offset of a known scale from a known epoch (such as the unix epoch). This makes it more portable between platforms. But if you are sending (for example) just the millisecond offset, then a fixed scale would usually be more efficient than a variant-length scale. Whether you need signed or unsigned depends on whether you need dates before your epoch ;-p