Let's say I have a bunch of dates as such:
2018-11-19 08:00:03
2018-11-19 12:00:02
2018-
Let's say I have a bunch of dates as such:
2018-11-19 08:00:03
2018-11-19 12:00:02
2018-
Basically, all your dates are more that a week from now (scroll down a bit for the code):
>>> a = '''2018-11-19 08:00:03
... 2018-11-19 12:00:02
... 2018-11-19 16:00:01
... 2018-11-19 20:00:10
... 2018-11-20 00:00:09
... 2018-11-20 04:00:08
... 2018-11-20 08:00:07
... 2018-11-20 12:00:06
... 2018-11-20 16:00:04
... 2018-11-20 20:00:03
... 2018-11-21 00:00:02
... 2018-11-21 04:00:01
... 2018-11-21 08:00:10
... 2018-11-21 12:00:09
... 2018-11-21 16:00:08
... 2018-11-21 20:00:07
... 2018-11-22 00:00:06
... 2018-11-22 04:00:04
... 2018-11-22 08:00:03
... 2018-11-22 12:00:02
... 2018-11-22 16:00:01
... 2018-11-22 20:00:10
... 2018-11-23 00:00:09
... 2018-11-23 04:00:08
... 2018-11-23 08:00:07
... 2018-11-23 12:00:05
... 2018-11-23 16:00:05
... 2018-11-23 20:00:04
... 2018-11-24 00:00:03
... 2018-11-24 04:00:01
... 2018-11-24 08:00:10
... 2018-11-24 12:00:09
... 2018-11-24 16:00:08
... 2018-11-24 20:00:08
... 2018-11-25 00:00:06
... 2018-11-25 04:00:04
... 2018-11-25 08:00:03
... 2018-11-25 12:00:02
... 2018-11-25 16:00:01
... 2018-11-25 20:00:10
... 2018-11-26 00:00:08
... 2018-11-26 04:00:07
... 2018-11-26 08:00:06
... 2018-11-26 12:00:05
... 2018-11-26 16:00:04
... 2018-11-26 20:00:02
... 2018-11-27 00:00:01
... 2018-11-27 04:00:10
... 2018-11-27 08:00:10
... 2018-11-27 12:00:08
... 2018-11-27 16:00:06
... 2018-11-27 20:00:05
... 2018-11-28 00:00:04
... 2018-11-28 04:00:03
... 2018-11-28 08:00:02
... 2018-11-28 12:00:11
... 2018-11-28 16:00:09
... 2018-11-28 20:00:08
... 2018-11-29 00:00:07
... 2018-11-29 04:00:06
... 2018-11-29 08:00:05
... 2018-11-29 12:00:04
... 2018-11-29 16:00:03
... 2018-11-29 20:00:02
... 2018-11-30 00:00:01
... 2018-11-30 04:00:10
... 2018-11-30 08:00:09
... 2018-11-30 12:00:08
... 2018-11-30 16:00:07
... 2018-11-30 20:00:06
... 2018-12-01 04:00:04
... 2018-12-01 12:00:02
... 2018-12-01 20:00:10
... 2018-12-02 04:00:08
... 2018-12-02 12:00:05
... 2018-12-02 20:00:04
... 2018-12-03 04:00:02
... 2018-12-03 12:00:10
... 2018-12-03 20:00:08
... 2018-12-04 04:00:01
... 2018-12-04 12:00:08
... 2018-12-04 20:00:05
... 2018-12-05 04:00:02
... 2018-12-05 12:00:09'''
>>> import datetime
>>> dates = [datetime.datetime.strptime( d, '%Y-%m-%d %H:%M:%S') for d in a.split('\n')]
>>> now = datetime.datetime.now()
>>> for d in dates :
... print d,
... if now - d > datetime.timedelta( days=7 ) :
... print '*'
... else :
... print
...
2018-11-19 08:00:03 *
2018-11-19 12:00:02 *
2018-11-19 16:00:01 *
2018-11-19 20:00:10 *
2018-11-20 00:00:09 *
2018-11-20 04:00:08 *
2018-11-20 08:00:07 *
2018-11-20 12:00:06 *
2018-11-20 16:00:04 *
2018-11-20 20:00:03 *
2018-11-21 00:00:02 *
2018-11-21 04:00:01 *
2018-11-21 08:00:10 *
2018-11-21 12:00:09 *
2018-11-21 16:00:08 *
2018-11-21 20:00:07 *
2018-11-22 00:00:06 *
2018-11-22 04:00:04 *
2018-11-22 08:00:03 *
2018-11-22 12:00:02 *
2018-11-22 16:00:01 *
2018-11-22 20:00:10 *
2018-11-23 00:00:09 *
2018-11-23 04:00:08 *
2018-11-23 08:00:07 *
2018-11-23 12:00:05 *
2018-11-23 16:00:05 *
2018-11-23 20:00:04 *
2018-11-24 00:00:03 *
2018-11-24 04:00:01 *
2018-11-24 08:00:10 *
2018-11-24 12:00:09 *
2018-11-24 16:00:08 *
2018-11-24 20:00:08 *
2018-11-25 00:00:06 *
2018-11-25 04:00:04 *
2018-11-25 08:00:03 *
2018-11-25 12:00:02 *
2018-11-25 16:00:01 *
2018-11-25 20:00:10 *
2018-11-26 00:00:08 *
2018-11-26 04:00:07 *
2018-11-26 08:00:06 *
2018-11-26 12:00:05 *
2018-11-26 16:00:04 *
2018-11-26 20:00:02 *
2018-11-27 00:00:01 *
2018-11-27 04:00:10 *
2018-11-27 08:00:10 *
2018-11-27 12:00:08 *
2018-11-27 16:00:06 *
2018-11-27 20:00:05 *
2018-11-28 00:00:04 *
2018-11-28 04:00:03 *
2018-11-28 08:00:02 *
2018-11-28 12:00:11 *
2018-11-28 16:00:09 *
2018-11-28 20:00:08 *
2018-11-29 00:00:07 *
2018-11-29 04:00:06 *
2018-11-29 08:00:05 *
2018-11-29 12:00:04 *
2018-11-29 16:00:03 *
2018-11-29 20:00:02 *
2018-11-30 00:00:01 *
2018-11-30 04:00:10 *
2018-11-30 08:00:09 *
2018-11-30 12:00:08 *
2018-11-30 16:00:07 *
2018-11-30 20:00:06 *
2018-12-01 04:00:04 *
2018-12-01 12:00:02 *
2018-12-01 20:00:10 *
2018-12-02 04:00:08 *
2018-12-02 12:00:05 *
2018-12-02 20:00:04 *
2018-12-03 04:00:02 *
2018-12-03 12:00:10 *
2018-12-03 20:00:08 *
2018-12-04 04:00:01 *
2018-12-04 12:00:08 *
2018-12-04 20:00:05 *
2018-12-05 04:00:02 *
2018-12-05 12:00:09 *
>>>
You can sort the list and do a binary search to find one that is closest to "a week from now".
See this answer on how to compare datetime objects.
Also see this answer on how to convert string to datetime.
To do the binary search, you can create a function like so and use the bisect module:
import bisect
def find_next_week(dates, current_date):
return dates[bisect.bisect(dates, current_date + datetime.timedelta(weeks=1)) - 1]
Which you can use like this(1):
>>> find_next_week(dates, datetime.datetime.strptime('2018-11-19 08:00:03', '%Y-%m-%d %H:%M:%S'))
datetime.datetime(2018, 11, 26, 4, 0, 7)
To convert this datetime object to the original filename, you can do this:
>>> datetime.datetime(2018, 11, 26, 4, 0, 7).strftime('%Y-%m-%d %H:%M:%S')
'2018-11-26 04:00:07'
(1) Remember to sort dates before passing it to the function because the function expects a sorted list.
[Edit: If the volume of your data is large, then I recommend the solution that uses bisect — for faster results.]
datestrings = '''2018-11-19 08:00:03
... 2018-11-19 12:00:02
... 2018-11-19 16:00:01
... 2018-11-19 20:00:10
... 2018-11-20 00:00:09
... 2018-11-20 04:00:08
... 2018-11-20 08:00:07
... 2018-11-20 12:00:06
... 2018-11-20 16:00:04
... 2018-11-20 20:00:03
... 2018-11-21 00:00:02
... 2018-11-21 04:00:01
... 2018-11-21 08:00:10
... 2018-11-21 12:00:09
... 2018-11-21 16:00:08
... 2018-11-21 20:00:07
... 2018-11-22 00:00:06
... 2018-11-22 04:00:04
... 2018-11-22 08:00:03
... 2018-11-22 12:00:02
... 2018-11-22 16:00:01
... 2018-11-22 20:00:10
... 2018-11-23 00:00:09
... 2018-11-23 04:00:08
... 2018-11-23 08:00:07
... 2018-11-23 12:00:05
... 2018-11-23 16:00:05
... 2018-11-23 20:00:04
... 2018-11-24 00:00:03
... 2018-11-24 04:00:01
... 2018-11-24 08:00:10
... 2018-11-24 12:00:09
... 2018-11-24 16:00:08
... 2018-11-24 20:00:08
... 2018-11-25 00:00:06
... 2018-11-25 04:00:04
... 2018-11-25 08:00:03
... 2018-11-25 12:00:02
... 2018-11-25 16:00:01
... 2018-11-25 20:00:10
... 2018-11-26 00:00:08
... 2018-11-26 04:00:07
... 2018-11-26 08:00:06
... 2018-11-26 12:00:05
... 2018-11-26 16:00:04
... 2018-11-26 20:00:02
... 2018-11-27 00:00:01
... 2018-11-27 04:00:10
... 2018-11-27 08:00:10
... 2018-11-27 12:00:08
... 2018-11-27 16:00:06
... 2018-11-27 20:00:05
... 2018-11-28 00:00:04
... 2018-11-28 04:00:03
... 2018-11-28 08:00:02
... 2018-11-28 12:00:11
... 2018-11-28 16:00:09
... 2018-11-28 20:00:08
... 2018-11-29 00:00:07
... 2018-11-29 04:00:06
... 2018-11-29 08:00:05
... 2018-11-29 12:00:04
... 2018-11-29 16:00:03
... 2018-11-29 20:00:02
... 2018-11-30 00:00:01
... 2018-11-30 04:00:10
... 2018-11-30 08:00:09
... 2018-11-30 12:00:08
... 2018-11-30 16:00:07
... 2018-11-30 20:00:06
... 2018-12-01 04:00:04
... 2018-12-01 12:00:02
... 2018-12-01 20:00:10
... 2018-12-02 04:00:08
... 2018-12-02 12:00:05
... 2018-12-02 20:00:04
... 2018-12-03 04:00:02
... 2018-12-03 12:00:10
... 2018-12-03 20:00:08
... 2018-12-04 04:00:01
... 2018-12-04 12:00:08
... 2018-12-04 20:00:05
... 2018-12-05 04:00:02
... 2018-12-05 12:00:09'''
First parse the date strings into datetime objects. Then find all dates that are less than a week from a given date. Then find the largest of all such dates.
from datetime import datetime, timedelta
dates = [datetime.strptime(d, '%Y-%m-%d %H:%M:%S') for d in datestrings.split('\n')]
first_date = datetime(2018, 11, 19, 8, 0, 3)
dates_in_week = [d for d in dates if d < first_date + timedelta(weeks = 1)]
last_date = max(dates_in_week)
last_date.strftime('%Y-%m-%d %H:%M:%S')