Repository: ckan/ckan

Issue:
render_datetime helper does not respect ckan.display_timezone configuration
**CKAN version**
N/A

**Describe the bug**
[`ckan.display_timezone`](https://docs.ckan.org/en/latest/maintaining/configuration.html#ckan-display-timezone) sets the timezone to use when displaying dates on the frontend.  

The `render_date` template helper however,  still uses UTC even if `ckan.display_timezone` is set to a non-UTC timezone.

**Expected behavior**
`render_datetime` should respect the display_timezone setting.



Discussion:
After further investigation, found that [snippets/local_friendly_datetime.html](https://github.com/ckan/ckan/blob/master/ckan/templates/snippets/local_friendly_datetime.html) is one way to render date in the localised format using the current display_timezone setting.

However, one problem with using a snippet (as with all snippets), is that they're primarily meant for use in templates, not for assigning values that can be readily consumed by Javascript as snippets produce surrounding comments that will need to be parsed out.

Further, the local_friendly_datetime.html snippet does not actually do the conversion.  It just creates an HTML snippet with the `automatic-local-datetime` class which is then parsed and converted to the local format by CKAN's main.js.

https://github.com/ckan/ckan/blob/d5ee18452ea1be5a1e2e1cbc14e454b1c6b0d4b4/ckan/public/base/javascript/main.js#L33-L41

Perhaps an additional optional parameter should be added to the `render_datetime` helper to do the localisation/TZ conversion without having to depend on a snippet which can be used everywhere, not just in templates.
`render_datetime` should respect display_timezone setting as it calls https://github.com/ckan/ckan/blob/5aad00921d68721decc0b31b7007d31e0b8b5c15/ckan/lib/helpers.py#L1709 which calls 
https://github.com/ckan/ckan/blob/5aad00921d68721decc0b31b7007d31e0b8b5c15/ckan/lib/helpers.py#L180 But of course there might be a bug in there. 
Yes. I thought so myself.

However, an earlier check on line 174 if a datetime has timezone info short-circuits it and never reaches line 180.

https://github.com/ckan/ckan/blob/5aad00921d68721decc0b31b7007d31e0b8b5c15/ckan/lib/helpers.py#L174-L182
@Zharktas @jqnatividad   I would like to work in this issue But I have following queries :
                          -  Is this issue is specific to a version of CKAN ?
                          -  Where time is displayed in CKAN UI?
Hi, @jqnatividad @Zharktas 

Im doing some research around this issue and i noticed few things:

I've changed `ckan.display_timezone` to different timezone than my local (e.g Canada/Saskatchewan) and commented out the javascript snippet (.automatic-local-datetime) 
![image](https://user-images.githubusercontent.com/72216462/124925913-3f7feb80-dffd-11eb-9e87-e52b072b898b.png)

As we can see the UI doesnt respect `ckan.display_timezone` and shows the UTC time.

I noticed that flask-babel [`format_datetime`](https://flask-babel.tkte.ch/#flask_babel.format_datetime) looks like converts the object to users timezone by default, if we pass `rebase=False` at line https://github.com/ckan/ckan/blob/master/ckan/lib/formatters.py#L55
it shows correct value.
![image](https://user-images.githubusercontent.com/72216462/124926717-2461ab80-dffe-11eb-9236-ce70999aa589.png)

What's your opinion over this? 

Resolving patch:
diff --git a/ckan/config/middleware/flask_app.py b/ckan/config/middleware/flask_app.py
--- a/ckan/config/middleware/flask_app.py
+++ b/ckan/config/middleware/flask_app.py
@@ -247,6 +247,7 @@ def ungettext_alias():
     app.config[u'BABEL_TRANSLATION_DIRECTORIES'] = ';'.join(i18n_dirs)
     app.config[u'BABEL_DOMAIN'] = 'ckan'
     app.config[u'BABEL_MULTIPLE_DOMAINS'] = ';'.join(i18n_domains)
+    app.config[u'BABEL_DEFAULT_TIMEZONE'] = str(helpers.get_display_timezone())
 
     babel = CKANBabel(app)
 


Test patch: