在VB6中将日期转换为FileTime

时间:2013-07-03 13:51:30

标签: vb6 date-conversion filetime

我正在创建一个需要查询现有数据库的VB6应用程序,我需要按日期进行过滤。数据库以Windows文件时间格式存储日期,即130159451238571322 <{1}} 2013-06-17 12:18:43.857

VB6中是否有办法获取当前日期/时间并将其转换为Windows文件时间?

2 个答案:

答案 0 :(得分:0)

经过一番研究后,我觉得没有任何本机函数或程序能够为你做到这一点(在VB6中,也就是说; VB.NET允许你做DateTime.Today.ToFileTime)但是,本文介绍了如何计算Windows文件时间 - File Times

考虑到这一点,您可以计算在1/1/1601午夜和当前日期和时间(UTC)之间发生的100纳秒间隔的数量,以创建准确的文件时间。

答案 1 :(得分:0)

VB6本身处理的唯一64位值是Currency类型的值。然而,这些是64位整数,隐含的缩放比例为10,000,因此二进制值1被视为0.0001用于算术目的。

但是,当您通过ADO从数据库中检索此类“文件时间”值时,您获得的通常是子类型Decimal的变体。这完全是另一个生物。

FILETIME是一个结构体,VB6中最接近的是“UDT”,更恰当地称为记录。所以当你在数据库中使用这些值时,你会发挥更多的轻浮。

以下是部分转化的示例:

Option Explicit

Private Type OneCurrency
    Curr As Currency
End Type

Private Sub cmdConvert_Click()
    Dim DateTime As Date
    Dim FileTimeRecord As FILETIME
    Dim DecFileTime As Variant
    Dim OC As OneCurrency

    'Convert:
    DateTime = CDate(txtTime.Text)
    FileTimeRecord = DateToFileTime(DateTime)
    LSet OC = FileTimeRecord
    DecFileTime = CDec(OC.Curr) * CDec(10000)

    'Display:
    With FileTimeRecord
        lblFileTime.Caption = Right$(String$(7, "0") & Hex$(.dwHighDateTime), 8) _
                            & ":" _
                            & Right$(String$(7, "0") & Hex$(.dwLowDateTime), 8) _
                            & vbNewLine _
                            & CStr(DecFileTime)
    End With

    'Convert back, display:
    OC.Curr = DecFileTime / 10000@
    LSet FileTimeRecord = OC
    lblTime.Caption = CStr(FileTimeToDate(FileTimeRecord))
End Sub

Private Sub cmdNow_Click()
    txtTime.Text = CStr(Now())
End Sub

表单代码依赖于位于其他地方的一些声明,例如静态模块:

Option Explicit

Public 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 FileTimeToSystemTime Lib "kernel32" ( _
    ByRef lpFileTime As FILETIME, _
    ByRef lpSystemTime As SYSTEMTIME) As Long

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

Public Const ZERO_DATE As Date = 0!

Public Function DateToFileTime(ByVal DateTime As Date) As FILETIME
    Dim stLocal As SYSTEMTIME
    Dim stUniversal As SYSTEMTIME
    Dim ftResult As FILETIME

    With stLocal
        .wYear = Year(DateTime)
        .wMonth = Month(DateTime)
        .wDay = Day(DateTime)
        .wHour = Hour(DateTime)
        .wMinute = Minute(DateTime)
        .wSecond = Second(DateTime)
    End With
    SystemTimeToFileTime stLocal, ftResult
    DateToFileTime = ftResult
End Function

Public Function FileTimeToDate(ByRef FT As FILETIME) As Date
    Dim stLocal As SYSTEMTIME

    With FT
        If (.dwHighDateTime <> 0) And (.dwLowDateTime <> 0) Then
            FileTimeToSystemTime FT, stLocal
            With stLocal
                FileTimeToDate = DateSerial(.wYear, .wMonth, .wDay) _
                               + TimeSerial(.wHour, .wMinute, .wSecond)
            End With
        Else
            FileTimeToDate = ZERO_DATE
        End If
    End With
End Function

我不确定那里的所有错误都已被压扁但是它应该足以让你开始。