【问题标题】:.NET get timezone by city or by longitude and latitude [duplicate].NET按城市或经度和纬度获取时区[重复]
【发布时间】:2011-02-04 16:55:19
【问题描述】:

我正在使用 .NET,我能够通过 IP 对用户城市进行地理定位。

有没有简单的方法让用户时区很长。和纬度。还是城市名?

不知道facebook是怎么做到的?

谢谢

【问题讨论】:

标签: .net timezone geolocation


【解决方案1】:

有一个 Web 服务可以用来按城市或经纬度获取时区:

http://www.earthtools.org/webservices.htm#timezone

【讨论】:

  • 这并不是真正的全时区 IMO。这是与 UTC 的当前偏移量 - 这意味着您不知道 DST 何时会更改等。遗憾的是它没有给出 Olsen 名称。
  • 是的,但响应确实包括本地时间(考虑夏令时,如果它有效),UTC 时间,以及夏令时是否有效 - 可能对许多人来说已经足够了目的。一个例子:earthtools.org/timezone-1.1/57.09/02.05
  • 这并没有考虑欧洲以外的夏令时,所以它本质上是毫无价值的。
【解决方案2】:

您可以从系统中获取时区列表并将其与城市名称进行比较:

http://msdn.microsoft.com/en-us/library/bb397781.aspx

这是脆弱的。

或者,您可以使用一些 Web 服务,将其传递 long/lat 并为您提供时区,但这意味着您被绑定到第三方 Web 服务。

【讨论】:

  • 如果您想运行自己的服务而不是依赖 Web 服务,您可以尝试 askgeo.com,它出售一个 Java 库,该库提供给定纬度/经度的时区 ID。
【解决方案3】:

我确信这段代码可以简化,但这对我来说可以得到 TimeZone ID 和 Name。

Private Sub checkTZ()
    Dim wc As WebClient = New WebClient()
    Dim str As String = Nothing
    Dim latPattern As String = "\bLatitude.*\<{1}"
    Dim latRegSearch As Regex = New Regex(latPattern)
    Dim lat As String = Nothing
    Dim lonPattern As String = "\bLongitude.*\<{1}"
    Dim lonRegSearch As Regex = New Regex(lonPattern)
    Dim lon As String = Nothing

    Try
        str = wc.DownloadString("http://www.ipinfodb.com/my_ip_location.php")
        lat = latRegSearch.Match(str).Value
        lon = lonRegSearch.Match(str).Value
    Catch ex As Exception
        str = "Not Found"
    End Try

    Dim pattern As String = "\<|\s|\:|\bLatitude|\bLongitude"
    Dim rgx As New Regex(pattern)
    lat = rgx.Replace(lat, "")
    lon = rgx.Replace(lon, "")

    Dim firefoxEpochDate As Date = "1/1/1970"
    Dim s_CurrentGoogleAPI As Long = DateDiff(DateInterval.Second, firefoxEpochDate, DateTime.UtcNow)
    Dim xmldoc As XmlDocument = New XmlDocument()
    Dim results2 As String = wc.DownloadString("https://maps.googleapis.com/maps/api/timezone/xml?location=" & lat & "," & lon & "&timestamp=" & s_CurrentGoogleAPI)
    xmldoc.LoadXml(results2)
    Dim tz_offset_hours As Double = (Convert.ToDouble(xmldoc.DocumentElement.Item("raw_offset").InnerText) + Convert.ToDouble(xmldoc.DocumentElement.Item("dst_offset").InnerText)) / 3600
End Sub

【讨论】:

    猜你喜欢
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    相关资源
    最近更新 更多