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

.NET get timezone by city or by longitude and latitude

本问题已经有最佳答案,请猛点这里访问。

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

有没有简单的方法来获取用户时区。 和拉特。 或城市名称?

我想知道facebook是怎么做的?

谢谢


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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

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

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


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

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

哪个是脆弱的。

或者,你可以使用一些网络服务,传递它长/拉,它给你时区,但这意味着你被束缚到第三方Web服务。