Advertisement
gabrieleb

MyDate

Dec 12th, 2021
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 KB | None | 0 0
  1. ddleap = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  2. ddnleap = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  3.  
  4.  
  5. class MyDate:
  6.  
  7.     def __init__(self, dd, mm, yy):
  8.         self.day = dd
  9.         self.month = mm
  10.         self.year = yy
  11.  
  12.         def setleap(year):
  13.             '''calcola la bisestilità dell'anno'''
  14.             if not (year % 400) or (not (year % 4) and year % 100):
  15.                 self.leap = True
  16.             else:
  17.                 self.leap = False
  18.  
  19.         setleap(yy)
  20.         if self.leap:
  21.             self.mmdays = ddleap
  22.         else:
  23.             self.mmdays = ddnleap
  24.  
  25.         def setesimo(leap):
  26.             '''calcola il numero ordinale del giorno nell'anno inserito tenendo conto della bisestilitò'''
  27.             ddth = 0
  28.             for index, ai in enumerate(self.mmdays):
  29.                 if index >= self.month - 1:
  30.                     ddth += self.day
  31.                     break
  32.                 else:
  33.                     ddth += ai
  34.  
  35.             self.ordinale = ddth
  36.  
  37.         setesimo(self.leap)
  38.  
  39.     def sumdate(self, num):
  40.         '''calcola la nuova data sommando il num'''
  41.         month = self.month - 1
  42.         year = self.year
  43.         mmdays = self.mmdays
  44.         def myupdate(month, year):
  45.             month += 1
  46.             if not month % 12:
  47.                 month = 0
  48.                 year += 1
  49.  
  50.         distance = mmdays[month] - self.day
  51.         if num <= distance:
  52.             return str(str(self.day + num) + "/" + str(month + 1) + "/" + str(year))
  53.  
  54.         num -= distance
  55.         month += 1
  56.         if not month % 12:
  57.             month = 0
  58.             year += 1
  59.  
  60.         while num > mmdays[month]:
  61.             if not (year % 400) or (not (year % 4) and year % 100):
  62.                 leap = True
  63.             else:
  64.                 leap = False
  65.             if leap:
  66.                 mmdays = ddleap
  67.             else:
  68.                 mmdays = ddnleap
  69.             num -= mmdays[month]
  70.             month += 1
  71.             if not month % 12:
  72.                 month = 0
  73.                 year += 1
  74.  
  75.         return str(str(num) + "/" + str(month + 1) + "/" + str(year))
  76.  
  77.  
  78. a = MyDate(5, 8, 2021)
  79.  
  80. if __name__ == '__main__':
  81.     print(a.day, a.month, a.year, "\n" + a.sumdate(5000))
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement