Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 298
WestCoastProjects Avatar asked Jun 26 '15 03:06

WestCoastProjects


1 Answers

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)
like image 90
Jakob Weisblat Avatar answered Oct 26 '22 00:10

Jakob Weisblat