在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 )数值是一样的。
分享到:
相关推荐
python学习资源
jfinal-undertow 用于开发、部署由 jfinal 开发的 web 项目
基于Andorid的音乐播放器项目设计(国外开源)实现源码,主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。
python学习资源
python学习资源
python学习一些项目和资源
【毕业设计】java-springboot+vue家具销售平台实现源码(完整前后端+mysql+说明文档+LunW).zip
HTML+CSS+JavaScarip开发的前端网页源代码
python学习资源
【毕业设计】java-springboot-vue健身房信息管理系统源码(完整前后端+mysql+说明文档+LunW).zip
成绩管理系统C/Go。大学生期末小作业,指针实现,C语言版本(ANSI C)和Go语言版本
1_基于大数据的智能菜品个性化推荐与点餐系统的设计与实现.docx
【毕业设计】java-springboot-vue交流互动平台实现源码(完整前后端+mysql+说明文档+LunW).zip
内容概要:本文主要探讨了在高并发情况下如何设计并优化火车票秒杀系统,确保系统的高性能与稳定性。通过对比分析三种库存管理模式(下单减库存、支付减库存、预扣库存),强调了预扣库存结合本地缓存及远程Redis统一库存的优势,同时介绍了如何利用Nginx的加权轮询策略、MQ消息队列异步处理等方式降低系统压力,保障交易完整性和数据一致性,防止超卖现象。 适用人群:具有一定互联网应用开发经验的研发人员和技术管理人员。 使用场景及目标:适用于电商、票务等行业需要处理大量瞬时并发请求的业务场景。其目标在于通过合理的架构规划,实现在高峰期保持平台的稳定运行,保证用户体验的同时最大化销售额。 其他说明:文中提及的技术细节如Epoll I/O多路复用模型以及分布式系统中的容错措施等内容,对于深入理解大规模并发系统的构建有着重要指导意义。
基于 OpenCV 和 PyTorch 的深度车牌识别
【毕业设计-java】springboot-vue教学资料管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip
此数据集包含有关出租车行程的详细信息,包括乘客人数、行程距离、付款类型、车费金额和行程时长。它可用于各种数据分析和机器学习应用程序,例如票价预测和乘车模式分析。
把代码放到Word中,通过开发工具——Visual Basic——插入模块,粘贴在里在,把在硅基流动中申请的API放到VBA代码中。在Word中,选择一个问题,运行这个DeepSeekV3的宏就可以实现在线问答
【毕业设计】java-springboot+vue机动车号牌管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip
【毕业设计】java-springboot-vue交通管理在线服务系统的开发源码(完整前后端+mysql+说明文档+LunW).zip