Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 将UTC时间转换为本地时间_Vba_Ms Access - Fatal编程技术网

Vba 将UTC时间转换为本地时间

Vba 将UTC时间转换为本地时间,vba,ms-access,Vba,Ms Access,我有一个MS Access应用程序,在两个不同的时区使用。相差7小时。我需要找到两个办公室都关闭的时间,这样我就可以关闭他们的数据库,对他们进行压缩、修复和备份 所以我不需要创建两个独立的前端,我会告诉关闭数据库,一个是在晚上1000点,另一个是在凌晨4点,我想我可以说在UTC凌晨00:30关闭数据库。 但我不知道如何在本地转换同一个。 现在,我关闭数据库的代码如下所示: Private Sub Form_Timer() Dim RunAtLocalTime As String RunAtLo

我有一个MS Access应用程序,在两个不同的时区使用。相差7小时。我需要找到两个办公室都关闭的时间,这样我就可以关闭他们的数据库,对他们进行压缩、修复和备份

所以我不需要创建两个独立的前端,我会告诉关闭数据库,一个是在晚上1000点,另一个是在凌晨4点,我想我可以说在UTC凌晨00:30关闭数据库。 但我不知道如何在本地转换同一个。 现在,我关闭数据库的代码如下所示:

Private Sub Form_Timer()
Dim RunAtLocalTime As String

RunAtLocalTime = Format(Now(), "HH:MM:SS")
If RunAtLocalTime = ("00:00:00") Then
        DoCmd.Quit
End If
End Sub
Private Sub Form_Timer()
Dim RunAtLocalTime As String
Dim UTCTIME As 

'''RunAtLocalTime = Convert(UTCTIME)
 RunAtLocalTime = Format(Now(), "HH:MM:SS")
 If RunAtLocalTime = ("00:00:00") Then
        DoCmd.Quit
End If
End Sub
我想这样做:

Private Sub Form_Timer()
Dim RunAtLocalTime As String

RunAtLocalTime = Format(Now(), "HH:MM:SS")
If RunAtLocalTime = ("00:00:00") Then
        DoCmd.Quit
End If
End Sub
Private Sub Form_Timer()
Dim RunAtLocalTime As String
Dim UTCTIME As 

'''RunAtLocalTime = Convert(UTCTIME)
 RunAtLocalTime = Format(Now(), "HH:MM:SS")
 If RunAtLocalTime = ("00:00:00") Then
        DoCmd.Quit
End If
End Sub

我发现了以下功能:

此项返回UTC时区偏移:

Option Compare Database
Option Explicit

Private Declare Function GetTimeZoneInformation Lib "kernel32" (lpTimeZoneInformation As TimeZoneInfo) As Long


Private Type SystemTime
        intYear As Integer
        intMonth As Integer
        intwDayOfWeek As Integer
        intDay As Integer
        intHour As Integer
        intMinute As Integer
        intSecond As Integer
        intMilliseconds As Integer
End Type


Private Type TimeZoneInfo
        lngBias As Long
        intStandardName(32) As Integer
        intStandardDate As SystemTime
        intStandardBias As Long
        intDaylightName(32) As Integer
        intDaylightDate As SystemTime
        intDaylightBias As Long
End Type

Public Function GetUTCOffset() As Date
    Dim lngRet As Long
    Dim udtTZI As TimeZoneInfo

    lngRet = GetTimeZoneInformation(udtTZI)
    GetUTCOffset = udtTZI.lngBias / 60 / 24
End Function
来源:[此网站](编辑链接不再有效,已删除)

这一个将时间转换为GMT:

Option Explicit
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 TIME_ZONE_INFORMATION
    Bias As Long
    StandardName(31) As Integer
    StandardDate As SYSTEMTIME
    StandardBias As Long
    DaylightName(31) As Integer
    DaylightDate As SYSTEMTIME
    DaylightBias As Long
End Type

