Thursday, August 21, 2014

Re: Feeling some serious timezone pain

On Wed, Aug 20, 2014 at 1:22 PM, christophw <chris.whiten@gmail.com> wrote:
> Hi there,
>
> I'm feeling some pain while wrangling with timezones in an office room
> booking application. I'm storing bookings in a MySQL database and want to
> display them in the timezone local to the office room, so it is not
> necessarily local to the user's local machine timezone.
>
> Here is my model's datetime definition:
> class Booking(models.Model):
> date_time = models.DateTimeField()
>
> When the booking is stored, it comes back from a post and I store it in the
> DB as such:
>
> dt = datetime.datetime.strptime(request.POST["date"] + " " +
> request.POST["time"], "%Y-%m-%d %H:%M")
> dt = pytz.timezone("America/Toronto").localize(dt)
>
> Printing the value of dt after that call, it looks correct.

Take your localized datetime that you think looks correct, and convert
it to UTC. Is it right? Is your localization taking in to account DST
correctly? "America/Toronto" is either Eastern Standard Time or
Eastern Daylight Time according to my brief research, and presumably
right now it is the latter.

Trying from scratch. The current time in Toronto is 9:38, this should
correspond to 1:38 when converted back to UTC

>>> from datetime import datetime
>>> import pytz
>>> dt = datetime.strptime('2014-8-21 9:38', "%Y-%m-%d %H:%M")
>>> dt2 = pytz.timezone("America/Toronto").localize(dt)
>>> dt2
datetime.datetime(2014, 8, 21, 9, 38, tzinfo=<DstTzInfo
'America/Toronto' EDT-1 day, 20:00:00 DST>)
>>> dt2.astimezone(pytz.utc)
datetime.datetime(2014, 8, 21, 13, 38, tzinfo=<UTC>)

I'm convinced this is a DST handling issue. If it is not when it goes
*in* to the database, perhaps it is when it comes *out*.
What do you have for settings.TIME_ZONE and settings.USE_TZ?
If you examine the localized dt that is pulled from the DB, what
tzinfo is set on it?

Cheers

Tom

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFHbX1LMx0Ey9Ehq8SESdss_JqftOHKD2oW-ed3H41B4AuQbMQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment