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

隐身守侯 提交于 2019-12-24 03:00: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:

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?


回答1:


"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.




回答2:


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


来源:https://stackoverflow.com/questions/4134718/facebook-events-and-timezones-how-to-convert-utc-datetime-to-what-facebook-expe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!