Facebook Events and timezones, how to convert UTC

2019-07-14 03:36发布

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?

2条回答
走好不送
2楼-- · 2019-07-14 03:52

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
查看更多
Summer. ? 凉城
3楼-- · 2019-07-14 04:03

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

查看更多
登录 后发表回答