From fe7e9de394df842f95d67801527ae71b945cbb29 Mon Sep 17 00:00:00 2001 From: Smiti Oswal <92663204+smitioswal@users.noreply.github.com> Date: Tue, 3 Oct 2023 22:18:49 +0530 Subject: [PATCH] Updated datetime.py --- python/datetime.py | 183 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 161 insertions(+), 22 deletions(-) diff --git a/python/datetime.py b/python/datetime.py index 8d8dc1de..5dec7e52 100644 --- a/python/datetime.py +++ b/python/datetime.py @@ -1,36 +1,175 @@ -from time import localtime +from time import time, localtime + +class timedelta: + def __init__(self, days=0, seconds=0): + self.days = days + self.seconds = seconds + + def __repr__(self) -> str: + return f"datetime.timedelta(days={self.days}, seconds={self.seconds})" + + def __eq__(self, other: 'timedelta') -> bool: + return self.days == other.days and self.seconds == other.seconds + + def __lt__(self, other: 'timedelta') -> bool: + return (self.days, self.seconds) < (other.days, other.seconds) + + def __le__(self, other: 'timedelta') -> bool: + return (self.days, self.seconds) <= (other.days, other.seconds) + + def __gt__(self, other: 'timedelta') -> bool: + return (self.days, self.seconds) > (other.days, other.seconds) + + def __ge__(self, other: 'timedelta') -> bool: + return (self.days, self.seconds) >= (other.days, other.seconds) + + +class timezone: + def __init__(self, delta: timedelta): + self.delta = delta + + def __repr__(self) -> str: + return f"datetime.timezone({self.delta})" + class date: - def __init__(self, year: int, month: int, day: int): + @staticmethod + def today() -> 'date': + t = localtime() + return date(t.tm_year, t.tm_mon, t.tm_mday) + + def __init__(self, year, month=None, day=None): self.year = year self.month = month self.day = day - @staticmethod - def today(): - t = localtime() - return date(t.tm_year, t.tm_mon, t.tm_mday) - - def __str__(self): - return f"{self.year}-{self.month:02}-{self.day:02}" - - def __repr__(self): + @property + def year(self) -> int: + return self._year + + @year.setter + def year(self, value: int): + self._year = value + + @property + def month(self) -> int: + return self._month + + @month.setter + def month(self, value: int): + self._month = value + + @property + def day(self) -> int: + return self._day + + @day.setter + def day(self, value: int): + self._day = value + + def __repr__(self) -> str: return f"datetime.date({self.year}, {self.month}, {self.day})" -class datetime(date): - def __init__(self, year: int, month: int, day: int, hour: int, minute: int, second: int): - super(datetime, self).__init__(year, month, day) - self.hour = hour - self.minute = minute - self.second = second + def __eq__(self, other: 'date') -> bool: + return (self.year, self.month, self.day) == (other.year, other.month, other.day) + def __lt__(self, other: 'date') -> bool: + return (self.year, self.month, self.day) < (other.year, other.month, other.day) + + def __le__(self, other: 'date') -> bool: + return (self.year, self.month, self.day) <= (other.year, other.month, other.day) + + def __gt__(self, other: 'date') -> bool: + return (self.year, self.month, self.day) > (other.year, other.month, other.day) + + def __ge__(self, other: 'date') -> bool: + return (self.year, self.month, self.day) >= (other.year, other.month, other.day) + + def __add__(self, other: timedelta) -> 'date': + # Add the timedelta to the current date and return a new date + pass + + def __sub__(self, other: timedelta) -> 'date': + # Subtract the timedelta from the current date and return a new date + pass + + +class datetime(date): @staticmethod - def now(): + def now() -> 'datetime': t = localtime() return datetime(t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec) - def __str__(self): - return f"{self.year}-{self.month:02}-{self.day:02} {self.hour:02}:{self.minute:02}:{self.second:02}" + def __init__(self, year, month=None, day=None, hour=None, minute=None, second=None, tzinfo=None): + super().__init__(year, month, day) + self.hour = hour + self.minute = minute + self.second = second + self.tzinfo = tzinfo - def __repr__(self): - return f"datetime.datetime({self.year}, {self.month}, {self.day}, {self.hour}, {self.minute}, {self.second})" \ No newline at end of file + @property + def hour(self) -> int: + return self._hour + + @hour.setter + def hour(self, value: int): + self._hour = value + + @property + def minute(self) -> int: + return self._minute + + @minute.setter + def minute(self, value: int): + self._minute = value + + @property + def second(self) -> int: + return self._second + + @second.setter + def second(self, value: int): + self._second = value + + @property + def tzinfo(self) -> timezone: + return self._tzinfo + + @tzinfo.setter + def tzinfo(self, value: timezone): + self._tzinfo = value + + def __repr__(self) -> str: + return f"datetime.datetime({self.year}, {self.month}, {self.day}, {self.hour}, {self.minute}, {self.second}, tzinfo={self.tzinfo})" + + def __eq__(self, other) -> bool: + return (self.year, self.month, self.day, self.hour, self.minute, self.second, self.tzinfo) == \ + (other.year, other.month, other.day, other.hour, other.minute, other.second, other.tzinfo) + + def __lt__(self, other) -> bool: + return (self.year, self.month, self.day, self.hour, self.minute, self.second, self.tzinfo) < \ + (other.year, other.month, other.day, other.hour, other.minute, other.second, other.tzinfo) + + def __le__(self, other) -> bool: + return (self.year, self.month, self.day, self.hour, self.minute, self.second, self.tzinfo) <= \ + (other.year, other.month, other.day, other.hour, other.minute, other.second, other.tzinfo) + + def __gt__(self, other) -> bool: + return (self.year, self.month, self.day, self.hour, self.minute, self.second, self.tzinfo) > \ + (other.year, other.month, other.day, other.hour, other.minute, other.second, other.tzinfo) + + def __ge__(self, other) -> bool: + return (self.year, self.month, self.day, self.hour, self.minute, self.second, self.tzinfo) >= \ + (other.year, other.month, other.day, other.hour, other.minute, other.second, other.tzinfo) + + def __add__(self, other: timedelta) -> 'datetime': + # Add the timedelta to the current datetime and return a new datetime + pass + + def __sub__(self, other: timedelta) -> 'datetime': + # Subtract the timedelta from the current datetime and return a new datetime + pass + + def timestamp(self) -> float: + # Return the timestamp of the current datetime + pass