Question

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?

Was it helpful?

Solution 2

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)))

OTHER TIPS

If you don't like the default behaviour (naive utc datetime, utc offset lost) you could provide your own constructor:

import dateutil.parser
import yaml

def timestamp_constructor(loader, node):
    return dateutil.parser.parse(node.value)
yaml.add_constructor(u'tag:yaml.org,2002:timestamp', timestamp_constructor)

print(repr(yaml.load("2001-12-14T21:59:43.10-05:00")))
# -> datetime.datetime(2001, 12, 14, 21, 59, 43, 100000, tzinfo=tzoffset(None, -18000))

This also affects Django when loading database fixtures from YAML files. There is a Django-specific workaround; see: Loaddata not dealing with timestamps and timezones properly

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top