2

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.

MegaBytes
  • 6,355
  • 2
  • 19
  • 36
  • Are you using celery for cornjob? – ruddra Mar 31 '15 at 10:16
  • @ruddra, celery, how? – MegaBytes Mar 31 '15 at 10:20
  • @ruddra, it should take consideration that it can be set from anywhere, it is basically targeted to USA region, so I set the time zone UTC, I tried with TIME_ZONE = 'America/Chicago', but It was not solve – MegaBytes Mar 31 '15 at 10:27
  • Actually I am asking how u r using cornjob. Anyways, this is a common and irritating problem. – ruddra Mar 31 '15 at 10:51
  • How is your time alert data storied internally? If you are using a database, it should get stored as a UTC timestamp. When you retrieve it from the database, you can convert the UTC time to local time. Django does that almost transparently but you have to make sure your project is timezone aware and you handle any conversion needed when you're getting input from the frontend. – kchan Mar 31 '15 at 11:02
  • @ruddra, I am using django-cronjob package for creating the cronjob. sorry, it may be a common problem but currently I am troubling with this problem. :) – MegaBytes Mar 31 '15 at 14:20

1 Answers1

2

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.

Community
  • 1
  • 1
ruddra
  • 50,746
  • 7
  • 78
  • 101
  • 1
    in the above solution you are saving the users time offset, and whenever the cronjob will execute, it will fetch users offset and add/ substract the time offset, but it is not fit in my example because it is not users specific or area specific. I tried this but it creates problem while check the time with AM/PM – MegaBytes Mar 31 '15 at 14:44
  • 1
    Is your time being saved as date object like 'datetime.now()'? If so, use 'dateime.timedelta' to convert offset into timedelta object and substract it from ur date. – ruddra Mar 31 '15 at 15:20
  • 1
    yes, you are right, it can be done using timedelta, but what about cronjob, because it running as per system time, and in cronjob I am checking last five minute time. – MegaBytes Mar 31 '15 at 15:34
  • 1
    Another way could be saving the offset in the Task itself, I mean you are storing task information somewhere, that means u can also store the offset of the origin of the task with it and do the calculation within the periodic task function @MegaBytes – ruddra Mar 31 '15 at 15:51
  • 1
    Hi Ruddra, as the answer you suggested is right in case, if I create the time alert for current date and time, what in case of I schedule a timer for next day any hour, in this case how to handle it. – MegaBytes Apr 01 '15 at 14:20
  • well, if its an periodic task, than it should be easy, you will keep a flag(or a Boolean field) with each Task and in the periodic task function(timer), you will check for the Task objects with False flag, and among those objects, check them which are within doing range, and execute the tasks, and Make the flags True. – ruddra Apr 01 '15 at 16:45