1

I have the following code:

comments = sorted(comments, key=lambda k: k['time_created'])

How to sort correctly if some elements have the different format, like 2017-12-14T17:42:30.345244+0000 and 2017-12-14 00:23:23.468560 and my code fail when trying to compare?

I need to save seconds accuracy.

Is it the good solution?

comments = sorted(comments, key=lambda k: self.unix_time_millis(k['time_created']), reverse=True)


@staticmethod
def unix_time_millis(dt):
    epoch = datetime.datetime.utcfromtimestamp(0)
    return (dt - epoch).total_seconds() * 1000.0
indigo153
  • 1,021
  • 2
  • 10
  • 23
  • you could map the timestamp to a datetime object and sort these. – T4rk1n Dec 14 '17 at 21:43
  • how to do this? could you provide some code please? – indigo153 Dec 14 '17 at 21:44
  • Did you try converting them all into one date time format? – robotHamster Dec 14 '17 at 21:45
  • In your place, I will convert everything to ms ans make the sort – Walid Da. Dec 14 '17 at 21:47
  • @WalidDaboubi how to do this? – indigo153 Dec 14 '17 at 21:48
  • It's a bit odd that they have different time formats in the same list. It will make your sort very slow if you have to try several date formats to see which one a particular entry contains. Do all these entries come from the same source or are they several sources each with a known date format? If it is the later than it might be best to convert them to the same format when you build the list. – Paul Rooney Dec 14 '17 at 21:49
  • Parse date as a string, get year, month, day, hour, minutes.. multiply to get seconds and add seconds – Walid Da. Dec 14 '17 at 21:50
  • Convert everything to one format, and compare that. Best if you can do that before you put the objects into the list, but you could also detect and convert the formats in the comparison function, before doing the comparison. – Jim Mischel Dec 14 '17 at 23:44

2 Answers2

1

Python datetime objects are comparable and therefore sortable. I assume that you currently don't use datetime objects but Strings. The following example code is taken from How to format date string via multiple formats in python

import dateutil.parser
dateutil.parser.parse(date_string)

You would then convert a list of strings to datetime objects via

list_of_dt_objs = [dateutil.parser.parse(str) for str in list_of_strings]

Please note that dateutil is an extension lib. So you have to install it, for instance via pip.

Omni
  • 1,002
  • 6
  • 12
0

Something like this:

import re
import operator

def convert_to_secs(date_string):
    multipliers = [31557600,2592000,86400,3600,60]
    date_in_secs = 0
    index = 0
    for v in re.split(':|-|T|\.|\+|\ ',date_string):
        if index < len(multipliers):
            date_in_secs = date_in_secs + int(v) * multipliers[index]
            index += 1
        else:
            break
    return date_in_secs

def sort_dates(my_dates_in_string):
    my_dates_dict = {}
    for string_date in my_dates_in_string:
        my_dates_dict[string_date] = convert_to_secs(string_date)
    return sorted(my_dates_dict.items(), key=operator.itemgetter(1))

print sort_dates(["2017-12-14T17:42:30.345244+0000", "2017-12-14 00:23:23.468560"])
Walid Da.
  • 948
  • 1
  • 7
  • 15