Added time zone field to user account settings, currently hidden behind a feature flag.

This commit is contained in:
Kevin Kim
2016-06-06 14:24:18 +00:00
parent 1d09f78742
commit 059985b9ec
20 changed files with 227 additions and 77 deletions

View File

@@ -5,8 +5,9 @@ Convenience methods for working with datetime objects
from datetime import datetime, timedelta
import re
from pytz import timezone, UTC, UnknownTimeZoneError
from django.utils.timezone import now
from django.utils.translation import pgettext, ugettext
from pytz import timezone, utc, UnknownTimeZoneError
def get_default_time_display(dtime):
@@ -52,7 +53,7 @@ def get_time_display(dtime, format_string=None, coerce_tz=None):
try:
to_tz = timezone(coerce_tz)
except UnknownTimeZoneError:
to_tz = UTC
to_tz = utc
dtime = to_tz.normalize(dtime.astimezone(to_tz))
if dtime is None or format_string is None:
return get_default_time_display(dtime)
@@ -62,6 +63,15 @@ def get_time_display(dtime, format_string=None, coerce_tz=None):
return get_default_time_display(dtime)
def get_formatted_time_zone(time_zone):
"""
Returns a formatted time zone (e.g. 'Asia/Tokyo (JST +0900)') for user account settings time zone drop down
"""
abbr = get_time_display(now(), '%Z', time_zone)
offset = get_time_display(now(), '%z', time_zone)
return "{name} ({abbr}, UTC{offset})".format(name=time_zone, abbr=abbr, offset=offset).replace("_", " ")
def almost_same_datetime(dt1, dt2, allowed_delta=timedelta(minutes=1)):
"""
Returns true if these are w/in a minute of each other. (in case secs saved to db
@@ -78,7 +88,7 @@ def to_timestamp(datetime_value):
Convert a datetime into a timestamp, represented as the number
of seconds since January 1, 1970 UTC.
"""
return int((datetime_value - datetime(1970, 1, 1, tzinfo=UTC)).total_seconds())
return int((datetime_value - datetime(1970, 1, 1, tzinfo=utc)).total_seconds())
def from_timestamp(timestamp):
@@ -89,7 +99,7 @@ def from_timestamp(timestamp):
If the timestamp cannot be converted, returns None instead.
"""
try:
return datetime.utcfromtimestamp(int(timestamp)).replace(tzinfo=UTC)
return datetime.utcfromtimestamp(int(timestamp)).replace(tzinfo=utc)
except (ValueError, TypeError):
return None