Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'objects,': 0.07; 'python': 0.09; 'destroyed.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'timestamp': 0.09; 'timestamps': 0.09; 'times,': 0.13; '"seconds': 0.16; 'behaviour.': 0.16; 'message- id:@dough.gmane.org': 0.16; 'naive': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'utc,': 0.16; "shouldn't": 0.17; '>>>': 0.18; 'app': 0.19; 'skip:p 30': 0.20; 'all,': 0.21; 'import': 0.21; "i'd": 0.22; "i've": 0.23; 'header:User-Agent:1': 0.26; 'subject:skip:d 10': 0.27; 'header:X-Complaints-To:1': 0.28; 'behaviour': 0.29; 'expect': 0.31; 'to:addr:python-list': 0.33; 'skip:d 20': 0.34; "can't": 0.34; 'something': 0.35; 'received:org': 0.36; 'but': 0.36; 'uses': 0.37; 'several': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'received:79': 0.61; 'first': 0.61; 'different': 0.63; 'confusing': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Damjan Subject: Confusing datetime.datetime Date: Thu, 05 Jul 2012 16:10:11 +0200 Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 79.126.129.60 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:13.0) Gecko/20120616 Thunderbird/13.0.1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 69 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1341497710 news.xs4all.nl 6851 [2001:888:2000:d::a6]:52117 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:24917 I've been struggling with an app that uses Postgresql/Psycopg2/SQLAlchemy and I've come to this confusing behaviour of datetime.datetime. First of all, the "Seconds since Epoch" timestamps are always in UTC, so shouldn't change with timezones. So I'd expect that a round trip of a timestamp through datetime.datetime, shouldn't change it. Now, all is good when I use a naive datetime.datetime -- TZ=UTC python >>> from datetime import datetime >>> dt = datetime.fromtimestamp(1341446400) >>> dt datetime.datetime(2012, 7, 5, 0, 0) >>> dt.strftime('%s') '1341446400' -- TZ=Asia/Tokyo python >>> from datetime import datetime >>> dt = datetime.fromtimestamp(1341446400) >>> dt datetime.datetime(2012, 7, 5, 9, 0) >>> dt.strftime('%s') '1341446400' But when I use an timezone aware datetime.datetime objects, the timestamp roundtrip is destroyed. I get 2 different timestamps. Am I missing something here, I've been reading the datetime documentation several times, but I can't understand what is the intended behaviour. -- TZ=UTC python >>> from datetime import datetime >>> import pytz >>> tz = pytz.timezone('Europe/Skopje') >>> dt = datetime.fromtimestamp(1341446400, tz) >>> dt datetime.datetime(2012, 7, 5, 2, 0, tzinfo=) >>> dt.strftime('%s') '1341453600' -- TZ=Asia/Tokyo python >>> from datetime import datetime >>> import pytz >>> tz = pytz.timezone('Europe/Skopje') >>> dt = datetime.fromtimestamp(1341446400, tz) >>> dt datetime.datetime(2012, 7, 5, 2, 0, tzinfo=) >>> dt.strftime('%s') '1341421200' Python 2.7.3, pytz 2012c -- damjan