Python: how to correctly convert from a string date in a particular time zone?
<p>I have input from a human in <code>YYYY-MM-DD HH:MM:SS</code> format that I know is supposed to be Los Angeles local time. In Python, how do I convert this to a <code>datetime.datetime</code> object that is unambiguous and correct? I am aware that the input is ambiguous during the autumn transition out of DST; I'm fine with either choice happening for dates within that hour as long as it is deterministic.</p>

<p>Here is my attempt, which I'm surprised to find doesn't work:</p>

<pre><code>&gt;&gt;&gt; import pytz
&gt;&gt;&gt; from dateutil import parser as date_parser
&gt;&gt;&gt; PACIFIC = pytz.timezone('US/Pacific')
&gt;&gt;&gt; result = date_parser.parse('2016-08-01 00:00:00 FOO', tzinfos={'FOO': PACIFIC})
&gt;&gt;&gt; result.utcoffset()
datetime.timedelta(-1, 57600)
&gt;&gt;&gt; str(result)
'2016-08-01 00:00:00-08:00'
</code></pre>

<p>Despite having asked for <code>US/Pacific</code> and this being a summer date, I get UTC-8 instead of UTC-7.</p>

<p>You can use <a href="https://pypi.python.org/pypi/dateparser" rel="nofollow">dateparser</a> for that.</p>

<p><pre></p>

<code>import dateparser
&gt;&gt;&gt; dateparser.parse('2016-08-01 00:00:00', settings={'TIMEZONE': 'US/Pacific', 'TO_TIMEZONE': 'UTC'})
datetime.datetime(2016, 8, 1, 7, 0)
</code></pre>

<p></p>

python datetime python-datetime python-dateutil