Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Peter Pearson Newsgroups: comp.lang.python Subject: Re: Datetime timezone trouble (was: Matplotlib X-axis timezone trouble) Date: 1 Jul 2015 19:09:03 GMT Lines: 48 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: individual.net X7t25sgoENDNMrthjGRywgwY9sCi/nWBGsDep/bNLVoOQF/FAX Cancel-Lock: sha1:u7ASAWdEZjf4BLL3Wo8lYODo/nA= User-Agent: slrn/pre1.0.0-18 (Linux) Xref: csiph.com comp.lang.python:93383 On Wed, 1 Jul 2015 10:55:02 -0600, Ian Kelly wrote: > On Wed, Jul 1, 2015 at 10:36 AM, Peter Pearson [wrote]: [snip] >> Here's a very simple demonstration that either something is wrong >> or I don't understand how datetime and tzinfo are supposed to work: >> >> $ python >> Python 2.7.3 (default, Mar 13 2014, 11:03:55) >>>>> from pytz import timezone >>>>> from datetime import datetime >>>>> pacific = timezone("US/Pacific") >>>>> print(datetime(2014, 7, 7, 12, tzinfo=pacific)) >> 2014-07-07 12:00:00-08:00 >>>>> print(datetime(2014, 1, 7, 12, tzinfo=pacific)) >> 2014-01-07 12:00:00-08:00 >>>>> >> >> The "-08:00" is appropriate in the second (January) case, but the >> first case is in July, and should have "-07:00". > > Use this instead: > >>>> print(pacific.localize(datetime(2014, 7, 7, 12))) > 2014-07-07 12:00:00-07:00 > > See http://pytz.sourceforge.net/#localized-times-and-date-arithmetic Excellent. Thank you. To summarize, for any similarly afflicted person who finds this thread: - http://pytz.sourceforge.net/#localized-times-and-date-arithmetic says: Unfortunately using the tzinfo argument of the standard datetime constructors ‘’does not work’’ with pytz for many timezones. It is safe for timezones without daylight saving transitions though, such as UTC: - PEP 0431 says (approximately) that datetime doesn't work as well as we might like with DST, but will be slow to change. - The "localize" function is pytz's way of working around this shortcoming of datetime's -- To email me, substitute nowhere->runbox, invalid->com.