Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raise when unable to detect name of local timezone #3576

Merged
merged 3 commits into from
Nov 26, 2019
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
4 changes: 3 additions & 1 deletion aiida/backends/djsite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

# Local time zone for this installation. Always choose the system timezone
# Local time zone for this installation. Always choose the system timezone.
# Note: This causes django to set the 'TZ' environment variable, which is read by tzlocal from then onwards.
# See https://docs.djangoproject.com/en/2.2/ref/settings/#std:setting-TIME_ZONE
TIME_ZONE = get_current_timezone().zone

SITE_ID = 1
Expand Down
7 changes: 6 additions & 1 deletion aiida/backends/tests/common/test_timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ class TimezoneTest(unittest.TestCase):
"""Tests for the timezone utility module."""

def test_timezone_now(self):
"""Test timezone.now function."""
"""Test timezone.now function.

Check that the time returned by AiiDA's timezone.now() function is compatible
with attaching a timezone to a "naive" time stamp using timezone.make_aware().
"""
delta = datetime.timedelta(minutes=1)
ref = timezone.now()

from_tz = timezone.make_aware(datetime.datetime.fromtimestamp(time.time()))
self.assertLessEqual(from_tz, ref + delta)
self.assertGreaterEqual(from_tz, ref - delta)
13 changes: 11 additions & 2 deletions aiida/common/timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ def get_current_timezone():
:return: current timezone
"""
from tzlocal import get_localzone
return get_localzone()
local = get_localzone()

if local.zone == 'local':
raise ValueError(
"Unable to detect name of local time zone. Please set 'TZ' environment variable, e.g."
" to 'Europe/Zurich'"
)
return local


def now():
Expand Down Expand Up @@ -70,8 +77,10 @@ def make_aware(value, timezone=None, is_dst=None):

if is_aware(value):
raise ValueError('make_aware expects a naive datetime, got %s' % value)

# This may be wrong around DST changes!
return value.replace(tzinfo=timezone)
# See http://pytz.sourceforge.net/#localized-times-and-date-arithmetic
return timezone.localize(value)


def localtime(value, timezone=None):
Expand Down
13 changes: 6 additions & 7 deletions aiida/manage/backup/backup_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from abc import abstractmethod, ABCMeta
import six
from dateutil.parser import parse
from pytz import timezone as ptimezone

from aiida.common import json
from aiida.common import timezone as dtimezone
Expand Down Expand Up @@ -136,11 +135,11 @@ def _read_backup_info_from_dict(self, backup_variables): # pylint: disable=too-
try:
self._oldest_object_bk = parse(backup_variables.get(self.OLDEST_OBJECT_BK_KEY))
if self._oldest_object_bk.tzinfo is None:
curr_timezone = str(dtimezone.get_current_timezone())
self._oldest_object_bk = ptimezone(curr_timezone).localize(self._oldest_object_bk)
curr_timezone = dtimezone.get_current_timezone()
self._oldest_object_bk = dtimezone.get_current_timezone().localize(self._oldest_object_bk)
self._logger.info(
'No timezone defined in the oldest modification date timestamp. Setting current timezone (%s).',
curr_timezone
curr_timezone.zone
)
# If it is not parsable...
except ValueError:
Expand Down Expand Up @@ -168,13 +167,13 @@ def _read_backup_info_from_dict(self, backup_variables): # pylint: disable=too-
self._end_date_of_backup = parse(backup_variables.get(self.END_DATE_OF_BACKUP_KEY))

if self._end_date_of_backup.tzinfo is None:
curr_timezone = str(dtimezone.get_current_timezone())
curr_timezone = dtimezone.get_current_timezone()
self._end_date_of_backup = \
ptimezone(curr_timezone).localize(
curr_timezone.localize(
self._end_date_of_backup)
self._logger.info(
'No timezone defined in the end date of backup timestamp. Setting current timezone (%s).',
curr_timezone
curr_timezone.zone
)

self._internal_end_date_of_backup = self._end_date_of_backup
Expand Down