Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Date():
- def __init__(self, dmy='01.01.1900', valid=1):
- day, month, year = map(int, dmy.split('.'))
- self.year = year
- self.month = month
- self.day = day
- if valid == 1:
- self.check_valid_date()
- def check_valid_date(self):
- y = range(1583, 3001)
- self.year = self.year if self.year in y else 1900
- m = range(1, 13)
- self.month = self.month if self.month in m else 1
- d = range(1, 32)
- self.day = self.day if self.day in d else 1
- if self.month == 2 and self.day >= 29:
- if leap(self.year):
- self.day = 29
- else:
- self.day = 28
- def __add__(self, other): # Добавил сложение двух дат
- '''
- Adding (1) days (other) to the date (self)
- (2) days, months, years (other) to the date (self)
- examples - (1) date {'year': 2020, 'month': 12, 'day': 31}
- new_date = date + 365
- new_date {'year': 2021, 'month': 12, 'day': 31}
- (2) date1 {'year': 2020, 'month': 12, 'day': 31}
- date2 {'year': 1, 'month': 1, 'day': 1}
- new_date = date1 + date2
- new_date {'year': 2022, 'month': 2, 'day': 1}
- args - (1) self (Date), other (int)
- (2) self (Date), other (Date(not valid))
- return - new_date (Date)
- '''
- if type(other) == Date:
- year = self.year + other.year
- month = self.month + other.month
- days = self.day + other.day
- while month > 12:
- year += 1
- month -= 12
- else:
- year = self.year
- month = self.month
- days = self.day + other
- for i in range(1, month): # Counting days from 01.01.self.year to self(day.month.year)
- days += day_m(year, i)
- month = 1
- while days > day_y(year):
- days -= day_y(year)
- year += 1
- while days > day_m(year, month):
- days -= day_m(year, month)
- month += 1
- return Date(str(days) + '.' + str(month) + '.' + str(year))
- def __sub__(self, other): # Добавил вычитание
- '''
- Substracting: (1) date and days
- (2) two dates
- examples - (1) date {'year': 2020, 'month': 12, 'day': 31}
- new_date = date - 365
- new_date {'year': 2020, 'month': 1, 'day': 1}
- (2) date1 {'year': 2020, 'month': 12, 'day': 31}
- date2 {'year': 2020, 'month': 1, 'day': 1}
- days = date1 - date2
- days (365)
- args - (1) self (Date), other (int)
- (2) self (Date), other (Date)
- (1) return - new_date (Date)
- (2) return - days (int)
- '''
- if type(other) == int:
- return self + -other
- else:
- latest = latest_date(self, other)
- if latest == self:
- earliest = other
- else:
- earliest = self
- return latest.count_days(earliest.year) - earliest.count_days(earliest.year)
- def __gt__(self, other): # Добавил сравнение знаком '>'
- if self.year > other.year:
- res = True
- elif self.year < other.year:
- res = False
- else:
- if self.month > other.month:
- res = True
- elif self.month < other.month:
- res = False
- else:
- if self.day > other.day:
- res = True
- else:
- res = False
- return res
- def __ge__(self, other): # Добавил сравнение знаком '>='
- if self.__dict__ == other.__dict__ or self.year > other.year:
- res = True
- elif self.year < other.year:
- res = False
- else:
- if self.month > other.month:
- res = True
- elif self.month < other.month:
- res = False
- else:
- if self.day > other.day:
- res = True
- else:
- res = False
- return res
- def __lt__(self, other): # Добавил сравнение знаком '<'
- if self.year < other.year:
- res = True
- elif self.year > other.year:
- res = False
- else:
- if self.month < other.month:
- res = True
- elif self.month > other.month:
- res = False
- else:
- if self.day < other.day:
- res = True
- else:
- res = False
- return res
- def __le__(self, other): # Добавил сравнение знаком '<='
- if self.__dict__ == other.__dict or self.year < other.year:
- res = True
- elif self.year > other.year:
- res = False
- else:
- if self.month < other.month:
- res = True
- elif self.month > other.month:
- res = False
- else:
- if self.day < other.day:
- res = True
- else:
- res = False
- return res
- def __eq__(self, other): # Добавил сравнение знаком '=='
- if self.__dict__ == other.__dict__:
- return True
- else:
- return False
- def format_date(self):
- '''
- Возвращает дату в виде строки в формате dd.mm.yyyy
- args - date (date) (class Date)
- return - (str)
- '''
- day = str(self.day)
- month = str(self.month)
- year = str(self.year)
- if len(day) == 1: day = '0' + day
- if len(month) == 1: month = '0' + month
- return day + '.' + month + '.' + year
- def print_date(self):
- print(self.format_date())
- def count_days(self, year=0):
- '''
- Counting days from date to the year.
- args - date (Date), year (int)
- return - days (int)
- '''
- days = 0
- for i in range(year, self.year):
- days += day_y(i)
- for i in range(1, self.month):
- days += day_m(self.year, i)
- days += self.day
- return days
- def find_week_day(self):
- '''
- код дня недели = (день + код месяца + код года) % 7
- Код месяца:
- 1 — январь, октябрь;
- 2 — май;
- 3 — август;
- 4 — февраль, март, ноябрь;
- 5 — июнь;
- 6 — декабрь, сентябрь;
- 0 — апрель, июль.
- код года = (6 + последние две цифры года + последние две цифры года / 4) % 7
- Код дня недели:
- 0 - Воскресенье
- 1 - Суббота
- 2 - Понедельник
- 3 - Вторник
- 4 - Среда
- 5 - Четверг
- 6 - Пятница
- '''
- mnth_codes = {1: 1,
- 10: 1,
- 5: 2,
- 8: 3,
- 2: 4,
- 3: 4,
- 11: 4,
- 6: 5,
- 12: 6,
- 9: 6,
- 4: 0,
- 7: 0}
- year_str = str(self.year)
- year_num = int(year_str[len(year_str)-2:])
- year_code = (6 + year_num + year_num // 4) % 7
- week_days = {0: (6, 'Суббота'),
- 1: (7, 'Воскресенье'),
- 2: (1, 'Понедельник'),
- 3: (2, 'Вторник'),
- 4: (3, 'Среда'),
- 5: (4, 'Четверг'),
- 6: (5, 'Пятница')}
- week_day = (self.day + mnth_codes[self.month] + year_code) % 7
- return week_days[week_day]
- def leap(year):
- '''
- Проверка на високосный гол
- args - year (int)
- return - True/False
- '''
- leap = ((year % 400 == 0) or (year % 100 != 0)) and (year % 4 == 0)
- return leap
- def day_m(year, month):
- '''
- Кол-во дней в месяце
- args - year, month (int)
- return - count_days (int)
- '''
- if month in (4, 6, 9, 11): days = 30
- elif month in (1, 3, 5, 7, 8, 10, 12): days = 31
- else:
- days = 28
- if leap(year):
- days += 1
- return days
- def day_y(year):
- '''
- args - year (int)
- return - days (int)
- '''
- if leap(year): return 366
- else: return 365
- def tomorrow(date):
- '''
- Дата на следующий
- args - date (Date)
- return tmrw_date (Date)
- '''
- tmrw_date = date
- if date.day != day_m(date.year, date.month):
- tmrw_date.day += 1
- elif date.month != 12:
- tmrw_date.month += 1
- tmrw_date.day = 1
- else:
- tmrw_date.year += 1
- tmrw_date.month = 1
- tmrw_date.day = 1
- return tmrw_date
- def yesterday(date):
- '''
- Находит дату вчерашнего дня
- args - date (Date)
- return - ystr_date (Date)
- '''
- ystr_date = date
- if date.day != 1:
- ystr_date.day -= 1
- elif date.month != 1:
- ystr_date.month = date.month - 1
- ystr_date.day = day_m(ystr_date.year, ystr_date.month)
- else:
- ystr_date.year = date.year - 1
- ystr_date.month = 12
- ystr_date.day = day_m(ystr_date.year, ystr_date.month)
- return ystr_date
- # def future(days, date):
- # '''
- # Прибавляет к начальной дате кол-во дней (days),
- # возвращает новую дату
- # args - days (int), date (Date)
- # return - date (Date)
- # '''
- # for i in range(1, date.month):
- # days += day_m(date.year, i)
- # days += date.day
- # while days > day_y(date.year):
- # days -= day_y(date.year)
- # date.year += 1
- # date.month = 1
- # while days > day_m(date.year, date.month):
- # days -= day_m(date.year, date.month)
- # date.month += 1
- # date.day = days
- # return date
- # def past(days, date):
- # '''
- # Находить дату которая была days дней назад
- # args - days (int), date (Date)
- # return - date (Date)
- # '''
- # while days >= day_y(date.year):
- # days -= day_y(date.year)
- # date.year -= 1
- # while days >= day_m(date.year, date.month):
- # days -= day_m(date.year, date.month)
- # date.month -= 1
- # while days > date.day:
- # date.month -= 1
- # days -= date.day
- # date.day = day_m(date.year, date.month)
- # date.day -= days
- # return date
- def latest_date(date1, date2):
- '''
- Находит позднюю дату из двух
- args - date1 (Date), date2 (Date)
- return - latest_date (Date) or 0 (int)
- '''
- # if date1.year > date2.year:
- # latest_date = date1
- # elif date1.year < date2.year:
- # latest_date = date2
- # else:
- # if date1.month > date2.month:
- # latest_date = date1
- # elif date1.month < date2.month:
- # latest_date = date2
- # else:
- # if date1.day > date2.day:
- # latest_date = date1
- # elif date1.day > date2.day:
- # latest_date = date2
- # else:
- # latest_date = date1
- if date1 > date2:
- latest_date = date1
- else:
- latest_date = date2
- return latest_date
- # def days_d1_d2(date1, date2):
- # '''
- # Находит промежуток между двумя
- # args - date1 (Date), date2(Date)
- # return - days (int)
- # '''
- # days = 0
- # latest = latest_date(date1, date2)
- # if latest == 0:
- # return 0
- # else:
- # if date1 == latest:
- # early = Date(date2.format_date())
- # else:
- # early = Date(date1.format_date())
- # days += latest.day
- # while latest.month > 1:
- # latest.month -= 1
- # days += day_m(latest.year, latest.month)
- # while latest.year > early.year:
- # latest.year -= 1
- # days += day_y(latest.year)
- # days -= early.day
- # while early.month > 1:
- # early.month -= 1
- # days -= day_m(early.year, early.month)
- # return days
- def input_date():
- y, m, d = input('Format: Year Month Day. ').split()
- s = d + '.' + m + '.' + y
- return s
- def main():
- date = Date(input_date())
- date.print_date()
- _, day = date.find_week_day()
- print(day)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement