当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

在VB中得到C语言函数time(&lnow )得到的数据

发表于: 2010-04-25   作者:bardo   来源:转载   浏览次数:
摘要: 在VB中得到C语言函数time(&lnow )得到的数据 ( 2006-10-18 15:30 ) VB中只能操作自己的DATE类型,同时,API操作也仅有SYSTEMTIME,FILETIME这两个类型可以操作,要得到C语言中的 time_t 这一类型的数据,确实很费脑筋。 然而,你不可能要求DLL编写者都了解VB,都提供VB可用的参数。为此,我们只能想法在VB中解决。
在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 )数值是一样的。

在VB中得到C语言函数time(&lnow )得到的数据

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
首先在页面引入jquery.js库文件 取表格当前行数据js代码: $(function() { $(".myclass").each(func
本文章为原创,转载请保留出处: http://www.eyeandroid.com/thread-10633-1-1.html Android中得到Sh
问题很简单,给自己mark一下,常用的情况见下图 在点击保存按钮的时候得到当前行的所有的值 $(funct
  “大数据时代”,大数据分析、大数据挖掘、大数据研究、大数据平台、大数据 是生产力等等。似乎
“大数据时代”,大数据分析、大数据挖掘、大数据研究、大数据平台、大数据 是生产力等等。似乎可以
看一个dump文件, 发现一个名字叫做"SharedResourceProviderJob”的类, 这个类是一个timer job. 管理
在Entity Framework中,有时我们想要得到真正的数据库表的名称,可以通过Metadata来得到。 直接上代
以Camera为例,看看应用程序如何获得拍照的图片。 Camera类中拍照的方法是Capture(),它通过继承类a
大伙应该在开发中遇到过这种情况,就是通过下拉框选择要显示的内容。如下图所示: 在没用到ajax技术
在Entity Framework中,有时我们想要得到真正的数据库表的名称,可以通过Metadata来得到。 直接上代
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号