3

I'm using .NET and I'm able to geolocate user city by IP.

Is there easy way to get user timezone by long. and lat. or city name?

I wonder how facebook is doing it?

Thank You

Daniil Harik
  • 4,321
  • 8
  • 52
  • 60

3 Answers3

0

You can get a list of timezones from the system and compare it to the city name:

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

Which is fragile.

Alternatively there are a few web services out there you can use, passing it long/lat and it gives you timezone, but that means you're tethered to the third part web service.

Michael Shimmins
  • 19,511
  • 7
  • 54
  • 90
  • If you want to run your own service rather than rely on a web service, you can try out http://askgeo.com, which sells a Java Library that gives a time zone ID for a given lat/lon. – James D May 08 '12 at 22:06
0

There is a web service you can use to get the timezone by city or longitude and latitude:

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

Cocowalla
  • 11,717
  • 5
  • 58
  • 93
  • 1
    That isn't really a full time zone IMO. It's a current offset from UTC - which means you don't know when DST will change etc. It's a shame it doesn't give an Olsen name. – Jon Skeet Apr 06 '10 at 07:13
  • True, but the response does include the local time (taking into account daylight savings time, if it's in effect), UTC time, and whether or not daylight savings time is in effect - probably enough for many purposes. An example: http://www.earthtools.org/timezone-1.1/57.09/02.05 – Cocowalla Apr 06 '10 at 07:23
  • 1
    This does not take into account DST outside of Europe, so it is essentially worthless. – James D May 08 '12 at 22:01
0

I'm sure this code can be simplified, but this worked for me to get the TimeZone ID and 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