.NET get time zone by city or by longitude and latitude

I am using .NET and I can geolocate the user's city by IP.

Is there an easy way to get the user's time zone by length. and lat. or city name?

I wonder how facebook does it?

thank

+3
source share
3 answers

There is a web service that you can use to get the time zone by city, longitude and latitude:

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

0
source

You can get a list of time zones from the system and compare them with the name of the city:

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

What is fragile.

-, , long/lat, , , - .

0

I am sure that this code can be simplified, but it helped me get the identifier and name of TimeZone.

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
0
source

Source: https://habr.com/ru/post/1739949/


All Articles