Django timezone support outside of templates

Django's timezone-specific output seems to apply only when rendering a template . Is there a way to get the same automatic conversion to the current active timezone for responses returning CSV or JSON?

+6
source share
1 answer

It seems that the main function called to convert datetimes to templates is django.utils.timezone.template_localtime() . Next to it in the source is another function of the localtime utility, which looks like this:

 def localtime(value, timezone=None): """ Converts an aware datetime.datetime to local time. Local time is defined by the current time zone, unless another time zone is specified. """ ... 

So, perhaps the following will work:

 from django.utils.timezone import localtime, get_current_timezone ... print localtime(obj.date_created, user.get_profile().timezone or get_current_timezone()) 
+1
source

Source: https://habr.com/ru/post/949777/


All Articles