id,summary,reporter,owner,description,type,status,priority,component,severity,resolution,keywords,cc
202,cannot roundtrip offset-aware datetime instances,aaugustin,xi,"I'd expect that `yaml.load(yaml.dump(foo) == foo` for reasonable values of `foo`.

However, this isn't true for timezone-aware datetimes:
{{{
>>> import datetime
>>> from pytz import utc
>>> import yaml
>>> dt = datetime.datetime(2011, 9, 1, 10, 20, 30, 405060, tzinfo=utc)
>>> yaml.load(yaml.dump(dt)) == dt
Traceback (most recent call last):
  File ""<stdin>"", line 1, in <module>
TypeError: can't compare offset-naive and offset-aware datetimes
>>> yaml.load(yaml.dump(dt))
datetime.datetime(2011, 9, 1, 10, 20, 30, 405060)
}}}

PyYAML dumps the offset correctly, but when it loads the value, it returns a naive datetime in UTC, with the offset susbtracted.

Instead, I suggest using a simple tzinfo class, such as the following (from http://docs.python.org/library/datetime.html) to represent offsets:

{{{
from datetime import timedelta, tzinfo

class FixedOffset(tzinfo):
    """"""Fixed offset in minutes east from UTC.""""""

    def __init__(self, offset, name):
        self.__offset = timedelta(minutes=offset)
        self.__name = name

    def utcoffset(self, dt):
        return self.__offset

    def tzname(self, dt):
        return self.__name

    def dst(self, dt):
        return timedelta(0)
}}}

Note that it often makes sense to handle naive datetimes (such as user input) in local times. In such cases, round-tripping a timezone-aware datetime through PyYAML — for example, dumping/loading fixtures in Django — will result in data corruption.

See also #25 and [https://bitbucket.org/aaugustin/django/src/3209d08a4ab8/django/db/models/fields/__init__.py#cl-700 this solution to the same problem].",enhancement,new,normal,pyyaml,normal,,,matt@…
