Advertisement
jimkilled

date V2 school

Nov 23rd, 2020
927
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.38 KB | None | 0 0
  1. class Date():
  2.     def __init__(self, dmy='01.01.1900', valid=1):
  3.         day, month, year = map(int, dmy.split('.'))
  4.         self.year = year
  5.         self.month = month
  6.         self.day = day
  7.         if valid == 1:
  8.             self.check_valid_date()
  9.  
  10.     def check_valid_date(self):
  11.         y = range(1583, 3001)
  12.         self.year = self.year if self.year in y else 1900
  13.         m = range(1, 13)
  14.         self.month = self.month if self.month in m else 1
  15.         d = range(1, 32)
  16.         self.day = self.day if self.day in d else 1
  17.  
  18.         if self.month == 2 and self.day >= 29:
  19.             if leap(self.year):
  20.                 self.day = 29
  21.             else:
  22.                 self.day = 28
  23.  
  24.     def __add__(self, other):                       # Добавил сложение двух дат
  25.         '''
  26.        Adding (1) days (other) to the date (self)
  27.               (2) days, months, years (other) to the date (self)
  28.  
  29.        examples -  (1) date {'year': 2020, 'month': 12, 'day': 31}
  30.                        new_date = date + 365
  31.                        new_date {'year': 2021, 'month': 12, 'day': 31}
  32.  
  33.                    (2) date1 {'year': 2020, 'month': 12, 'day': 31}
  34.                        date2 {'year': 1,    'month': 1,   'day': 1}
  35.                        new_date = date1 + date2
  36.                        new_date {'year': 2022, 'month': 2, 'day': 1}
  37.  
  38.        args - (1) self (Date), other (int)
  39.               (2) self (Date), other (Date(not valid))
  40.        return - new_date (Date)
  41.        '''
  42.         if type(other) == Date:
  43.             year = self.year + other.year
  44.             month = self.month + other.month
  45.             days = self.day + other.day
  46.             while month > 12:
  47.                 year += 1
  48.                 month -= 12
  49.         else:
  50.             year = self.year
  51.             month = self.month
  52.             days = self.day + other
  53.  
  54.         for i in range(1, month):      # Counting days from 01.01.self.year to self(day.month.year)
  55.             days += day_m(year, i)
  56.         month = 1
  57.  
  58.         while days > day_y(year):
  59.             days -= day_y(year)
  60.             year += 1
  61.  
  62.         while days > day_m(year, month):
  63.             days -= day_m(year, month)
  64.             month += 1
  65.  
  66.         return Date(str(days) + '.' + str(month) + '.' + str(year))
  67.  
  68.     def __sub__(self, other):                       # Добавил вычитание
  69.         '''
  70.        Substracting: (1) date and days
  71.                      (2) two dates
  72.  
  73.        examples -  (1) date {'year': 2020, 'month': 12, 'day': 31}
  74.                        new_date = date - 365
  75.                        new_date {'year': 2020, 'month': 1, 'day': 1}
  76.  
  77.                    (2) date1 {'year': 2020, 'month': 12, 'day': 31}
  78.                        date2 {'year': 2020, 'month': 1,  'day': 1}
  79.                        days = date1 - date2
  80.                        days (365)
  81.        args -  (1) self (Date), other (int)
  82.                (2) self (Date), other (Date)
  83.        (1) return - new_date (Date)
  84.        (2) return - days (int)
  85.  
  86.        '''
  87.         if type(other) == int:
  88.             return self + -other
  89.         else:
  90.             latest = latest_date(self, other)
  91.  
  92.             if latest == self:
  93.                 earliest = other
  94.             else:
  95.                 earliest = self
  96.  
  97.             return latest.count_days(earliest.year) - earliest.count_days(earliest.year)
  98.  
  99.     def __gt__(self, other):                    # Добавил сравнение знаком '>'
  100.         if self.year > other.year:
  101.             res = True
  102.         elif self.year < other.year:
  103.             res = False
  104.         else:
  105.             if self.month > other.month:
  106.                 res = True
  107.             elif self.month < other.month:
  108.                 res = False
  109.             else:
  110.                 if self.day > other.day:
  111.                     res = True
  112.                 else:
  113.                     res = False
  114.         return res
  115.  
  116.     def __ge__(self, other):                    # Добавил сравнение знаком '>='
  117.         if self.__dict__ == other.__dict__ or self.year > other.year:
  118.             res = True
  119.         elif self.year < other.year:
  120.             res = False
  121.         else:
  122.             if self.month > other.month:
  123.                 res = True
  124.             elif self.month < other.month:
  125.                 res = False
  126.             else:
  127.                 if self.day > other.day:
  128.                     res = True
  129.                 else:
  130.                     res = False
  131.         return res
  132.  
  133.     def __lt__(self, other):                    # Добавил сравнение знаком '<'
  134.         if self.year < other.year:
  135.             res = True
  136.         elif self.year > other.year:
  137.             res = False
  138.         else:
  139.             if self.month < other.month:
  140.                 res = True
  141.             elif self.month > other.month:
  142.                 res = False
  143.             else:
  144.                 if self.day < other.day:
  145.                     res = True
  146.                 else:
  147.                     res = False
  148.         return res
  149.  
  150.     def __le__(self, other):                    # Добавил сравнение знаком '<='
  151.         if self.__dict__ == other.__dict or self.year < other.year:
  152.             res = True
  153.         elif self.year > other.year:
  154.             res = False
  155.         else:
  156.             if self.month < other.month:
  157.                 res = True
  158.             elif self.month > other.month:
  159.                 res = False
  160.             else:
  161.                 if self.day < other.day:
  162.                     res = True
  163.                 else:
  164.                     res = False
  165.         return res
  166.  
  167.     def __eq__(self, other):                    # Добавил сравнение знаком '=='
  168.         if self.__dict__ == other.__dict__:
  169.             return True
  170.         else:
  171.             return False
  172.  
  173.     def format_date(self):
  174.         '''
  175.        Возвращает дату в виде строки в формате dd.mm.yyyy
  176.        args - date (date)  (class Date)
  177.        return - (str)
  178.        '''
  179.         day = str(self.day)
  180.         month = str(self.month)
  181.         year = str(self.year)
  182.         if len(day) == 1: day = '0' + day
  183.         if len(month) == 1: month = '0' + month
  184.         return day + '.' + month + '.' + year
  185.  
  186.     def print_date(self):
  187.         print(self.format_date())
  188.  
  189.     def count_days(self, year=0):
  190.         '''
  191.        Counting days from date to the year.
  192.        args - date (Date), year (int)
  193.        return - days (int)
  194.        '''
  195.         days = 0
  196.         for i in range(year, self.year):
  197.             days += day_y(i)
  198.         for i in range(1, self.month):
  199.             days += day_m(self.year, i)
  200.         days += self.day
  201.         return days
  202.  
  203.     def find_week_day(self):
  204.         '''
  205.        код дня недели = (день + код месяца + код года) % 7
  206.  
  207.        Код месяца:
  208.                1 — январь, октябрь;
  209.                2 — май;
  210.                3 — август;
  211.                4 — февраль, март, ноябрь;
  212.                5 — июнь;
  213.                6 — декабрь, сентябрь;
  214.                0 — апрель, июль.
  215.  
  216.        код года = (6 + последние две цифры года + последние две цифры года / 4) % 7
  217.  
  218.        Код дня недели:
  219.                0 - Воскресенье
  220.                1 - Суббота
  221.                2 - Понедельник
  222.                3 - Вторник
  223.                4 - Среда
  224.                5 - Четверг
  225.                6 - Пятница
  226.        '''
  227.         mnth_codes = {1: 1,
  228.                       10: 1,
  229.                       5: 2,
  230.                       8: 3,
  231.                       2: 4,
  232.                       3: 4,
  233.                       11: 4,
  234.                       6: 5,
  235.                       12: 6,
  236.                       9: 6,
  237.                       4: 0,
  238.                       7: 0}
  239.  
  240.         year_str = str(self.year)
  241.         year_num = int(year_str[len(year_str)-2:])
  242.         year_code = (6 + year_num + year_num // 4) % 7
  243.  
  244.         week_days = {0: (6, 'Суббота'),
  245.                      1: (7, 'Воскресенье'),
  246.                      2: (1, 'Понедельник'),
  247.                      3: (2, 'Вторник'),
  248.                      4: (3, 'Среда'),
  249.                      5: (4, 'Четверг'),
  250.                      6: (5, 'Пятница')}
  251.  
  252.         week_day = (self.day + mnth_codes[self.month] + year_code) % 7
  253.         return week_days[week_day]
  254.  
  255.  
  256. def leap(year):
  257.     '''
  258.    Проверка на високосный гол
  259.    args - year (int)
  260.    return - True/False
  261.    '''
  262.     leap = ((year % 400 == 0) or (year % 100 != 0)) and (year % 4 == 0)
  263.     return leap
  264.  
  265.  
  266. def day_m(year, month):
  267.     '''
  268.    Кол-во дней в месяце
  269.    args - year, month (int)
  270.    return - count_days (int)
  271.    '''
  272.     if month in (4, 6, 9, 11): days = 30
  273.     elif month in (1, 3, 5, 7, 8, 10, 12): days = 31
  274.     else:
  275.         days = 28
  276.         if leap(year):
  277.             days += 1
  278.     return days
  279.  
  280.  
  281. def day_y(year):
  282.     '''
  283.    args - year (int)
  284.    return - days (int)
  285.    '''
  286.     if leap(year): return 366
  287.     else: return 365
  288.  
  289.  
  290. def tomorrow(date):
  291.     '''
  292.    Дата на следующий
  293.    args - date (Date)
  294.    return tmrw_date (Date)
  295.    '''
  296.     tmrw_date = date
  297.     if date.day != day_m(date.year, date.month):
  298.         tmrw_date.day += 1
  299.     elif date.month != 12:
  300.         tmrw_date.month += 1
  301.         tmrw_date.day = 1
  302.     else:
  303.         tmrw_date.year += 1
  304.         tmrw_date.month = 1
  305.         tmrw_date.day = 1
  306.     return tmrw_date
  307.  
  308.  
  309. def yesterday(date):
  310.     '''
  311.    Находит дату вчерашнего дня
  312.    args - date (Date)
  313.    return - ystr_date (Date)
  314.    '''
  315.     ystr_date = date
  316.     if date.day != 1:
  317.         ystr_date.day -= 1
  318.     elif date.month != 1:
  319.         ystr_date.month = date.month - 1
  320.         ystr_date.day = day_m(ystr_date.year, ystr_date.month)
  321.     else:
  322.         ystr_date.year = date.year - 1
  323.         ystr_date.month = 12
  324.         ystr_date.day = day_m(ystr_date.year, ystr_date.month)
  325.     return ystr_date
  326.  
  327.  
  328. # def future(days, date):
  329. #     '''
  330. #     Прибавляет к начальной дате кол-во дней (days),
  331. #     возвращает новую дату
  332. #     args - days (int), date (Date)
  333. #     return - date (Date)
  334. #     '''
  335. #     for i in range(1, date.month):
  336. #         days += day_m(date.year, i)
  337. #     days += date.day
  338. #     while days > day_y(date.year):
  339. #         days -= day_y(date.year)
  340. #         date.year += 1
  341. #     date.month = 1
  342. #     while days > day_m(date.year, date.month):
  343. #         days -= day_m(date.year, date.month)
  344. #         date.month += 1
  345. #     date.day = days
  346. #     return date
  347.  
  348.  
  349. # def past(days, date):
  350. #     '''
  351. #     Находить дату которая была days дней назад
  352. #     args - days (int), date (Date)
  353. #     return - date (Date)
  354. #     '''
  355. #     while days >= day_y(date.year):
  356. #         days -= day_y(date.year)
  357. #         date.year -= 1
  358. #     while days >= day_m(date.year, date.month):
  359. #         days -= day_m(date.year, date.month)
  360. #         date.month -= 1
  361. #     while days > date.day:
  362. #         date.month -= 1
  363. #         days -= date.day
  364. #         date.day = day_m(date.year, date.month)
  365. #     date.day -= days
  366. #     return date
  367.  
  368.  
  369. def latest_date(date1, date2):
  370.     '''
  371.    Находит позднюю дату из двух
  372.    args - date1 (Date), date2 (Date)
  373.    return - latest_date (Date) or 0 (int)
  374.    '''
  375.     # if date1.year > date2.year:
  376.     #     latest_date = date1
  377.     # elif date1.year < date2.year:
  378.     #     latest_date = date2
  379.     # else:
  380.     #     if date1.month > date2.month:
  381.     #         latest_date = date1
  382.     #     elif date1.month < date2.month:
  383.     #         latest_date = date2
  384.     #     else:
  385.     #         if date1.day > date2.day:
  386.     #             latest_date = date1
  387.     #         elif date1.day > date2.day:
  388.     #             latest_date = date2
  389.     #         else:
  390.     #             latest_date = date1
  391.     if date1 > date2:
  392.         latest_date = date1
  393.     else:
  394.         latest_date = date2
  395.     return latest_date
  396.  
  397.  
  398. # def days_d1_d2(date1, date2):
  399. #     '''
  400. #     Находит промежуток между двумя
  401. #     args - date1 (Date), date2(Date)
  402. #     return - days (int)
  403. #     '''
  404. #     days = 0
  405. #     latest = latest_date(date1, date2)
  406. #     if latest == 0:
  407. #         return 0
  408. #     else:
  409. #         if date1 == latest:
  410. #             early = Date(date2.format_date())
  411. #         else:
  412. #             early = Date(date1.format_date())
  413.  
  414. #         days += latest.day
  415. #         while latest.month > 1:
  416. #             latest.month -= 1
  417. #             days += day_m(latest.year, latest.month)
  418. #         while latest.year > early.year:
  419. #             latest.year -= 1
  420. #             days += day_y(latest.year)
  421.  
  422. #         days -= early.day
  423. #         while early.month > 1:
  424. #             early.month -= 1
  425. #             days -= day_m(early.year, early.month)
  426. #     return days
  427.  
  428.  
  429. def input_date():
  430.     y, m, d = input('Format: Year Month Day. ').split()
  431.     s = d + '.' + m + '.' + y
  432.     return s
  433.  
  434.  
  435. def main():
  436.     date = Date(input_date())
  437.     date.print_date()
  438.     _, day = date.find_week_day()
  439.     print(day)
  440.  
  441. main()
  442.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement