This project is read-only.

ToDateTime (FILETIME) extension method does not convert time correctly (fix included)


There is an issue with a method
public static DateTime ToDateTime(this System.Runtime.InteropServices.ComTypes.FILETIME ft) in
remote devices line:549.

The method does not convert back to correct local time.

The following operation is incorrect:

long hFT2 = (((long)ft.dwHighDateTime) << 32) + ft.dwLowDateTime;

and should be converted to the following:
long hFT2 = (((long)ft.dwHighDateTime) << 32) | (long)ft.dwLowDateTime;

As the bottom part of the FILETIME could be a negative value it will subtract bottom part rather then add to it; need to use bitwise or.

Here is a minimum program to demonstrate the issue.
     DateTime dt1 = new DateTime(2012,7,22,14,58,23);

      var ft1 = dt1.ToFILETIME();

      var dt2 = ft1.ToDateTime();

      if (dt1 != dt2) {
                    throw new Exception("Extension methods FILETIME/DateTime faulty!");