TopPicLogo TopPicText

Перевод времени в GMT и обратно

По материалам: http://www.excelfox.com/forum/f2/get-standard-gmt-time-from-the-system-using-vba-542/


Проверено, - работает (MSA-2010x32)

'--------------------------------------------------------------------
' Module    : modGMT_Time
' Author    : Rick Rothstein
' SRS       : http://www.excelfox.com/forum/f2/get-standard-gmt-time-from-the-system-using-vba-542/
' Date      : 01.02.2014
' Purpose   : Перевод времени в GMT и обратно
'
'--------------------------------------------------------------------
'
Option Compare Database
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

Public Function Local2GMT(dtLocalDate As Date) As Date
'Перевод местного времени в GMT
    Local2GMT = DateAdd("s", -GetLocalToGMTDifference(), dtLocalDate)
End Function

Public Function GMT2Local(gmtTime As Date) As Date
'Перевод GMT в местное
    GMT2Local = DateAdd("s", GetLocalToGMTDifference(), gmtTime)
End Function

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 TimeZoneInf As TIME_ZONE_INFORMATION
    Dim Ret As Long
    Dim Diff As Long
    Ret = GetTimeZoneInformation(TimeZoneInf)
    Diff = -TimeZoneInf.Bias * 60
    GetLocalToGMTDifference = Diff
    If Ret = TIME_ZONE_ID_DAYLIGHT& Then
        If TimeZoneInf.DaylightDate.wMonth <> 0 Then
            GetLocalToGMTDifference = Diff - TimeZoneInf.DaylightBias * 60
        End If
    End If
End Function
Назад ToTop
L.E. 06.05.2017
Рейтинг@Mail.ru