Search code examples
c#.netbitwise-operatorsoverflowexceptionfiletime

What is the safest way to subtract two System.Runtime.InteropServices.ComTypes.FILETIME objects


I wonder what is the safest way to subtract two System.Runtime.InteropServices.ComTypes.FILETIME objects? I used the following code but sometimes it gives me ArithmaticOverflow exception due to the negative number in Low 32-bit values. I am not sure enclosing the body with unchecked will serve the purpose or not. Please give me some suggestion on how to do it safely without getting any runtime exception or CS0675 warning message.

private static UInt64 SubtractTimes(FILETIME a, FILETIME b)
        {
            UInt64 aInt = ((UInt64)(a.dwHighDateTime << 32)) | (UInt32)a.dwLowDateTime;
            UInt64 bInt = ((UInt64)(b.dwHighDateTime << 32)) | (UInt32)b.dwLowDateTime;
            return aInt - bInt;
        }

Solution

  • You need to use unchecked to suppress the exception:

        public static long FileTime2Long(FILETIME ft) {
            uint low = unchecked((uint)ft.dwLowDateTime);
            return (long)ft.dwHighDateTime << 32 | low;
        }
    
        static void Test() {
            FILETIME ft = new FILETIME();
            ft.dwHighDateTime = 1;
            ft.dwLowDateTime = -1;
            long value = FileTime2Long(ft);
            Debug.Assert(value == 0x1ffffffff);
        }
    

    If desired, you can then convert to DateTime with DateTime.FromFileTimeUtc().