Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.017 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; '16,': 0.03; 'melbourne': 0.05; '22,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; '39,': 0.16; 'fiddle': 0.16; 'finney': 0.16; 'personally,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'utc': 0.16; 'utc.': 0.16; 'values:': 0.16; ':-)': 0.16; '>>>': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'skip': 0.24; 'specify': 0.24; 'tend': 0.24; 'earlier': 0.24; '15,': 0.26; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'skip:p 30': 0.29; 'requests': 0.31; 'continually': 0.31; 'writes:': 0.31; 'trouble': 0.34; 'skip:d 20': 0.34; "can't": 0.35; 'convert': 0.35; 'but': 0.35; 'really': 0.36; 'library.': 0.36; 'received:com.au': 0.36; 'right?': 0.36; 'ben': 0.38; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'subject:" ': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; "you're": 0.61; 'first': 0.61; 'national': 0.62; 'skip:n 10': 0.64; 'more': 0.64; '30,': 0.65; 'alone.': 0.84; 'faces,': 0.84; 'manage,': 0.84; 'pain': 0.84; 'politicians': 0.84; 'received:125': 0.84; 'worth,': 0.84; '\xe2\x80\x9cwe': 0.84; 'good,': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ben Finney Subject: Re: UTC "timezone" causing brain explosions Date: Thu, 30 Jan 2014 09:44:58 +1100 References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: vmx15867.hosting24.com.au X-Public-Key-ID: 0xBD41714B X-Public-Key-Fingerprint: 9CFE 12B0 791A 4267 887F 520C B7AC 2E51 BD41 714B X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-gpg.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux) Cancel-Lock: sha1:XzSXyC00jwpUDYkjL3D3wzO0kTo= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1391035514 news.xs4all.nl 2887 [2001:888:2000:d::a6]:46973 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64954 Skip Montanaro writes: > Following up on my earlier note about UTC v. GMT, I am having some > trouble grokking attempts to convert a datetime into UTC. For what it's worth, you're not alone. Time zones are a hairy beast to manage, made all the more difficult because national politicians continually fiddle with them which means they can't just be a built-in part of the Python standard library. > Consider these three values: > > >>> import pytz > >>> UTC = pytz.timezone("UTC") > >>> UTC > > >>> LOCAL_TZ = pytz.timezone("America/Chicago") > >>> LOCAL_TZ > > >>> now = datetime.datetime.now() > >>> now > datetime.datetime(2014, 1, 29, 15, 39, 35, 263666) > > All well and good, right? Not really :-) You avoid the pain if you never create naive datetime values in the first place. Instead, specify the timezone for the value you're creating:: >>> now = datetime.datetime.now(tz=LOCAL_TZ) >>> now datetime.datetime(2014, 1, 29, 16, 35, 7, 900526, tzinfo=) That way the value will respond correctly to requests to convert it to whatever timezone you specify:: >>> MELBOURNE = pytz.timezone("Australia/Melbourne") >>> now.astimezone(MELBOURNE) datetime.datetime(2014, 1, 30, 9, 35, 7, 900526, tzinfo=) >>> now.astimezone(UTC) datetime.datetime(2014, 1, 29, 22, 35, 7, 900526, tzinfo=) >>> now.astimezone(LOCAL_TZ) datetime.datetime(2014, 1, 29, 16, 35, 7, 900526, tzinfo=) -- \ “We tend to scoff at the beliefs of the ancients. But we can't | `\ scoff at them personally, to their faces, and this is what | _o__) annoys me.” —Jack Handey | Ben Finney