using System; using System.Runtime.InteropServices.ComTypes; namespace Vanara.Extensions { /// Extensions for . public static class FileTimeExtensions { /// Compares two instances of and returns an integer that indicates whether the first instance is earlier than, the same as, or later than the second instance. /// The first object to compare. /// The second object to compare. /// A signed number indicating the relative values of t1 and t2. /// /// Value TypeCondition /// Less than zerot1 is earlier than t2. /// Zerot1 is the same as t2. /// Greater than zerot1 is later than t2. /// /// public static int CompareTo(this FILETIME ft, FILETIME other) { var a = ft.ToUInt64(); var b = other.ToUInt64(); return a > b ? 1 : (a < b ? -1 : 0); } /// Compares two structures for equality. /// The first value. /// The second value. /// true if the current object is equal to the parameter; otherwise, false. public static bool Equals(FILETIME ft1, FILETIME ft2) => ft1.dwHighDateTime == ft2.dwHighDateTime && ft1.dwLowDateTime == ft2.dwLowDateTime; /// Creates a from a 64-bit value. /// The value to be converted. /// The return value is a created from the supplied 64-bit value. public static FILETIME MakeFILETIME(ulong ul) => new FILETIME { dwHighDateTime = (int)(ul >> 32), dwLowDateTime = (int)(ul & 0xFFFFFFFF) }; /// Converts a structure to a structure. /// The value to convert. /// The value to use to determine local or UTC time. /// The resulting structure. public static DateTime ToDateTime(this FILETIME ft, DateTimeKind kind = DateTimeKind.Local) { var hFT2 = ft.ToInt64(); return kind == DateTimeKind.Utc ? DateTime.FromFileTimeUtc(hFT2) : DateTime.FromFileTime(hFT2); } /// Converts a structure to a structure using the local time. /// The value to convert. /// The resulting structure as the local time. public static FILETIME ToFileTimeStruct(this DateTime dt) => MakeFILETIME(unchecked((ulong)dt.ToFileTimeUtc())); /// Converts a structure to a structure. /// The value to convert. /// The resulting structure as a time span. public static FILETIME ToFileTimeStruct(this TimeSpan ts) => MakeFILETIME(unchecked((ulong)-ts.Ticks)); /// Converts a structure to its signed 64-bit representation. /// The value to be converted. /// The return value is a signed 64-bit value that represented the . public static long ToInt64(this FILETIME ft) => ((long)ft.dwHighDateTime << 32) | (uint)ft.dwLowDateTime; /// Returns a that represents the instance. /// The to convert. /// A standard or custom date and time format string. See notes for DateTime.ToString(). /// An object that supplies culture-specific formatting information. /// /// A string representation of value of the current object as specified by and . /// public static string ToString(this FILETIME ft, string format, IFormatProvider provider = null) => ft.ToInt64() < 0 ? ft.ToTimeSpan().ToString() : ft.ToDateTime().ToString(format, provider); /// Converts a structure to a structure. /// The value to convert. /// The resulting structure. public static TimeSpan ToTimeSpan(this FILETIME ft) => new TimeSpan(ft.ToInt64()); /// Converts a structure to its 64-bit representation. /// The value to be converted. /// The return value is a 64-bit value that represented the . public static ulong ToUInt64(this FILETIME ft) => ((ulong)ft.dwHighDateTime << 32) | (uint)ft.dwLowDateTime; } }