简体   繁体   中英

The update of exchangelib remove the attribute 'localize' from EWSTimeZone

I was using an old version of the library exchangelib. All was fine with my code until some users of my App start to have some issues. Long story short, I had to install for me and the rest of us the last version of exchangelib exchangelib==4.7.2

So my question is: how can I replace the method: tz.localize(EWSDateTime.from_datetime(dt_start_time)) to filter the emails in my INBOX (or else)

Please find a small part of the code so it will be easier to read:

from exchangelib import Credentials, Account, Configuration, DELEGATE, FileAttachment
from exchangelib import EWSTimeZone, EWSDateTime
import datetime as dt

# fill in  with your Credentials
_outAcctName = ''
_pwd = ''
_subjectEmailToLookFor = ''

o_cred= Credentials(username = _outAcctName, password = _pwd)   
o_account = Account(credentials = o_cred, primary_smtp_address = _outAcctName, autodiscover = True, access_type = DELEGATE)
o_inbox = o_account.inbox

# Filtering
d_paramFilter = {}
d_paramFilter['subject__icontains'] = _subjectEmailToLookFor
dt_start_time = dt.datetime.strptime('2022-03-13', '%Y-%m-%d')
dt_end_time = dt.datetime.strptime( '2022-03-15', '%Y-%m-%d')
tz = EWSTimeZone.localzone()
try:
    tz_start =  tz.localize(EWSDateTime.from_datetime(dt_start_time))
    tz_end =    tz.localize(EWSDateTime.from_datetime(dt_end_time))
    d_paramFilter['datetime_received__range'] = (tz_start, tz_end)
except Exception as err:
    print(' ERROR 1: |{}|'.format(err))
try:
    o_emails = o_inbox.filter(**d_paramFilter)
except Exception as err:
    print(' ERROR 2: |{}|'.format(err))

I got now the error: ERROR 1: | 'EWSTimeZone' object has no attribute 'localize' |

I am aware of the following documentation. But that does not say what to use to have the same functionality.

Try to use the following:

tz_start =  EWSDateTime.from_datetime(dt_start_time).astimezone(tz)
tz_end =    EWSDateTime.from_datetime(dt_end_time).astimezone(tz)

It worked for me

Newer versions of exchangelib have an implementation of timezones that support directly assigning a timezone to a date:

EWSDateTime(2017, 9, 5, 8, 30, tzinfo=EWSTimeZone('Europe/Copenhagen'))

This is based on the new zoneinfo module in Python 3.9 and has the same syntax and guarantees.

Unrelated to the question, but you can simplify your code by calling EWSDateTime.strptime() directly:

EWSDateTime.strptime("2022-03-13", "%Y-%m-%d")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM