Set custom timezone in Django/PostgreSQL (Indian Standard Time)

感情迁移 提交于 2019-12-19 03:59:09

问题


In Django documentation for setting timezone, the list of available choices for timezone are actually postgres timezone parameter strings. So it seems Django uses Postgres for retrieving time.

If so, then the problem is that IST is used to denote both Indian & Israel Standard Time, but postgres uses IST for Israel Standard Time (potentially confusing over a 6th of world's population) and there is NO timezone string for Indian Standard Time.

Not just that, Postgres also misses timezone for some other countries like Nepal (GMT+5:30) and some Pacific Islands.

So, is there any way by which I can set custom timezone string (like GMT+5:30 for India, GMT+5:45 for Nepal, etc.) in Postgres or Django?


回答1:


For India:

SELECT now() AT TIME ZONE 'Asia/Calcutta';
SELECT now()::timestamp AT TIME ZONE 'Asia/Kolkata';
SELECT now()::timestamp AT TIME ZONE '5:30';

For Nepal:

SELECT now()::timestamp AT TIME ZONE 'Asia/Katmandu';
SELECT now()::timestamp AT TIME ZONE 'NPT';

To set the time zone for the whole session:

SET time zone 'Asia/Calcutta';

To reset it (to the time zone set in postgresql.conf:

RESET time zone;

Find more in the system views pg_timezone_names and pg_timezone_abbrevs

SELECT *
FROM   pg_timezone_names
WHERE  utc_offset BETWEEN '05:00:00' AND '06:00:00'
ORDER  BY utc_offset;

SELECT *
FROM   pg_timezone_abbrevs
WHERE  utc_offset BETWEEN '05:00:00' AND '06:00:00'
ORDER  BY utc_offset;

PostgreSQL manual about AT TIME ZONE construct. About time zones.



来源:https://stackoverflow.com/questions/9815412/set-custom-timezone-in-django-postgresql-indian-standard-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!