7

I am working on Python/Django project. I am trying to let user select date and time using jQuery plugin datetimepicker add-on. So when I select now option and post data django is saving the time in UTC offset. This is what is saved in database, 2017-03-30 13:38:00+00:00. I need to convert this time from user's timezone and save it in system as utc. Because later I will have script running which will look for data in database which is less than the utc time.

Actually the script is to let user post information on website and let them chose the publish date and time. So for example, If use posted an article which will be published on April 2nd 1pm Chicago time, I don't want other users to read the article before this time. So people around the world can read article as soon as it is April 2nd and 1PM in Chicago. So how can I make this functionality work?

My solution was to get the time and remove it's timezone information using replace(tzinfo=pytz.timezone('America/Chicago')) and when I print the time, I am getting 2017-03-30 13:38:00-05:51. The actual offset right now is -05:00. Can anyone help me to and tell me what I am doing wrong?

What I am doing for form is that I have publish_date object in my model and I am using django forms to create form. I have added class as an attribute in it and using jquery plugin,

$('.datepicker').datetimepicker({
                timeFormat: 'HH:mm',
                stepHour: 1,
                stepMinute: 1,
            });

So when User submits the form, on post method this my code,

form = PublishForm(request.POST)

if form.is_valid():

                f = form.save(commit=False)
                f.created_by_user_id = request.user.id
                f.save()

and than to get the date all I am doing is f.publish_date and the other options I have used lice replace and localize are pretty standard.

Thanks

Amit Pandya
  • 361
  • 1
  • 3
  • 19
  • So even though the time is saved as a UTC offset, it isn't a UTC time? – Mark Ransom Mar 30 '17 at 19:09
  • Maybe http://stackoverflow.com/questions/11473721/weird-timezone-issue-with-pytz explains the issue. – Mark Ransom Mar 30 '17 at 19:11
  • It is not the right UTC time. For instance, I am posting form from Chicago at 12PM so it should save the time as 5PM in UTC instead of that it is taking 12PM as utc time which would be 7AM Chicago time. – Amit Pandya Mar 30 '17 at 19:12
  • So you appear to have two problems. First is that the `datetimepicker` is saving a UTC date/time when it should be applying a timezone. Second is that using `pytz` is using the wrong offset. The link I gave you earlier will definitely help with the second problem. – Mark Ransom Mar 30 '17 at 19:18
  • I tried using `localize` option but it is giving me error `Not naive datetime (tzinfo is already set)` as timezone is already set by datetimepicker. – Amit Pandya Mar 30 '17 at 19:21
  • You should post some more code on how you're getting that initial `datetime` object in Python. – Mark Ransom Mar 30 '17 at 19:25
  • @MarkRansom please find updated code. – Amit Pandya Mar 30 '17 at 19:32

2 Answers2

14

As noted in the comments, you appear to have two problems. First is that the datetimepicker is saving a date and time with a UTC timezone offset, when it should be applying a different timezone or leaving off the timezone offset entirely. Second is that pytz is using the wrong offset.

I don't know how to fix the first problem, but I can help you with the second. You need to use pytz's localize function. This only works when the datetime doesn't have a timezone attached, but since you know the timezone is incorrect anyway you can delete it first.

tz = pytz.timezone('America/Chicago')
dt = tz.localize(dt.replace(tzinfo=None))
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
7

The naming of the datetime replace(tzinfo = ...) function is unfortunate. In fact, its behaviour is random. Do not use this!

Mark's answer is the way to go. Use localize.

paolov
  • 2,139
  • 1
  • 34
  • 43