Django Timezone Confusion; Postgres and Apache

Setup: multiple Django sites on the same set of servers served by the same Apache process group. Some sites - Eastern TK; some of them are central. A database is a PSQL running on a separate server.

When I started, I did not think about how different sites would handle time intervals; I think I saw the TIMEZONE setting in Django and just thought that she would "handle it." Now I see the cracks.

The first problem: the time zone seems to be flipping between East and Central. My understanding is that the search on this site is due to the fact that os environ var for TZ is installed in the Apache process, depending on which Django site processes the request, and if this process processes the request for the site on another TZ, the time the belt is wrong. I believe that the solution I found here was that sites of different time zones should have different groups of processes. Please correct if you are mistaken.

The second problem: locally on Linux I made a. / Manage.py server from one of my sites with central time (I am in the east). I created an asset whose publication date was correctly displayed one hour after the administrator. Looking at the actual PostgresSQL record, the date publishing timezone is still displayed as -04. Does Postgres use only the time zone of the server / computer itself and ignore any TZ settings in Django? So, all records stored on the Postgres server in Eastern time will be displayed as -04 or -05 depending on summer savings?

If someone else has done something similar, the tips will be appreciated. Even if I separated Apache processes for central sites so that their TZ settings did not overlap, I still have a Postgres problem. And then I am curious; if the PSQL timestamp is central and the TZ parameter is eastern, say, do the datetime data take into account the TZ fields? those. if you use datetime.datetime.now () when Django is set to EST and it returns 2:00 PM, then you have a content filter by publication date less than this result, will it only consider TZ search for content whose publication time is was 1:00 PM CST or earlier?

+4
source share
1 answer

Below is some information about handling time zones in Django and Postgres, but I strongly recommend dealing exclusively with UTC on the backend and only convert to the local time zone in the interface when presenting the UTC timestamp to the user. In Python, you can get the current time in UTC through datetime.datetime.utcnow() . I even put my servers in the UTC time zone, but this is not strictly necessary.

Multiple time zones do not work in Django; see this ticket . The datetime objects in the Python standard library are the least time zones, and you need a library like pytz to fix this, but as far as I know, Django still returns naive datetime objects, not the ones known in the time zone that you can create using pytz.

Postgres will check several places to determine the time zone, including the TZ environment variable, but TZ should be in the postgres process environment:

PostgreSQL 8.5.3. Timezone

If the time zone is not specified in postgresql.conf and as the postmaster command line switch, the server attempts to use the default TZ environment variable time zone. If TZ is not defined or none of the time zone names is known on the PostgreSQL server, it tries to determine the default operating system time zone by checking the behavior of the C library function local time (). The default time zone is selected as the closest match among the famous PostgreSQL time zones.

+2
source

Source: https://habr.com/ru/post/1305138/


All Articles