diff --git a/ckan/lib/formatters.py b/ckan/lib/formatters.py index 3066c6a21d0..e917b65adc1 100644 --- a/ckan/lib/formatters.py +++ b/ckan/lib/formatters.py @@ -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 @@ -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 @@ -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: diff --git a/ckan/lib/helpers.py b/ckan/lib/helpers.py index f604da3ab7d..882d9d097fe 100644 --- a/ckan/lib/helpers.py +++ b/ckan/lib/helpers.py @@ -963,6 +963,10 @@ 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 @@ -970,7 +974,8 @@ def render_datetime(datetime_, date_format=None, with_hours=False): 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):