From: https://github.com/metomi/isodatetime/issues/147
The smallest value used may also have a decimal fraction, as in "P0.5Y" to indicate half a year. This decimal fraction may be specified with either a comma or a full stop, as in "P0,5Y" or "P0.5Y".
import metomi.isodatetime.parsers as parse
d = parse.DurationParser().parse('P1Y')
d.get_days_and_seconds()
(365.0, 0.0)
d = parse.DurationParser().parse('P0.5Y')
d.get_days_and_seconds()
--------------------------------------------------------------------------- ISO8601SyntaxError Traceback (most recent call last) ~/Development/python/workspace/notebooks/venv/lib/python3.7/site-packages/metomi/isodatetime/parsers.py in parse(self, expression) 618 allow_truncated=False, --> 619 assumed_time_zone=(0, 0) 620 ) ~/Development/python/workspace/notebooks/venv/lib/python3.7/site-packages/metomi/isodatetime/parsers.py in parse_timepoint_expression(timepoint_expression, validate, **kwargs) 643 parser = TimePointParser(**kwargs) --> 644 return parser.parse(timepoint_expression, validate=validate) ~/Development/python/workspace/notebooks/venv/lib/python3.7/site-packages/metomi/isodatetime/parsers.py in parse(self, timepoint_string, dump_format, dump_as_parsed, validate) 259 """ --> 260 date_info, time_info, parsed_expr = self.get_info(timepoint_string) 261 if dump_as_parsed: ~/Development/python/workspace/notebooks/venv/lib/python3.7/site-packages/metomi/isodatetime/parsers.py in get_info(self, timepoint_string) 466 date = date_time_time_zone[0] --> 467 keys, date_info = self.get_date_info(date) 468 format_key, type_key, date_expr = keys ~/Development/python/workspace/notebooks/venv/lib/python3.7/site-packages/metomi/isodatetime/parsers.py in get_date_info(self, date_string, bad_types) 426 result.groupdict()) --> 427 raise ISO8601SyntaxError("date", date_string) 428 ISO8601SyntaxError: Invalid ISO 8601 date representation: 0.5Y During handling of the above exception, another exception occurred: ISO8601SyntaxError Traceback (most recent call last) <ipython-input-2-32c359c8415e> in <module> ----> 1 d = parse.DurationParser().parse('P0.5Y') 2 d.get_days_and_seconds() ~/Development/python/workspace/notebooks/venv/lib/python3.7/site-packages/metomi/isodatetime/parsers.py in parse(self, expression) 620 ) 621 except ISO8601SyntaxError: --> 622 raise ISO8601SyntaxError("duration", expression) 623 if timepoint.get_is_week_date(): 624 raise ISO8601SyntaxError("duration", expression) ISO8601SyntaxError: Invalid ISO 8601 duration representation: P0.5Y
import iso8601
# iso8601 does not seem to support periods
iso8601.parse_date("P1Y")
--------------------------------------------------------------------------- ParseError Traceback (most recent call last) <ipython-input-3-38e514b82c7e> in <module> 1 import iso8601 2 # iso8601 does not seem to support periods ----> 3 iso8601.parse_date("P1Y") ~/Development/python/workspace/notebooks/venv/lib/python3.7/site-packages/iso8601/iso8601.py in parse_date(datestring, default_timezone) 193 m = ISO8601_REGEX.match(datestring) 194 if not m: --> 195 raise ParseError("Unable to parse date string %r" % datestring) 196 groups = m.groupdict() 197 ParseError: Unable to parse date string 'P1Y'
import pendulum
pendulum.parse("P1Y")
Duration(years=1, days=0)
pendulum.parse("P0.5Y")
--------------------------------------------------------------------------- ParserError Traceback (most recent call last) <ipython-input-5-39eff8703077> in <module> ----> 1 pendulum.parse("P0.5Y") ~/Development/python/workspace/notebooks/venv/lib/python3.7/site-packages/pendulum/parser.py in parse(text, **options) 18 options["now"] = options.get("now", pendulum.get_test_now()) 19 ---> 20 return _parse(text, **options) 21 22 ~/Development/python/workspace/notebooks/venv/lib/python3.7/site-packages/pendulum/parser.py in _parse(text, **options) 34 return pendulum.now() 35 ---> 36 parsed = base_parse(text, **options) 37 38 if isinstance(parsed, datetime.datetime): ~/Development/python/workspace/notebooks/venv/lib/python3.7/site-packages/pendulum/parsing/__init__.py in parse(text, **options) 68 _options.update(options) 69 ---> 70 return _normalize(_parse(text, **_options), **_options) 71 72 ~/Development/python/workspace/notebooks/venv/lib/python3.7/site-packages/pendulum/parsing/__init__.py in _parse(text, **options) 122 # If not strict 123 if options.get("strict", True): --> 124 raise ParserError("Unable to parse string [{}]".format(text)) 125 126 try: ParserError: Unable to parse string [P0.5Y]