【问题标题】:Find DST timestamps between two timestamps查找两个时间戳之间的 DST 时间戳
【发布时间】:2020-09-26 22:14:45
【问题描述】:

有没有办法知道所有 DST 调整时间戳和两个时间戳之间的偏移量? 如果有不止一个 DST 调整,我想要所有实例的时间戳和偏移量。这里的偏差是指如果时钟向前移动 1 小时 offset=60,如果时钟向后移动 1 小时 offset=-60

例如

Time Zone: Pacific Time (UTC -8:00)
Start Time: 1 Nov 2020 0:00:00
End Time: 4 Nov 2020 0:00:00
Result: 1 Nov 2020 2:00:00 And Offset of -60 Minutes

【问题讨论】:

  • 谷歌“vba dst调整”,带你here
  • @HansPassant 这仅适用于当前日期,如果我想使用这种方法,我必须在开始时间和结束时间之间的所有日子里进行迭代。

标签: vba windows


【解决方案1】:

你可以使用我的项目VBA.Timezone-Windows中的函数BiasWindowsTimezone(下):

StartTime = #2020/11/01 00:00:00#
EndTime = #2020/11/04 00:00:00#
BiasDiff = BiasWindowsTimezone("Pacific", True, StartTime) - BiasWindowsTimezone("Pacific", True, EndTime)
BiasDiff  -> -60 

ExcelAccess 的演示应用程序包括在内。如果您运行 Excel 应用程序,请务必先调用函数 ReloadTimezones 来创建包含数据的工作表。

' Returns the timezone bias as specified in Windows from
' the name (key) of a timezone entry in the Registry.
' Accepts values without the common trailing "Standard Time".
'
' If Dst is true, and the current date is within daylight saving time,
' bias for daylight saving time is returned.
' If Date1 is specified, the bias of that date is returned.
'
' Returns a bias of zero if a timezone is not found.
'
' Examples:
'   Bias = BiasWindowsTimezone("Argentina")
'   Bias -> 180     ' Found
'
'   Bias = BiasWindowsTimezone("Argentina Standard Time")
'   Bias -> 180     ' Found.
'
'   Bias = BiasWindowsTimezone("Germany")
'   Bias -> 0       ' Not found.
'
'   Bias = BiasWindowsTimezone("Western Europe")
'   Bias -> 0       ' Not found.
'
'   Bias = BiasWindowsTimezone("W. Europe")
'   Bias -> -60     ' Found.
'
'   Bias = BiasWindowsTimezone("Paraguay", True, #2018-07-07#)
'   Bias -> 240     ' Found.
'
'   Bias = BiasWindowsTimezone("Paraguay", True, #2018-02-11#)
'   Bias -> 180     ' Found. DST.
'
'   Bias = BiasWindowsTimezone("Paraguay", False, #2018-02-11#)
'   Bias -> 240     ' Found.
'
' 2018-11-16. Gustav Brock. Cactus Data ApS, CPH.
'
Public Function BiasWindowsTimezone( _
    ByVal TimezoneName As String, _
    Optional Dst As Boolean, _
    Optional Date1 As Date) _
    As Long
    
    Static Entries()    As TimezoneEntry
    Static LastName     As String
    Static LastYear     As Integer
    
    Static Entry        As TimezoneEntry
    Dim ThisName        As String
    Dim ThisYear        As Integer
    Dim StandardDate    As Date
    Dim DaylightDate    As Date
    Dim DeltaBias       As Long
    Dim Bias            As Long
    
    If Trim(TimezoneName) = "" Then
        ' Nothing to look up.
        Exit Function
    Else
        ThisName = Trim(TimezoneName)
        ThisYear = Year(Date1)
        If LastName = ThisName And LastYear = ThisYear Then
            ' Use cached data.
        Else
            ' Retrieve the single entry or - if not found - an empty entry.
            Entries = RegistryTimezoneItems(ThisName, (ThisYear))
            Entry = Entries(LBound(Entries))
            LastName = ThisName
            LastYear = ThisYear
        End If
        If _
            StrComp(Entry.Name, TimezoneName, vbTextCompare) = 0 Or _
            StrComp(Replace(Entry.Name, StandardTimeLabel, ""), TimezoneName, vbTextCompare) = 0 Then
            ' Windows timezone found.
            
            ' Default is standard bias.
            DeltaBias = Entry.Tzi.StandardBias
            If Dst = True Then
                ' Return daylight bias if Date1 is of daylight saving time.
                StandardDate = DateSystemTime(Entry.Tzi.StandardDate)
                DaylightDate = DateSystemTime(Entry.Tzi.DaylightDate)
                
                If DaylightDate < StandardDate Then
                    ' Northern Hemisphere.
                    If DateDiff("s", DaylightDate, Date1) >= 0 And DateDiff("s", Date1, StandardDate) > 0 Then
                        ' Daylight time.
                        DeltaBias = Entry.Tzi.DaylightBias
                    Else
                        ' Standard time.
                    End If
                Else
                    ' Southern Hemisphere.
                    If DateDiff("s", DaylightDate, Date1) >= 0 Or DateDiff("s", Date1, StandardDate) > 0 Then
                        ' Daylight time.
                        DeltaBias = Entry.Tzi.DaylightBias
                    Else
                        ' Standard time.
                    End If
                End If
                
            End If
            ' Calculate total bias.
            Bias = Entry.Bias + DeltaBias
        End If
    End If

    BiasWindowsTimezone = Bias

End Function

【讨论】:

    猜你喜欢
    • 2014-07-12
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    • 2017-05-12
    • 2016-11-20
    相关资源
    最近更新 更多