将 EWS datetime_received 的时区从标准更改为 +2

Changing timezone from EWS datetime_received from standard to +2

我正在尝试将时区从 UTC+0 更改为 GMT+2。我已经尝试了很多,但我就是想不通任何帮助都会很棒

from exchangelib import Credentials, Account

credentials = Credentials(email, wachtwoord)
acc = Account(email, credentials=credentials, autodiscover=True)

for item in acc.inbox.all().order_by('-datetime_received')[:100]:

    print(item.datetime_received)
    #print: 2021-13-09 11:08:31+00:00 
    #print expected: 2021-13-09 13:08:31+02+00

您可以使用pytz显示不同时区的日期时间对象。

示例:

import pytz
from datetime import datetime

ts = datetime.now()
tz = pytz.timezone('Etc/GMT+2')
print(ts.astimezone(tz))

最终解决方案:

from exchangelib import Credentials, Account
from datetime import datetime

import pytz

credentials = Credentials(email, password)
acc = Account(email, credentials=credentials, autodiscover=True)
tz = pytz.timezone('Europe/Amsterdam')

for item in acc.inbox.all().order_by('-datetime_received')[:100]:
    dt = item.datetime_received.astimezone(tz)
    print(dt)