python内置的方法,太简单了。。。
from datetime import datetime,timedelta
d = datetime.now()
z = d + timedelta(days=-7)
print str(z).split(' ')[0].split('-')[1]
import calendar
DAY_QUERY = 4
def day_end_month(year, month):
cal = calendar.month(year, month)
return cal[-3:-1]
def modify_time(time_str):
# kwargs = {'earliest_time':'2017-02-20T00:00:00.000',
# 'latest_time':'',
# 'search_mode':'normal',
# 'exec_mode':'blocking'}
kwargs = {'earliest_time': '',
'latest_time': '',
'search_mode': 'normal',
'exec_mode': 'blocking'}
append_hms = 'T00:00:00.000'
# time_str: 2017-02-20
tmp_year = time_str.split('-')[0]
tmp_month = time_str.split('-')[1]
tmp_day = time_str.split('-')[2]
tmp_month_int = int(tmp_month)
tmp_day_int = int(tmp_day)
if tmp_day_int > 9+DAY_QUERY:
# just modify month
day_gt_13 = tmp_year + '-' + tmp_month + '-' + str(tmp_day_int - DAY_QUERY)
kwargs['earliest_time'] = day_gt_13 + append_hms
kwargs['latest_time'] = time_str + append_hms
# 4-12
if tmp_day_int > DAY_QUERY and tmp_day_int < 10+DAY_QUERY and tmp_month_int > 9:
# modify day with add 0(zero)
day_gt_3_and_lt_13 = tmp_year + '-' + tmp_month + '-' + '0' + str(tmp_day_int - DAY_QUERY)
kwargs['earliest_time'] = day_gt_3_and_lt_13 + append_hms
kwargs['latest_time'] = time_str + append_hms
if tmp_day_int > DAY_QUERY and tmp_day_int < 10+DAY_QUERY and tmp_month_int > 1 and tmp_month_int < 10:
x_day_gt_3_and_lt_13 = tmp_year + '-' + tmp_month + '-' + '0' + str(tmp_day_int - DAY_QUERY)
kwargs['earliest_time'] = x_day_gt_3_and_lt_13 + append_hms
kwargs['latest_time'] = time_str + append_hms
# 1-3 and month > 1
if tmp_day_int < 1+DAY_QUERY and tmp_month_int > 1:
# modify day and month
# put year and month with int
tmp_new_day_gt_1 = int(day_end_month(int(tmp_year), tmp_month_int-1))
# str(tmp_new_day_gt_1 - (3 - tmp_day_int)): get end of day of last month
month_day_lt_4_and_month_gt_1 = tmp_year + '-' + '0' + str(tmp_month_int - 1) + '-' + str(
tmp_new_day_gt_1 - (DAY_QUERY - tmp_day_int))
kwargs['earliest_time'] = month_day_lt_4_and_month_gt_1 + append_hms
kwargs['latest_time'] = time_str + append_hms
if tmp_day_int < 1+DAY_QUERY and tmp_month_int == 1:
pass
# modify year, month and day
tmp_new_month_eq_1 = '12'
tmp_new_year_eq_1 = str(int(tmp_year) - 1)
tmp_new_day_eq_1 = int(day_end_month(int(tmp_year), tmp_month_int))
year_lt_4_and_month_eq_1 = tmp_new_year_eq_1 + '-' + tmp_new_month_eq_1 + '-' + str(
tmp_new_day_eq_1 - (DAY_QUERY - tmp_day_int))
kwargs['earliest_time'] = year_lt_4_and_month_eq_1 + append_hms
kwargs['latest_time'] = time_str + append_hms
if tmp_day_int > DAY_QUERY and tmp_day_int < 10+DAY_QUERY and tmp_month_int == 1:
l_day_gt_3_and_lt_13 = tmp_year + '-' + tmp_month + '-' + '0' + str(tmp_day_int - DAY_QUERY)
kwargs['earliest_time'] = l_day_gt_3_and_lt_13 + append_hms
kwargs['latest_time'] = time_str + append_hms
if tmp_day_int > 9+DAY_QUERY and tmp_month_int == 1:
n_day_gt_13 = tmp_year + '-' + tmp_month + '-' + str(tmp_day_int - DAY_QUERY)
kwargs['earliest_time'] = n_day_gt_13 + append_hms
kwargs['latest_time'] = time_str + append_hms
return kwargs
print modify_time('2017-01-12')