Created
August 15, 2017 13:33
-
-
Save cantoniazzi/530f4a4e79c6cd5ecf660399befac795 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
''' | |
Author: Cássio Antoniazzi | |
email: [email protected] | |
''' | |
months = {} | |
months[1] = 31 | |
months[2] = 28 | |
months[3] = 31 | |
months[4] = 30 | |
months[5] = 31 | |
months[6] = 30 | |
months[7] = 30 | |
months[8] = 31 | |
months[9] = 30 | |
months[10] = 31 | |
months[11] = 30 | |
months[12] = 31 | |
''' | |
descrip: recursive function to add dates | |
params: dictionary with day, month, year | |
return: dictionary with day, month, year | |
''' | |
def add_date(tmp_date): | |
date_return = {} | |
while True: | |
tmp_day = int(int(months[int(tmp_date['month'])]) - int(int(tmp_date['day']))) | |
if tmp_day > 0: | |
tmp_date['day'] = tmp_day | |
else: | |
tmp_date['day'] = tmp_day * - 1 | |
tmp_date['month'] += 1 | |
if int(tmp_date['month']) > 12: | |
tmp_date['month'] = 1 | |
tmp_date['year'] += 1 | |
if tmp_date['day'] > months[tmp_date['month']]: | |
if tmp_date['month'] > 12: | |
return add_date(tmp_date) | |
else: | |
date_return['day'] = tmp_date['day'] | |
date_return['month'] = tmp_date['month'] | |
date_return['year'] = tmp_date['year'] | |
return date_return | |
''' | |
descrip: recursive function to sub dates | |
params: dictionary with day, month, year | |
return: dictionary with day, month, year | |
''' | |
def sub_date(tmp_date): | |
date_return = {} | |
if tmp_date['day'] <= 0: | |
tmp_date['month'] -= 1 | |
if int(tmp_date['month']) <= 0: | |
tmp_date['month'] = 12 - tmp_date['month'] | |
tmp_date['year'] -= 1 | |
tmp_date['day'] = months[tmp_date['month']] - (tmp_date['day'] * - 1) | |
if tmp_date['day'] < 0: | |
return sub_date(tmp_date) | |
else: | |
date_return['day'] = tmp_date['day'] | |
date_return['month'] = tmp_date['month'] | |
date_return['year'] = tmp_date['year'] | |
return date_return | |
def change_date(date, op, value): | |
if op not in ('+', '-'): # check operator is valid | |
raise ValueError('This "{}" is not operator valid.'.format(op)) | |
tmp_date = date.split(' ') | |
day, month, year = tmp_date[0].split('/') | |
hour, minute = tmp_date[1].split(':') | |
return_date = { | |
'day': int(day), | |
'month': int(month), | |
'year': int(year), | |
'hour': int(hour), | |
'minute': int(minute) | |
} | |
if op == '+': | |
return_date['hour'] += int(value / 60) | |
return_date['minute'] += value % 60 | |
if return_date['minute'] >= 60: | |
return_date['hour'] += int(return_date['minute'] / 60) | |
return_date['minute'] = return_date['minute'] % 60 | |
if return_date['hour'] >= 24: | |
return_date['day'] += int(return_date['hour'] / 24) | |
return_date['hour'] = return_date['hour'] % 24 | |
if return_date['day'] > months[return_date['month']]: | |
tmp_date = add_date({ | |
'day': return_date['day'], | |
'month': return_date['month'], | |
'year': return_date['year'] | |
}) | |
return_date['day'] = tmp_date['day'] | |
return_date['month'] = tmp_date['month'] | |
return_date['year'] = tmp_date['year'] | |
else: | |
return_date['hour'] -= int(value / 60) | |
return_date['minute'] -= (value % 60) | |
if return_date['minute'] < 0: | |
return_date['hour'] -= int(return_date['minute'] / 60) | |
return_date['minute'] = int(return_date['minute'] % 60) | |
if return_date['hour'] < 0: | |
return_date['day'] -= (int(return_date['hour'] / 24) * - 1) | |
return_date['hour'] = int(return_date['hour'] % 24) | |
if int(return_date['day']) <= 0: | |
tmp_date = sub_date({ | |
'day': return_date['day'], | |
'month': return_date['month'], | |
'year': return_date['year'] | |
}) | |
return_date['day'] = tmp_date['day'] | |
return_date['month'] = tmp_date['month'] | |
return_date['year'] = tmp_date['year'] | |
return_date = str(return_date['day']) + '/' + str(return_date['month']) + '/' + str(return_date['year']) + \ | |
' ' + str(return_date['hour']) + ':' + str(return_date['minute']) | |
return return_date |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment