3

I have a UTC timestamp that I would like to convert to a local timestamp for given latitude+longitude coordinates. I have seen multiple StackOverflow answers on how to convert a timestamp to the local time in a given timezone, but my issue is that the timezone for a given location depends on the time of year, since that affects daylight savings (so the location alone is insufficient to determine the time zone). For instance, a given UTC timestamp might correspond to 12:30am in a given location, but adding 86400 seconds to it corresponds to 11:30pm of the same day (in the event that daylight savings was changed that day).

Is there a library that can convert an absolute timestamp to a local date and time given latitude and longitude coordinates, or some way to figure out whether a location is experiencing DST at a given time?

BallpointBen
  • 9,406
  • 1
  • 32
  • 62
  • To be clear, the steps would be: for each lat/lon, get the timezone; plug in the timestamp and check if it's DST; change the timezone to DST if appropriate; convert the timestamp to this final timezone? – BallpointBen Jan 04 '19 at 17:51
  • See the dup question to get the time zone from lat/lon. Then use a library like [dateutil](https://pypi.org/project/python-dateutil/) or [pytz](https://pypi.org/project/pytz/) to do the rest. – Matt Johnson-Pint Jan 04 '19 at 18:05
  • It looks like this does the job: https://stackoverflow.com/a/6617418/1256113 – TheNavigat Jan 04 '19 at 18:07
  • You don’t do any manual DST changes. You make a proper UTC `datetime`, and you convert it to another timezone. The library worries about DSTs and all other pitfalls. – deceze Jan 04 '19 at 18:13
  • @deceze I don’t know which time zone to convert to though, because it’s not clear whether DST is in effect – BallpointBen Jan 04 '19 at 18:14
  • 2
    A proper timezone is something like `Europe/London`. The library (e.g. `pytz`) will know whether DST is in effect there for any given timestamp. – deceze Jan 04 '19 at 18:16
  • Ah, that solves it. Thanks – BallpointBen Jan 04 '19 at 18:31

0 Answers0