none
What different for two functions? RRS feed

  • Question

  • 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

    Saturday, February 22, 2020 11:49 AM

Answers

  • Sorry, i found the values of two functions are same, so they are same...
    • Marked as answer by gaxjyxq Saturday, February 22, 2020 12:33 PM
    Saturday, February 22, 2020 12:33 PM

All replies

  • Hello,

    Have you looked at built in functions rather than use API

    DateTime.FromFileTime(Integer), DateTimeFromFileTimeUtc(Integer), DateTimeToLocalTime etc


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, February 22, 2020 12:15 PM
    Moderator
  • Sorry, i found the values of two functions are same, so they are same...
    • Marked as answer by gaxjyxq Saturday, February 22, 2020 12:33 PM
    Saturday, February 22, 2020 12:33 PM