Thursday, August 21, 2014

Re: Feeling some serious timezone pain

You could consider passing the timestamp in UTC to the template and
then convert it to browser's timezone using javascript function -
convertUTCDateToLocalDate(data)?

-Subodh

On Wed, Aug 20, 2014 at 5:22 AM, 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.
>
> Later on, I pull that booking out of my database by ID and render it in my
> template, rendering the datetime like this:
>
> <small>{{booking.date_time}}</small>
>
> I've tried re-interpreting the timezone after pulling it out of the
> database, to no avail. When I print the datetime object directly after it is
> pulled out of MySQL, I get it in UTC like so:
> 2014-08-30 16:00:00+00:00
>
> Re-interpreting it to force it into the office's local timezone, I get the
> correct result but 1 hour off is still rendered in the web browser
> # returns 2014-08-30 12:00:00-04:00
> booking.date_time =
> pytz.timezone("America/Toronto").normalize(booking.date_time)
>
>
>
> The result of this is the time is rendered 1 hour behind the actual time.
> This is consistent across all datetimes I've tried, and I seem to be unable
> to get Django to render the correct time.
>
> My question is this: Why is my datetime consistently rendering 1 hour off,
> and how do I wrangle it into rendering at the true time?
>
> --
> 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/2dbd562a-5db3-4787-80d2-5a0f93f07860%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
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/CALr9Q3ZYx0jfee1NwiQmMfi_%2BaW14H9bXOULd%3Db5SxC-Uua0eg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment