将 unix 时间戳调整为与 Yahoo Finance 相同

Adjsut the unix timestamp to be the same on Yahoo Finance

在 Sheet1 中,我在 B1 中有代码(比如 "AA"),在 B2 中有开始日期(比如 21/4/2009),在 B3 中有结束日期(比如 23/4/2009)

当手动导航到所需的 url 时,我得到了这样的 link https://finance.yahoo.com/quote/AA/history?period1=1240290000&period2=1240462800&interval=1d&filter=history&frequency=1d

但是当使用代码来构造 link 时,我得到的 UNIX 时间戳几乎没有像这样 https://finance.yahoo.com/quote/AA/history?period1=1240272000&period2=1240444800&interval=1d&filter=history&frequency=1d

例如在两个 link 中的通知期 1 怎么调整代码才能和yahoo的link一致?

我试过类似的东西

period1 = ToUnix(.Range("B2").Value & " 05:00:00")

这解决了那些日期的问题,但没有解决其他不同日期的问题,所以我的逻辑不正确

这是我试过的代码

Sub Yahoo_Finance()
Dim ws          As Worksheet
Dim sURL        As String
Dim sTicker     As String
Dim period1     As Long
Dim period2     As Long
Dim r           As Long

Set ws = ThisWorkbook.Worksheets("Sheet1")
r = 6

With CreateObject("MSXML2.ServerXMLHTTP")
    With ws
        sTicker = .Range("B1").Value
        period1 = ToUnix(.Range("B2").Value & " 05:00:00")
        period2 = ToUnix(.Range("B3").Value & " 05:00:00")
    End With

    sURL = "https://finance.yahoo.com/quote/" & sTicker & "/history?period1=" & period1 & "&period2=" & period2 & "&interval=1d&filter=history&frequency=1d"

    Debug.Print sURL
End With
End Sub

Public Function ToUnix(dt) As Long
ToUnix = DateDiff("s", "1/1/1970", dt)
End Function

据我观察,它从指定开始日期前一天晚上 11 点到结束日期前一天晚上 11 点运行。因此,代码中 DateAdd -1 天从工作表中的日期中删除 1 天并确保小时为 23:00:00。然后网址匹配我。

Public Sub Yahoo_Finance()
    Dim ws          As Worksheet
    Dim sURL        As String
    Dim sTicker     As String
    Dim period1     As Long
    Dim period2     As Long

    Set ws = ThisWorkbook.Worksheets("Sheet1")

    With CreateObject("MSXML2.ServerXMLHTTP")
        With ws
            sTicker = .Range("B1").Value
            period1 = ToUnix(DateAdd("d", -1, .Range("B2").Value) & "23:00:00")
            period2 = ToUnix(DateAdd("d", -1, .Range("B3").Value) & "23:00:00")
        End With

        sURL = "https://finance.yahoo.com/quote/" & sTicker & "/history?period1=" & period1 & "&period2=" & period2 & "&interval=1d&filter=history&frequency=1d"

        Debug.Print sURL
    End With
End Sub

对于 GMT 本地时间转换,您可以通过 Rick Rothstein

尝试代码
Function Local2GMT(dtLocalDate As Date) As Date
    Local2GMT = DateAdd("s", -GetLocalToGMTDifference(), dtLocalDate)
End Function

Function GMT2Local(gmtTime As Date) As Date
    GMT2Local = DateAdd("s", GetLocalToGMTDifference(), gmtTime)
End Function

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

例如(根据 OP 反馈.. 不需要 DateAdd 调整和)

period1 = ToUnix(Local2GMT(.Range("B2").Value))