Search code examples
pythontimezoneisopyyaml

Can PyYAML parse iso8601 dates?


According to the YAML spec, iso8601 dates with timezones should be recognised. However, on trying to parse them using PyYAML 3.10 (on Windows 7 with ActivePython 2.7.2.5) I get naive dates:

In [7]: yaml.load("2001-12-14t21:59:43.10-05:00")
Out[7]: datetime.datetime(2001, 12, 15, 2, 59, 43, 100000)

In [8]: yaml.load("2001-12-14 21:59:43.10 -5")
Out[8]: datetime.datetime(2001, 12, 15, 2, 59, 43, 100000)

(First format is the strict iso8601 and second is the 'relaxed' format; examples taken directly from YAML spec.)

Is this expected behaviour, or is my PyYaml not working correctly?


Solution

  • This is fixed as of pyyaml 5.3 (Github Pull Request)

    >>> yaml.safe_load('2020-12-17t14:40:00+02:00')
    datetime.datetime(2020, 12, 17, 14, 40, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200)))