在VB中得到C语言函数time(&lnow )得到的数据
在VB中得到C语言函数time(&lnow )得到的数据 ( 2006-10-18 15:30 )
VB中只能操作自己的DATE类型,同时,API操作也仅有SYSTEMTIME,FILETIME这两个类型可以操作,要得到C语言中的 time_t 这一类型的数据,确实很费脑筋。
然而,你不可能要求DLL编写者都了解VB,都提供VB可用的参数。为此,我们只能想法在VB中解决。
要解决这一问题,必须要解决的是:GMTTIME,UTCTIME之间的转换的问题。
以下是VB源码:
Option Explicit
'=============================================================================== ' Constants '===============================================================================
Private Const TIME_ZONE_ID_INVALID = &HFFFFFFFF Private Const TIME_ZONE_ID_UNKNOWN = 0 Private Const TIME_ZONE_ID_STANDARD = 1 Private Const TIME_ZONE_ID_DAYLIGHT = 2
'=============================================================================== ' Types '=============================================================================== 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 Type FILETIME ' Win32 date dwLowDateTime As Long dwHighDateTime As Long End Type
Private Type TIME_ZONE_INFORMATION Bias As Long StandardName(63) As Byte StandardDate As SYSTEMTIME StandardBias As Long DaylightName(63) As Byte DaylightDate As SYSTEMTIME DaylightBias As Long End Type
'=============================================================================== ' Private Members '===============================================================================
'=============================================================================== ' Declares '=============================================================================== Private Declare Function FileTimeToLocalFileTime Lib "kernel32" _ (lpFileTime As FILETIME, lpLocalFileTime 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
Private Declare Function LocalFileTimeToFileTime Lib "kernel32" _ (lpLocalFileTime As FILETIME, lpFileTime As FILETIME) As Long
Friend Function UtcToLocalFileTime(FILETIME As FILETIME) As FILETIME '=============================================================================== ' UtcToLocalFileTime - Converts local FILETIME to UTC/GMT FILETIME. '===============================================================================
Dim Success As Boolean
' Exit if null date supplied. If FILETIME.dwHighDateTime = 0 And FILETIME.dwLowDateTime = 0 Then Exit Function End If
' Convert to local time Success = FileTimeToLocalFileTime(FILETIME, UtcToLocalFileTime) Debug.Assert Success
End Function
Friend Function UtcFromLocalFileTime(FILETIME As FILETIME) As FILETIME '=============================================================================== ' UtcFromLocalFileTime - Converts UTC/GMT FILETIME to a local FILETIME. '===============================================================================
Dim Success As Boolean
' Exit if null date supplied. If FILETIME.dwHighDateTime = 0 And FILETIME.dwLowDateTime = 0 Then Exit Function End If
' Convert to UTC time Success = LocalFileTimeToFileTime(FILETIME, UtcFromLocalFileTime) Debug.Assert Success
End Function
Friend Function FileTimeToDate(FILETIME As FILETIME) As Date '=============================================================================== ' FileTimeToDate - Converts FILETIME structure to a VB Date data type. ' ' NOTE: The FILETIME structure is a structure of 100-nanosecond intervals since ' January 1, 1601. The VB Date data type is a floating point value Where the ' value to the left of the decimal is the number of days since December 30, ' 1899, and the value to the right of the decimal represents the time. the ' hour of the time value can be calculated by multiplying by 24, with the ' remainder multiplied by 60 to get the minutes, and that remainder can then ' be multiplied by 60 to get the seconds. ' ' ' FileTime The FILETIME structure to convert. ' ' RETURNS A date/time value in the intrinsic VB Date data type. ' '===============================================================================
Dim Success As Boolean Dim SysTime As SYSTEMTIME
Success = FileTimeToSystemTime(FILETIME, SysTime) Debug.Assert Success
' Create a date/time value from the system time parts With SysTime FileTimeToDate = DateSerial(.wYear, .wMonth, .wDay) + _ TimeSerial(.wHour, .wMinute, .wSecond) End With
End Function
Friend Function FileTimeFromDate(FromDate As Date) As FILETIME '=============================================================================== ' FileTimeFromDate - Converts a VB Date data type to a FILETIME structure. ' ' NOTE: The FILETIME structure is a structure of 100-nanosecond intervals since ' January 1, 1601. The VB Date data type is a floating point value Where the ' value to the left of the decimal is the number of days since December 30, ' 1899, and the value to the right of the decimal represents the time. the ' hour of the time value can be calculated by multiplying by 24, with the ' remainder multiplied by 60 to get the minutes, and that remainder can then ' be multiplied by 60 to get the seconds. ' ' ' FromDate The VB DAte to convert. ' ' RETURNS A date/time value in the native Win32 FILETIME structure. ' '===============================================================================
Dim Success As Boolean Dim SysTime As SYSTEMTIME
' Create SYSTEMTIME from each date part in a date/time value. With SysTime .wYear = Year(FromDate) .wMonth = Month(FromDate) .wDay = Day(FromDate) .wHour = Hour(FromDate) .wMinute = Minute(FromDate) .wSecond = Second(FromDate) End With
' convert the SYSTEMTIME to the FILETIME Success = SystemTimeToFileTime(SysTime, FileTimeFromDate) Debug.Assert Success
End Function
Public Function UtcToLocalTime(ByVal UtcTime As Date) As Date '=============================================================================== ' UtcToLocalTime - Converts UTC/GMT time to local time. '===============================================================================
Dim FILETIME As FILETIME
FILETIME = FileTimeFromDate(UtcTime) FILETIME = UtcToLocalFileTime(FILETIME) UtcToLocalTime = FileTimeToDate(FILETIME)
End Function
Public Function UtcFromLocalTime(ByVal LocalTime As Date) As Date '=============================================================================== ' UtcFromLocalTime - Converts local time to UTC/GMT time. '===============================================================================
Dim FILETIME As FILETIME
FILETIME = FileTimeFromDate(LocalTime) FILETIME = UtcFromLocalFileTime(FILETIME) UtcFromLocalTime = FileTimeToDate(FILETIME)
End Function
Public Function GetUTCLongTime() As Long
Dim UtcDate As Date Dim UTCLongTime As Long
UtcDate = UtcFromLocalTime(Now())
UTCLongTime = DateDiff("s", "1970-01-01 00:00:00", UtcDate)
GetUTCLongTime = UTCLongTime
End Function
将此代码存为CLASS文件名为CGUSTime
然后,你即可以:
Dim lnow As Long
Dim Gus As CGUSTime
Set Gus = New CGUSTime
lnow = Gus.GetUTCLongTime()
Set Gus = Nothing
那么,这里得到的lnow 与C语言中 time(&lnow )数值是一样的。