Convert time_t to DateTime

jazzwhistle

Member
Joined
Oct 17, 2006
Messages
7
Programming Experience
1-3
Hello,

I am trying to convert a time_t string to a readable format.

Hex Workshop shows the bytes as "90 10 47 45" and tells me that this is 32 bit little endian, and corresponds to "10:00:00 31/10/2006", exactly as it should, but so far I have been unable to do this in VB.

The following code

VB.NET:
        Dim dateData As DateTime
        Dim time_t_string = "10:00:00 31/10/2006"
        dateData = Convert.ToDateTime(time_t_string)
        return dateData.ToBinary

tells me that the LONG I should be passing to my function should be "632979216000000000" but I am unable to generate this long however i interpret "90 10 47 45"...

My latest attemps is this, and please bear in mind I am a newcomer to VB:
(The values are in a Byte array called filecontents, ending at position byteindex)

VB.NET:
 Dim bytestr As String = ""
Dim tempbytestr As String = ""
         For j = byteindex To byteindex - 3 Step -1

                        tempbytestr = Format(filecontents(j), "X")
                        If tempbytestr.Length = 1 Then tempbytestr = "0" & tempbytestr
                        bytestr = bytestr & tempbytestr
         Next
                    bytestr = bytestr & "00000000" ' It Seems I need 64bit string to get time and date??
                                                               'Then Convert Hex String to LONG
                    Dim dateData As Long
                    dateData = Convert.ToInt64(bytestr, 16)
                    
                    return DateTime.FromBinary(dateData)

But this gives a DateTime of "04/02/1206 12:13:39"

I have tried many different methods, but none gives me the right values :/ Can anyone put me on the right track?
Thank you in advance
 
Last edited:
For anyone who is interested, I finally worked this out thanks to Brad Abrams:

bytestr contains the 4 byte hex string (backwards, as mine is little endian):


VB.NET:
Dim dateData As Long
                    dateData = Convert.ToInt32(bytestr, 16)
                    Dim win32FileTime As Long = 10000000 * dateData + 116444736000000000
return DateTime.FromFileTimeUtc(win32FileTime)
 
Back
Top