- 如果
USE_TZ=True;
需要设置好mysql时区转化问题- macos下的解决方法:
mysql_tzinfo_to_sql /usr/share/zoneinfo > zone_import.sql cat zone_import.sql | mysql -u root mysql -p mysql -u root mysql -p select CONVERT_TZ(now(), 'UTC', 'Asia/Shanghai'); mysql -u root -p -e "flush tables;" mysql
- macos下的解决方法:
- 使用USE_TZ = True,需要注意问题:
参考:http://blog.csdn.net/w6299702/article/details/38782607
启用 USE_TZ = True 后,处理时间方面,有两条 “黄金法则”:
1. 保证存储到数据库中的是 UTC 时间;
2. 在函数之间传递时间参数时,确保时间已经转换成 UTC 时间;
比如,通常获取当前时间用的是:
import datetime
now = datetime.datetime.now()
启用 USE_TZ = True 后,需要写成:
import datetime
from django.utils.timezone import utc
utcnow = datetime.datetime.utcnow().replace(tzinfo=utc)
- 模板
除非应用支持用户设置自己所在的时区,通常我们不需要关心模板的时区问题。模板在展示时间的时候,会使用 settings.TIME_ZONE 中的设置自动把 UTC 时间转成 settings.TIME_ZONE 所在时区的时间渲染。
TIME_ZONE = 'Asia/Shanghai'
- 查看mysql时区信息
show variables like '%time_zone%'; select now(); SELECT @@global.time_zone, @@session.time_zone;
发表回复