Can anyone help explain the difference between DateTime.ToBinary() and DateTime.ToFileTime()? As far as I can tell they seem to always return the same value (when dealing with UTC times at least). The same applies to DateTime.FromBinary() and DateTime.FromFileTime().
I've tried using Reflector and I can see some differences, I just don't understand the relevance of the magic numbers:
public long ToBinary()
{
if (this.Kind != DateTimeKind.Local)
{
return (long) this.dateData;
}
TimeSpan utcOffset = TimeZoneInfo.Local.GetUtcOffset(this, TimeZoneInfoOptions.NoThrowOnInvalidTime);
long num2 = this.Ticks - utcOffset.Ticks;
if (num2 < 0L)
{
num2 = 0x4000000000000000L + num2;
}
return (num2 | -9223372036854775808L);
}
public long ToFileTime()
{
return this.ToUniversalTime().ToFileTimeUtc();
}
public long ToFileTimeUtc()
{
long num = ((this.InternalKind & 9223372036854775808L) != 0L) ? this.ToUniversalTime().InternalTicks : this.InternalTicks;
num -= 0x701ce1722770000L;
if (num < 0L)
{
throw new ArgumentOutOfRangeException(null, Environment.GetResourceString("ArgumentOutOfRange_FileTimeInvalid"));
}
return num;
}
public static DateTime FromFileTime(long fileTime)
{
return FromFileTimeUtc(fileTime).ToLocalTime();
}
public static DateTime FromFileTimeUtc(long fileTime)
{
if ((fileTime < 0L) || (fileTime > 0x24c85a5ed1c03fffL))
{
throw new ArgumentOutOfRangeException("fileTime", Environment.GetResourceString("ArgumentOutOfRange_FileTimeInvalid"));
}
return new DateTime(fileTime + 0x701ce1722770000L, DateTimeKind.Utc);
}
public static DateTime FromBinary(long dateData)
{
long num2;
if ((dateData & -9223372036854775808L) == 0L)
{
return FromBinaryRaw(dateData);
}
long ticks = dateData & 0x3fffffffffffffffL;
if (ticks > 0x3fffff36d5964000L)
{
ticks -= 0x4000000000000000L;
}
bool isAmbiguousLocalDst = false;
if (ticks < 0L)
{
num2 = TimeZoneInfo.Local.GetUtcOffset(MinValue, TimeZoneInfoOptions.NoThrowOnInvalidTime).Ticks;
}
else if (ticks > 0x2bca2875f4373fffL)
{
num2 = TimeZoneInfo.Local.GetUtcOffset(MaxValue, TimeZoneInfoOptions.NoThrowOnInvalidTime).Ticks;
}
else
{
DateTime time = new DateTime(ticks, DateTimeKind.Utc);
bool isDaylightSavings = false;
num2 = TimeZoneInfo.GetUtcOffsetFromUtc(time, TimeZoneInfo.Local, out isDaylightSavings, out isAmbiguousLocalDst).Ticks;
}
ticks += num2;
if (ticks < 0L)
{
ticks += 0xc92a69c000L;
}
if ((ticks < 0L) || (ticks > 0x2bca2875f4373fffL))
{
throw new ArgumentException(Environment.GetResourceString("Argument_DateTimeBadBinaryData"), "dateData");
}
return new DateTime(ticks, DateTimeKind.Local, isAmbiguousLocalDst);
}