how to parse time stamps in Python from PyPI packages (eg. 12-Oct-2010 06:40)
Question: I'm wondering if there is a straightforward way to do this, since PyPI is the
Python packaging authority, it seems like parsing these time stamps (into the
epoch time perhaps) should then be handled in Python somewhat easily, but it
eludes me how to do this.

As an example, here are the time stamps of when different versions of IPython
were uploaded:

<https://pypi.python.org/packages/source/i/ipython/>

Could someone please explain to me how to parse these time stamps with Python
into the epoch in a sensible way.


Answer: Instead of trying to parse the directory listing output, use the PyPI API.

There are:

  * a [JSON API](https://wiki.python.org/moin/PyPIJSON?action=show&redirect=PyPiJson)
  * a [XML-RPC interface](https://wiki.python.org/moin/PyPIXmlRpc?action=show&redirect=PyPiXmlRpc)
  * and a [HTTP API](http://www.python.org/dev/peps/pep-0301/)

which give you _parsed_ access to that information. For example, the [JSON
link for ipython release
0.10.1](https://pypi.python.org/pypi/ipython/0.10.1/json) gives you ISO8601
dates:

    
    
    "urls": [
        {
            "has_sig": false, 
            "upload_time": "2010-10-12T08:40:01", 
            "comment_text": "", 
            "python_version": "source", 
            "url": "https://pypi.python.org/packages/source/i/ipython/ipython-0.10.1.tar.gz", 
            "md5_digest": "54ae47079b0e9a0998593a99ce76ec1f", 
            "downloads": 20100, 
            "filename": "ipython-0.10.1.tar.gz", 
            "packagetype": "sdist", 
            "size": 5837840
        }, 
        {
            "has_sig": false, 
            "upload_time": "2010-10-12T08:40:38", 
            "comment_text": "", 
            "python_version": "source", 
            "url": "https://pypi.python.org/packages/source/i/ipython/ipython-0.10.1.zip", 
            "md5_digest": "f636c7ea03ff626a6ef9bd9a901de691", 
            "downloads": 29725, 
            "filename": "ipython-0.10.1.zip", 
            "packagetype": "sdist", 
            "size": 6419900
        }
    ]
    

which can be parsed into [`datetime.datetime`
objects](http://docs.python.org/2/library/datetime.html):

    
    
    >>> import datetime
    >>> datestr = "2010-10-12T08:40:01"
    >>> datetime.datetime.strptime(datestr, '%Y-%m-%dT%H:%M:%S')
    datetime.datetime(2010, 10, 12, 8, 40, 1)
    

That said, the date format in your sample listing can be parsed with:

    
    
    >>> datestr = "12-Oct-2010 06:40"
    >>> datetime.datetime.strptime(datestr, '%m-%b-%Y %H:%M')
    datetime.datetime(2010, 12, 1, 6, 40)
    

but the behavior of the `%b` placeholder is dependent on your locale; it'll
only work correctly here when that is set to C or english.

The best way to parse _arbitrary_ dates then, is to use the [`python-dateutil`
package](http://labix.org/python-dateutil) (an add-on install), as it's parser
is very robust:

    
    
    >>> from dateutil.parser import parse
    >>> parse(datestr)
    datetime.datetime(2010, 10, 12, 6, 40)
    

