This is a simplified version of what my problem is but here is the gist of it: I have a script that sorts emails by using Regex to find specific patterns. One of the items I sort by is the date an email was received. I use the datetime module to format the date string, like so:
s1 = datetime.strptime(regex_obtained_str, '%B %d, %Y, %I:%M:%S %p').strftime('%m/%d/%Y %I:%M:%S %p')
Now, let's say I have parsed 4 emails and these are the results from the Regex function:
s1 = 'March 15, 2013 8:58:34 PM'
s2 = 'March 15, 2013 11:10:10 AM'
s3 = 'March 15, 2013 11:16:06 AM'
s4 = 'April 15, 2013 12:02:04 PM'
Which turns into this after strftime formatting:
s1 = '03/15/2013 08:58:34 PM'
s2 = '03/15/2013 11:10:10 AM'
s3 = '03/15/2013 11:16:06 AM'
s4 = '04/15/2013 12:02:04 PM'
If I put these all in a list in random order and perform a sort() function, like so:
L = [s2, s1, s3, s4]
L.sort()
The output is:
['03/15/2013 08:58:34 PM', '03/15/2013 11:10:10 AM', '03/15/2013 11:16:06 AM', '04/15/2013 12:02:04 PM']
As you can see, the "AM" and "PM" is not being taken into account when sorting. I've looked at a NUMBER of other questions similar to this on StackOverflow, and all of them suggest changing %H to %I in the strptime()/stftime() functions to account for the 12 hour clock, but I already have done that. Can anyone please suggest how to take %p into account when sorting? Thanks!