summaryrefslogtreecommitdiff
path: root/django/utils/timezone.py
diff options
context:
space:
mode:
Diffstat (limited to 'django/utils/timezone.py')
-rw-r--r--django/utils/timezone.py52
1 files changed, 34 insertions, 18 deletions
diff --git a/django/utils/timezone.py b/django/utils/timezone.py
index 9572c99bac..71b160448e 100644
--- a/django/utils/timezone.py
+++ b/django/utils/timezone.py
@@ -20,12 +20,21 @@ from django.conf import settings
from django.utils.deprecation import RemovedInDjango50Warning
__all__ = [
- 'utc', 'get_fixed_timezone',
- 'get_default_timezone', 'get_default_timezone_name',
- 'get_current_timezone', 'get_current_timezone_name',
- 'activate', 'deactivate', 'override',
- 'localtime', 'now',
- 'is_aware', 'is_naive', 'make_aware', 'make_naive',
+ "utc",
+ "get_fixed_timezone",
+ "get_default_timezone",
+ "get_default_timezone_name",
+ "get_current_timezone",
+ "get_current_timezone_name",
+ "activate",
+ "deactivate",
+ "override",
+ "localtime",
+ "now",
+ "is_aware",
+ "is_naive",
+ "make_aware",
+ "make_naive",
]
# RemovedInDjango50Warning: sentinel for deprecation of is_dst parameters.
@@ -39,8 +48,8 @@ def get_fixed_timezone(offset):
"""Return a tzinfo instance with a fixed offset from UTC."""
if isinstance(offset, timedelta):
offset = offset.total_seconds() // 60
- sign = '-' if offset < 0 else '+'
- hhmm = '%02d%02d' % divmod(abs(offset), 60)
+ sign = "-" if offset < 0 else "+"
+ hhmm = "%02d%02d" % divmod(abs(offset), 60)
name = sign + hhmm
return timezone(timedelta(minutes=offset), name)
@@ -56,6 +65,7 @@ def get_default_timezone():
"""
if settings.USE_DEPRECATED_PYTZ:
import pytz
+
return pytz.timezone(settings.TIME_ZONE)
return zoneinfo.ZoneInfo(settings.TIME_ZONE)
@@ -86,6 +96,7 @@ def _get_timezone_name(timezone):
"""
return timezone.tzname(None) or str(timezone)
+
# Timezone selection functions.
# These functions don't change os.environ['TZ'] and call time.tzset()
@@ -104,6 +115,7 @@ def activate(timezone):
elif isinstance(timezone, str):
if settings.USE_DEPRECATED_PYTZ:
import pytz
+
_active.value = pytz.timezone(timezone)
else:
_active.value = zoneinfo.ZoneInfo(timezone)
@@ -133,11 +145,12 @@ class override(ContextDecorator):
time zone name, or ``None``. If it is ``None``, Django enables the default
time zone.
"""
+
def __init__(self, timezone):
self.timezone = timezone
def __enter__(self):
- self.old_timezone = getattr(_active, 'value', None)
+ self.old_timezone = getattr(_active, "value", None)
if self.timezone is None:
deactivate()
else:
@@ -152,6 +165,7 @@ class override(ContextDecorator):
# Templates
+
def template_localtime(value, use_tz=None):
"""
Check if value is a datetime and converts it to local time if necessary.
@@ -162,16 +176,17 @@ def template_localtime(value, use_tz=None):
This function is designed for use by the template engine.
"""
should_convert = (
- isinstance(value, datetime) and
- (settings.USE_TZ if use_tz is None else use_tz) and
- not is_naive(value) and
- getattr(value, 'convert_to_local_time', True)
+ isinstance(value, datetime)
+ and (settings.USE_TZ if use_tz is None else use_tz)
+ and not is_naive(value)
+ and getattr(value, "convert_to_local_time", True)
)
return localtime(value) if should_convert else value
# Utilities
+
def localtime(value=None, timezone=None):
"""
Convert an aware datetime.datetime to local time.
@@ -215,6 +230,7 @@ def now():
# By design, these four functions don't perform any checks on their arguments.
# The caller should ensure that they don't receive an invalid value like None.
+
def is_aware(value):
"""
Determine if a given datetime.datetime is aware.
@@ -247,9 +263,9 @@ def make_aware(value, timezone=None, is_dst=NOT_PASSED):
is_dst = None
else:
warnings.warn(
- 'The is_dst argument to make_aware(), used by the Trunc() '
- 'database functions and QuerySet.datetimes(), is deprecated as it '
- 'has no effect with zoneinfo time zones.',
+ "The is_dst argument to make_aware(), used by the Trunc() "
+ "database functions and QuerySet.datetimes(), is deprecated as it "
+ "has no effect with zoneinfo time zones.",
RemovedInDjango50Warning,
)
if timezone is None:
@@ -260,8 +276,7 @@ def make_aware(value, timezone=None, is_dst=NOT_PASSED):
else:
# Check that we won't overwrite the timezone of an aware datetime.
if is_aware(value):
- raise ValueError(
- "make_aware expects a naive datetime, got %s" % value)
+ raise ValueError("make_aware expects a naive datetime, got %s" % value)
# This may be wrong around DST changes!
return value.replace(tzinfo=timezone)
@@ -315,6 +330,7 @@ def _is_pytz_zone(tz):
def _datetime_ambiguous_or_imaginary(dt, tz):
if _is_pytz_zone(tz):
import pytz
+
try:
tz.utcoffset(dt)
except (pytz.AmbiguousTimeError, pytz.NonExistentTimeError):