Valhalla Legends Forums Archive | Visual Basic Programming | Filetime Tutorial

AuthorMessageTime
Networks
Can someone help me understand how to create a time and date and the convert it back out?

[code]

Public Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
Public Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Public Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
Public Declare Function GetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long

  Public Type FILETIME
          dwLowDateTime As Long
          dwHighDateTime As Long
  End Type
  Public Type SYSTEMTIME
          wYear As Integer
          wMonth As Integer
          wDayOfWeek As Integer
          wDay As Integer
          wHour As Integer
          wMinute As Integer
          wSecond As Integer
          wMilliseconds As Integer
  End Type
 
  Public FT As FILETIME
  Public ST As SYSTEMTIME

[/code]

I believe those are the standard declares. Build off of that.
February 4, 2005, 12:51 AM
CrAz3D
GetSystemTime then SystemTimeToFileTime...that would give you a current FileTime I think.
February 4, 2005, 3:25 AM
Grok
[code]
Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type
Private Type SYSTEMTIME
    wYear As Integer
    wMonth As Integer
    wDayOfWeek As Integer
    wDay As Integer
    wHour As Integer
    wMinute As Integer
    wSecond As Integer
    wMilliseconds As Integer
End Type

Private Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
Private Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Private Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long

' Convert a Date into a SYSTEMTIME.
Private Sub DateToSystemTime(ByVal the_date As Date, ByRef system_time As SYSTEMTIME)
    With system_time
        .wYear = Year(the_date)
        .wMonth = Month(the_date)
        .wDay = Day(the_date)
        .wHour = Hour(the_date)
        .wMinute = Minute(the_date)
        .wSecond = Second(the_date)
    End With
End Sub
' Convert a SYSTEMTIME into a Date.
Private Sub SystemTimeToDate(system_time As SYSTEMTIME, ByRef the_date As Date)
    With system_time
        the_date = CDate( _
            Format$(.wMonth) & "/" & _
            Format$(.wDay) & "/" & _
            Format$(.wYear) & " " & _
            Format$(.wHour) & ":" & _
            Format$(.wMinute, "00") & ":" & _
            Format$(.wSecond, "00"))
    End With
End Sub
[/code]
February 4, 2005, 1:23 PM
Networks
How do I out put a filetime code which I can later convert back to find the time, date, year, month, everything. How do I convert this also? This is what I want something like:

Output: 134590283042 (this is probably unrealistic)
and I want to be able to convert that to find it's date, month, etc mentioned above.

I want a simple tutorial of how to create a filetime code and be able to convert it back out.
February 4, 2005, 5:43 PM
CrAz3D
[code]
Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type
Private Type SYSTEMTIME
    wYear As Integer
    wMonth As Integer
    wDayOfWeek As Integer
    wDay As Integer
    wHour As Integer
    wMinute As Integer
    wSecond As Integer
    wMilliseconds As Integer
End Type

Private Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
Private Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Private Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long
  Public FT As FILETIME
  Public ST As SYSTEMTIME[/code]

[code]
GetSystemTime ST
SystemTimeToFileTime ST, FT
[/code] Now you have your file time

[code]FileTimeToLocalFileTime FT, FT
FileTimeToSystemTime FT, ST

