python - Format These Dates And Get Time Passed -


i have python list of dates , i'm using min , max find recent , oldest (first, best method?), need format dates can figure out current time , subtract oldest date in list can "in last 27 minutes..." can state how many days, hours, or minutes have past since oldest. here list (the dates change depending on i'm pulling) can see current format. how info need?

[u'sun oct 06 18:00:55 +0000 2013', u'sun oct 06 17:57:41 +0000 2013', u'sun oct 06 17:55:44 +0000 2013', u'sun oct 06 17:54:10 +0000 2013', u'sun oct 06 17:35:58 +0000 2013', u'sun oct 06 17:35:58 +0000 2013', u'sun oct 06 17:35:25 +0000 2013', u'sun oct 06 17:34:39 +0000 2013', u'sun oct 06 17:34:39 +0000 2013', u'sun oct 06 17:34:39 +0000 2013', u'sun oct 06 17:30:35 +0000 2013', u'sun oct 06 17:25:28 +0000 2013', u'sun oct 06 17:24:04 +0000 2013', u'sun oct 06 17:24:04 +0000 2013', u'sun oct 06 17:22:08 +0000 2013', u'sun oct 06 17:22:08 +0000 2013', u'sun oct 06 17:21:00 +0000 2013', u'sun oct 06 17:18:49 +0000 2013', u'sun oct 06 17:18:49 +0000 2013', u'sun oct 06 17:15:29 +0000 2013', u'sun oct 06 17:15:29 +0000 2013', u'sun oct 06 17:13:35 +0000 2013', u'sun oct 06 17:12:18 +0000 2013', u'sun oct 06 17:12:00 +0000 2013', u'sun oct 06 17:07:34 +0000 2013', u'sun oct 06 17:03:59 +0000 2013'] 

you won't oldest , newest date/time entries list entries using min , max - "fri" come before "mon", example. you'll want put things data structure represents date/time stamps properly.

fortunately, python's datetime module comes method convert lots of date/time stamp strings proper representation - datetime.datetime.strptime. see the guide how use it.

once that's done can use min , max , timedelta compute difference.

from datetime import datetime  # start initial list = [u'sun oct 06 18:00:55 +0000 2013', u'sun oct 06 17:57:41 +0000 2013', u'sun oct 06 17:55:44 +0000 2013', u'sun oct 06 17:54:10 +0000 2013', u'sun oct 06 17:35:58 +0000 2013', u'sun oct 06 17:35:58 +0000 2013', u'sun oct 06 17:35:25 +0000 2013', u'sun oct 06 17:34:39 +0000 2013', u'sun oct 06 17:34:39 +0000 2013', u'sun oct 06 17:34:39 +0000 2013', u'sun oct 06 17:30:35 +0000 2013', u'sun oct 06 17:25:28 +0000 2013', u'sun oct 06 17:24:04 +0000 2013', u'sun oct 06 17:24:04 +0000 2013', u'sun oct 06 17:22:08 +0000 2013', u'sun oct 06 17:22:08 +0000 2013', u'sun oct 06 17:21:00 +0000 2013', u'sun oct 06 17:18:49 +0000 2013', u'sun oct 06 17:18:49 +0000 2013', u'sun oct 06 17:15:29 +0000 2013', u'sun oct 06 17:15:29 +0000 2013', u'sun oct 06 17:13:35 +0000 2013', u'sun oct 06 17:12:18 +0000 2013', u'sun oct 06 17:12:00 +0000 2013', u'sun oct 06 17:07:34 +0000 2013', u'sun oct 06 17:03:59 +0000 2013']  # format string date/time stamps using # on python 3.3 on windows can use format # s_format = "%a %b %d %h:%m:%s %z %y" # however, python 2.7 on windows doesn't work that. if of date/time stamps use same timezone can do: s_format = "%a %b %d %h:%m:%s +0000 %y"  # convert text list datetime objects = [datetime.strptime(d, s_format) d in a]  # extremes oldest = min(a) newest = max(a)  # if substract oldest newest timedelta object, can give total number of seconds between them. can use calculate days, hours, , minutes. delta = int((newest - oldest).total_seconds()) delta_days, rem = divmod(delta, 86400) delta_hours, rem = divmod(rem, 3600) delta_minutes, delta_seconds = divmod(rem, 60) 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -