English 中文(简体)
How to manage timezones in a web application?
原标题:

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. 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

问题回答

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

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.)

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.





相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签