mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 19:40:18 +00:00
...
This commit is contained in:
parent
36ce3951c6
commit
dcfed8f7b7
@ -1,48 +1,39 @@
|
|||||||
|
|
||||||
from time import localtime
|
from time import localtime
|
||||||
|
|
||||||
|
|
||||||
class timedelta:
|
class timedelta:
|
||||||
def __init__(self, days=0, seconds=0):
|
def __init__(self, days=0, seconds=0):
|
||||||
self.days = days
|
self.days = days
|
||||||
self.seconds = seconds
|
self.seconds = seconds
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f"datetime.timedelta({self.days}, {self.seconds})"
|
return f"datetime.timedelta(days={self.days}, seconds={self.seconds})"
|
||||||
|
|
||||||
def check(self, other, class_type):
|
|
||||||
if type(other) is not class_type:
|
|
||||||
return NotImplemented
|
|
||||||
|
|
||||||
def __eq__(self, other: 'timedelta') -> bool:
|
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)
|
return (self.days, self.seconds) == (other.days, other.seconds)
|
||||||
|
|
||||||
def __lt__(self, other: 'timedelta') -> bool:
|
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)
|
return (self.days, self.seconds) < (other.days, other.seconds)
|
||||||
|
|
||||||
def __le__(self, other: 'timedelta') -> bool:
|
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)
|
return (self.days, self.seconds) <= (other.days, other.seconds)
|
||||||
|
|
||||||
def __gt__(self, other: 'timedelta') -> bool:
|
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)
|
return (self.days, self.seconds) > (other.days, other.seconds)
|
||||||
|
|
||||||
def __ge__(self, other: 'timedelta') -> bool:
|
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)
|
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:
|
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.year = year
|
||||||
@ -54,28 +45,29 @@ class 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)
|
||||||
|
|
||||||
def check(self, other, class_type):
|
|
||||||
if type(other) is not class_type:
|
|
||||||
return NotImplemented
|
|
||||||
|
|
||||||
def __eq__(self, other: 'date') -> bool:
|
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)
|
return (self.year, self.month, self.day) == (other.year, other.month, other.day)
|
||||||
|
|
||||||
def __lt__(self, other: 'date') -> bool:
|
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)
|
return (self.year, self.month, self.day) < (other.year, other.month, other.day)
|
||||||
|
|
||||||
def __le__(self, other: 'date') -> bool:
|
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)
|
return (self.year, self.month, self.day) <= (other.year, other.month, other.day)
|
||||||
|
|
||||||
def __gt__(self, other: 'date') -> bool:
|
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)
|
return (self.year, self.month, self.day) > (other.year, other.month, other.day)
|
||||||
|
|
||||||
def __ge__(self, other: 'date') -> bool:
|
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)
|
return (self.year, self.month, self.day) >= (other.year, other.month, other.day)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
@ -86,7 +78,7 @@ class date:
|
|||||||
|
|
||||||
|
|
||||||
class datetime(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)
|
super().__init__(year, month, day)
|
||||||
# Validate and set hour, minute, and second
|
# Validate and set hour, minute, and second
|
||||||
if hour is not None and not 0 <= hour <= 23:
|
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:
|
if second is not None and 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 = second
|
||||||
self.tzinfo = tzinfo
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def now():
|
def now():
|
||||||
t = localtime()
|
t = localtime()
|
||||||
return datetime(t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec)
|
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):
|
def __str__(self):
|
||||||
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}"
|
||||||
|
|
||||||
@ -116,34 +103,40 @@ class datetime(date):
|
|||||||
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:
|
||||||
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) ==\
|
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:
|
||||||
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) <\
|
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:
|
||||||
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) <=\
|
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:
|
||||||
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) >\
|
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:
|
||||||
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) >=\
|
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:
|
||||||
return self.now()
|
raise NotImplementedError
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user