mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
revised-changes
This commit is contained in:
parent
1ad67650ac
commit
9a30f4d5b5
@ -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,27 +10,38 @@ 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):
|
||||
@ -42,35 +54,37 @@ class date:
|
||||
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,6 +105,10 @@ 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}"
|
||||
|
||||
@ -98,36 +116,34 @@ 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)
|
||||
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)
|
||||
(other.year, other.month, other.day,
|
||||
other.hour, other.minute, other.second)
|
||||
|
||||
def __lt__(self, other) -> bool:
|
||||
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)
|
||||
(other.year, other.month, other.day,
|
||||
other.hour, other.minute, other.second)
|
||||
|
||||
def __le__(self, other) -> bool:
|
||||
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)
|
||||
(other.year, other.month, other.day,
|
||||
other.hour, other.minute, other.second)
|
||||
|
||||
def __gt__(self, other) -> bool:
|
||||
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)
|
||||
(other.year, other.month, other.day,
|
||||
other.hour, other.minute, other.second)
|
||||
|
||||
def __ge__(self, other) -> bool:
|
||||
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 __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
|
||||
(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()
|
||||
|
39
tests/80_datetime.py
Normal file
39
tests/80_datetime.py
Normal file
@ -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()
|
Loading…
x
Reference in New Issue
Block a user