Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions openquake/hazardlib/shakemap/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
from xml.parsers.expat import ExpatError
from datetime import datetime
from zoneinfo import ZoneInfo
from timezonefinder import TimezoneFinder

from shapely.geometry import shape, Point, Polygon
import pandas as pd
Expand Down Expand Up @@ -470,7 +469,12 @@ def utc_to_local_time(utc_timestamp, lon, lat):
Convert a timestamp '%Y-%m-%dT%H:%M:%S.%fZ' into a datetime object
"""
utc_time = datetime.strptime(utc_timestamp, '%Y-%m-%dT%H:%M:%S.%fZ')
timezone_str = TimezoneFinder().timezone_at(lng=lon, lat=lat)
try:
from timezonefinder import TimezoneFinder
except ImportError:
timezone_str = None
else:
timezone_str = TimezoneFinder().timezone_at(lng=lon, lat=lat)
if timezone_str is None:
logging.warning('Could not determine the timezone. Using the UTC time')
return utc_time
Expand Down
8 changes: 8 additions & 0 deletions openquake/hazardlib/tests/shakemap/parsers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@


class ShakemapParsersTestCase(unittest.TestCase):
@classmethod
def setUp(cls):
try:
import timezonefinder
except ImportError:
raise unittest.SkipTest('Missing timezonefinder')
else:
del timezonefinder

def test_utc_to_local_time(self):
loctime = utc_to_local_time('2011-01-01T12:00:00.0Z', 10., 45.)
Expand Down
8 changes: 8 additions & 0 deletions openquake/hazardlib/tests/shakemap/validate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@


class ImpactValidateTestCase(unittest.TestCase):
@classmethod
def setUp(cls):
try:
import timezonefinder
except ImportError:
raise unittest.SkipTest('Missing timezonefinder')
else:
del timezonefinder

def test_1(self):
POST = {'usgs_id': 'us6000jllz', 'approach': 'use_shakemap_from_usgs'}
Expand Down
9 changes: 9 additions & 0 deletions openquake/server/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ def ready(self):
raise ValueError(
f'Invalid application mode: "{settings.APPLICATION_MODE}".'
f' It must be one of {settings.APPLICATION_MODES}')
if settings.APPLICATION_MODE == 'IMPACT':
try:
# NOTE: optional dependency needed for IMPACT
from timezonefinder import TimezoneFinder # noqa
except ImportError:
raise ImportError(
'The python package "timezonefinder" is not installed.'
' It is required in order to convert the UTC time to'
' the local time of the event.')
if (settings.LOCKDOWN and 'django_pam.auth.backends.PAMBackend'
not in settings.AUTHENTICATION_BACKENDS):
# check essential constants are defined
Expand Down