Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: adf2fb3dcb
Fetching contributors…

Cannot retrieve contributors at this time

42 lines (32 sloc) 1.325 kb
from django.conf import settings
from django.core.exceptions import ValidationError
from django.utils.encoding import smart_str
import pytz
def localtime_for_timezone(value, timezone):
Given a ``datetime.datetime`` object in UTC and a timezone represented as
a string, return the localized time for the timezone.
return adjust_datetime_to_timezone(value, settings.TIME_ZONE, timezone)
def adjust_datetime_to_timezone(value, from_tz, to_tz=None):
Given a ``datetime`` object adjust it according to the from_tz timezone
string into the to_tz timezone string.
if to_tz is None:
to_tz = settings.TIME_ZONE
if value.tzinfo is None:
if not hasattr(from_tz, "localize"):
from_tz = pytz.timezone(smart_str(from_tz))
value = from_tz.localize(value)
return value.astimezone(pytz.timezone(smart_str(to_tz)))
def coerce_timezone_value(value):
return pytz.timezone(value)
except pytz.UnknownTimeZoneError:
raise ValidationError("Unknown timezone")
def validate_timezone_max_length(max_length, zones):
def reducer(x, y):
return x and (len(y) <= max_length)
if not reduce(reducer, zones, True):
raise Exception("timezones.fields.TimeZoneField MAX_TIMEZONE_LENGTH is too small")
Jump to Line
Something went wrong with that request. Please try again.