diff --git a/neon_utils/location_utils.py b/neon_utils/location_utils.py index 96ac6e88..84716078 100644 --- a/neon_utils/location_utils.py +++ b/neon_utils/location_utils.py @@ -25,11 +25,13 @@ # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +from time import time -import pendulum +import pytz from datetime import datetime from typing import Optional, Union + from dateutil.tz import tzlocal from timezonefinder import TimezoneFinder from re import sub @@ -143,10 +145,11 @@ def get_timezone(lat, lng) -> (str, float): Note that some coordinates do not have a city, but may have a county. :param lat: latitude :param lng: longitude - :return: timezone name, offset from GMT + :return: timezone name, offset in hours from UTC """ timezone = TimezoneFinder().timezone_at(lng=float(lng), lat=float(lat)) - offset = pendulum.from_timestamp(0, timezone).offset_hours + offset = pytz.timezone(timezone).utcoffset( + datetime.now()).total_seconds() / 3600 return timezone, offset diff --git a/requirements/requirements.txt b/requirements/requirements.txt index 69d4432c..0082c8f2 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -1,6 +1,6 @@ ovos-bus-client~=0.0.3 combo-lock~=0.2 -pendulum~=2.1 +pytz>=2022.1 timezonefinder~=5.2 nltk~=3.5 pyyaml>=5.4,<7.0 diff --git a/tests/location_util_tests.py b/tests/location_util_tests.py index 954948b6..ec5048b5 100644 --- a/tests/location_util_tests.py +++ b/tests/location_util_tests.py @@ -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"))