Google Calendar bot: Include time zone offsets in timestamps.
Fixes: #3769
This commit is contained in:
parent
060b680674
commit
478785b415
|
@ -1,11 +1,16 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
#
|
||||||
|
# This script depends on python-dateutil and python-pytz for properly handling
|
||||||
|
# times and time zones of calendar events.
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
import datetime
|
import datetime
|
||||||
|
import dateutil.parser
|
||||||
import httplib2
|
import httplib2
|
||||||
import itertools
|
import itertools
|
||||||
import logging
|
import logging
|
||||||
import optparse
|
import optparse
|
||||||
import os
|
import os
|
||||||
|
import pytz
|
||||||
from six.moves import urllib
|
from six.moves import urllib
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
@ -114,13 +119,26 @@ def get_events():
|
||||||
|
|
||||||
for event in feed["items"]:
|
for event in feed["items"]:
|
||||||
try:
|
try:
|
||||||
start = event["start"]["dateTime"]
|
start = dateutil.parser.parse(event["start"]["dateTime"])
|
||||||
|
# According to the API documentation, a time zone offset is required
|
||||||
|
# for start.dateTime unless a time zone is explicitly specified in
|
||||||
|
# start.timeZone.
|
||||||
|
if start.tzinfo is None:
|
||||||
|
event_timezone = pytz.timezone(event["start"]["timeZone"])
|
||||||
|
# pytz timezones include an extra localize method that's not part
|
||||||
|
# of the tzinfo base class.
|
||||||
|
start = event_timezone.localize(start) # type: ignore
|
||||||
except KeyError:
|
except KeyError:
|
||||||
start = event["start"]["date"]
|
# All-day events can have only a date.
|
||||||
start = start[:19]
|
start_naive = dateutil.parser.parse(event["start"]["date"])
|
||||||
# All-day events can have only a date
|
|
||||||
fmt = '%Y-%m-%dT%H:%M:%S' if 'T' in start else '%Y-%m-%d'
|
# All-day events don't have a time zone offset; instead, we use the
|
||||||
start = datetime.datetime.strptime(start, fmt)
|
# time zone of the calendar.
|
||||||
|
calendar_timezone = pytz.timezone(feed["timeZone"])
|
||||||
|
# pytz timezones include an extra localize method that's not part
|
||||||
|
# of the tzinfo base class.
|
||||||
|
start = calendar_timezone.localize(start_naive) # type: ignore
|
||||||
|
|
||||||
try:
|
try:
|
||||||
yield (event["id"], start, event["summary"])
|
yield (event["id"], start, event["summary"])
|
||||||
except KeyError:
|
except KeyError:
|
||||||
|
@ -133,7 +151,7 @@ def send_reminders():
|
||||||
|
|
||||||
messages = []
|
messages = []
|
||||||
keys = set()
|
keys = set()
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now(tz=pytz.utc)
|
||||||
|
|
||||||
for id, start, summary in events:
|
for id, start, summary in events:
|
||||||
dt = start - now
|
dt = start - now
|
||||||
|
|
Loading…
Reference in a new issue