Enhancements for Robustness and Readability in Date and Time Handling Code

This commit is contained in:
Madhav Agarwal 2024-03-30 23:47:02 +05:30 committed by GitHub
parent cb15db1f0e
commit 5b923290b9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,11 +1,11 @@
from time import localtime from time import localtime
class timedelta: class timedelta:
def __init__(self, days=0, seconds=0): def __init__(self, days: int = 0, seconds: int = 0):
self.days = days self.days: int = days
self.seconds = seconds self.seconds: int = seconds
def __repr__(self): def __repr__(self) -> str:
return f"datetime.timedelta(days={self.days}, seconds={self.seconds})" return f"datetime.timedelta(days={self.days}, seconds={self.seconds})"
def __eq__(self, other: 'timedelta') -> bool: def __eq__(self, other: 'timedelta') -> bool:
@ -36,12 +36,12 @@ class timedelta:
class date: class date:
def __init__(self, year: int, month: int, day: int): def __init__(self, year: int, month: int, day: int):
self.year = year self.year: int = year
self.month = month self.month: int = month
self.day = day self.day: int = day
@staticmethod @staticmethod
def today(): def today() -> 'date':
t = localtime() t = localtime()
return date(t.tm_year, t.tm_mon, t.tm_mday) return date(t.tm_year, t.tm_mon, t.tm_mday)
@ -70,79 +70,72 @@ class date:
return NotImplemented return NotImplemented
return (self.year, self.month, self.day) >= (other.year, other.month, other.day) return (self.year, self.month, self.day) >= (other.year, other.month, other.day)
def __str__(self): def __str__(self) -> str:
return f"{self.year}-{self.month:02}-{self.day:02}" return f"{self.year}-{self.month:02}-{self.day:02}"
def __repr__(self): def __repr__(self) -> str:
return f"datetime.date({self.year}, {self.month}, {self.day})" return f"datetime.date({self.year}, {self.month}, {self.day})"
class datetime(date): class datetime(date):
def __init__(self, year: int, month: int, day: int, hour: int, minute: int, second: int): def __init__(self, year: int, month: int, day: int, hour: int, minute: int, second: int):
super().__init__(year, month, day) super().__init__(year, month, day)
# Validate and set hour, minute, and second
if not 0 <= hour <= 23: if not 0 <= hour <= 23:
raise ValueError("Hour must be between 0 and 23") raise ValueError("Hour must be between 0 and 23")
self.hour = hour self.hour: int = hour
if not 0 <= minute <= 59: if not 0 <= minute <= 59:
raise ValueError("Minute must be between 0 and 59") raise ValueError("Minute must be between 0 and 59")
self.minute = minute self.minute: int = minute
if not 0 <= second <= 59: if not 0 <= second <= 59:
raise ValueError("Second must be between 0 and 59") raise ValueError("Second must be between 0 and 59")
self.second = second self.second: int = second
def date(self) -> date: def date(self) -> date:
return date(self.year, self.month, self.day) return date(self.year, self.month, self.day)
@staticmethod @staticmethod
def now(): def now() -> 'datetime':
t = localtime() t = localtime()
tm_sec = t.tm_sec tm_sec = t.tm_sec
if tm_sec == 60: if tm_sec == 60:
tm_sec = 59 tm_sec = 59
return datetime(t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, tm_sec) return datetime(t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, tm_sec)
def __str__(self): def __str__(self) -> str:
return f"{self.year}-{self.month:02}-{self.day:02} {self.hour:02}:{self.minute:02}:{self.second:02}" return f"{self.year}-{self.month:02}-{self.day:02} {self.hour:02}:{self.minute:02}:{self.second:02}"
def __repr__(self): def __repr__(self) -> str:
return f"datetime.datetime({self.year}, {self.month}, {self.day}, {self.hour}, {self.minute}, {self.second})" return f"datetime.datetime({self.year}, {self.month}, {self.day}, {self.hour}, {self.minute}, {self.second})"
def __eq__(self, other) -> bool: def __eq__(self, other) -> bool:
if type(other) is not datetime: if type(other) is not datetime:
return NotImplemented return NotImplemented
return (self.year, self.month, self.day, self.hour, self.minute, self.second) ==\ return (self.year, self.month, self.day, self.hour, self.minute, self.second) ==\
(other.year, other.month, other.day, (other.year, other.month, other.day, other.hour, other.minute, other.second)
other.hour, other.minute, other.second)
def __lt__(self, other) -> bool: def __lt__(self, other) -> bool:
if type(other) is not datetime: if type(other) is not datetime:
return NotImplemented return NotImplemented
return (self.year, self.month, self.day, self.hour, self.minute, self.second) <\ return (self.year, self.month, self.day, self.hour, self.minute, self.second) <\
(other.year, other.month, other.day, (other.year, other.month, other.day, other.hour, other.minute, other.second)
other.hour, other.minute, other.second)
def __le__(self, other) -> bool: def __le__(self, other) -> bool:
if type(other) is not datetime: if type(other) is not datetime:
return NotImplemented return NotImplemented
return (self.year, self.month, self.day, self.hour, self.minute, self.second) <=\ return (self.year, self.month, self.day, self.hour, self.minute, self.second) <=\
(other.year, other.month, other.day, (other.year, other.month, other.day, other.hour, other.minute, other.second)
other.hour, other.minute, other.second)
def __gt__(self, other) -> bool: def __gt__(self, other) -> bool:
if type(other) is not datetime: if type(other) is not datetime:
return NotImplemented return NotImplemented
return (self.year, self.month, self.day, self.hour, self.minute, self.second) >\ return (self.year, self.month, self.day, self.hour, self.minute, self.second) >\
(other.year, other.month, other.day, (other.year, other.month, other.day, other.hour, other.minute, other.second)
other.hour, other.minute, other.second)
def __ge__(self, other) -> bool: def __ge__(self, other) -> bool:
if type(other) is not datetime: if type(other) is not datetime:
return NotImplemented return NotImplemented
return (self.year, self.month, self.day, self.hour, self.minute, self.second) >=\ return (self.year, self.month, self.day, self.hour, self.minute, self.second) >=\
(other.year, other.month, other.day, (other.year, other.month, other.day, other.hour, other.minute, other.second)
other.hour, other.minute, other.second)
def timestamp(self) -> float: def timestamp(self) -> float:
raise NotImplementedError raise NotImplementedError("Method 'timestamp' needs to be implemented in subclasses")