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

TIME_ZONE = 'UTC'

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

TIME_ZONE = 'UTC+2'

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

谢谢!


当前回答

以下是有效的时区列表:

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

你可以使用达卡的时区

TIME_ZONE = 'Asia/Dhaka'

UTC+06:00(达卡或阿斯塔纳)

其他回答

添加Preferred TIME_ZONE并设置USE_TZ为False,如下所示

TIME_ZONE = '亚洲/加尔各答' USE_TZ = False

希望它能起作用。

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对象,并在模板和表单中将它们转换为最终用户的时区。

要从tz数据库中获取所有有效的时区名称(id),你可以在Python中使用pytz模块:

>>> import pytz # $ pip install pytz
>>> pytz.all_timezones_set
LazySet({'Africa/Abidjan',
         'Africa/Accra',
         'Africa/Addis_Ababa',
         'Africa/Algiers',
         'Africa/Asmara',
         'Africa/Asmera',
         ...
         'UTC',
         'Universal',
         'W-SU',
         'WET',
         'Zulu'})

这对我来说很好(我仍然在本地测试应用程序)。 在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

我发现这个问题是想把我的Django项目的settings.py文件中的时区更改为英国。

使用jfs的解决方案中的tz数据库,我找到了答案:

    TIME_ZONE = 'Europe/London'