开发者

Facebook Events and timezones, how to convert UTC datetime to what facebook expects?

开发者 https://www.devze.com 2023-01-24 18:13 出处:网络
My application needs to create facebook events. Everything works fine, but I can\'t get the timezones correct. The start/end dates are all wrong. Facebook\'s Event API docs say this:

My application needs to create facebook events. Everything works fine, but I can't get the timezones correct. The start/end dates are all wrong. Facebook's Event API docs say this:

Note: The start_time and end_time are the times that were input by the event creator, converted to UTC after assuming that they were in Pacific time (Daylight Savings or Standard, depending on the date of the 开发者_开发问答event), then converted into Unix epoch time.

(source)

I can't figure out what that means.

My web application is a python (django) site. Given a datetime object which has the start/end time in UTC, what is the magical incantation of pytz calls to get the correct time to send to facebook?


It means that if the user enters "12 am" you're supposed to assume its "12 am pacific time", convert that to UTC so its "8 am GMT" and turn that into a unix epoch.

You can do it like this:

import datetime, calendar

date_local = datetime.datetime.now() # some date in some timezone
date_utc = date_local.utctimetuple() 
unix_time = calendar.timegm(date_utc) # to unix time


"Unix epoch time" simply means "number of seconds since January 1, 1970 (not counting leap seconds)".

By the way, that Facebook Event API description is so bizarre, I can't believe it is right as described. What they seem to be asking for is:

  1. The time that was input by the event creator;
  2. Interpreted as if a local time in the Pacific time zone, with the daylight saving rules for that zone in effect;
  3. Converted to UTC.

I live in the timezone UTC+0. So if I schedule an event at 2010-11-09 12:00:00 UTC, the time that actually gets submitted to Facebook is (the Unix time corresponding to) 2010-11-09 20:00:00 UTC. How can that be right? Maybe I've misunderstood.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号