Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why python datetime replace timezone is returning different timezone?

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

like image 204
Amit Pandya Avatar asked Mar 30 '17 18:03

Amit Pandya


People also ask

How does Python handle different time zones?

The parameter pytz. timezone allows us to specify the timezone information as a string. We can pass in any available timezone and will get the current date time of that timezone, and it will also print the offset with respect to the UTC. i.e., the difference between UTC timezone(+00:00) and the specified time zone.

Does Python datetime include timezone?

One of the modules that helps you work with date and time in Python is datetime . With the datetime module, you can get the current date and time, or the current date and time in a particular time zone. In this article, I will show you how to get the current time in Python with the datetime module.


1 Answers

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))
like image 143
Mark Ransom Avatar answered Nov 14 '22 23:11

Mark Ransom