Python 如何在 Django 中设置时区?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29311354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set the timezone in Django?
提问by jeff
In my django project's settings.py
file, I have this line :
在我的 django 项目settings.py
文件中,我有这一行:
TIME_ZONE = 'UTC'
But I want my app to run in UTC+2 timezone, so I changed it to
但我希望我的应用程序在 UTC+2 时区运行,所以我将其更改为
TIME_ZONE = 'UTC+2'
It gives the error ValueError: Incorrect timezone setting: UTC+2
. What is the correct way of doing this?
它给出了错误ValueError: Incorrect timezone setting: UTC+2
。这样做的正确方法是什么?
Thanks!
谢谢!
采纳答案by Selcuk
Here is the list of valid timezones:
以下是有效时区列表:
http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
You can use
您可以使用
TIME_ZONE = 'Europe/Istanbul'
for UTC+02:00
UTC+02:00
回答by Crast
Choose a valid timezone from the tzinfo database. They tend to take the form e.g. Africa/Gaborne
and US/Eastern
从tzinfo 数据库中选择一个有效的时区。他们倾向于采取这样的形式,例如Africa/Gaborne
和US/Eastern
Find the one which matches the city nearest you, or the one which has your timezone, then set your value of TIME_ZONE
to match.
找到与您最近的城市相匹配的城市,或者与您的时区相匹配的城市,然后将您的值设置TIME_ZONE
为匹配。
回答by jfs
To get a set of all valid timezone names (ids) from the tz database, you could use pytz
module in Python:
要从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'})
回答by Mark
I found this question looking to change the timezone in my Django project's settings.py
file to the United Kingdom.
我发现这个问题希望将我的 Django 项目settings.py
文件中的时区更改为英国。
Using the tz database in jfs' solution I found the answer:
在 jfs 的解决方案中使用 tz 数据库我找到了答案:
TIME_ZONE = 'Europe/London'
回答by Freman Zhang
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 UTCto the backend database.
Then use template tagto 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>
将 TIME_ZONE 更改为您的本地时区,并在“setting.py”中保持 USE_TZ 为 True:
TIME_ZONE = '亚洲/上海'
USE_I18N = 真
USE_L10N = 真
USE_TZ = 真
这会将 datetime 对象作为UTC写入并存储到后端数据库。
然后使用模板标签转换前端模板中的 UTC 时间,如下所示:
<td> {% load tz %} {% get_current_timezone as tz %} {% timezone tz %} {{ message.log_date | time:'H:i:s' }} {% endtimezone %} </td>
or use the template filtersconcisely:
或者简洁地使用模板过滤器:
<td>
{% load tz %}
{{ message.log_date | localtime | time:'H:i:s' }}
</td>
You could check more details in the official doc: Default time zone and current time zone
When support for time zones is enabled, Django stores datetime information in UTC in the database, uses time-zone-aware datetime objects internally, and translates them to the end user's time zone in templates and forms.
您可以在官方文档中查看更多详细信息:默认时区和当前时区
启用对时区的支持后,Django 将日期时间信息以 UTC 格式存储在数据库中,在内部使用时区感知日期时间对象,并在模板和表单中将它们转换为最终用户的时区。
回答by ANFAS PV
Valid timeZone values are based on the tz (timezone) database used by Linux and other Unix systems. The values are strings (xsd:string) in the form “Area/Location,” in which:
有效的时区值基于 Linux 和其他 Unix 系统使用的 tz(时区)数据库。这些值是“区域/位置”形式的字符串 (xsd:string) ,其中:
Areais a continent or ocean name. Area currently includes:
区域是大陆或海洋的名称。区域目前包括:
- Africa
- America (both North America and South America)
- Antarctica
- Arctic
- Asia
- Atlantic
- Australia
- Europe
- Etc (administrative zone. For example, “Etc/UTC” represents Coordinated Universal Time.)
- Indian
- Pacific
- 非洲
- 美国(北美和南美)
- 南极洲
- 北极
- 亚洲
- 大西洋
- 澳大利亚
- 欧洲
- Etc(管理区域。例如,“Etc/UTC”代表协调世界时。)
- 印度人
- 太平洋
Locationis the city, island, or other regional name.
位置是城市、岛屿或其他区域名称。
The zone names and output abbreviations adhere to POSIX (portable operating system interface) UNIX conventions, which uses positive (+) signs west of Greenwich and negative (-) signs east of Greenwich, which is the opposite of what is generally expected. For example, “Etc/GMT+4” corresponds to 4 hours behind UTC (that is, west of Greenwich) rather than 4 hours ahead of UTC (Coordinated Universal Time) (east of Greenwich).
区域名称和输出缩写遵循 POSIX(便携式操作系统接口)UNIX 约定,它使用格林威治以西的正 (+) 号和格林威治以东的负 (-) 号,这与通常预期的相反。例如,“Etc/GMT+4”对应于 UTC 后 4 小时(即格林威治以西),而不是 UTC(协调世界时)(格林威治以东)提前 4 小时。
Here is a list all valid timezones
You can change time zone in your settings.py as follows
您可以在 settings.py 中更改时区,如下所示
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Asia/Kolkata'
USE_I18N = True
USE_L10N = True
USE_TZ = True
回答by Punnerud
回答by S.Rasool Mirtalebi
download latest pytz file (pytz-2019.3.tar.gz) from:
https://pypi.org/simple/pytz/
copy and extract it to
site_packages
directory on yor projectin cmd go to the extracted folder and run:
python setup.py install
TIME_ZONE = 'Etc/GMT+2'
or country name
从以下位置下载最新的 pytz 文件 (pytz-2019.3.tar.gz):
https://pypi.org/simple/pytz/
复制并解压到
site_packages
你的项目目录在 cmd 中转到解压缩的文件夹并运行:
python setup.py install
TIME_ZONE = 'Etc/GMT+2'
或国家名称