Date after adding given number of days to the given date | Return if year is leap year or not . ; Given a date , returns number of days elapsed from the beginning of the current year ( 1 stjan ) . ; Given a year and days elapsed in it , finds date by storing results in d and m . ; Add x days to the given date . ; y2 is going to store result year and offset2 is going to store offset days in result year . ; x may store thousands of days . We find correct year and offset in the year . ; Find values of day and month from offset of result year . ; Driven Program
def isLeap ( y ) : NEW_LINE INDENT if ( y % 100 != 0 and y % 4 == 0 or y % 400 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def offsetDays ( d , m , y ) : NEW_LINE INDENT offset = d NEW_LINE switcher = { 10 : 30 , 9 : 31 , 8 : 30 , 7 : 31 , 6 : 31 , 5 : 30 , 4 : 31 , 3 : 30 , 2 : 31 , 1 : 28 , 0 : 31 } NEW_LINE if ( isLeap ( y ) and m > 1 ) : NEW_LINE INDENT offset += 1 NEW_LINE DEDENT offset += switcher . get ( m ) NEW_LINE return offset NEW_LINE DEDENT def revoffsetDays ( offset , y , d , m ) : NEW_LINE INDENT month = [ 0 , 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ] NEW_LINE if ( isLeap ( y ) ) : NEW_LINE INDENT month [ 2 ] = 29 NEW_LINE DEDENT for i in range ( 1 , 13 ) : NEW_LINE INDENT if ( offset <= month [ i ] ) : NEW_LINE INDENT break NEW_LINE DEDENT offset = offset - month [ i ] NEW_LINE DEDENT d [ 0 ] = offset NEW_LINE m [ 0 ] = i + 1 NEW_LINE DEDENT def addDays ( d1 , m1 , y1 , x ) : NEW_LINE INDENT offset1 = offsetDays ( d1 , m1 , y1 ) NEW_LINE if isLeap ( y1 ) : NEW_LINE INDENT remDays = 366 - offset1 NEW_LINE DEDENT else : NEW_LINE INDENT remDays = 365 - offset1 NEW_LINE DEDENT if ( x <= remDays ) : NEW_LINE INDENT y2 = y1 NEW_LINE offset2 = offset1 + x NEW_LINE DEDENT else : NEW_LINE INDENT x -= remDays NEW_LINE y2 = y1 + 1 NEW_LINE if isLeap ( y2 ) : NEW_LINE INDENT y2days = 366 NEW_LINE DEDENT else : NEW_LINE INDENT y2days = 365 NEW_LINE DEDENT while ( x >= y2days ) : NEW_LINE INDENT x -= y2days NEW_LINE y2 += 1 NEW_LINE if isLeap ( y2 ) : NEW_LINE INDENT y2days = 366 NEW_LINE DEDENT else : NEW_LINE INDENT y2days = 365 NEW_LINE DEDENT DEDENT offset2 = x NEW_LINE DEDENT m2 = [ 0 ] NEW_LINE d2 = [ 0 ] NEW_LINE revoffsetDays ( offset2 , y2 , d2 , m2 ) NEW_LINE print ( " d2 ▁ = ▁ " , * d2 , " , ▁ m2 ▁ = ▁ " , * m2 , " , ▁ y2 ▁ = ▁ " , y2 , sep = " " ) NEW_LINE DEDENT d = 14 NEW_LINE m = 3 NEW_LINE y = 2015 NEW_LINE x = 366 NEW_LINE addDays ( d , m , y , x ) NEW_LINE