From 2fc835232b9dd2e3afa9ab6b6e10af72a1f2e308 Mon Sep 17 00:00:00 2001 From: M Sai Kiran <116418856+MSaiKiran9@users.noreply.github.com> Date: Sat, 7 Oct 2023 11:07:25 +0530 Subject: [PATCH 1/4] datetime-upt1 --- python/datetime.py | 107 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 102 insertions(+), 5 deletions(-) diff --git a/python/datetime.py b/python/datetime.py index 8d8dc1de..ea19dd73 100644 --- a/python/datetime.py +++ b/python/datetime.py @@ -1,7 +1,38 @@ -from time import localtime +from time import localtime, time as current_time +from datetime import datetime as dt, timedelta as td #internally using datetime , timedelta for __add__,__sub__ + +class timedelta: + def __init__(self, days=0, seconds=0): + self.days = days + self.seconds = seconds + + def __repr__(self): + return f"datetime.timedelta({self.days}, {self.seconds})" + + def __eq__(self, other: 'timedelta') -> bool: + return (self.days, self.seconds) == (other.days, 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): + return f"datetime.timezone({self._delta})" class date: - def __init__(self, year: int, month: int, day: int): + def __init__(self, year: int, month: int=None, day: int=None): self.year = year self.month = month self.day = day @@ -11,6 +42,29 @@ class date: t = localtime() return date(t.tm_year, t.tm_mon, t.tm_mday) + def __eq__(self, other: 'date') -> bool: + return self.year == other.year and self.month == other.month and self.day == 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': + new_date=dt(self.year,self.month,self.day)+td(other.days,other.seconds) + return date(new_date.year,new_date.month,new_date.day) + + def __sub__(self, other: 'timedelta') -> 'date': + new_date=dt(self.year,self.month,self.day)-td(other.days,other.seconds) + return date(new_date.year,new_date.month,new_date.day) + def __str__(self): return f"{self.year}-{self.month:02}-{self.day:02}" @@ -18,11 +72,19 @@ class date: 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) + def __init__(self, year: int, month: int = None, day: int = None, hour: int = None, minute: int = None, second: int = None, tzinfo: timezone = None): + super().__init__(year, month, day) + # Validate and set hour, minute, and second + if hour is not None and not 0 <= hour <= 23: + raise ValueError("Hour must be between 0 and 23") self.hour = hour + if minute is not None and not 0 <= minute <= 59: + raise ValueError("Minute must be between 0 and 59") self.minute = minute + if second is not None and not 0 <= second <= 59: + raise ValueError("Second must be between 0 and 59") self.second = second + self.tzinfo = tzinfo @staticmethod def now(): @@ -33,4 +95,39 @@ class datetime(date): return f"{self.year}-{self.month:02}-{self.day:02} {self.hour:02}:{self.minute:02}:{self.second:02}" 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 + return f"datetime.datetime({self.year}, {self.month}, {self.day}, {self.hour}, {self.minute}, {self.second})" + + def __eq__(self, other) -> bool: + return (self.year, self.month, self.day, self.hour, self.minute, self.second) == \ + (other.year, other.month, other.day, other.hour, other.minute, other.second) + + def __lt__(self, other) -> bool: + return (self.year, self.month, self.day, self.hour, self.minute, self.second) < \ + (other.year, other.month, other.day, other.hour, other.minute, other.second) + + def __le__(self, other) -> bool: + return (self.year, self.month, self.day, self.hour, self.minute, self.second) <= \ + (other.year, other.month, other.day, other.hour, other.minute, other.second) + + def __gt__(self, other) -> bool: + return (self.year, self.month, self.day, self.hour, self.minute, self.second) > \ + (other.year, other.month, other.day, other.hour, other.minute, other.second) + + def __ge__(self, other) -> bool: + return (self.year, self.month, self.day, self.hour, self.minute, self.second) >= \ + (other.year, other.month, other.day, other.hour, other.minute, other.second) + + def __add__(self, other: 'timedelta') -> 'datetime': + new_datetime = dt(self.year, self.month, self.day, self.hour, self.minute, self.second) + td(days=other.days, seconds=other.seconds) + return datetime(new_datetime.year, new_datetime.month, new_datetime.day, new_datetime.hour, new_datetime.minute, new_datetime.second) + + def __sub__(self, other: 'timedelta') -> 'datetime': + total_seconds = self.second + other.seconds + other.days * 86400 + new_datetime = self - timedelta(seconds=total_seconds) + return new_datetime + + def timestamp(self) -> float: + delta = self - datetime(1970, 1, 1) + return delta.total_seconds() + + From 9a30f4d5b599ca46f7ef1ccb6a59a93809baf4ec Mon Sep 17 00:00:00 2001 From: M Sai Kiran <116418856+MSaiKiran9@users.noreply.github.com> Date: Mon, 9 Oct 2023 20:27:21 +0530 Subject: [PATCH 2/4] revised-changes --- python/datetime.py | 104 +++++++++++++++++++++++++------------------ tests/80_datetime.py | 39 ++++++++++++++++ 2 files changed, 99 insertions(+), 44 deletions(-) create mode 100644 tests/80_datetime.py diff --git a/python/datetime.py b/python/datetime.py index ea19dd73..3e94ecd7 100644 --- a/python/datetime.py +++ b/python/datetime.py @@ -1,5 +1,6 @@ -from time import localtime, time as current_time -from datetime import datetime as dt, timedelta as td #internally using datetime , timedelta for __add__,__sub__ + +from time import localtime + class timedelta: def __init__(self, days=0, seconds=0): @@ -9,30 +10,41 @@ class timedelta: def __repr__(self): return f"datetime.timedelta({self.days}, {self.seconds})" + def check(self, other, class_type): + if not isinstance(other, class_type): + raise NotImplementedError("incompatible types / not implemented") + def __eq__(self, other: 'timedelta') -> bool: + self.check(other, timedelta) return (self.days, self.seconds) == (other.days, other.seconds) def __lt__(self, other: 'timedelta') -> bool: + self.check(other, timedelta) return (self.days, self.seconds) < (other.days, other.seconds) def __le__(self, other: 'timedelta') -> bool: + self.check(other, timedelta) return (self.days, self.seconds) <= (other.days, other.seconds) def __gt__(self, other: 'timedelta') -> bool: + self.check(other, timedelta) return (self.days, self.seconds) > (other.days, other.seconds) def __ge__(self, other: 'timedelta') -> bool: + self.check(other, timedelta) return (self.days, self.seconds) >= (other.days, other.seconds) + class timezone: def __init__(self, delta: timedelta): - self._delta = delta + self.delta = delta def __repr__(self): - return f"datetime.timezone({self._delta})" + return f"datetime.timezone({self.delta})" + class date: - def __init__(self, year: int, month: int=None, day: int=None): + def __init__(self, year: int, month: int = None, day: int = None): self.year = year self.month = month self.day = day @@ -41,36 +53,38 @@ class date: def today(): t = localtime() return date(t.tm_year, t.tm_mon, t.tm_mday) - + + def check(self, other, class_type): + if not isinstance(other, class_type): + raise NotImplementedError("incompatible types / not implemented") + def __eq__(self, other: 'date') -> bool: - return self.year == other.year and self.month == other.month and self.day == other.day - + self.check(other, date) + return (self.year, self.month, self.day) == (other.year, other.month, other.day) + def __lt__(self, other: 'date') -> bool: + self.check(other, date) return (self.year, self.month, self.day) < (other.year, other.month, other.day) - + def __le__(self, other: 'date') -> bool: + self.check(other, date) return (self.year, self.month, self.day) <= (other.year, other.month, other.day) - + def __gt__(self, other: 'date') -> bool: + self.check(other, date) return (self.year, self.month, self.day) > (other.year, other.month, other.day) def __ge__(self, other: 'date') -> bool: + self.check(other, date) return (self.year, self.month, self.day) >= (other.year, other.month, other.day) - - def __add__(self, other: 'timedelta') -> 'date': - new_date=dt(self.year,self.month,self.day)+td(other.days,other.seconds) - return date(new_date.year,new_date.month,new_date.day) - - def __sub__(self, other: 'timedelta') -> 'date': - new_date=dt(self.year,self.month,self.day)-td(other.days,other.seconds) - return date(new_date.year,new_date.month,new_date.day) def __str__(self): return f"{self.year}-{self.month:02}-{self.day:02}" - + def __repr__(self): return f"datetime.date({self.year}, {self.month}, {self.day})" + class datetime(date): def __init__(self, year: int, month: int = None, day: int = None, hour: int = None, minute: int = None, second: int = None, tzinfo: timezone = None): super().__init__(year, month, day) @@ -91,43 +105,45 @@ class datetime(date): t = localtime() return datetime(t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec) + def check(self, other, class_type): + if not isinstance(other, class_type): + raise NotImplementedError("incompatible types / not implemented") + def __str__(self): return f"{self.year}-{self.month:02}-{self.day:02} {self.hour:02}:{self.minute:02}:{self.second:02}" def __repr__(self): return f"datetime.datetime({self.year}, {self.month}, {self.day}, {self.hour}, {self.minute}, {self.second})" - + def __eq__(self, other) -> bool: - return (self.year, self.month, self.day, self.hour, self.minute, self.second) == \ - (other.year, other.month, other.day, other.hour, other.minute, other.second) + self.check(other, datetime) + return (self.year, self.month, self.day, self.hour, self.minute, self.second) ==\ + (other.year, other.month, other.day, + other.hour, other.minute, other.second) def __lt__(self, other) -> bool: - return (self.year, self.month, self.day, self.hour, self.minute, self.second) < \ - (other.year, other.month, other.day, other.hour, other.minute, other.second) + self.check(other, datetime) + return (self.year, self.month, self.day, self.hour, self.minute, self.second) <\ + (other.year, other.month, other.day, + other.hour, other.minute, other.second) def __le__(self, other) -> bool: - return (self.year, self.month, self.day, self.hour, self.minute, self.second) <= \ - (other.year, other.month, other.day, other.hour, other.minute, other.second) + self.check(other, datetime) + return (self.year, self.month, self.day, self.hour, self.minute, self.second) <=\ + (other.year, other.month, other.day, + other.hour, other.minute, other.second) def __gt__(self, other) -> bool: - return (self.year, self.month, self.day, self.hour, self.minute, self.second) > \ - (other.year, other.month, other.day, other.hour, other.minute, other.second) + self.check(other, datetime) + return (self.year, self.month, self.day, self.hour, self.minute, self.second) >\ + (other.year, other.month, other.day, + other.hour, other.minute, other.second) def __ge__(self, other) -> bool: - return (self.year, self.month, self.day, self.hour, self.minute, self.second) >= \ - (other.year, other.month, other.day, other.hour, other.minute, other.second) - - def __add__(self, other: 'timedelta') -> 'datetime': - new_datetime = dt(self.year, self.month, self.day, self.hour, self.minute, self.second) + td(days=other.days, seconds=other.seconds) - return datetime(new_datetime.year, new_datetime.month, new_datetime.day, new_datetime.hour, new_datetime.minute, new_datetime.second) - - def __sub__(self, other: 'timedelta') -> 'datetime': - total_seconds = self.second + other.seconds + other.days * 86400 - new_datetime = self - timedelta(seconds=total_seconds) - return new_datetime - + self.check(other, datetime) + return (self.year, self.month, self.day, self.hour, self.minute, self.second) >=\ + (other.year, other.month, other.day, + other.hour, other.minute, other.second) + def timestamp(self) -> float: - delta = self - datetime(1970, 1, 1) - return delta.total_seconds() - - + return self.now() diff --git a/tests/80_datetime.py b/tests/80_datetime.py new file mode 100644 index 00000000..d5c3e3f7 --- /dev/null +++ b/tests/80_datetime.py @@ -0,0 +1,39 @@ +import datetime + + +def test_timedelta(): + assert datetime.timedelta(days=1) == datetime.timedelta(days=1) + assert datetime.timedelta(days=1) != datetime.timedelta(days=2) + assert datetime.timedelta(days=1, seconds=1) >= datetime.timedelta(days=1) + assert datetime.timedelta(days=0, seconds=1) <= datetime.timedelta(days=1) + assert datetime.timedelta(days=1, seconds=1) < datetime.timedelta(days=2) + assert datetime.timedelta(days=1, seconds=1) > datetime.timedelta(days=0) + + +def test_date(): + assert datetime.date(2023, 8, 5) == datetime.date(2023, 8, 5) + assert datetime.date(2023, 8, 5) != datetime.date(2023, 8, 6) + assert datetime.date(2024, 8, 5) >= datetime.date(2023, 8, 6) + assert datetime.date(2023, 8, 5) <= datetime.date(2023, 8, 6) + assert datetime.date(2024, 8, 5) > datetime.date(2023, 8, 6) + assert datetime.date(2023, 8, 5) < datetime.date(2024, 8, 6) + + +def test_datetime(): + assert datetime.datetime( + 2023, 8, 5, 12, 0, 0) == datetime.datetime(2023, 8, 5, 12, 0, 0) + assert datetime.datetime( + 2023, 8, 5, 12, 0, 0) != datetime.datetime(2023, 8, 5, 12, 1, 0) + assert datetime.datetime( + 2023, 8, 5, 12, 0, 0) >= datetime.datetime(2023, 8, 5, 12, 0, 0) + assert datetime.datetime( + 2023, 8, 5, 12, 30, 0) > datetime.datetime(2023, 8, 5, 12, 1, 0) + assert datetime.datetime( + 2023, 8, 5, 12, 0, 0) < datetime.datetime(2023, 8, 5, 12, 1, 0) + assert datetime.datetime( + 2023, 8, 5, 12, 0, 0) <= datetime.datetime(2023, 8, 5, 12, 1, 0) + + +test_timedelta() +test_date() +test_datetime() From 36ce3951c63d24a73bf63536ebec7bfa60a23044 Mon Sep 17 00:00:00 2001 From: M Sai Kiran <116418856+MSaiKiran9@users.noreply.github.com> Date: Mon, 9 Oct 2023 21:01:18 +0530 Subject: [PATCH 3/4] rev2 --- python/datetime.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/python/datetime.py b/python/datetime.py index 3e94ecd7..96c4cfdf 100644 --- a/python/datetime.py +++ b/python/datetime.py @@ -11,8 +11,8 @@ class timedelta: return f"datetime.timedelta({self.days}, {self.seconds})" def check(self, other, class_type): - if not isinstance(other, class_type): - raise NotImplementedError("incompatible types / not implemented") + if type(other) is not class_type: + return NotImplemented def __eq__(self, other: 'timedelta') -> bool: self.check(other, timedelta) @@ -55,8 +55,8 @@ class date: return date(t.tm_year, t.tm_mon, t.tm_mday) def check(self, other, class_type): - if not isinstance(other, class_type): - raise NotImplementedError("incompatible types / not implemented") + if type(other) is not class_type: + return NotImplemented def __eq__(self, other: 'date') -> bool: self.check(other, date) @@ -106,8 +106,8 @@ class datetime(date): return datetime(t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec) def check(self, other, class_type): - if not isinstance(other, class_type): - raise NotImplementedError("incompatible types / not implemented") + if type(other) is not class_type: + return NotImplemented def __str__(self): return f"{self.year}-{self.month:02}-{self.day:02} {self.hour:02}:{self.minute:02}:{self.second:02}" From dcfed8f7b7954cab94bd52a2d7c70603ece59c52 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Wed, 11 Oct 2023 16:11:50 +0800 Subject: [PATCH 4/4] ... --- python/datetime.py | 75 +++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 41 deletions(-) diff --git a/python/datetime.py b/python/datetime.py index 96c4cfdf..9328e8a1 100644 --- a/python/datetime.py +++ b/python/datetime.py @@ -1,48 +1,39 @@ - from time import localtime - class timedelta: def __init__(self, days=0, seconds=0): self.days = days self.seconds = seconds def __repr__(self): - return f"datetime.timedelta({self.days}, {self.seconds})" - - def check(self, other, class_type): - if type(other) is not class_type: - return NotImplemented + return f"datetime.timedelta(days={self.days}, seconds={self.seconds})" def __eq__(self, other: 'timedelta') -> bool: - self.check(other, timedelta) + if type(other) is not timedelta: + return NotImplemented return (self.days, self.seconds) == (other.days, other.seconds) def __lt__(self, other: 'timedelta') -> bool: - self.check(other, timedelta) + if type(other) is not timedelta: + return NotImplemented return (self.days, self.seconds) < (other.days, other.seconds) def __le__(self, other: 'timedelta') -> bool: - self.check(other, timedelta) + if type(other) is not timedelta: + return NotImplemented return (self.days, self.seconds) <= (other.days, other.seconds) def __gt__(self, other: 'timedelta') -> bool: - self.check(other, timedelta) + if type(other) is not timedelta: + return NotImplemented return (self.days, self.seconds) > (other.days, other.seconds) def __ge__(self, other: 'timedelta') -> bool: - self.check(other, timedelta) + if type(other) is not timedelta: + return NotImplemented return (self.days, self.seconds) >= (other.days, other.seconds) -class timezone: - def __init__(self, delta: timedelta): - self.delta = delta - - def __repr__(self): - return f"datetime.timezone({self.delta})" - - class date: def __init__(self, year: int, month: int = None, day: int = None): self.year = year @@ -54,28 +45,29 @@ class date: t = localtime() return date(t.tm_year, t.tm_mon, t.tm_mday) - def check(self, other, class_type): - if type(other) is not class_type: - return NotImplemented - def __eq__(self, other: 'date') -> bool: - self.check(other, date) + if type(other) is not date: + return NotImplemented return (self.year, self.month, self.day) == (other.year, other.month, other.day) def __lt__(self, other: 'date') -> bool: - self.check(other, date) + if type(other) is not date: + return NotImplemented return (self.year, self.month, self.day) < (other.year, other.month, other.day) def __le__(self, other: 'date') -> bool: - self.check(other, date) + if type(other) is not date: + return NotImplemented return (self.year, self.month, self.day) <= (other.year, other.month, other.day) def __gt__(self, other: 'date') -> bool: - self.check(other, date) + if type(other) is not date: + return NotImplemented return (self.year, self.month, self.day) > (other.year, other.month, other.day) def __ge__(self, other: 'date') -> bool: - self.check(other, date) + if type(other) is not date: + return NotImplemented return (self.year, self.month, self.day) >= (other.year, other.month, other.day) def __str__(self): @@ -86,7 +78,7 @@ class date: class datetime(date): - def __init__(self, year: int, month: int = None, day: int = None, hour: int = None, minute: int = None, second: int = None, tzinfo: timezone = None): + def __init__(self, year: int, month: int = None, day: int = None, hour: int = None, minute: int = None, second: int = None): super().__init__(year, month, day) # Validate and set hour, minute, and second if hour is not None and not 0 <= hour <= 23: @@ -98,17 +90,12 @@ class datetime(date): if second is not None and not 0 <= second <= 59: raise ValueError("Second must be between 0 and 59") self.second = second - self.tzinfo = tzinfo @staticmethod def now(): t = localtime() return datetime(t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec) - def check(self, other, class_type): - if type(other) is not class_type: - return NotImplemented - def __str__(self): return f"{self.year}-{self.month:02}-{self.day:02} {self.hour:02}:{self.minute:02}:{self.second:02}" @@ -116,34 +103,40 @@ class datetime(date): return f"datetime.datetime({self.year}, {self.month}, {self.day}, {self.hour}, {self.minute}, {self.second})" def __eq__(self, other) -> bool: - self.check(other, datetime) + if type(other) is not datetime: + return NotImplemented return (self.year, self.month, self.day, self.hour, self.minute, self.second) ==\ (other.year, other.month, other.day, other.hour, other.minute, other.second) def __lt__(self, other) -> bool: - self.check(other, datetime) + if type(other) is not datetime: + return NotImplemented return (self.year, self.month, self.day, self.hour, self.minute, self.second) <\ (other.year, other.month, other.day, other.hour, other.minute, other.second) def __le__(self, other) -> bool: - self.check(other, datetime) + if type(other) is not datetime: + return NotImplemented return (self.year, self.month, self.day, self.hour, self.minute, self.second) <=\ (other.year, other.month, other.day, other.hour, other.minute, other.second) def __gt__(self, other) -> bool: - self.check(other, datetime) + if type(other) is not datetime: + return NotImplemented return (self.year, self.month, self.day, self.hour, self.minute, self.second) >\ (other.year, other.month, other.day, other.hour, other.minute, other.second) def __ge__(self, other) -> bool: - self.check(other, datetime) + if type(other) is not datetime: + return NotImplemented return (self.year, self.month, self.day, self.hour, self.minute, self.second) >=\ (other.year, other.month, other.day, other.hour, other.minute, other.second) def timestamp(self) -> float: - return self.now() + raise NotImplementedError +