Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if user.utc_offset:
timezone = pytz.FixedOffset(user.utc_offset/60)
# pytz FixedOffset instances return None for dst(), but then
# astimezone() complains, so we just pretend like the DST offset
# is always 0.
timezone.dst = lambda self: datetime.timedelta(0)
return timezone
# Starting in April 2018*, Twitter no longer returns timezone data for users.
# We default to PST since it's more likely to be accurate (at least for
# Mihai) than UTC. The above code thus will never run, but keep it in case
# Twitter changes their mind.
#
# * https://twittercommunity.com/t/upcoming-changes-to-the-developer-platform/104603
return pytz.timezone('America/Los_Angeles')
def get_tzinfo(timezone=None):
"""Returns a ``datetime.tzinfo`` object for the given timezone. This is
called by :func:`format_datetime` and :func:`format_time` when a tzinfo
is not provided.
:param timezone:
The timezone name from the Olson database, e.g.: 'America/Chicago'.
If not set, uses the default configuration value.
:return:
A ``datetime.tzinfo`` object.
"""
if timezone is None:
timezone = get_config(__name__, 'timezone')
if timezone not in _timezones:
_timezones[timezone] = pytz.timezone(timezone)
return _timezones[timezone]
def validate(self, value):
value = super(TimezoneProperty, self).validate(value)
if value is None or hasattr(value, 'zone'):
return value
elif isinstance(value, basestring):
return pytz.timezone(value)
raise db.BadValueError("Property %s must be a pytz timezone or string."
% self.name)
def get_timezone(timezone=None):
"""Returns a ``datetime.tzinfo`` object for the given timezone. This is
called by [[#format_datetime]] and [[#format_time]] when a tzinfo
is not provided.
:param timezone:
The timezone name from the Olson database, e.g.: ``America/Chicago``.
If not set, uses the default configuration value.
:returns:
A ``datetime.tzinfo`` object.
"""
return pytz.timezone(timezone or get_config(__name__, 'timezone'))
def validate(self, value):
value = super(TimezoneProperty, self).validate(value)
if value is None or hasattr(value, 'zone'):
return value
elif isinstance(value, basestring):
return pytz.timezone(value)
raise db.BadValueError("Property %s must be a pytz timezone or string."
% self.name)
def get_timezone(timezone=None):
"""Returns a ``datetime.tzinfo`` object for the given timezone. This is
called by :func:`format_datetime` and :func:`format_time` when a tzinfo
is not provided.
* timezone:
The timezone name from the Olson database, e.g.: 'America/Chicago'.
If not set, uses the default configuration value.
* Return:
A ``datetime.tzinfo`` object.
"""
return pytz.timezone(timezone or get_config(__name__, 'timezone'))
def get_timezone(timezone=None):
"""Returns a ``datetime.tzinfo`` object for the given timezone. This is
called by [[#format_datetime]] and [[#format_time]] when a tzinfo
is not provided.
:param timezone:
The timezone name from the Olson database, e.g.: ``America/Chicago``.
If not set, uses the default configuration value.
:returns:
A ``datetime.tzinfo`` object.
"""
return pytz.timezone(timezone or Tipfy.app.get_config(__name__, 'timezone'))
def make_value_from_datastore(self, value):
return pytz.timezone(value)
def validate(self, value):
value = super(TimezoneProperty, self).validate(value)
if value is None or hasattr(value, 'zone'):
return value
elif isinstance(value, basestring):
return pytz.timezone(value)
raise db.BadValueError("Property %s must be a pytz timezone or string."
% self.name)