【问题标题】:Adjsut the unix timestamp to be the same on Yahoo Finance在 Yahoo Finance 上将 unix 时间戳调整为相同
【发布时间】:2019-07-21 05:36:36
【问题描述】:

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

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

但是当使用代码来构建链接时,我得到的 UNIX 时间戳略有不同,如下所示 https://finance.yahoo.com/quote/AA/history?period1=1240272000&period2=1240444800&interval=1d&filter=history&frequency=1d

Notice period1 例如在两个链接中 如何调整代码以与雅虎的链接相同?

我尝试过类似的方法

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

【问题讨论】:

  • 我很困惑。你是说在上面你现在使用你的 ToUnix 函数得到匹配的 url,但是其他 url 结构有问题?
  • 如果你省略“05:00:00”部分,你会得到不同的url。我的意思是在使用按日期过滤时,我需要获得与浏览器中相同的 url。尝试其他旧日期并注意浏览器上的 url 和在我的代码中构造的 url 没有这部分“05:00:00”

标签: excel vba yahoo-finance


【解决方案1】:

据我观察,它从指定日期前一天晚上 11 点开始,到结束日期前一天晚上 11 点。因此,DateAdd -1 day 在代码中从表格中的日期中删除 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))

【讨论】:

  • 非常感谢,我在 2012 年 3 月 1 日至 2012 年 3 月 5 日期间导航并搜索了 AA,并将链接复制到文本文件 https://finance.yahoo.com/quote/AA/history?period1=1330552800&amp;period2=1330898400&amp;interval=1d&amp;filter=history&amp;frequency=1d,然后在更改 B2 中的日期后运行代码B3 并获得了网址https://finance.yahoo.com/quote/AA/history?period1=1330513200&amp;period2=1330858800&amp;interval=1d&amp;filter=history&amp;frequency=1d。如您所见,它们仍然不同
  • 我认为这与格林威治标准时间有关,因为我已将 23:00:00 更改为 11:00:00 م 但这也不起作用。当我尝试period1 = ToUnix(DateAdd("d", -1, .Range("B2").Value) &amp; " 10:00:00 م") 时,它工作得很好,我尝试了几个具有这种格式的日期并且很好。有没有办法根据当地时间和格林威治标准时间的时差进行调整?
  • 不确定您是如何看待该网站的,因为上面的代码对我来说非常完美,无需调整。我使用的是 GMT,所以我想您可能需要转换。
  • 你可以试试这个:excelfox.com/forum/showthread.php/… 我假设该网站预计格林威治标准时间,但尚未验证。
  • 太完美了。我使用了 excelfox 链接上的代码,在尝试period1 = ToUnix(Local2GMT(.Range("B2").Value)) 时它对我来说效果很好。非常感谢您的大力帮助。
猜你喜欢
  • 1970-01-01
  • 2013-04-07
  • 1970-01-01
  • 2023-03-18
  • 2020-10-19
  • 2020-07-29
  • 1970-01-01
  • 2021-09-01
  • 1970-01-01
相关资源
最近更新 更多