.NET按城市或经度和纬度获取时区

时间:2010-04-06 06:58:55

标签: .net timezone geolocation

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

是否可以轻松获取用户时区。和拉特。或城市名称?

我想知道facebook是如何做到的?

谢谢

3 个答案:

答案 0 :(得分:0)

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

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

哪个是脆弱的。

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

答案 1 :(得分:0)

您可以使用网络服务按城市或经度和纬度获取时区:

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

答案 2 :(得分:0)

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

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