在我的django项目的settings.py文件中,我有这样一行:

TIME_ZONE = 'UTC'

但我想让我的应用程序在UTC+2时区运行,所以我把它改成了

TIME_ZONE = 'UTC+2'

它给出错误ValueError:不正确的时区设置:UTC+2。正确的做法是什么?

谢谢!


当前回答

从tzinfo数据库中选择一个有效的时区。它们倾向于采用例如Africa/Gaborne和US/Eastern的形式

找到与您最近的城市相匹配的城市,或者与您的时区相匹配的城市,然后将TIME_ZONE的值设置为匹配。

其他回答

以下是有效的时区列表:

http://en.wikipedia.org/wiki/List_of_tz_database_time_zones

你可以使用

TIME_ZONE = 'Europe/Istanbul'

对于 UTC+02:00

从tzinfo数据库中选择一个有效的时区。它们倾向于采用例如Africa/Gaborne和US/Eastern的形式

找到与您最近的城市相匹配的城市,或者与您的时区相匹配的城市,然后将TIME_ZONE的值设置为匹配。

Change the TIME_ZONE to your local time zone, and keep USE_TZ as True in 'setting.py': TIME_ZONE = 'Asia/Shanghai' USE_I18N = True USE_L10N = True USE_TZ = True This will write and store the datetime object as UTC to the backend database. Then use template tag to convert the UTC time in your frontend template as such: <td> {% load tz %} {% get_current_timezone as tz %} {% timezone tz %} {{ message.log_date | time:'H:i:s' }} {% endtimezone %} </td>

或者简洁地使用模板过滤器:

                <td> 
                    {% load tz %}
                    {{ message.log_date | localtime | time:'H:i:s' }}
                </td>

您可以在官方文档中查看更多详细信息:默认时区和当前时区 当支持时区时,Django在数据库中存储UTC格式的datetime信息,在内部使用时区感知的datetime对象,并在模板和表单中将它们转换为最终用户的时区。

这对我来说很好(我仍然在本地测试应用程序)。 在settings.py文件类型中:

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Beirut'

USE_I18N = True

USE_L10N = True

USE_TZ = False

#使用您自己的TIME_ZONE: https://gist.github.com/heyalexej/8bf688fd67d7199be4a1682b3eec7568

正确使用区域/位置格式。 例子:

TIME_ZONE = 'Asia/Kolkata'