Tom*_*Tom 6 django timezone django-timezone
Django的时区感知输出显然仅适用于渲染模板.有没有办法让返回CSV或JSON的响应的当前活动时区进行相同的自动转换?
似乎在模板中调用转换日期时间的底层函数是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.
"""
...
Run Code Online (Sandbox Code Playgroud)
所以也许以下方法会起作用:
from django.utils.timezone import localtime, get_current_timezone
...
print localtime(obj.date_created, user.get_profile().timezone or get_current_timezone())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
310 次 |
| 最近记录: |