Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- class Date:
- def __init__(self,day,month,year):
- dni = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
- if(day>dni[month-1] or day<1 or type(day)==float):
- raise ValueError('Nie ma takiego dnia w tym miesiacu ! ')
- if(year<0 or type(year)==float):
- raise ValueError("Podales zly rok ! ")
- if(month<0 or month>12 or type(month)==float):
- raise ValueError("Podales zly miesiac")
- self.dzien= day
- self.miesiac= month
- self.rok = year
- def isleap(self):
- if self.rok%4==0:
- return True
- else:
- return False
- def days(self):
- wynik=(self.rok-1)*365
- przestepne=math.floor((self.rok-1)/4)
- wynik+=przestepne
- dni = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
- for i in range(self.miesiac-1):
- wynik+=dni[i-1]
- wynik+=self.dzien
- if(self.isleap()==True):
- wynik+=1
- return wynik-1
- def __str__(self):
- miesiace=["Styczen", "Luty", "Marzec", "Kwiecien",
- "Maj", "Czerwiec", "Lipiec", "Sierpien","Wrzesien", "Pazdziernik", "Listopad", "Grudzien"]
- return '{} {} {}'.format(self.dzien, miesiace[self.miesiac-1], self.rok)
- def __eq__(self, otherdate):
- return (self.dzien==otherdate.dzien and self.miesiac==otherdate.miesiac and self.rok==otherdate.rok)
- def __lt__(self, other):
- if other.rok>self.rok:
- return False
- elif other.rok==self.rok and other.miesiac>self.miesiac:
- return False
- elif other.rok==self.rok and other.miesiac==self.miesiac and other.dzien>self.dzien:
- return False
- else:
- return True
- def __sub__(self,other_date):
- a=self.days()
- b=other_date.days()
- if self>other_date:
- return b-a
- else:
- return a-b
- def __add__(self, days):
- m=self.miesiac
- r=self.rok
- d=self.dzien
- dni = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
- if days>0:
- for i in range (days):
- if(d==dni[m-1]) :
- d=0
- m=m+1
- if (m>12):
- r+=1
- m=1
- d+=1
- else:
- for i in range(math.abs(days)-1):
- if (m == 1 and d == 0):
- m = 12
- r -= 1
- d = dni[11]+1
- if (d == 0):
- m-=1
- d = dni[m]
- m = m + 1
- d -= 1
- self.miesiac=m
- self.rok=r
- self.dzien=d
- return self
- data1=Date(25,2,1997)
- d=data1.days()
- data2=Date(24,2,1997)
- print(data1<data2)
- print(data1==data2)
- print(d)
- print(data1+10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement