Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!ecngs!feeder2.ecngs.de!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'python.': 0.02; 'represents': 0.05; 'subject:Python': 0.06; '*not*': 0.07; 'none,': 0.07; 'agree,': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'implements': 0.09; 'message-id:@stoneleaf.us': 0.09; '~ethan~': 0.09; 'jan': 0.12; 'posted': 0.15; '"to': 0.16; 'accepts': 0.16; 'adjustment': 0.16; 'none.': 0.16; 'objects.': 0.16; 'recipe': 0.16; 'represents.': 0.16; 'roy': 0.16; 'simple.': 0.16; 'skip:= 80': 0.16; 'time).': 0.16; 'time.time()': 0.16; 'timestamp': 0.16; 'utc': 0.16; 'utc.': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'module': 0.19; 'thu,': 0.19; 'otherwise,': 0.22; 'saying': 0.22; 'header :User-Agent:1': 0.23; 'url:dev': 0.24; "haven't": 0.24; 'looks': 0.24; '(for': 0.26; 'somewhere': 0.26; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'fixed': 0.29; 'chris': 0.29; 'said,': 0.30; "i'm": 0.30; 'code': 0.31; '>>>>': 0.31; 'argue': 0.31; 'object.': 0.31; 'class': 0.32; 'url:python': 0.33; 'maybe': 0.34; 'skip:d 20': 0.34; 'subject:the': 0.34; "i'd": 0.34; "can't": 0.35; 'classes': 0.35; 'objects': 0.35; 'but': 0.35; 'there': 0.35; 'version': 0.36; 'module.': 0.36; 'charset:us-ascii': 0.36; 'url:org': 0.36; 'example,': 0.37; 'so,': 0.37; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'skip:. 10': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'new': 0.61; 'received:173': 0.61; 'simple': 0.61; 'offer': 0.62; 'information': 0.63; 'our': 0.64; 'provide': 0.64; 'account': 0.65; 'smith': 0.68; 'saving': 0.69; 'increasingly': 0.84; 'scratch,': 0.84; 'notion': 0.91; 'obtained': 0.96; 'instant': 0.97 Date: Wed, 08 Jan 2014 21:31:02 -0800 From: Ethan Furman User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121010 Thunderbird/16.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: the Gravity of Python 2 References: <78d91$52cbf8e9$541826b9$29485@cache1.tilbu1.nb.home.nl> <4b702$52cc262e$541826b9$22985@cache80.multikabel.net> <4cbf$52cc2e82$541826b9$11761@cache70.multikabel.net> <686$52cd4640$541826b9$21896@cache1.tilbu1.nb.home.nl> In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator3304.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-BWhitelist: no X-Source-IP: 173.12.184.233 X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: ([173.12.184.233]) [173.12.184.233]:47111 X-Source-Auth: ethan+stoneleaf.us X-Email-Count: 1 X-Source-Cap: dG9idWs7dG9idWs7Z2F0b3IzMzA0Lmhvc3RnYXRvci5jb20= 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: 63 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1389246686 news.xs4all.nl 2886 [2001:888:2000:d::a6]:44669 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:63570 On 01/08/2014 08:34 PM, Chris Angelico wrote: > On Thu, Jan 9, 2014 at 3:29 PM, Roy Smith wrote: >> So, I'd like to see your code which generates an aware UTC datetime >> object in Python. And then we can argue about whether it's simple or >> not :-) > > Like I said, I don't use the datetime module. But fundamentally, an > aware datetime represents an instant in time - which is exactly what a > POSIX timestamp ("time_t") represents. So I can't offer exact code for > working with them, other than to say that time.time() is precisely > accomplishing the same job. Maybe there needs to be a recipe posted > somewhere saying "To create a datetime from a POSIX time, do this". > > This is simple: > >>>> time.time() > 1389242048.669482 I agree, and I'm sure Roy also agrees, that that is simple. However, that is *not* a timezone-aware datetime. The point of having the datetime module is so we all don't have to reroll our own from scratch, and yet this particular (and increasingly important) operation wasn't intuitive nor easy. http://docs.python.org/dev/library/datetime.html?highlight=datetime#datetime.tzinfo =================================================================================== class datetime.tzinfo An abstract base class for time zone information objects. These are used by the datetime and time classes to provide a customizable notion of time adjustment (for example, to account for time zone and/or daylight saving time). class datetime.timezone A class that implements the tzinfo abstract base class as a fixed offset from the UTC. New in version 3.2. Objects of these types are immutable. Objects of the date type are always naive. An object of type time or datetime may be naive or aware. A datetime object d is aware if d.tzinfo is not None and d.tzinfo.utcoffset(d) does not return None. If d.tzinfo is None, or if d.tzinfo is not None but d.tzinfo.utcoffset(d) returns None, d is naive. A time object t is aware if t.tzinfo is not None and t.tzinfo.utcoffset(None) does not return None. Otherwise, t is naive. =================================================================================== That is not simple. This however, may qualify (emphasis added): =================================================================================== classmethod datetime.utcnow() Return the current UTC date and time, with tzinfo None. This is like now(), but returns the current UTC date and time, as a naive datetime object. An aware current UTC datetime can be obtained by calling *datetime.now(timezone.utc)*. See also now(). =================================================================================== It looks like the .fromtimestamp also accepts tz=timezone.utc, so perhaps that is the simple way to get the timezone aware datetime. I haven't tested, I'm going to bed. :/ -- ~Ethan~