the Gravity of Python 2

Ethan Furman ethan at stoneleaf.us
Thu Jan 9 00:31:02 EST 2014


On 01/08/2014 08:34 PM, Chris Angelico wrote:
> On Thu, Jan 9, 2014 at 3:29 PM, Roy Smith <roy at panix.com> 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~



More information about the Python-list mailing list