Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 947
Charles Roper Avatar asked Nov 08 '12 17:11

Charles Roper


1 Answers

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))
like image 112
jfs Avatar answered Sep 28 '22 00:09

jfs