VB/OLE DATE conversion in .NET CF 1.0
For a .NET CF 1.0 program I'm working I have to use a VB/OLE Automation DATE from C#. The .NET methods FromOADate and ToOADate are not available on .NET CF 1.0 so I ported the VB.NET implementation of FromOADate and ToOADate to C#
public static System.DateTime OleDateToDateTime(double date)
{
const Int32 hex1 = 86400000; //0x5265c00
const Int64 hex2 = 59926435200000; //0x3680b5e1fc00
const Int64 hex3 = 315537897600000; //0x11efae44cb400
const Int32 hex4 = 10000; //0x2710
const double real1 = 86400000;
const double real2 = 2958466;
const double real3 = -657435;
if (date >= real2 || date <= real3)
{
throw new ArgumentException("Arg_OleAutDateInvalid");
}
double toAdd = date >= 0 ? 0.5 : -0.5;
long loc0 = (long) (date * real1 + toAdd);
if (loc0 < 0)
{
loc0 -= (2 * getReminder(loc0, hex1));
}
loc0 += hex2;
if (loc0 < 0 || loc0 >= hex3)
{
throw new ArgumentException("Arg_OleAutDateScale");
}
return new DateTime(loc0 * hex4);
}
public static double DateTimeToOleDate(System.DateTime date)
{
const Int32 hex1 = 86400000; // '0x5265c00
const Int64 hex2 = 599264352000000000; // '0x85103c0cb83c000
const Int64 hex3 = 31241376000000000; // '0x6efdddaec64000
const Int32 hex4 = 10000; // '0x2710
const Int64 hex5 = 864000000000; // '0xc92a69c000
const double real1 = 86400000;
long ticks = date.Ticks;
if (0 == ticks)
{
return 0.0;
}
if (ticks < hex5)
{
ticks += hex2;
}
if (ticks < hex3)
{
throw new ArgumentException("Arg_OleAutDateInvalid");
}
long loc1;
long loc0 = (long) ((ticks - hex2) / hex4);
if (loc0 < 0)
{
loc1 = getReminder(loc0, hex1);
if (0 != loc1)
{
loc0 -= (hex1 + loc1) * (2);
}
}
return ((double) loc0) / real1;
}
private static long getReminder(long value1, long value2)
{
long res;
res = value1 - value2 * (value1 / value2);
}
No comments:
Post a Comment