Search code examples
pythonexchangelib

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.


Solution

  • 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