I am trying to convert the Facebook start_time of time. As per Facebook's documentation, Facebook uses ISO-8601 strings to describe precise time with offset from UTC of format:
%Y-%m-%dT%H:%M:%S+OFFSET
On extracting from the API, some events come out as local time e.g. example 1, and others come out as my user's time e.g. example 2 (note that the location of event 1 and 2 is the same).
example 1) eid = 163113227202707, start_time = 2013-06-18T22:00:00-0700
example 2) eid = 590809384285866, start_time = 2013-06-17T01:00:00-0300
I need to use local event times including for daylight savings.
I went about creating a table for about 1500 cities of the format:
city timezone dst offset
Abidjan GMT +0000
Abuja WAT +0100
Accra GMT +0000
Adama EAT +0300
Addis Ababa EAT +0300
Agadez WAT +0100
Agadir WEST DST +0100
Al Jizah EET +0200
Alexandria EET +0200
Algiers CET +0100
Ali Sabieh EAT +0300
Antananarivo EAT +0300
Antsirabe EAT +0300
Arusha EAT +0300
Asmara EAT +0300
Bafatá GMT +0000
Bamako GMT +0000
Bambari WAT +0100
Bamenda WAT +0100
Bangui WAT +0100
Banjul GMT +0000
Bata WAT +0100
Beira CAT +0200
Benghazi CST DST +0200
Benin City WAT +0100
Berbérati WAT +0100
However, since this is static it doesn't help with daylight savings.
On reading the document about pytz I'm not sure how to query the timezone based on the city name. Is this even possible?
Essentially, I am trying to create the following pseudo code:
start_time = 2013-06-17T01:00:00-0300
UTC_start_time = normalize_time_to_UTC(start_time)
offset = lookup_offset_given_city_and_UTC(city, UTC)
local_start_time(UTC_start_time, offset)
Update:
I first normalize the time based on the offset provided by Facebook by parsing the string as follows:
def fql_local_time_to_utc(start_time):
if len(fql_timestamp) < 20:
return fql_timestamp
else:
timestamp = fql_timestamp[:19]
sign = fql_timestamp[19]
hours = fql_timestamp[20:22]
minutes = fql_timestamp[22:24]
offset = int(hours)+int(minutes)/60
if sign =="+":
offset = -offset
timestamp_object = datetime.datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S')
utc_object = timestamp_object + datetime.timedelta(hours = offset)
return utc_object
Next, I look up the location_city of the venue_id and try to match it in the above database to which I manually added a column called "pytz_timezone_name". After finding the relevant timezone via a join query, I apply the following code.
try:
tz = db.fetchone()["pytz_timezone_name"]
tz_object = timezone(tz)
local_event_time = timezone('UTC').localize(utc_time).astimezone(tz_object)
local_time = local_event_time.strftime('%Y-%m-%dT%H:%M:%S%z')
except:
local_time = start_time