Set custom timezone in Django/PostgreSQL (Indian S

2019-02-15 11:14发布

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条回答
Ridiculous、
2楼-- · 2019-02-15 12:05

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.

查看更多
登录 后发表回答