Msgbox ST.wDay[/code]
Now you have your system time.
February 4, 2005, 6:26 PM
Networks
I don't think anyone is getting one I am trying to say so...Here's an example. I wanted to store a users date,time, EVERYTHING using this filetime thing so I don't have to actually record time and date it's all in one crazy string provided by filetime? Anyway, I will save it and I want to be able to access it again and be able to know when they were added to this database, the time, EVERYTHING provided by filetime? Is this possible and HOW DO I DO THAT?
February 4, 2005, 10:02 PM
CrAz3D
Look @ the SystemTime structure, filetime seperates, or w/e, into that & then you have the day, the year, the hour, the minute.
February 4, 2005, 11:09 PM
Grok
[quote author=Networks link=topic=10417.msg98232#msg98232 date=1107554544]
I don't think anyone is getting one I am trying to say so...Here's an example. I wanted to store a users date,time, EVERYTHING using this filetime thing so I don't have to actually record time and date it's all in one crazy string provided by filetime? Anyway, I will save it and I want to be able to access it again and be able to know when they were added to this database, the time, EVERYTHING provided by filetime? Is this possible and HOW DO I DO THAT?
[/quote]

Ah, you want to know when a new row was added to your database?  SQL Server provides a datatype specifically for that purpose, it is called timestamp!  If you don't want to use timestamp, you can use a datetime, and set its default value to GetDate(), a built-in function which returns a datetime of the current system time.
February 7, 2005, 4:43 PM
Adron
[quote author=Networks link=topic=10417.msg98211#msg98211 date=1107539025]
How do I out put a filetime code which I can later convert back to find the time, date, year, month, everything. How do I convert this also? This is what I want something like:

Output: 134590283042 (this is probably unrealistic)
and I want to be able to convert that to find it's date, month, etc mentioned above.

I want a simple tutorial of how to create a filetime code and be able to convert it back out.
[/quote]

[code]
Dim ft as filetime
debug.print "Output:", ft.dwlowdatetime, ft.dwhighdatetime
[/code]

Now we've showed you how to output a filetime and how to convert it into year/month/date/time form and back. Is it possible that you aren't understanding what we're actually telling you?
February 12, 2005, 7:25 PM
Networks
I probably should've responded to this so here it is, with the help of LoRd[nK] I was able to figure it out and mod it to my liking. The original module lord sent didn't work correctly but I fixed it so that it would work the way I needed it to:

[code]

Option Explicit

'Created by: LoRd[nK]
'Modded by: Networks

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal numBYTEs As Long)

Private Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)

Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Private Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long

Private Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
Private Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long

Public ST As SYSTEMTIME
Private Type SYSTEMTIME
    wYear As Integer
    wMonth As Integer
    wDayOfWeek As Integer
    wDay As Integer
    wHour As Integer
    wMinute As Integer
    wSecond As Integer
    wMilliseconds As Integer
End Type

Public FT As FILETIME
Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type


Public Function Get_FILETIME() As Long
    Dim lngFT As Long
    ' Format Time
    Call GetSystemTime(ST)                          ' Get Current SYSTEMTIME (GMT)
    Call SystemTimeToFileTime(ST, FT)          ' Convert SYSTEMTIME to FILETIME
    Call CopyMemory(lngFT, FT, LenB(FT))      ' Convert FILETIME To Long
    Get_FILETIME = lngFT
End Function

Public Sub Set_SYSTEMTIME(ByVal lngFT As Long)
    Call Get_FILETIME
   
    ' Retrieve Time
    Call CopyMemory(FT, lngFT, Len(lngFT))      ' Convert Long To FILETIME
    Call FileTimeToLocalFileTime(FT, FT)            ' Convert FILETIME From GMT To Local Time
    Call FileTimeToSystemTime(FT, ST)            ' Convert FILETIME To SYSTEM TIME
   
'Debug.Print Format(ST.wMonth, "00") & "/" & Format(ST.wDay, "00") & "/" & Format(ST.wYear, "0000") & " " & _
Format(ST.wHour, "00") & ":" & Format(ST.wMinute, "00") & ":" & Format(ST.wSecond, "00") & "." & Format(ST.wMilliseconds, "000")
End Sub

Public Function Get_Date() As String
    Get_Date = Format(ST.wMonth, "00") & "/" & Format(ST.wDay, "00") & "/" & Format(ST.wYear, "0000")
End Function

Public Function Get_Time() As String
    Get_Time = Format(ST.wHour, "00") & ":" & Format(ST.wMinute, "00") & ":" & Format(ST.wSecond, "00")
End Function

[/code]

To get the current filetime you simply use: GET_FILETIME (This is the value you would use to store)

When you want to convert a FILETIME you use: Set_SYSTEMTIME(<value>) and you would use Get_Date and Get_Time to get the date and time for that FILETIME value. You have to call Set_SYSTEMTIME to set the stored FILETIME before getting the time and date you want in plain text.
February 13, 2005, 3:48 PM
KkBlazekK
On the topic of filetimes, I am getting a highdatetime that is higher then a long.. so I'm overflowing, is there something to fix this and still get the correct time/date?

Any comments would be appreciated.
February 13, 2005, 4:52 PM
LoRd
[quote author=Blaze - (S-1-0-0) link=topic=10417.msg99585#msg99585 date=1108313533]
On the topic of filetimes, I am getting a highdatetime that is higher then a long.. so I'm overflowing, is there something to fix this and still get the correct time/date?

Any comments would be appreciated.
[/quote]

Yes.  Here's the unmodified module that I made for Networks:

[code]' FILETIME/SYSTEMTIME Conversions
' Copyright (C) 2005 LoRd[nK] (RVictim87@gmail.com)
' Disclaimer: If you discredit me, you shall die! >:|

Option Explicit

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal numBYTEs As Long)

Public Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)

Public Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Public Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long

Public Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
Public Declare Function LocalFileTimeToFileTime Lib "kernel32" (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long

Public Type SYSTEMTIME
    wYear As Integer
    wMonth As Integer
    wDayOfWeek As Integer
    wDay As Integer
    wHour As Integer
    wMinute As Integer
    wSecond As Integer
    wMilliseconds As Integer
End Type

Public Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type

Private Const OFFSET_4 = 4294967296#
Private Const MAXINT_4 = 2147483647

Public Function DoJunk()
' FILETIME/SYSTEMTIME Conversions

Dim lngFT As Long
Dim dblFT As Double

Dim ST As SYSTEMTIME
Dim FT As FILETIME

' Format Time
Call GetSystemTime(ST)                    ' Get Current SYSTEMTIME (GMT)
Call SystemTimeToFileTime(ST, FT)        ' Convert SYSTEMTIME to FILETIME
Call CopyMemory(lngFT, FT, LenB(FT))      ' Convert FILETIME To Long
Debug.Print "Signed Value: " & lngFT

dblFT = LongToUnsigned(lngFT)            ' Convert Signed Value To Unsigned Value (Visual BASIC sucks! :p)
Debug.Print "Unsigned Value: " & dblFT

' Retrieve Time
lngFT = UnsignedToLong(dblFT)            ' Convert Unsigned Value To Signed Value

Call CopyMemory(FT, lngFT, Len(lngFT))    ' Convert Long To FILETIME
Call FileTimeToLocalFileTime(FT, FT)      ' Convert FILETIME From GMT To Local Time
Call FileTimeToSystemTime(FT, ST)        ' Convert FILETIME To SYSTEM TIME

Debug.Print "SYSTEMTIME: " & Format(ST.wMonth, "00") & "/" & Format(ST.wDay, "00") & "/" & Format(ST.wYear, "0000") & " " & _
    Format(ST.wHour, "00") & ":" & Format(ST.wMinute, "00") & ":" & Format(ST.wSecond, "00") & "." & Format(ST.wMilliseconds, "000")
End Function

Public Function LongToUnsigned(Value As Long) As Double
If Value < 0 Then
    LongToUnsigned = Value + OFFSET_4
Else
    LongToUnsigned = Value
End If
End Function
Public Function UnsignedToLong(Value As Double) As Long
If Value < 0 Or Value >= OFFSET_4 Then Error 6 ' Overflow
If Value <= MAXINT_4 Then
    UnsignedToLong = Value
Else
    UnsignedToLong = Value - OFFSET_4
End If
End Function
[/code]
February 13, 2005, 6:03 PM
HdxBmx27
I get how u used pretty much all of that, cept:
[code]
Private Const OFFSET_4 = 4294967296#
Private Const MAXINT_4 = 2147483647[/code]
Just wondering how are those 2 number the correct ones?
I understand whay you did. I jsut want to know WHY you did it this spacific way?
~-~(HDX)~-~
February 14, 2005, 3:11 AM
LoRd
Since I explained it to you on AIM, I'm not going to repeat myself here, but here is the documentation for the functions that I posted if anyone else needs help.
February 14, 2005, 3:27 AM
KkBlazekK
Thanks lord, yesterday when you gave me that on aim I thought you misunderstanded me, but after looking at the code I see it was exactly what I needed.
February 15, 2005, 1:08 AM

Search