3

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

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

4

2 回答 2

0

经过一番研究,在我看来,没有任何本机函数或过程可以为您执行此操作(在 VB6 中,即;VB.NET 允许您执行此操作DateTime.Today.ToFileTime)但是,本文解释了 Windows 文件时间如何计算 - 文件时间

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

于 2013-07-03T14:07:23.103 回答
0

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

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

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

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

于 2013-07-03T16:07:14.917 回答