Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. how do I Convert Filetime to datetim

how do I Convert Filetime to datetim

Scheduled Pinned Locked Moved Visual Basic
csharphelpquestion
13 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Dave Kreskowiak

    I haven't tested it, but it should be more like this:

    Private Shared Function SystemTimeToDateTime(ByRef st As SYSTEMTIME) As DateTime
    Dim ft As FILETIME = New FILETIME
    NativeMethods.SystemTimeToFileTime(st, ft)
    Dim dt As DateTime
    dt = New DateTime(((CType(ft.dwHighDateTime, Long)) << 32) Or CType(ft.dwLowDateTime, UInt32))
    Return dt
    End Function

    First, at the end of this equation, convert the value of ft.dwLowDateTime to a UNSIGNED Integer, not a signed integer. This can make all the difference in the world. On top of that, you can't add this value to the front of the equation. All kinds of addition carry overs will screw up the results. You must use a bit-wise OR, just like in the original C# code. But, of course, the results will depend on the correct definition of the FILETIME and SYSTEMTIME structures. Hopefully, you didn't interchange signed integers for unsigned?? Dave Kreskowiak Microsoft MVP - Visual Basic -- modified at 12:39 Thursday 13th April, 2006

    D Offline
    D Offline
    David M J
    wrote on last edited by
    #3

    Thanks for the response. I am taking the filetime from the System.Runtime.InteropServices. I am not defining my own structure. However when I use CType(ft.dwLowDateTime, UInt32) I get the error message that I cannot convert from integer to UInt32 for some reason VB assumes the ft.dwLowDateTime is an integer, as in C# this doesn't seem to be the problem. How can I change that? Cheers David Think, try, think, think, try, think, think, think, try, ASK, think, try, advance on step and start over...

    D 1 Reply Last reply
    0
    • D David M J

      Thanks for the response. I am taking the filetime from the System.Runtime.InteropServices. I am not defining my own structure. However when I use CType(ft.dwLowDateTime, UInt32) I get the error message that I cannot convert from integer to UInt32 for some reason VB assumes the ft.dwLowDateTime is an integer, as in C# this doesn't seem to be the problem. How can I change that? Cheers David Think, try, think, think, try, think, think, think, try, ASK, think, try, advance on step and start over...

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #4

      I finally found the code you're trying to convert by hand. I got this to work:

      Private Declare Auto Function SystemTimeToFileTime Lib "kernel32" ( \_
              <\[In\]()> ByRef st As SYSTEMTIME, \_
              ByRef lpFileTime AS FILETIME) \_
              As Boolean
      

      <StructLayout(LayoutKind.Sequential)> _
      Private Structure SYSTEMTIME
      Public wYear As UInt16
      Public wMonth As UInt16
      Public wDayOfWeek As UInt16
      Public wDay As UInt16
      Public wHour As UInt16
      Public wMinute As UInt16
      Public wSecond As UInt16
      Public wMillisecond As UInt16
      End Structure
      .
      .
      .
      Private Sub Button1_Click(blah, blah) Handles Button1.Click
      Dim st As SYSTEMTIME
      With st
      .wDay = 14
      .wMonth = 4
      .wYear = 3606 ' 2006 + 1600
      .wHour = 14
      .wMinute = 15
      .wSecond = 31
      End With
       
      Dim ft As New FILETIME
      SystemTimeToFileTime(st, ft)
       
      Dim dt As New DateTime(((CType(ft.dwHighDateTime, Long)) << 32) Or _
      CType(ft.dwLowDateTime, UInt32))
      Debug.WriteLine(dt)
      End Sub

      Dave Kreskowiak Microsoft MVP - Visual Basic

      D 1 Reply Last reply
      0
      • D Dave Kreskowiak

        I finally found the code you're trying to convert by hand. I got this to work:

        Private Declare Auto Function SystemTimeToFileTime Lib "kernel32" ( \_
                <\[In\]()> ByRef st As SYSTEMTIME, \_
                ByRef lpFileTime AS FILETIME) \_
                As Boolean
        

        <StructLayout(LayoutKind.Sequential)> _
        Private Structure SYSTEMTIME
        Public wYear As UInt16
        Public wMonth As UInt16
        Public wDayOfWeek As UInt16
        Public wDay As UInt16
        Public wHour As UInt16
        Public wMinute As UInt16
        Public wSecond As UInt16
        Public wMillisecond As UInt16
        End Structure
        .
        .
        .
        Private Sub Button1_Click(blah, blah) Handles Button1.Click
        Dim st As SYSTEMTIME
        With st
        .wDay = 14
        .wMonth = 4
        .wYear = 3606 ' 2006 + 1600
        .wHour = 14
        .wMinute = 15
        .wSecond = 31
        End With
         
        Dim ft As New FILETIME
        SystemTimeToFileTime(st, ft)
         
        Dim dt As New DateTime(((CType(ft.dwHighDateTime, Long)) << 32) Or _
        CType(ft.dwLowDateTime, UInt32))
        Debug.WriteLine(dt)
        End Sub

        Dave Kreskowiak Microsoft MVP - Visual Basic

        D Offline
        D Offline
        David M J
        wrote on last edited by
        #5

        I am getting imediate troubles with .wDay = 14 saying that that cannot be converted to Uint16. Could it be that I need to change one of these option setting. Forgot the name, but as you used to have option strict etc. Be the way thanks for the help, much appreciated. Just one other thing what does << mean can't find that in the help file as it is an illegal entry there. Think, try, think, think, try, think, think, think, try, ASK, think, try, advance on step and start over...

        D 1 Reply Last reply
        0
        • D David M J

          I am getting imediate troubles with .wDay = 14 saying that that cannot be converted to Uint16. Could it be that I need to change one of these option setting. Forgot the name, but as you used to have option strict etc. Be the way thanks for the help, much appreciated. Just one other thing what does << mean can't find that in the help file as it is an illegal entry there. Think, try, think, think, try, think, think, think, try, ASK, think, try, advance on step and start over...

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #6

          I'm using VB 2005, and you're not. That's why the problems... "<<" is a bit-wise shift operator that finally appeared (many years too late!) in 2005. To get the same effect using math, you need to multiply the value by 2^(number of bits to shift left). Sooooo, the same operation can be done like this:

          Dim dt As New DateTime(((CType(ft.dwHighDateTime, Long)) * (2 ^ 32) Or CType(ft.dwLowDateTime, UInt32))

          Because of the math, it's quite a bit slower, but it does the same thing. As for the other problem, IIRC, since the values involved are small, you can get away with changing the data types in the SYSTEMTIME structure to Int16 and it'll still work.

          <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 wMillisecond As Int16
          End Structure

          Dave Kreskowiak Microsoft MVP - Visual Basic

          D 1 Reply Last reply
          0
          • D Dave Kreskowiak

            I'm using VB 2005, and you're not. That's why the problems... "<<" is a bit-wise shift operator that finally appeared (many years too late!) in 2005. To get the same effect using math, you need to multiply the value by 2^(number of bits to shift left). Sooooo, the same operation can be done like this:

            Dim dt As New DateTime(((CType(ft.dwHighDateTime, Long)) * (2 ^ 32) Or CType(ft.dwLowDateTime, UInt32))

            Because of the math, it's quite a bit slower, but it does the same thing. As for the other problem, IIRC, since the values involved are small, you can get away with changing the data types in the SYSTEMTIME structure to Int16 and it'll still work.

            <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 wMillisecond As Int16
            End Structure

            Dave Kreskowiak Microsoft MVP - Visual Basic

            D Offline
            D Offline
            David M J
            wrote on last edited by
            #7

            Then I still have the problem that I cannot convert the dwLowDateTime to Uint32. As in the original problem. The thing I don't get is that this convertion works fine for C# and vb.net Think, try, think, think, try, think, think, think, try, ASK, think, try, advance on step and start over...

            D 1 Reply Last reply
            0
            • D David M J

              Then I still have the problem that I cannot convert the dwLowDateTime to Uint32. As in the original problem. The thing I don't get is that this convertion works fine for C# and vb.net Think, try, think, think, try, think, think, think, try, ASK, think, try, advance on step and start over...

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #8

              OK. You can change that UInt32 to Long an it'll work. Dave Kreskowiak Microsoft MVP - Visual Basic

              D 1 Reply Last reply
              0
              • D Dave Kreskowiak

                OK. You can change that UInt32 to Long an it'll work. Dave Kreskowiak Microsoft MVP - Visual Basic

                D Offline
                D Offline
                David M J
                wrote on last edited by
                #9

                Hi, Tried this, assuming that is what you meant ... Dim dt As DateTime = New DateTime(((CType(ft.dwHighDateTime, Long)) << 32) Or CType(ft.dwLowDateTime, Long)) But then I get the exception message: "Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks. Parameter name: ticks" Think, try, think, think, try, think, think, think, try, ASK, think, try, advance on step and start over...

                D 1 Reply Last reply
                0
                • D David M J

                  Hi, Tried this, assuming that is what you meant ... Dim dt As DateTime = New DateTime(((CType(ft.dwHighDateTime, Long)) << 32) Or CType(ft.dwLowDateTime, Long)) But then I get the exception message: "Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks. Parameter name: ticks" Think, try, think, think, try, think, think, think, try, ASK, think, try, advance on step and start over...

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #10

                  Hmmmm...I never got that message in testing. What's the value of the High and Low in ft when it fails? Even better, also, what's the values in st before the call to SystemTimeToFileTime? Dave Kreskowiak Microsoft MVP - Visual Basic -- modified at 18:50 Thursday 13th April, 2006

                  D 1 Reply Last reply
                  0
                  • D Dave Kreskowiak

                    Hmmmm...I never got that message in testing. What's the value of the High and Low in ft when it fails? Even better, also, what's the values in st before the call to SystemTimeToFileTime? Dave Kreskowiak Microsoft MVP - Visual Basic -- modified at 18:50 Thursday 13th April, 2006

                    D Offline
                    D Offline
                    David M J
                    wrote on last edited by
                    #11

                    Here are the value for st : wDay: 31 wDayOfWeek: 5 wHour: 23 wMilliseconds: 0 wMinute: 0 wMonth: 12 wSecond: 0 wYear: 3604 whith wich values are you testing? Think, try, think, think, try, think, think, think, try, ASK, think, try, advance on step and start over...

                    D 1 Reply Last reply
                    0
                    • D David M J

                      Here are the value for st : wDay: 31 wDayOfWeek: 5 wHour: 23 wMilliseconds: 0 wMinute: 0 wMonth: 12 wSecond: 0 wYear: 3604 whith wich values are you testing? Think, try, think, think, try, think, think, think, try, ASK, think, try, advance on step and start over...

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #12

                      OK. This should do it. The problem is that the bitwise math isn't working as it should because one of the value types in the FILETIME structure isn't what it appeared to be.

                      Dim dt As New DateTime((ft.dwHighDateTime \* (2 ^ 32)) + ft.dwLowDateTime)
                      

                      I'd beat the snot out of this code before putting it into production. Dave Kreskowiak Microsoft MVP - Visual Basic -- modified at 19:42 Thursday 13th April, 2006

                      D 1 Reply Last reply
                      0
                      • D Dave Kreskowiak

                        OK. This should do it. The problem is that the bitwise math isn't working as it should because one of the value types in the FILETIME structure isn't what it appeared to be.

                        Dim dt As New DateTime((ft.dwHighDateTime \* (2 ^ 32)) + ft.dwLowDateTime)
                        

                        I'd beat the snot out of this code before putting it into production. Dave Kreskowiak Microsoft MVP - Visual Basic -- modified at 19:42 Thursday 13th April, 2006

                        D Offline
                        D Offline
                        David M J
                        wrote on last edited by
                        #13

                        Hi Dave, Fixed it in a more convential way ... : Private Shared Function SystemTimeToDateTime(ByRef st As SYSTEMTIME) As DateTime Dim dt As DateTime Dim dtFormat As String Dim en As New CultureInfo("en-GB") dtFormat = "dd/MM/yyyy HH:mm" With st dt = DateTime.ParseExact( _ Format$(.wDay, "00") & "/" & _ Format$(.wMonth, "00") & "/" & _ Format$(.wYear - 1600) & " " & _ Format$(.wHour, "00") & ":" & _ Format$(.wMinute, "00"), dtFormat, en.DateTimeFormat) ' & ":" & _ ' Format$(.wSecond, "00"), dtFormat, en.DateTimeFormat) End With Return dt End Function Gues it is a bit less efficient, not sure how much though. Your remark about the code and the snot was that just about this problem or also about the rest of the code. If so could you give me some pointers, guess it is never too late to learn. :)) Cheers David Think, try, think, think, try, think, think, think, try, ASK, think, try, advance on step and start over...

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups