• Welcome to Valhalla Legends Archive.
 

Filetime Tutorial

Started by Networks, February 03, 2005, 06:51 PM

Previous topic - Next topic

Networks

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



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



I believe those are the standard declares. Build off of that.

CrAz3D

GetSystemTime then SystemTimeToFileTime...that would give you a current FileTime I think.
rebundance - having or being in excess of sheer stupidity
(ré-bun-dance)
Quote from: Spht on June 22, 2004, 07:32 PMSlap.
Quote from: Adron on January 28, 2005, 09:17 AMIn a way, I believe that religion is inherently evil, which includes Christianity. I'd also say Christianity is eviller than Buddhism (has more potential for evil).
Quote from: iago on April 19, 2005, 01:06 PM
CrAz3D's ... is too big vertically, at least, too big with ... iago ...

Grok


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

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.

CrAz3D


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



GetSystemTime ST
SystemTimeToFileTime ST, FT
Now you have your file time

FileTimeToLocalFileTime FT, FT
FileTimeToSystemTime FT, ST

Msgbox ST.wDay

Now you have your system time.
rebundance - having or being in excess of sheer stupidity
(ré-bun-dance)
Quote from: Spht on June 22, 2004, 07:32 PMSlap.
Quote from: Adron on January 28, 2005, 09:17 AMIn a way, I believe that religion is inherently evil, which includes Christianity. I'd also say Christianity is eviller than Buddhism (has more potential for evil).
Quote from: iago on April 19, 2005, 01:06 PM
CrAz3D's ... is too big vertically, at least, too big with ... iago ...

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?

CrAz3D

Look @ the SystemTime structure, filetime seperates, or w/e, into that & then you have the day, the year, the hour, the minute.
rebundance - having or being in excess of sheer stupidity
(ré-bun-dance)
Quote from: Spht on June 22, 2004, 07:32 PMSlap.
Quote from: Adron on January 28, 2005, 09:17 AMIn a way, I believe that religion is inherently evil, which includes Christianity. I'd also say Christianity is eviller than Buddhism (has more potential for evil).
Quote from: iago on April 19, 2005, 01:06 PM
CrAz3D's ... is too big vertically, at least, too big with ... iago ...

Grok

Quote from: Networks on February 04, 2005, 04:02 PM
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?

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.

Adron

Quote from: Networks on February 04, 2005, 11:43 AM
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.


Dim ft as filetime
debug.print "Output:", ft.dwlowdatetime, ft.dwhighdatetime


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?

Networks

#9
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:



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



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.

Blaze

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
Mitosis: Haha, Im great arent I!
hismajesty[yL]: No

Eric

Quote from: Blaze - (S-1-0-0) on February 13, 2005, 10:52 AM
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.

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

' FILETIME/SYSTEMTIME Conversions
' Copyright (C) 2005 LoRd[nK] ([email protected])
' 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

Hdx

I get how u used pretty much all of that, cept:

Private Const OFFSET_4 = 4294967296#
Private Const MAXINT_4 = 2147483647

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)~-~

Proud host of the JBLS server www.JBLS.org.
JBLS.org Status:
JBLS/BNLS Server Status

Eric

#13
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.

Blaze

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.
Quote
Mitosis: Haha, Im great arent I!
hismajesty[yL]: No