使用 dateutil.parser.parse 抛出两位数年份日期的 ValueError

Throw ValueError for two digit year dates with dateutil.parser.parse

在做一些数据清理时,我注意到 dateutil.parser.parse 未能拒绝某个格式错误的日期,认为其中的第一个数字是两位数的年份。可以强制此库将两位数年份视为无效吗?

示例:

from dateutil.parser import parse
parse('22-23 February')

输出:

datetime.datetime(2022, 2, 23, 0, 0)

我设法通过 parserinfo 参数将自定义 dateutil.parser.parserinfo 对象传递给 dateutil.parser.parse 来解决这个问题。幸运的是,dateutil.parser.parserinfo 有一个 convertyear 方法可以在派生的 class 中重载,以便对年份执行额外的验证。

from dateutil.parser import parse, parserinfo

class NoTwoDigitYearParserInfo(parserinfo):
    def convertyear(self, year, century_specified=False):
        if year < 100 and not century_specified:
            raise ValueError('Two digit years are not supported.')
        return parserinfo.convertyear(self, year, century_specified)

parse('22-23 February', parserinfo = NoTwoDigitYearParserInfo())

输出:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/site-packages/dateutil/parser.py", line 1162, in parse
    return parser(parserinfo).parse(timestr, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/dateutil/parser.py", line 552, in parse
    res, skipped_tokens = self._parse(timestr, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/dateutil/parser.py", line 1055, in _parse
    if not info.validate(res):
  File "/usr/local/lib/python3.5/site-packages/dateutil/parser.py", line 360, in validate
    res.year = self.convertyear(res.year, res.century_specified)
  File "<stdin>", line 4, in convertyear
ValueError: Two digit years are not supported.