ρss
ρss

Reputation: 5315

Timezone issue with pyExchange

I am working with pyExchange on windows 7 machine. I have a simple python v2.7 script that retrieves the Outlook calendar events from the exchange server. The script is provided below:

Code:

from pyexchange import Exchange2010Service, ExchangeNTLMAuthConnection
from datetime import datetime
import time
from pytz import timezone

def getEvents():
        URL = u'https://xxxxx.de/EWS/Exchange.asmx'
        USERNAME = u'MS.LOCAL\\xxxxx'
        PASSWORD = u"xxxxxx"

        connection = ExchangeNTLMAuthConnection(url=URL,
                                            username=USERNAME,
                                            password=PASSWORD)

        service = Exchange2010Service(connection)

        timestamp = datetime.now()
        print timestamp.strftime('%Y, %m, %d, %H, %M, %S')

        print time.timezone

        eventsList = service.calendar().list_events(
            start=timezone("Europe/Amsterdam").localize(datetime(2015, 1, 19, 0, 0, 0)),
            end=timezone("Europe/Amsterdam").localize(datetime(2015, 1, 19, 23, 59, 59)),
            details=True
        )

        for event in eventsList.events:
            print "{start} {stop} - {subject} - {room}".format(
                start=event.start,
                stop=event.end,
                subject=event.subject,
                room=event.location
            )

getEvents()

Problem:

The timestamp of the events doesn't match the timestamp of the events in Outlook. I created the events manually using the Outlook as well as using a pyExchange script.

For eg: If I create an event from 11:00 AM - 11:30 AM in Outlook, then the above script will return the timestamp of that event as 10:00 AM - 10:30 AM. The time is one hour less/back.

If I check my time.timezone it returns W. Europe Standard Time. I have specified my timezone in the script too ie. Europe/Amsterdam. But still the problem persists. Also I checked the timezone settings in Outlook. Shown below: enter image description here

I logged into the Exchange server and it is also in the same timezone as my client machine.

Any suggestion regarding why the time is not correct for the events? Is this a bug in pyExchange? I would really appreciate it, if some one can test this and report back here, just to be sure that its not only me who is facing this issue.

Upvotes: 3

Views: 1158

Answers (1)

Rachel Sanders
Rachel Sanders

Reputation: 5874

I looked and it's probably not a bug in pyexchange, but how you're handling timezones. No shame, they're sadly extremely confusing in Python.

First, the package is returning event dates in UTC and not your local time. You're seeing an hour off the expected time because your timezone is +1 UTC. Here's an event I pulled from my calendar using your script (this is start/end/name/room):

2015-01-19 20:00:00+00:00 2015-01-19 21:00:00+00:00 - Lunch - Cafe

Note the +00:00 - that means it's in UTC. Noon here in California is 20:00 UTC.

Always, always, use UTC when handling datetimes. Here's some doc from the pytz folk on why localtimes are dangerous.

PyExchange tries to have your back and will convert localtime to UTC, but it always returns UTC. That's on purpose because see the previous link.

Now, to answer your question on getting this to work. First, convert your local time to UTC using these handy tips:

  1. Use datetime.now(pytz.utc) to get the current datetime
  2. Don't use datetime(…, tzinfo=timezone) to create a timezone aware datetime object, it's broken. Instead, create the datetime object and call timezone.localize on it.

For you, that means you have to do ugly stuff like:

start = timezone("Europe/Amsterdam").localize(datetime(2015, 1, 19, 0, 0, 0))
start = start.astimezone(pytz.utc)

Then, when you want to display UTC dates as your own time, do:

event.start.astimezone(timezone("Europe/Amsterdam"))

When I do that, I see this output from your script:

2015-01-19 21:00:00+01:00 2015-01-19 22:00:00+01:00 - Lunch - Cafe

which I would expect. Noon my time is 9pm your time.

Here's a gist of your script that I changed. Take a look and see if it fixes your problem. If not, I'm happy to look again!

Upvotes: 1

Related Questions