Tuesday, March 18, 2014

Client side timestamp handling and timezone

Hello there,

I’ve been struggling with this issue for the last few days and I cannot seem to find the way to fix it. I’ve this web page with a form where a user is requested to enter a date and a time of the day. Then there is a search button which does a JSON post back to the server with certain information. The user selected date/time is sent also back to the server as part of this information. Right now, before sending data back to the server the selected date/time is converted to a timestamp, which is assumed to be in the client’s time zone.

The server code attempts to convert this timestamp back to a valid datetime object, by means of:

datetime.datetime.fromtimestamp(int(self.request_data.get(TIME_OF_PICKUP_FIELD)))

but the function fromtimestamp is also applying the timezone conversation to the provide value which turns the datetime one hour ahead/behind the user’s selected value. What’s strange to me is that if I run this code outside Django’s shell:

>>>datetime.datetime.fromtimestamp(1395160800)

the result is: datetime.datetime(2014,3,18,12,40)

which is correct since (client’s timezone is America/Chicago and server’s is America/Havana).

But if I try the same code with python manage.py shell:

>>>datetime.datetime.fromtimestamp(1395160800)

the result is: datetime.datetime(2014, 3, 18, 11, 40)

I’ve tried with USE_TZ=False (TIME_ZONE setting is America/Chicago and server local time is America/Havana) and I still get the same result. I guess a work around this issue would be to send the timestamp in UTC but I haven’t found a reliable way to do so in Javascript.

I would appreciate any help.

Bests,

Yoanis.

No comments:

Post a Comment