what's the fastest way of converting a string time stamp into epoch time with python?
Question: I need to do lots of conversation from string time stamp like '2012-09-08
12:23:33' into a seconds which is based on epoch time.Then i need to get time
gap between two timestamp.I tried two different ways:

    
    
    date1 = '2012-09-08'
    time2 = '12:23:33'
    timelist1 = map(int, date1.split('-') + time1.split(':'))
    
    date2 = '2012-09-08'
    time2 = '12:23:33'
    timelist2 = map(int, date2.split('-') + time2.split(':'))
    delta = datetime.datetime(*timelist2) - datetime.datetime(*timelist1)
    print delta.seconds
    

The second way is:

    
    
    date1 = '2012-09-08'
    time1 = '12:23:33'
    d1 = datetime.datetime.strptime(date1 + ' ' + time1, "%Y-%m-%d %H:%M:%S")
    seconds1 = time.mktime(d1.timetuple())
    ....
    seconds2 = time.mktime(d2.timetuple())
    print seconds2-deconds1
    

However these two ways are not fast enough because I have almost 100 millions
actions to do.Any suggestion?


Answer: You'd be much better off using the [`datetime.datetime.strptime()`
function](http://docs.python.org/library/datetime.html#datetime.datetime.strptime),
then subtract the two results:

    
    
    import datetime
    
    date1, time1 = '2012-09-08', '12:23:33'
    date2, time2 = '2012-09-08', '12:23:33'
    
    dt1 = datetime.datetime.strptime(date1 + ' ' + time1, "%Y-%m-%d %H:%M:%S")
    dt2 = datetime.datetime.strptime(date2 + ' ' + time2, "%Y-%m-%d %H:%M:%S")
    
    print (dt1 - dt2).total_seconds()
    

Note that `datetime.timedelta.seconds` gives you just the _remainder_ in
seconds, there is also a `.days` attribute. Use
[`.total_seconds()`](http://docs.python.org/library/datetime.html#datetime.timedelta.total_seconds)
instead, it's much more convenient and saves you having to take the `.days`
value into account.

Using the `datetime.datetime.strptime()` method, plus `timedelta` methods, is
keeping the majority of the work in C code and should be faster.

Note that neither method is very fast because of the parsing step. Time tests:

    
    
    >>> import timeit
    >>> def parse_datetime():
    ...     date1, time1 = '2012-09-08', '12:23:33'
    ...     date2, time2 = '2012-09-08', '12:23:33'
    ...     dt1 = datetime.datetime.strptime(date1 + ' ' + time1, "%Y-%m-%d %H:%M:%S")
    ...     dt2 = datetime.datetime.strptime(date2 + ' ' + time2, "%Y-%m-%d %H:%M:%S")
    ...     (dt1 - dt2).total_seconds()
    ... 
    >>> def parse_time():
    ...     d1 = time.strptime(date1 + ' ' + time1, "%Y-%m-%d %H:%M:%S")
    ...     d2 = time.strptime(date2 + ' ' + time2, "%Y-%m-%d %H:%M:%S")
    ...     seconds1 = time.mktime(d1)
    ...     seconds2 = time.mktime(d2)
    ...     seconds1 - seconds2
    ... 
    >>> timeit.timeit('test()', 'from __main__ import parse_datetime as test', number=10000)
    0.6590030193328857
    >>> timeit.timeit('test()', 'from __main__ import parse_time as test', number=10000)
    0.7742340564727783
    

where the second method just uses the `time.strptime()` function (which
outputs a timetuple directly).

There are no faster avenues to parsing a date-time string that I am aware of
though.