Private Declare Function GetTimeZoneInformation Lib "kernel32" (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long
'Purpose     :  Converts local time to GMT.
'Inputs      :  dtLocalDate                 The local data time to return as GMT.
'Outputs     :  Returns the local time in GMT.
'Author      :  Andrew Baker
'Date        :  13/11/2002 10:16
'Notes       :
'Revisions   :

Public Function ConvertLocalToGMT(dtLocalDate As Date) As Date
    Dim lSecsDiff As Long

    'Get the GMT time diff
    lSecsDiff = GetLocalToGMTDifference()
    'Return the time in GMT
    ConvertLocalToGMT = DateAdd("s", -lSecsDiff, dtLocalDate)
End Function

'Purpose     :  Converts GMT time to local time.
'Inputs      :  dtLocalDate                 The GMT data time to return as local time.
'Outputs     :  Returns GMT as local time.
'Author      :  Andrew Baker
'Date        :  13/11/2002 10:16
'Notes       :
'Revisions   :

Public Function ConvertGMTToLocal(gmtTime As Date) As Date
    Dim Differerence As Long

    Differerence = GetLocalToGMTDifference()
    ConvertGMTToLocal = DateAdd("s", Differerence, gmtTime)
End Function

'Purpose     :  Returns the time lDiff between local and GMT (secs).
'Inputs      :  dtLocalDate                 The local data time to return as GMT.
'Outputs     :  Returns the local time in GMT.
'Author      :  Andrew Baker
'Date        :  13/11/2002 10:16
'Notes       :  A positive number indicates your ahead of GMT.
'Revisions   :

Public Function GetLocalToGMTDifference() As Long
    Const TIME_ZONE_ID_INVALID& = &HFFFFFFFF
    Const TIME_ZONE_ID_STANDARD& = 1
    Const TIME_ZONE_ID_UNKNOWN& = 0
    Const TIME_ZONE_ID_DAYLIGHT& = 2

    Dim tTimeZoneInf As TIME_ZONE_INFORMATION
    Dim lRet As Long
    Dim lDiff As Long

    'Get time zone info
    lRet = GetTimeZoneInformation(tTimeZoneInf)

    'Convert diff to secs
    lDiff = -tTimeZoneInf.Bias * 60
    GetLocalToGMTDifference = lDiff

    'Check if we are in daylight saving time.
    If lRet = TIME_ZONE_ID_DAYLIGHT& Then
        'In daylight savings, apply the bias
        If tTimeZoneInf.DaylightDate.wMonth <> 0 Then
            'if tTimeZoneInf.DaylightDate.wMonth = 0 then the daylight
            'saving time change doesn't occur
            GetLocalToGMTDifference = lDiff - tTimeZoneInf.DaylightBias * 60
        End If
    End If
End Function
选项显式
私有类型SYSTEMTIME
作为整数的wYear
作为整数的wmmonth
wDayOfWeek作为整数
作为整数的wDay
wHour作为整数
wMinute为整数
秒作为整数
wmill秒为整数
端型
专用类型时区信息
只要有偏见
标准名称(31)为整数
标准日期作为系统时间
标准偏差只要
DaylightName(31)为整数
DaylightDate作为系统时间
白昼偏长
端型
私有声明函数GetTimeZoneInformation Lib“kernel32”(lpTimeZoneInformation作为时区信息)长度为
'目的:将当地时间转换为GMT。
'输入:dtLocalDate返回为GMT的本地数据时间。
'输出:返回本地时间(GMT)。
作者:安德鲁·贝克
'日期:13/11/2002 10:16
'注:
“修订:
公共函数ConvertLocalToGMT(dtLocalDate作为日期)作为日期
暗淡的,如长的
'获取GMT时间差
lSecsDiff=GetLocalToGMTDifference()
'以GMT返回时间
ConvertLocalToGMT=DateAdd(“s”、-lSecsDiff、dtLocalDate)
端函数
'目的:将格林尼治时间转换为当地时间。
'输入:dtLocalDate作为本地时间返回的GMT数据时间。
'输出:将GMT作为本地时间返回。
作者:安德鲁·贝克
'日期:13/11/2002 10:16
'注:
“修订:
公共函数转换器GMTTOLOCAL(gmtTime As Date)As Date
模糊的差异
differrence=GetLocalToGMTDifference()
convertgmtotolical=DateAdd(“s”,differrence,gmtTime)
端函数
'用途:返回本地和GMT之间的时间lDiff(秒)。
'输入:dtLocalDate返回为GMT的本地数据时间。
'输出:返回本地时间(GMT)。
作者:安德鲁·贝克
'日期:13/11/2002 10:16
“注意:正数表示您在格林尼治时间之前。
“修订:
公共函数GetLocalToGMTDifference()的长度为
常量时区ID无效&=&HFFFFFFFF
常数时区ID标准&=1
常量时区ID未知&=0
常数时区ID日光&=2
Dim tTimeZoneInf作为时区信息
暗淡的lRet尽可能长
变暗,变长
'获取时区信息
lRet=GetTimeZoneInformation(TTTimeZoneInfo)
'将差异转换为秒
lDiff=-tTimeZoneInf.Bias*60
GetLocalToGMTDifference=lDiff
'检查我们是否在夏令时。
如果lRet=时区ID日光,则
“在夏令时,应用偏见
如果tTimeZoneInf.DaylightDate.wMonth为0,则
'如果tTimeZoneInf.DaylightDate.wMonth=0,则日光
'保存时间更改不会发生
GetLocalToGMTDifference=lDiff-tTimeZoneInf.DaylightBias*60
如果结束
如果结束
端函数
资料来源:

第二个我相信也使用第一个


我相信这两种方法都能满足您的需要。

正如@ashleedawg所提到的,调用
LocalFileTimeToFileTime
可能会返回意外的结果,因为它使用时区和夏令时的当前设置

要通过调用和将本地时间转换为UTC时间,并将其转换为UTC时间,请执行以下操作:

Private Declare PtrSafe Function GetTimeZoneInformation Lib "kernel32" (lpTimeZoneInformation As Any) As Long
Private Declare PtrSafe Function SystemTimeToTzSpecificLocalTime Lib "kernel32" (lpTimeZoneInformation As Any, lpUniversalTime As Any, lpLocalTime As Any) As Long
Private Declare PtrSafe Function TzSpecificLocalTimeToSystemTime Lib "kernel32" (lpTimeZoneInformation As Any, lpLocalTime As Any, lpUniversalTime As Any) As Long
Private Declare PtrSafe Function SystemTimeToVariantTime Lib "OleAut32" (lpSystemTime As Any, pvtime As Any) As Long
Private Declare PtrSafe Function VariantTimeToSystemTime Lib "OleAut32" (ByVal pvtime As Double, lpSystemTime As Any) As Long


Public Function LocalTimeToUtc(ByVal Value As Date) As Date
  Dim tm(0 To 15) As Integer, tzi(0 To 171) As Byte
  GetTimeZoneInformation tzi(0)
  VariantTimeToSystemTime Value, tm(0)
  TzSpecificLocalTimeToSystemTime tzi(0), tm(0), tm(8)
  SystemTimeToVariantTime tm(8), LocalTimeToUtc
End Function

Public Function UtcToLocalTime(ByVal Value As Date) As Date
  Dim tm(0 To 15) As Integer, tzi(0 To 171) As Byte
  GetTimeZoneInformation tzi(0)
  VariantTimeToSystemTime Value, tm(0)
  SystemTimeToTzSpecificLocalTime tzi(0), tm(0), tm(8)
  SystemTimeToVariantTime tm(8), UtcToLocalTime
End Function

Public Function LocalTimeToUnixTime(ByVal Value As Date) As Double
  LocalTimeToUnixTime = (LocalTimeToUtc(Value) - 25569) * 86400
End Function

Public Function UnixTimeToLocalTime(ByVal Value As Double) As Date
  UnixTimeToLocalTime = UtcToLocalTime(Value / 86400 + 25569)
End Function

请注意时区转换使用的方法,包括与UTC标准之间的转换。时区规则(包括夏令时的差异)一开始就令人困惑,因为它们不仅因地区或国家而异,在某些情况下还因州或县而异

为了进一步混淆问题,这些规则在不断演变,因此出于逻辑原因(比如地球上剩下的一半,希望是朝着消除夏令时的方向发展),有时也不那么合乎逻辑(国家领导人一时兴起改变规则),有时则沟通不当()

甚至加拿大/美国也有一个编码员经常忘记解释的问题此站点(或此页面!)上的其他解决方案错误计算了某些情况或时间范围。

理想情况下,我们都会使用相同的方法从同一个地方获取信息。未来和历史时区信息管理局被认为是


下面的转换方法解释了所有夏令时和时区差异,我通过冗长的分析和权威文档(如Unicode)努力确认了这一点

我最小化了空间和效率,只包括与我的目的相关的功能:UTC时间和本地时间之间的转换,以及历元时间戳和本地时间之间的转换。这是由Tim Hall改编的。
历元时间戳,也称Unix时间是自1970年1月1日以来的秒数,在许多API和其他编程资源中用作标准时间格式。更多信息请访问,以及

我建议将其单独放在一个模块中

Option Explicit
'UTC/Local Time Conversion
'Adapted from code by Tim Hall published at https://github.com/VBA-tools/VBA-UtcConverter

'PUBLIC FUNCTIONS:
'    - UTCtoLocal(utc_UtcDate As Date) As Date     converts UTC datetimes to local
'    - LocalToUTC(utc_LocalDate As Date) As Date   converts local DateTime to UTC
'    - TimestampToLocal(st As String) As Date      converts epoch timestamp to Local Time
'    - LocalToTimestamp(dt as date) as String      converts Local Time to timestamp
'Accuracy confirmed for several variations of time zones & DST rules. (ashleedawg)
'===============================================================================

Private Type utc_SYSTEMTIME
    utc_wYear As Integer: utc_wMonth As Integer: utc_wDayOfWeek As Integer: utc_wDay As Integer
    utc_wHour As Integer: utc_wMinute As Integer: utc_wSecond As Integer: utc_wMilliseconds As Integer
End Type

Private Type utc_TIME_ZONE_INFORMATION
    utc_Bias As Long: utc_StandardName(0 To 31) As Integer: utc_StandardDate As utc_SYSTEMTIME: utc_StandardBias As Long
    utc_DaylightName(0 To 31) As Integer: utc_DaylightDate As utc_SYSTEMTIME: utc_DaylightBias As Long
End Type

'http://msdn.microsoft.com/library/windows/desktop/ms724421.aspx /ms724949.aspx /ms725485.aspx
Private Declare PtrSafe Function utc_GetTimeZoneInformation Lib "kernel32" Alias "GetTimeZoneInformation" _
    (utc_lpTimeZoneInformation As utc_TIME_ZONE_INFORMATION) As Long
Private Declare PtrSafe Function utc_SystemTimeToTzSpecificLocalTime Lib "kernel32" Alias "SystemTimeToTzSpecificLocalTime" _
    (utc_lpTimeZoneInformation As utc_TIME_ZONE_INFORMATION, utc_lpUniversalTime As utc_SYSTEMTIME, utc_lpLocalTime As utc_SYSTEMTIME) As Long
Private Declare PtrSafe Function utc_TzSpecificLocalTimeToSystemTime Lib "kernel32" Alias "TzSpecificLocalTimeToSystemTime" _
    (utc_lpTimeZoneInformation As utc_TIME_ZONE_INFORMATION, utc_lpLocalTime As utc_SYSTEMTIME, utc_lpUniversalTime As utc_SYSTEMTIME) As Long

Private Function utc_DateToSystemTime(utc_Value As Date) As utc_SYSTEMTIME ' "Helper Function" for Public subs (below)
    With utc_DateToSystemTime
        .utc_wYear = Year(utc_Value): .utc_wMonth = Month(utc_Value): .utc_wDay = Day(utc_Value)
        .utc_wHour = Hour(utc_Value): .utc_wMinute = Minute(utc_Value): .utc_wSecond = Second(utc_Value): .utc_wMilliseconds = 0
    End With
End Function

Private Function utc_SystemTimeToDate(utc_Value As utc_SYSTEMTIME) As Date ' "Helper Function" for Public Functions (below)
    utc_SystemTimeToDate = DateSerial(utc_Value.utc_wYear, utc_Value.utc_wMonth, utc_Value.utc_wDay) + _
        TimeSerial(utc_Value.utc_wHour, utc_Value.utc_wMinute, utc_Value.utc_wSecond)
End Function

'===============================================================================
Public Function TimestampToLocal(st As String) As Date
    TimestampToLocal = UTCtoLocal((Val(st) / 86400) + 25569)
End Function
Public Function LocalToTimestamp(dt As Date) As String
    LocalToTimestamp = (LocalToUTC(dt) - 25569) * 86400
End Function

Public Function UTCtoLocal(utc_UtcDate As Date) As Date
    On Error GoTo errorUTC
    Dim utc_TimeZoneInfo As utc_TIME_ZONE_INFORMATION, utc_LocalDate As utc_SYSTEMTIME
    utc_GetTimeZoneInformation utc_TimeZoneInfo
    utc_SystemTimeToTzSpecificLocalTime utc_TimeZoneInfo, utc_DateToSystemTime(utc_UtcDate), utc_LocalDate
    UTCtoLocal = utc_SystemTimeToDate(utc_LocalDate)
    Exit Function
errorUTC:
    Debug.Print "UTC parsing error: " & Err.Number & " - " & Err.Description: Stop
End Function

Public Function LocalToUTC(utc_LocalDate As Date) As Date
    On Error GoTo errorUTC
    Dim utc_TimeZoneInfo As utc_TIME_ZONE_INFORMATION, utc_UtcDate As utc_SYSTEMTIME
    utc_GetTimeZoneInformation utc_TimeZoneInfo
    utc_TzSpecificLocalTimeToSystemTime utc_TimeZoneInfo, utc_DateToSystemTime(utc_LocalDate), utc_UtcDate
    LocalToUTC = utc_SystemTimeToDate(utc_UtcDate)
    Exit Function
errorUTC:
    Debug.Print "UTC conversion error: " & Err.Number & " - " & Err.Description: Stop
End Function
我知道这似乎是一个可怕的代码,只是为了增加/减少几个小时的时间,但我煞费苦心地研究,希望找到一个可靠的方法