WebFaction
Community site: login faq

I've set my .bashrc TZ variable (described here), and the TIME_ZONE variable in my django settings file (described here).

If I run python2.7 from the shell and execute datetime.now(), it shows the correct time.

However if I run python2.7 manage.py shell, datetime.now() shows the wrong time. More importantly, times saved in the database are wrong.

How do I fix this?

Thanks.

asked 12 Sep '11, 05:32

pbg
1547
accept rate: 0%

Doh. I just figured out what was wrong. I had the TIME_ZONE set twice in my settings file. I didn't realise it is there by default, so I added it at the top. Then of course it was overwritten by the default declared further down.

(16 Sep '11, 20:31) pbg

I would suggest doing the time zone conversions on the Django application side and not on the database side. It is simple to create a function that performs the correct conversion from the time zone using the Python datetime module, and then you just call this function in your code whenever you need to display a time. (You can even implement it as a decorator onto your standard time function (see this example).

The same decorator concept could even be used to submit the time-zone-changing query each time your code connects to the database. This needs to be done because each query has a new connection to the database and resets the time zone variable to the server default.

permanent link

answered 12 Sep '11, 07:16

timg ♦♦
1.3k4
accept rate: 30%

Is this the only way?

I'd much rather set a single time zone for the whole site and not worry about conversions. The website is very localized so there's no need to convert to multiple time zones for different users.

Thanks for answering.

(12 Sep '11, 07:41) pbg

I have just tested and your above solution should work on all WebFaction servers. If you are still having problem, please open a support ticket so we can investigate the cause.

(12 Sep '11, 08:14) timg ♦♦
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×905
×108
×4
×3

question asked: 12 Sep '11, 05:32

question was seen: 4,747 times

last updated: 16 Sep '11, 20:31

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM