How to serialize a DateTime object (for example, using BinaryWriter) and save its full state?
I had the impression that the time of the date was represented only by an internal long integer and that this integer was available as the Ticks property for DateTime. However, looking at the implementation, the Ticks property actually returns a subset of the actual internal data that is stored in ulong called dateData
Ticks (which just receives InternalTicks) is implemented as follows:
public long InternalTicks { get { return (long) this.dateData & 4611686018427387903L; } }
As far as I can see, this means that dateData may contain information that is not displayed using the Ticks property.
Even stranger, BinaryFormatter Serializing DateTime does this in GetObjectData ():
info.AddValue("ticks", this.InternalTicks); info.AddValue("dateData", this.dateData);
This will give two threads in a thread, where one of them will be easily restored from the other!
How can I serialize my DateTime without risk of losing any internal state (preferably, of course, in just 8 bytes and without reflection). I think maybe it can be thrown (insecurely), right in the oolong?
Or am I worried for no reason, will the Ticks property encode all the necessary state?
source share