Reputation: 139
Hi I Have a Hotel Management system, in this for different hotels(Subdomains) different Time zones are required. how can i do this?
Upvotes: 5
Views: 7590
Reputation: 15756
There's very nice Django documentation regarding timezones. You'll need to determine exactly what you mean when you say you'd like a different time zone per subdomain. Normally, the Django TIME_ZONE
setting is used to specify a default time zone. In your case however, you effectively want a different default TIME_ZONE
setting depending on which subdomain is being accessed.
Probably the most robust way to achieve this goal is:
import django.utils.timezone
from pytz import timezone
class TimezoneMiddleware(object):
def process_request(self, request):
# Put logic here to choose timezone based on domain.
if request.META['HTTP_HOST'] == 'hotel1.subdomain.com':
tz = timezone('US/Eastern')
else:
tz = timezone('UTC')
if tz:
django.utils.timezone.activate(tz)
else:
django.utils.timezone.deactivate()
Now, when Django handles date entered or displayed, it will use the timezone selected by middleware. This doesn't solve the problem of having users in a different time zone to the subdomain for which the time zone is set. Normally, this problem would be solved by having each Django user store their preferred time zone, and the middleware would choose the time zone based on their session data as described in the documentation.
Upvotes: 15
Reputation: 739
Below code can be used to change timezone.
import os,time
os.environ['TZ'] = 'Asia/Shanghai'
There is a list of timezone in http://en.wikipedia.org/wiki/List_of_tz_database_time_zones.
Pytz is another good choice to manage timezone in python.
Upvotes: 0