What is the best way to display the exact current time and date in Django?

Currently my method involves creating a context processor which is then included in sitewide. However, I notice that I have problems associated with daylight saving time changes. I live outside the USA. By the way, my server is located in the USA, but its time zone is set to mine. However, since we do not consider daylight saving time, it is always an hour earlier than my local time.

Any idea how to explain this? I have used timedelta -1 in the past, but for some reason I don’t feel that this is the correct solution.

+3
source share
5 answers

, , Django. Django , , TIME_ZONE ​​ settings.py, , . , Django, .

: http://docs.djangoproject.com/en/dev/ref/settings/#time-zone.

, TIME_ZONE Zoneinfo (, America/Belize), GMT-6, . Zoneinfo, Django . , Django.

{% now h:ia %}

, 9:30 .

+5

, , - :

{% now M d, Y h-ia %}

- : 23 2009 . 9:21 .

. http://docs.djangoproject.com/en/dev/ref/templates/builtins/

+1

Django 1.4 django.utils.timezone

+1

, , , .

Linux, /etc/localtime

/usr/share/zoneinfo. /usr/share/zoneinfo/America/New_York, /usr/share/zoneinfo/America/Belize *, :

ln -sf /usr/share/zoneinfo/America/Belize /etc/localtime

, * nix. , Solaris /usr/share/lib/zoneinfo

It is assumed that you have root access on the computer, since you have access to change the system clock.

* Your location is indicated as Belize on your profile if you were wondering where I got it.

0
source

If you are only concerned about displaying the date and time on the client side, you can simply use Javascript, which will use the date and time of the client computer.

There may be reasons why you do not want to do this, but I thought I would throw them away anyway.

0
source

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


All Articles