Ashish
Ashish

Reputation: 459

Django - Working With different Timezones

I am working on a Django application on Dietitian Portal, in which there are client from different countries.Now For appointment booking for client i need to send availabe time slots to user according to dietitian's timezone. Now the problem is that if dietitian's timezone is Asia/Calcutta and client's timezone is Us/Eastern or other.when the client is requesting for slots than there is a date 19 and accoring to dietitian's timezone 20th, So how can i manage this that i can cover the whole day of dietitian in client's timezone

It works Fine if date is same but if two different dates are there than the problem comes.Client is not able to fetch slots of dietitian because
according to dietitian's timezone date is 20th.

Upvotes: 2

Views: 247

Answers (2)

ruddra
ruddra

Reputation: 52028

I think you can take a similar approach to the steps mentioned in the documentation:

First, get Timezone info from user:

from pytz import country_timezones

class User(...):
    country_code = models.CharField(...)

    def get_tz_info(self):
       return country_timezones(self.country_code)[0]

Then, write a MIDDLEWARE to activate localized timezone:

import pytz

from django.utils import timezone
from django.utils.deprecation import MiddlewareMixin

class TimezoneMiddleware(MiddlewareMixin):
    def process_request(self, request):
        tzname = request.user.get_tz_info()
        if tzname:
            timezone.activate(pytz.timezone(tzname))
        else:
            timezone.deactivate()

Upvotes: 1

Cipher
Cipher

Reputation: 2132

You can use UTC Timezone - link. You can find this code in your settings.py, if not put this -

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

Your browser will automatically convert to local timezones

Upvotes: 0

Related Questions