G
gaxjyxq
Guest
Two functions can convert filetime to date, but seem the result is not same, which right?
1.
Public Function FileTimeToLong(ByVal ft As ComTypes.FILETIME) As Long
Dim tmp As Long
tmp = (CLng(ft.dwHighDateTime) << 32) + ft.dwLowDateTime
Return tmp
End Function
Public Function FileTimeToDateTime(ByVal ft As ComTypes.FILETIME) As DateTime
Dim dt As DateTime = DateTime.MaxValue
Dim tmp As Long = FileTimeToLong(ft)
dt = DateTime.FromFileTime(tmp)
Return dt
End Function
2.
<StructLayout(LayoutKind.Sequential)>
Private Structure SYSTEMTIME
Public wYear As Int16
Public wMonth As Int16
Public wDayOfWeek As Int16
Public wDay As Int16
Public wHour As Int16
Public wMinute As Int16
Public wSecond As Int16
Public wMilliseconds As Int16
End Structure
<DllImport("kernel32.dll")>
Private Function SystemTimeToTzSpecificLocalTime(ByVal lpTimeZoneInformation As IntPtr, ByRef lpUniversalTime As SYSTEMTIME, ByRef lpLocalTime As SYSTEMTIME) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
<DllImport("kernel32.dll")>
Private Function FileTimeToSystemTime(ByRef lpFileTime As ComTypes.FILETIME, ByRef lpSystemTime As SYSTEMTIME) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
Public function FileTimeToLocalTime(ByVal ft As ComTypes.FILETIME) As DateTime
Dim st As New SYSTEMTIME
If FileTimeToSystemTime(ft, st) Then
Dim cst As New SYSTEMTIME
If SystemTimeToTzSpecificLocalTime(IntPtr.Zero, st, cst) Then
Return (New Date(cst.wYear, cst.wMonth, st.wDay, cst.wHour, cst.wMinute, cst.wSecond))
End If
End If
End Function
Continue reading...
1.
Public Function FileTimeToLong(ByVal ft As ComTypes.FILETIME) As Long
Dim tmp As Long
tmp = (CLng(ft.dwHighDateTime) << 32) + ft.dwLowDateTime
Return tmp
End Function
Public Function FileTimeToDateTime(ByVal ft As ComTypes.FILETIME) As DateTime
Dim dt As DateTime = DateTime.MaxValue
Dim tmp As Long = FileTimeToLong(ft)
dt = DateTime.FromFileTime(tmp)
Return dt
End Function
2.
<StructLayout(LayoutKind.Sequential)>
Private Structure SYSTEMTIME
Public wYear As Int16
Public wMonth As Int16
Public wDayOfWeek As Int16
Public wDay As Int16
Public wHour As Int16
Public wMinute As Int16
Public wSecond As Int16
Public wMilliseconds As Int16
End Structure
<DllImport("kernel32.dll")>
Private Function SystemTimeToTzSpecificLocalTime(ByVal lpTimeZoneInformation As IntPtr, ByRef lpUniversalTime As SYSTEMTIME, ByRef lpLocalTime As SYSTEMTIME) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
<DllImport("kernel32.dll")>
Private Function FileTimeToSystemTime(ByRef lpFileTime As ComTypes.FILETIME, ByRef lpSystemTime As SYSTEMTIME) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
Public function FileTimeToLocalTime(ByVal ft As ComTypes.FILETIME) As DateTime
Dim st As New SYSTEMTIME
If FileTimeToSystemTime(ft, st) Then
Dim cst As New SYSTEMTIME
If SystemTimeToTzSpecificLocalTime(IntPtr.Zero, st, cst) Then
Return (New Date(cst.wYear, cst.wMonth, st.wDay, cst.wHour, cst.wMinute, cst.wSecond))
End If
End If
End Function
Continue reading...