# Python实现获取某个月份及其附近6个月份

2017/11/23 09:49

# -*- coding:utf-8 -*-
import time

def get_months_nearby(year, month):
"""
获取包含给定月份在内的附近的7个月份
:param year:
:param month:
:return:
"""
months = []
# temp_year = year
# temp_month = month
this_year = time.strftime('%Y', time.localtime())
# this_month = time.strftime('%m', time.localtime())
first_array = [1, 2, 3]
second_array = [4, 5, 6, 7, 8, 9]
third_array = [10, 11, 12]
this_month = 3
if int(year) == int(this_year):
if int(month) == int(month):
if int(this_month) <= 6:
last_start_month = int(this_month)+6
last_end_month = 13
for i in range(last_start_month, last_end_month, 1):
month_object = {'year': int(this_year)-1, 'month': i}
months.append(month_object)
start_month = 1
end_month = int(this_month) + 1
for i in range(start_month, end_month, 1):
month_object = {'year': int(this_year), 'month': i}
months.append(month_object)
else:
start_month = int(this_month)-6
end_month = int(this_month) + 1
for i in range(start_month, end_month, 1):
month_object = {'year': int(this_year), 'month': i}
months.append(month_object)
else:
dis = int(this_month)-int(month)
if dis <= 3:
if int(this_month) >= 7:
start_month = int(this_month)-6
end_month = this_month
for i in range(start_month, end_month, 1):
month_object = {'year': int(this_year), 'month': i}
months.append(month_object)
else:
last_start_month = int(this_month) + 6
last_end_month = 13
for i in range(last_start_month, last_end_month, 1):
month_object = {'year': int(this_year) - 1, 'month': i}
months.append(month_object)
start_month = 1
end_month = int(this_month) + 1
for i in range(start_month, end_month, 1):
month_object = {'year': int(this_year), 'month': i}
months.append(month_object)
else:
start_month = int(this_month)-3
end_month = int(this_month) + 4
for i in range(start_month, end_month, 1):
month_object = {'year': int(this_year), 'month': i}
months.append(month_object)
else:
if int(this_year)-int(year) == 1:
if int(month) in second_array:
start_month = int(month) - 3
end_month = int(month) + 4
for i in range(start_month, end_month, 1):
month_object = {'year': int(year), 'month': i}
months.append(month_object)
elif int(month) in first_array:
last_start_month = int(month) + 6
last_end_month = 13
for i in range(last_start_month, last_end_month, 1):
month_object = {'year': int(year) - 1, 'month': i}
months.append(month_object)
start_month = 1
end_month = int(month) + 1
for i in range(start_month, end_month, 1):
month_object = {'year': int(year), 'month': i}
months.append(month_object)
elif int(month) in third_array:
if int(this_month) >= 3:
last_start_month = int(month)-3
last_end_month = 13
for i in range(last_start_month, last_end_month, 1):
month_object = {'year': int(year), 'month': i}
months.append(month_object)
start_month = 1
end_month = int(month) - 8
for i in range(start_month, end_month, 1):
month_object = {'year': int(year)+1, 'month': i}
months.append(month_object)
else:
if int(this_month) == 2:
if int(month) == 12:
last_start_month = int(month) - 4
last_end_month = 13
for i in range(last_start_month, last_end_month, 1):
month_object = {'year': int(year), 'month': i}
months.append(month_object)
start_month = 1
end_month = this_month
for i in range(start_month, end_month, 1):
month_object = {'year': int(year) + 1, 'month': i}
months.append(month_object)
else:
last_start_month = int(month) - 3
last_end_month = 13
for i in range(last_start_month, last_end_month, 1):
month_object = {'year': int(year), 'month': i}
months.append(month_object)
start_month = 1
end_month = int(month) - 8
for i in range(start_month, end_month, 1):
month_object = {'year': int(year) + 1, 'month': i}
months.append(month_object)
else:
if int(month) == 10:
last_start_month = int(month) - 3
last_end_month = 13
for i in range(last_start_month, last_end_month, 1):
month_object = {'year': int(year), 'month': i}
months.append(month_object)
start_month = 1
end_month = int(month) - 8
for i in range(start_month, end_month, 1):
month_object = {'year': int(year) + 1, 'month': i}
months.append(month_object)
else:
last_start_month = 7
last_end_month = 13
for i in range(last_start_month, last_end_month, 1):
month_object = {'year': int(year), 'month': i}
months.append(month_object)
start_month = 1
end_month = 2
for i in range(start_month, end_month, 1):
month_object = {'year': int(year) + 1, 'month': i}
months.append(month_object)
else:
if int(month) in second_array:
start_month = int(month) - 3
end_month = int(month) + 4
for i in range(start_month, end_month, 1):
month_object = {'year': int(year), 'month': i}
months.append(month_object)
elif int(month) in first_array:
last_start_month = int(month) + 6
last_end_month = 13
for i in range(last_start_month, last_end_month, 1):
month_object = {'year': int(year) - 1, 'month': i}
months.append(month_object)
start_month = 1
end_month = int(month) + 1
for i in range(start_month, end_month, 1):
month_object = {'year': int(year), 'month': i}
months.append(month_object)
elif int(month) in third_array:
last_start_month = int(month) - 3
last_end_month = 13
for i in range(last_start_month, last_end_month, 1):
month_object = {'year': int(year), 'month': i}
months.append(month_object)
start_month = 1
end_month = int(month) - 8
for i in range(start_month, end_month, 1):
month_object = {'year': int(year) + 1, 'month': i}
months.append(month_object)
return months

if __name__ == '__main__':
ms = get_months_nearby(2016, 11)
print ms

0 评论
0 收藏
0