Django的时区感知输出显然是only applies when rendering a template。对于返回CSV或JSON的响应,有没有办法将其自动转换到当前活动的时区?
发布于 2013-10-10 02:44:34
为了在模板中转换日期时间而调用的底层函数似乎是django.utils.timezone.template_localtime()。在源代码中紧挨着它的是另一个实用函数localtime,它看起来如下所示:
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.
"""
...因此,下面的方法可能会起作用:
from django.utils.timezone import localtime, get_current_timezone
...
print localtime(obj.date_created, user.get_profile().timezone or get_current_timezone())https://stackoverflow.com/questions/17732034
复制相似问题