问题:如何在Django中设置时区?

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

TIME_ZONE = 'UTC'

但是我希望我的应用程序在UTC + 2时区运行,所以我将其更改为

TIME_ZONE = 'UTC+2'

它给出了错误ValueError: Incorrect timezone setting: UTC+2。正确的做法是什么?

谢谢!

In my django project’s settings.py file, I have this line :

TIME_ZONE = 'UTC'

But I want my app to run in UTC+2 timezone, so I changed it to

TIME_ZONE = 'UTC+2'

It gives the error ValueError: Incorrect timezone setting: UTC+2. What is the correct way of doing this?

Thanks!


回答 0

以下是有效时区的列表:

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

您可以使用

TIME_ZONE = 'Europe/Istanbul'

适用于UTC + 02:00

Here is the list of valid timezones:

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

You can use

TIME_ZONE = 'Europe/Istanbul'

for UTC+02:00


回答 1

要从tz数据库获取一组所有有效的时区名称(id),可以在Python中使用pytzmodule

>>> 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'})

To get a set of all valid timezone names (ids) from the tz database, you could use pytz module in Python:

>>> 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'})

回答 2

tzinfo数据库中选择一个有效的时区。他们往往采取的形式如Africa/GaborneUS/Eastern

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

Choose a valid timezone from the tzinfo database. They tend to take the form e.g. Africa/Gaborne and 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.


回答 3

有效的timeZone值基于Linux和其他Unix系统使用的tz(时区)数据库。值是形式为“ Area / Location ”的字符串(xsd:string),其中:

区域是大陆或海洋的名称。当前区域包括:

  • 非洲
  • 美国(北美和南美)
  • 南极洲
  • 北极
  • 亚洲
  • 大西洋
  • 澳大利亚
  • 欧洲
  • Etc(行政区域。例如,“ Etc / UTC”代表协调世界时。)
  • 印第安人
  • 太平洋地区

位置是城市,岛屿或其他区域名称。

区域名称和输出缩写遵循POSIX(便携式操作系统接口)UNIX约定,该约定使用格林威治以西的正号(+)和格林威治以东的负号(-),这与通常预期的相反。例如,“ Etc / GMT + 4”对应于UTC(格林威治以西)之后4小时,而不是UTC(格林尼治东部)协调世界时之前4小时。

这是所有有效时区的列表

您可以按以下方式在settings.py中更改时区

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Kolkata'

USE_I18N = True

USE_L10N = True

USE_TZ = True

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:

Area is 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

Location is 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).

Here is a list all valid timezones

You can change time zone in your settings.py as follows

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Kolkata'

USE_I18N = True

USE_L10N = True

USE_TZ = True

回答 4

我发现了这个问题,希望将Django项目settings.py文件中的时区更改为英国。

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

    TIME_ZONE = 'Europe/London'

I found this question looking to change the timezone in my Django project’s settings.py file to the United Kingdom.

Using the tz database in jfs’ solution I found the answer:

    TIME_ZONE = 'Europe/London'

回答 5

  1. 将TIME_ZONE更改为您的本地时区,并在“ setting.py”中将USE_TZ保持为True:

    TIME_ZONE =’亚洲/上海’

    USE_I18N =真

    USE_L10N =真

    USE_TZ =真

  2. 这会将datetime对象作为UTC写入并存储到后端数据库。

  3. 然后,使用template标记将前端模板中的UTC时间转换为:

                <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>
  1. 您可以在官方文档中查看更多详细信息:默认时区和当前时区

    启用时区支持后,Django将日期时间信息存储在数据库中的UTC中,在内部使用可识别时区的日期时间对象,并将其以模板和形式转换为最终用户的时区。

  1. 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

  2. This will write and store the datetime object as UTC to the backend database.

  3. 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>
    

or use the template filters concisely:

                <td> 
                    {% load tz %}
                    {{ message.log_date | localtime | time:'H:i:s' }}
                </td>
  1. 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.


回答 6

通用解决方案,基于Django的TZ名称支持:

UTC-2 = 'Etc/GMT+2'
UTC-1 = 'Etc/GMT+1'
UTC = 'Etc/GMT+0'
UTC+1 = 'Etc/GMT-1'
UTC+2 = 'Etc/GMT-2'

+/-特意切换。

Universal solution, based on Django’s TZ name support:

UTC-2 = 'Etc/GMT+2'
UTC-1 = 'Etc/GMT+1'
UTC = 'Etc/GMT+0'
UTC+1 = 'Etc/GMT-1'
UTC+2 = 'Etc/GMT-2'

+/- is intentionally switched.


回答 7

  1. 从以下位置下载最新的pytz文件(pytz-2019.3.tar.gz):

    https://pypi.org/simple/pytz/
  2. 复制并将其解压缩到您site_packages项目的目录中

  3. 在cmd中,转到解压缩的文件夹并运行:

    python setup.py install
  4. TIME_ZONE = 'Etc/GMT+2' 或国家名称

  1. download latest pytz file (pytz-2019.3.tar.gz) from:

    https://pypi.org/simple/pytz/
    
  2. copy and extract it to site_packages directory on yor project

  3. in cmd go to the extracted folder and run:

    python setup.py install
    
  4. TIME_ZONE = 'Etc/GMT+2' or country name


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。