Skip to content

Commit

Permalink
[#2494] Displayed all datetimes with the users utc offset
Browse files Browse the repository at this point in the history
- This is either saved in the beaker session
- otherwise it defaults to zero
- The display of a full date also shows the currently used timezone
  incl. the offset (e.g. UTC+2)
  • Loading branch information
Stefan Oderbolz committed Jun 30, 2015
1 parent 58abf68 commit dee22ac
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
32 changes: 23 additions & 9 deletions ckan/lib/formatters.py
Expand Up @@ -72,7 +72,7 @@ def _month_dec():
_month_sept, _month_oct, _month_nov, _month_dec]


def localised_nice_date(datetime_, show_date=False, with_hours=False):
def localised_nice_date(datetime_, show_date=False, with_hours=False, utc_offset_mins=0):
''' Returns a friendly localised unicode representation of a datetime.
:param datetime_: The date to format
Expand Down Expand Up @@ -132,16 +132,29 @@ def months_between(date1, date2):
# all dates are considered UTC internally,
# change output if `ckan.timezone` is available
tz_datetime = datetime_.replace(tzinfo=pytz.utc)
timezone_name = config.get('ckan.timezone', '')
try:
tz_datetime = tz_datetime.astimezone(
pytz.timezone(config.get('ckan.timezone', ''))
pytz.timezone(timezone_name)
)
timezone_display_name = tc_dateimt.tzinfo.zone
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', '')
if timezone_name != '':
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. You can specify the special value '
'`browser` to displayed the dates according to the browser '
'settings of the visiting user.' % timezone_name
)
offset = datetime.timedelta(minutes=utc_offset_mins)
tz_datetime = tz_datetime + offset

utc_offset_hours = utc_offset_mins / 60
timezone_display_name = "UTC{1:+0.{0}f}".format(
int(utc_offset_hours % 1 > 0),
utc_offset_hours
)

# actual date
Expand All @@ -151,11 +164,12 @@ def months_between(date1, date2):
'day': tz_datetime.day,
'year': tz_datetime.year,
'month': _MONTH_FUNCTIONS[tz_datetime.month - 1](),
'timezone': tz_datetime.tzinfo.zone,
'timezone': timezone_display_name,
}

if with_hours:
return (
# NOTE: This is for translating dates like `April 24, 2013, 10:45 (UTC)`
# NOTE: This is for translating dates like `April 24, 2013, 10:45 (UTC+2)`
_('{month} {day}, {year}, {hour:02}:{min:02} ({timezone})') \
.format(**details))
else:
Expand Down
7 changes: 6 additions & 1 deletion ckan/lib/helpers.py
Expand Up @@ -963,14 +963,19 @@ def render_datetime(datetime_, date_format=None, with_hours=False):
:rtype: string
'''
datetime_ = _datestamp_to_datetime(datetime_)
try:
utc_offset_mins = int(session.get('utc_offset_mins', 0))
except TypeError:
utc_offset_mins = 0
if not datetime_:
return ''
# if date_format was supplied we use it
if date_format:
return datetime_.strftime(date_format)
# the localised date
return formatters.localised_nice_date(datetime_, show_date=True,
with_hours=with_hours)
with_hours=with_hours,
utc_offset_mins=utc_offset_mins)


def date_str_to_datetime(date_str):
Expand Down

0 comments on commit dee22ac

Please sign in to comment.