Last active
January 17, 2020 20:41
-
-
Save endersonmenezes/d3eb3ca0ada0db8007cd0062c43f136e to your computer and use it in GitHub Desktop.
This code makes you enter a date range, it will divide that range into 12 sections, checking if a target is in those 12 sections and what the index of that section is.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#%% | |
from datetime import * | |
from dateutil.relativedelta import * | |
from dateutil.rrule import * | |
start_date = '01/01/2019' | |
end_date = '31/12/2019' # Not necessary | |
target = '01/07/2019' | |
chunk_size = 12 | |
def chunk_it(list_input: list): | |
avg = len(list_input) / float(12) | |
list_out = [] | |
last = 0.0 | |
while last < len(list_input): | |
list_out.append(list_input[int(last):int(last + avg)]) | |
last += avg | |
return list_out | |
def get_section(start, target): | |
start_date = datetime.strptime(start, '%d/%m/%Y') | |
target_date = datetime.strptime(target, '%d/%m/%Y') | |
end_date = start_date + relativedelta(days=+365) | |
all_dates = list(rrule(DAILY, dtstart=start_date, until=end_date)) | |
if target_date not in all_dates: | |
return -1 | |
all_dates = chunk_it(all_dates) | |
for part in all_dates: | |
if target_date in part: | |
return all_dates.index(part) | |
print(get_section(start_date, target)) | |
#%% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment