0

Given a location (lat, lng) and a date/time local to that location, how do I convert the date/time to UTC? I don't have the UTC offset or time zone of the location.

All of the time zone APIs I've looked at require a UTC timestamp but all I have is a local timestamp for the location, which obviously doesn't work properly to identity DST change-over etc.

Is there another way to do this?

dxiv
  • 16,984
  • 2
  • 27
  • 49
Tyson
  • 1
  • You an API that gives the **time zone** and convert it yourself. But SO is not the place for this – Charlieface Jan 11 '21 at 22:47
  • 1
    Pretty much what dxiv said. Except here is a C# specific thread: https://stackoverflow.com/questions/33639571/get-local-time-based-on-coordinates – Jack Jan 11 '21 at 22:57
  • @dxiv Unfortunately not, as to use those APIs I would need to convert my local time to UTC, which is the thing I can't do because I don't know the offset to apply. – Tyson Jan 11 '21 at 23:00
  • @Tyson No, not at all. You first convert (lat, long) to a timezone, then once you know the timezone you can convert the local time to whatever else you wish. – dxiv Jan 11 '21 at 23:01
  • @Tyson I don't know where you got the idea that you need UTC to call those APIs. e.g. https://timezonedb.com/references/get-time-zone You put in the lat&lon, it gives you back time zone, time zone offset, and UtcNow. – Ben Voigt Jan 11 '21 at 23:04
  • @BenVoigt A timestamp is required because I need to know the time zone in effect at the location at a particular time (not the current time). This is important because a location (e.g. Sydney) may be UTC+10 at a given time, then change to UTC+11 one second later when DST begins. – Tyson Jan 11 '21 at 23:10
  • @Tyson: Once you have a timezone name, you can look up in a time zone database all the rules for when each different offset applies. But normally the LocalToUniversal API of your choice (e.g. NodaTime `DateTimeZone.MapLocal()`) handles that part for you, you just need to know what to pass that API for the "which timezone" parameter (for NodaTime, that's the `this` object you call the function on), and the lat/lon -> timezone resolution supplies the value for that parameter. – Ben Voigt Jan 11 '21 at 23:12
  • @Tyson I think you may be confusing "timezone" with "UTC offset". They are not the same, see for example the [Time Zone != Offset](https://stackoverflow.com/tags/timezone/info) section of the wiki. The location gives you the timezone, then the timezone + local time gives you the UTC offset. – dxiv Jan 11 '21 at 23:15
  • @Tyson: The specific code you should probably use is in [this answer](https://stackoverflow.com/a/54095998/634824). – Matt Johnson-Pint Jan 12 '21 at 00:15
  • Thanks everyone for your input. As suggested, I'll use an API to resolve the location's time zone then NodaTime or similar to perform the conversion from local time. – Tyson Jan 12 '21 at 01:33

0 Answers0