views:

369

answers:

4

Hi, I wan't to manage the different timezones of my users in my web application, but I have no idea where to start. I have to save the local time of each user in my database?, or maybe make the conversion to a UTC time, save it, and then make the conversion again to show it?, or there is another way? For example, if one of my users make an appointment in his local time, I have to convert it to UTC store it in my database, and then when he need it, convert it again to his local time an show it?? By the way, I'm using Django. Thanks.

+1  A: 

Try the Django snippit UTC DateTime field. It has everything you'll need right out of the box, practically.

Mike Atlas
For another ready-to-use Django solution there is http://code.google.com/p/django-timezones/
Van Gale
A: 

Using a single "database time" timezone is, IMO, the best options because:-

  • It greatly simplifies routines for dealing with people in different timezones
  • It allows for easier conversions to other timezones
  • Anyone seeing the data out of context of the user's view can assume that it's UTC, and not have to guess if "12:45" for one record is "12:45" for another
  • Standards are good

So, yes. Store the user's timezone in the database, then store all times in a common time (Like UTC). Convert them when the user views anything including time. It's the simplest solution that doesn't just force all users to pretend to be in the same timezone.

Dylan Lacey
A: 

make the conversion to a UTC time, save it, and then make the conversion again to show it?

Yes, even if you only have one local timezone, you should generally be storing your dates as UTC timestamps in the database, and converting it to and from text in the appropriate timezone in your webapps input and output stages. When you have a per-user timezone stored it's then easy to switch out the default timezone for the customised one.

pytz is the usual solution for selecting and converting timezones. (Although personally I hacked up my own less overwhelming collection of timezones.)

bobince
+1  A: 
  1. Store date/time as UTC (Not sure what the Python command for that is)

  2. When outputting dates, wrap them in a helper function that gets the current user's time zone preference, then adjust the time/date for the offset, then output it from the application.

http://docs.python.org/library/time.html

http://docs.python.org/library/datetime.html

Baddie