title: How can I use a dateutil.tz.tz.tzoffset to localize a timezone naive datetime created using strptime?
body: <p>How can I use an object, which is of type dateutil.tz.tz.tzoffset, to localize a timezone naive datetime object with correct timezone and DST info so it can be properly converted to UTC? Alternatively, how do I convert dateutil.tz.tz.tzoffset to a pytz.timezone?</p>
  
  <p>I have been unable to find good information on localizing a datetime object using dateutil.tz.tz.tzoffset object. See below for a few of the better articles I've reviewed.</p>
  
  <p>Background Info:</p>
  
  <p>I am dealing with numerous date strings, most of which have no timezone information. In some cases the time is in GMT, in others it's local. I have to first determine the timezone of the device creating these various logs, then parse the various date strings, add the add timezone if it's a local time, and finally convert it to UTC.</p>
  
  <p>I almost have this all working, except the only reliable way to determine the TimeZone is from a text file which has the date in the format of EDT, IST, etc so I have used the top up-voted post at the below link to accomplish this using dateutil's parser.parse() function and sending it a dict for it's tzinfos argument. (<a href="https://stackoverflow.com/questions/1703546/parsing-date-time-string-with-timezone-abbreviated-name-in-python">Parsing date/time string with timezone abbreviated name in Python?</a>) </p>
  
  <p>However, this leaves me with a DateTime which has a tzinfo type=dateutil.tz.tz.tzoffset. That's ok by me, except then I need to use this tzinfo to localize the strings that do not contain Timezone info, and the dateutil.tz.tz.tzoffset type does not have a localize option like pytz.timezone does, which is the crux of my problem.</p>
  
  <p>Am I making this too hard? Do I just replace the tzinfo in the timezone naive datetime with the dateutil.tz.tz.tzoffset object I've saved?</p>
  
  <p>Code:</p>
  
  <p>Below reads a date string and saves it as a datetime object, saves the timezone in a var for later use, and then converts the datestring to UTC:</p>
  
  <pre><code>from dateutil.parser import parse as parsedate
  import pytz
  from pytz import timezone
  from datetime import datetime
  
  timestr = 'Sat, 5/01/2019 8:00PM EDT' #only reliable source of timezone info
  dtfromstrEDT = parsedate(timestr, tzinfos=tzd) #tzd is created from the above link
  mytimeZone = dtfromstrEDT.tzinfo  # save local timezone
  dtUTC = dtfromstrEDT.astimezone(pytz.timezone('UTC'))  # convert to utc
  </code></pre>
  
  <p>Now here is a new timezone naive datestring. It was recorded in local time same as above (EDT) and so I want to use my saved mytimeZone var to localize it and convert to UTC. I use the standard strptime to read it in as a naive datetime. However, when using mytimeZone to localize the Naive datetime, it fails. Error below. I understand the error; but I don't know how to accomplish what I need:</p>
  
  <pre><code>timestrnaive = 'Mar 15 12:09:20 2019' #in EDT time, same as above string but without any timezone info
  dtfromstrNaive = datetime.strptime(timestrnaive, "%b %d %H:%M:%S %Y")
  dtlocalized = mytimeZone.localize(dtfromstrNaive, is_dst=True)  
  # the above is where it fails with provided error below
  # however I can do this instead if I had a pytz.timezone object:
  loc_tz = pytz.timezone('America/New_York')
  dtlocalized = loc_tz.localize(dtfromstrNaive, is_dst=True) 
  dtUTC2 = dtlocalized.astimezone(pytz.timezone('UTC'))  # convert to utc
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:\Examples\timezones.py", line 221, in &lt;module&gt;
      dtlocalized = mytimeZone.localize(dtfromstrNaive, is_dst=True)  # convert to whatever tz is stored in self.timeZone
  AttributeError: 'tzoffset' object has no attribute 'localize'
  </code></pre>
  
  <p>Reviewed the following:</p>
  
  <p><a href="https://stackoverflow.com/questions/22304951/localize-datetime-timezone-aware-from-timezone-offset">Localize datetime (timezone aware) from timezone offset</a> - I am not sure how to apply that to this problem since I have a dateutil.tz.tz.tzoffset object, not the raw utc time.</p>
  
  <p><a href="https://stackoverflow.com/questions/1703546/parsing-date-time-string-with-timezone-abbreviated-name-in-python">Parsing date/time string with timezone abbreviated name in Python?</a> - This got me where I could read timezone data such as EDT and PDT.</p>
  
  <p><a href="https://stackoverflow.com/questions/7065164/how-to-make-an-unaware-datetime-timezone-aware-in-python">How to make an unaware datetime timezone aware in python</a> - This does not help because it doesn't address the dateutil.tz.tz.tzoffset issue I mention.</p>