Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. import math
  2. class Date:
  3. def __init__(self,day,month,year):
  4. dni = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  5. if(day>dni[month-1] or day<1 or type(day)==float):
  6. raise ValueError('Nie ma takiego dnia w tym miesiacu ! ')
  7. if(year<0 or type(year)==float):
  8. raise ValueError("Podales zly rok ! ")
  9. if(month<0 or month>12 or type(month)==float):
  10. raise ValueError("Podales zly miesiac")
  11. self.dzien= day
  12. self.miesiac= month
  13. self.rok = year
  14. def isleap(self):
  15. if self.rok%4==0:
  16. return True
  17. else:
  18. return False
  19. def days(self):
  20. wynik=(self.rok-1)*365
  21. przestepne=math.floor((self.rok-1)/4)
  22. wynik+=przestepne
  23. dni = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  24. for i in range(self.miesiac-1):
  25. wynik+=dni[i-1]
  26. wynik+=self.dzien
  27. if(self.isleap()==True):
  28. wynik+=1
  29. return wynik-1
  30. def __str__(self):
  31. miesiace=["Styczen", "Luty", "Marzec", "Kwiecien",
  32. "Maj", "Czerwiec", "Lipiec", "Sierpien","Wrzesien", "Pazdziernik", "Listopad", "Grudzien"]
  33. return '{} {} {}'.format(self.dzien, miesiace[self.miesiac-1], self.rok)
  34. def __eq__(self, otherdate):
  35. return (self.dzien==otherdate.dzien and self.miesiac==otherdate.miesiac and self.rok==otherdate.rok)
  36. def __lt__(self, other):
  37. if other.rok>self.rok:
  38. return False
  39. elif other.rok==self.rok and other.miesiac>self.miesiac:
  40. return False
  41. elif other.rok==self.rok and other.miesiac==self.miesiac and other.dzien>self.dzien:
  42. return False
  43. else:
  44. return True
  45. def __sub__(self,other_date):
  46. a=self.days()
  47. b=other_date.days()
  48. if self>other_date:
  49. return b-a
  50. else:
  51. return a-b
  52. def __add__(self, days):
  53. m=self.miesiac
  54. r=self.rok
  55. d=self.dzien
  56. dni = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  57. if days>0:
  58. for i in range (days):
  59. if(d==dni[m-1]) :
  60. d=0
  61. m=m+1
  62. if (m>12):
  63. r+=1
  64. m=1
  65. d+=1
  66. else:
  67. for i in range(math.abs(days)-1):
  68. if (m == 1 and d == 0):
  69. m = 12
  70. r -= 1
  71. d = dni[11]+1
  72. if (d == 0):
  73. m-=1
  74. d = dni[m]
  75. m = m + 1
  76. d -= 1
  77. self.miesiac=m
  78. self.rok=r
  79. self.dzien=d
  80. return self
  81.  
  82. data1=Date(25,2,1997)
  83. d=data1.days()
  84. data2=Date(24,2,1997)
  85. print(data1<data2)
  86. print(data1==data2)
  87. print(d)
  88. print(data1+10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement