Skip to content

Commit

Permalink
Troubleshoot offset calculation in get_timezone
Browse files Browse the repository at this point in the history
Add second timezone test case to account for runner location
  • Loading branch information
NeonDaniel committed May 31, 2024
1 parent ba9b5ee commit fd67de4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 2 additions & 4 deletions neon_utils/location_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,8 @@ def get_timezone(lat, lng) -> (str, float):
:return: timezone name, offset in hours from UTC
"""
timezone = TimezoneFinder().timezone_at(lng=float(lng), lat=float(lat))
_time = time()
utc_timestamp = datetime.utcfromtimestamp(_time).timestamp()
local_timestamp = datetime.fromtimestamp(_time, tz=pytz.timezone(timezone)).timestamp()
offset = (local_timestamp - utc_timestamp) / 3600
offset = pytz.timezone(timezone).utcoffset(
datetime.now()).total_seconds() / 3600
return timezone, offset


Expand Down
6 changes: 6 additions & 0 deletions tests/location_util_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ def test_get_timezone_from_coords(self):
self.assertIsInstance(offset, float)
self.assertIn(offset, (-7.0, -8.0))

lat = 35.0000
lon = 103.000
timezone, offset = get_timezone(lat, lon)
self.assertEqual(timezone, "Asia/Shanghai")
self.assertEqual(offset, 8.0)

def test_to_system_time(self):
from neon_utils.location_utils import to_system_time
tz_aware_dt = datetime.now(gettz("America/NewYork"))
Expand Down

0 comments on commit fd67de4

Please sign in to comment.