Skip to content

Commit

Permalink
Expose the display_timezone through a helper
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentGoderre committed Feb 11, 2016
1 parent 5e86b3e commit c01461b
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions ckan/lib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,10 @@ def _datestamp_to_datetime(datetime_):
# all dates are considered UTC internally,
# change output if `ckan.display_timezone` is available
datetime_ = datetime_.replace(tzinfo=pytz.utc)
timezone_name = config.get('ckan.display_timezone', '')
if timezone_name == 'server':
local_tz = tzlocal.get_localzone()
return datetime_.astimezone(local_tz)
timezone = get_display_timezone()

try:
datetime_ = datetime_.astimezone(
pytz.timezone(timezone_name)
)
datetime_ = datetime_.astimezone(timezone)
except pytz.UnknownTimeZoneError:
if timezone_name != '':
log.warning(
Expand Down Expand Up @@ -1010,6 +1005,19 @@ def _range(self, regexp_match):
return re.sub(current_page_span, current_page_link, html)


def get_display_timezone():
''' Returns a pytz timezone for the display_timezone setting in the
configuration file or UTC if not specified.
:rtype: timezone
'''
timezone_name = config.get('ckan.display_timezone') or 'utc'

if timezone_name == 'server':
return tzlocal.get_localzone()

return pytz.timezone(timezone_name)


def render_datetime(datetime_, date_format=None, with_hours=False):
'''Render a datetime object or timestamp string as a localised date or
in the requested format.
Expand Down Expand Up @@ -2158,6 +2166,7 @@ def get_translated(data_dict, field):
'linked_gravatar',
'gravatar',
'pager_url',
'get_display_timezone',
'render_datetime',
'date_str_to_datetime',
'parse_rfc_2822_date',
Expand Down

0 comments on commit c01461b

Please sign in to comment.