0

Let's say I have a bunch of dates as such:

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

From the first date, I want to figure out the last date a week from then. So, from "2018-11-19 08:00:03", I want to get "2018-11-26 04:00:07" (a week from 11/19/2018 at 8:00:03 would be anytime on or before 11/26/2018 at 8:00:03).

I'm for some reason having a mental lapse with this. Do I do something to figure out the difference in datetime? How am I to elegantly check whether or not it's the last date in the current week?

Salvatore
  • 10,815
  • 4
  • 31
  • 69
  • Use pandas and timedelta(days = 7) – DavidE Oct 10 '19 at 02:46
  • Could you add more precision, what is a week? 7 days from a given date, Sunday to Sunday, or maybe Monday? Do you want all the date that match the last week day or only the closet date time? – Florian Bernard Oct 10 '19 at 02:47
  • 7 days - the last datetime that falls within 7 days – BlahZayBlahZay Oct 10 '19 at 02:50
  • You should accept an answer if that solves your issue. Otherwise the question remains in the queue for more answers. Also, if you find an answer helpful you should upvote it. – ba_ul Oct 10 '19 at 15:54

3 Answers3

1

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 *
>>> 
lenik
  • 23,228
  • 4
  • 34
  • 43
1

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.

smac89
  • 39,374
  • 15
  • 132
  • 179
1

[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')

ba_ul
  • 2,049
  • 5
  • 22
  • 35