Overflowerror: Signed Integer Is Greater Than Maximum When Parsing Date in Python

Overflowerror: Signed Integer Is Greater Than Maximum When Parsing Date in Python

When attempting to parse a numerically formatted date:

s ="2506181306"

using the appropriate date format:

DateFormat = '%d%m%H%M%S'
dateutil.parser.parse(s).strftime(DateFormat)

We end up with an "integer greater than maximum" error:

In [15]:     dateutil.parser.parse(s).strftime(DateFormat)
---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
<ipython-input-15-9eead825f5c9> in <module>()
----> 1 dateutil.parser.parse(s).strftime(DateFormat)

/usr/local/lib/python2.7/site-packages/dateutil/parser.pyc in parse(timestr, parserinfo, **kwargs)
   1006         return parser(parserinfo).parse(timestr, **kwargs)
   1007     else:
-> 1008         return DEFAULTPARSER.parse(timestr, **kwargs)
   1009
   1010

/usr/local/lib/python2.7/site-packages/dateutil/parser.pyc in parse(self, timestr, default, ignoretz, tzinfos, **kwargs)
    402                 repl[attr] = value
    403
--> 404         ret = default.replace(**repl)
    405
    406         if res.weekday is not None and not res.day:

OverflowError: signed integer is greater than maximum

So then what is the proper way to parse that date? Is that "greater than maximum" referring to an individual field within the date - or to the entire string?

Update Jakob provided correct way

In [21]: from datetime import datetime

In [22]:     datetime.strptime(s,DateFormat)
Out[22]: datetime.datetime(1900, 6, 25, 18, 13, 6)

1 Answer

DateFormat = '%d%m%H%M%S'
dateutil.parser.parse(s).strftime(DateFormat)

You're trying to parse this without setting the format first. The error is thrown from parse, the call chain doesn't even make it to the strftime call. It's trying to choose a format that makes sense to it for that string, and the one it chooses seems to be a unix epoch timestamp, but that's supposed to be 31 bits (because signed) at most, so it errors.

What you probably want is:

datetime.strptime(s,DateFormat)
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Marcus Vance
Author

Marcus Vance

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.