Skip to content

Commit

Permalink
[#2494] New option to change the timezone of displayed datetimes
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Oderbolz committed Jun 30, 2015
1 parent c8fcc9d commit 88218c2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
38 changes: 30 additions & 8 deletions ckan/lib/formatters.py
@@ -1,11 +1,15 @@
import datetime

import pytz
import logging
from pylons import config
from babel import numbers

import ckan.lib.i18n as i18n

from ckan.common import _, ungettext

log = logging.getLogger(__name__)


##################################################
# #
Expand Down Expand Up @@ -124,18 +128,36 @@ def months_between(date1, date2):
months).format(months=months)
return ungettext('over {years} year ago', 'over {years} years ago',
months / 12).format(years=months / 12)

# all dates are considered UTC internally,
# change output if `ckan.timezone` is available
tz_datetime = datetime_.replace(tzinfo=pytz.utc)
try:
tz_datetime = tz_datetime.astimezone(
pytz.timezone(config.get('ckan.timezone', ''))
)
except pytz.UnknownTimeZoneError:
log.warning(
'Timezone `%s` not found. '
'Please provide a valid timezone setting in `ckan.timezone` '
'or leave the field empty. All valid values can be found in '
'pytz.all_timezones.' % config.get('ckan.timezone', '')
)

# actual date
details = {
'min': datetime_.minute,
'hour': datetime_.hour,
'day': datetime_.day,
'year': datetime_.year,
'month': _MONTH_FUNCTIONS[datetime_.month - 1](),
'min': tz_datetime.minute,
'hour': tz_datetime.hour,
'day': tz_datetime.day,
'year': tz_datetime.year,
'month': _MONTH_FUNCTIONS[tz_datetime.month - 1](),
'timezone': tz_datetime.tzinfo.zone,
}
if with_hours:
return (
# NOTE: This is for translating dates like `April 24, 2013, 10:45`
_('{month} {day}, {year}, {hour:02}:{min:02}').format(**details))
# NOTE: This is for translating dates like `April 24, 2013, 10:45 (UTC)`
_('{month} {day}, {year}, {hour:02}:{min:02} ({timezone})') \
.format(**details))
else:
return (
# NOTE: This is for translating dates like `April 24, 2013`
Expand Down
15 changes: 15 additions & 0 deletions doc/maintaining/configuration.rst
Expand Up @@ -1556,6 +1556,21 @@ Default value: (none)

By default, the locales are searched for in the ``ckan/i18n`` directory. Use this option if you want to use another folder.

.. _ckan.timezone:

ckan.timezone
^^^^^^^^^^^^^

Example::

ckan.timezone = Europe/Zurich

Default value: UTC

By default, all datetimes are considered to be in the UTC timezone. Use this option to change the displayed dates on the frontend. Internally, the dates are always saved as UTC. This option only changes the way the dates are displayed.

The valid values for this options [can be found at pytz](http://pytz.sourceforge.net/#helpers) (``pytz.all_timezones``)

.. _ckan.root_path:

ckan.root_path
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -41,3 +41,4 @@ unicodecsv==0.9.4
vdm==0.13
wsgiref==0.1.2
zope.interface==4.1.1
pytz==2012j

0 comments on commit 88218c2

Please sign in to comment.