Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get local time based on coordinates

Tags:

c#

datetime

I am programming an application that should give the local time based on the coordinates (lat&long) that you give it.

I only know of 2 methods to do that:

1st: Get the TimeZone Name, and then find its local time. 2nd: Use the Google API and receive the time as an offset and UTC not Local.

I decided to use the 1st method because seemed easier, so I decided to use the GeoTimeZone to get the Time Zone... Problem is that then I don´t know how to get the local time on that TimeZone... Here´s the code I wrote to get the TimeZone name.

string tz = TimeZoneLookup.GetTimeZone(lat, lon).Result;

variables lat & lon are of course the coordinates.

Thank you!

Edit: My question is how can I get the LocalTime on that TimeZone?

like image 1000
CTABUYO Avatar asked Nov 10 '15 21:11

CTABUYO


People also ask

How do you find time zones using latitude and longitude?

Here's how longitude converts into your personal time zone: 15 degrees of longitude = 1 hour difference; 1 degree longitude = 4 minutes difference. 15 minutes of longitude = 1 minute difference; 1 minute of longitude = 4 seconds difference.

Are time zones based on longitude?

Time zones are another arbitrary societal choice, like the origin point of the Prime Meridian. They are based on longitude and defined by Earth's rotation, which completes a full circle (360 degrees) each day (24 hours). Each hour then, Earth rotates through 360/24 = 15° of longitude: the width of one time zone.

What do we call the latitude and longitude of a specific place?

Both the latitude and longitude parts together are the coordinates. In other words, where you say: This is the coordinate of the point.


1 Answers

Here's my solution. It works offline (so no call to an api). It's fast and the packages are widely used and available on Nuget.

string tzIana = TimeZoneLookup.GetTimeZone(lat, lng).Result;
TimeZoneInfo tzInfo = TZConvert.GetTimeZoneInfo(tzIana);
DateTimeOffset convertedTime = TimeZoneInfo.ConvertTime(DateTimeOffset.UtcNow, tzInfo);
like image 192
Éric Bergeron Avatar answered Sep 17 '22 17:09

Éric Bergeron