MegaBytes
MegaBytes

Reputation: 6385

Time zone difference between client and server?

I am getting one issue with my client server web based application. I have developed a portal using Django framework. my server is situated on AWS (north Virginia). it is one type of time alert application. my issue is when I set time from UI side from india, it is getting stored as per indian time. But the cronjob on server side execute it as per server time(as per server instance time).
e.g. I have set time 3.00 PM, then it should create alert on 3.00PM, but it create alerts as per server time 9.00 AM. It is timezone issue but I couldnot understand how to handle this situation.

In Settings.py

LANGUAGE_CODE = 'en-us'

#TIME_ZONE = 'Asia/Kolkata'
#TIME_ZONE = 'America/Chicago'
TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

#USE_TZ = True

I am using jquerydatetime picker on client side, which gets a default system time. Please suggest a way how can I solve this issue.

Upvotes: 2

Views: 909

Answers (1)

ruddra
ruddra

Reputation: 51988

Well, a solution can be using JQuery and store the offset of the client. For example, let us have a field in user Model of the system:

class CustomUser(models.Model):
    user = models.OneToOneField(User)
    time_offset = models.DecimalField(default=Decimal('0.0'),max_digits=3, decimal_places=1)

And (reference from this SO answer) make a ajax request to ur custom view and save this value in the user model.

$(document).ready(function(){
    var now = new Date()
    var value = now.getTimezoneOffset()

    $.ajax({
        url: "your-url",
        type: "post", // or "get"
        data: value,
        success: function(data) {

          console.log(data);
        }});

});

# Ajax request view

import json
def post(request):
  if request.POST():
      data = {}
      try:
         get_value= request.body
         custom_user = CustomUser.objects.get(user=request.user)
         custom_user.time_offset = get_value
         custom_user.save()
         data['success'] = 'Success'
         return HttpResponse(json.dumps(data), content_type="application/json")
      except Exception as e:
         data['error'] = e
         return HttpResponse(json.dumps(data), content_type="application/json")

And now you have offset, so when you run your cornjob, just add/subtract the time offset.

Upvotes: 2

Related Questions