[Zope3-dev] mini-RFC: times and timezones

Gary Poster gary at zope.com
Thu Feb 24 10:21:38 EST 2005


On Feb 24, 2005, at 10:13 AM, Tim Peters wrote:

> [...]
>
> [Gary Poster]
>>>    * zope.i18n should grow a convenience method to convert dates from
>>> utc to another timezone [6]
>
> [Garrett Smith]
>> It's surprising that Python's datetime module doesn't do this.
>
> I'm more surprised that people think it doesn't <wink>.  For dt an
> aware datetime.datetime, dt.astimezone(newtz) converts dt from dt's
> time zone to time zone newtz.

Yay!  cool, and sorry I hadn't seen that.

> If dt's time zone is UTC to begin with,
> that's all it takes.  tzinfo subclasses also inherit an appropriate
> fromutc(dt) method, which converts dt, viewed as being in UTC, to the
> tzinfo subclass's time zone.  astimezone() calls fromutc() under the
> covers, as explained in the docs.
>
> This is all obscured by that while Python ships with an abstract
> tzinfo base class, and a bunch of implementation code for time zone
> conversion, it doesn't ship with any concrete tzinfo subclass -- not
> even one for UTC.  An app has to supply its own, catering to whichever
> detailed time zone rules it's willing to live with.
>
> Backing up a bit:
>
>>> - working with mixed Python timezone-aware and -naive datetimes is a
>>> big pain (i.e., they don't work together well, and are not intended 
>>> to
>>> AFAIK).
>
> That's correct.  Guido viewed them as different kinds of beasts, and
> expected that most people would use the naive species most often.
>
> A possibility I didn't see mentioned is to store aware datetimes but
> with a UTC tzinfo subclass.  Then they're all stored in UTC, but play
> nicely with other aware datetimes in other time zones.

That's actually precisely what I was proposing.  That was the 
zope.i18n.utc tzinfo I mentioned.

> A consideration I didn't see mentioned is that lots of work went into
> ensuring that datetime objects have efficient pickle representations
> (small size, and very fast to pickle and unpickle).  Of course this
> works best for naive datetimes -- pickling an aware datetime drags in
> all the machinery for pickling its instance of a tzinfo subclass too.

I am under the impression that the datetimeutils tzinfo implementations 
are pickle-friendly as well.  That's what I was intending to use as the 
basis of zope.i18n.utc.

Gary



More information about the Zope3-dev mailing list