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?
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With