Created
August 19, 2011 14:29
-
-
Save RaD/1156930 to your computer and use it in GitHub Desktop.
Function to calculate the count of defined week day in date range
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
def days_count_for_date_range(index, begin, end): | |
""" | |
Метод для вычисления количества появлений определённого дня недели | |
внутри указанного диапазона дат, включительно. | |
@type index: integer | |
@param index: Индекс дня недели, понедельник=0. | |
@type begin: datetime.date | |
@param begin: Дата начала диапазона. | |
@type end: datetime.date | |
@param end: Дата конца диапазона. | |
@rtype: integer | |
@return: Количество дней. | |
""" | |
count = 0 | |
offset = index - begin.weekday() | |
# вычисляем первую дату с нужным днём недели | |
if index < 0: | |
offset += 7 | |
day = begin + timedelta(days=offset) | |
# считаем количество дат | |
while day <= end: | |
count += 1 | |
day += timedelta(days=7) | |
return count |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is similar to: