mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 19:40:18 +00:00
Merge pull request #162 from MSaiKiran9/datetime-file
Datetime file update
This commit is contained in:
commit
f837ace29c
@ -1,7 +1,41 @@
|
|||||||
from time import localtime
|
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(days={self.days}, seconds={self.seconds})"
|
||||||
|
|
||||||
|
def __eq__(self, other: 'timedelta') -> bool:
|
||||||
|
if type(other) is not timedelta:
|
||||||
|
return NotImplemented
|
||||||
|
return (self.days, self.seconds) == (other.days, other.seconds)
|
||||||
|
|
||||||
|
def __lt__(self, other: 'timedelta') -> bool:
|
||||||
|
if type(other) is not timedelta:
|
||||||
|
return NotImplemented
|
||||||
|
return (self.days, self.seconds) < (other.days, other.seconds)
|
||||||
|
|
||||||
|
def __le__(self, other: 'timedelta') -> bool:
|
||||||
|
if type(other) is not timedelta:
|
||||||
|
return NotImplemented
|
||||||
|
return (self.days, self.seconds) <= (other.days, other.seconds)
|
||||||
|
|
||||||
|
def __gt__(self, other: 'timedelta') -> bool:
|
||||||
|
if type(other) is not timedelta:
|
||||||
|
return NotImplemented
|
||||||
|
return (self.days, self.seconds) > (other.days, other.seconds)
|
||||||
|
|
||||||
|
def __ge__(self, other: 'timedelta') -> bool:
|
||||||
|
if type(other) is not timedelta:
|
||||||
|
return NotImplemented
|
||||||
|
return (self.days, self.seconds) >= (other.days, other.seconds)
|
||||||
|
|
||||||
|
|
||||||
class date:
|
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.year = year
|
||||||
self.month = month
|
self.month = month
|
||||||
self.day = day
|
self.day = day
|
||||||
@ -11,17 +45,50 @@ 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 __eq__(self, other: 'date') -> bool:
|
||||||
|
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:
|
||||||
|
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:
|
||||||
|
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:
|
||||||
|
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:
|
||||||
|
if type(other) is not date:
|
||||||
|
return NotImplemented
|
||||||
|
return (self.year, self.month, self.day) >= (other.year, other.month, other.day)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
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):
|
||||||
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 = None, day: int = None, hour: int = None, minute: int = None, second: int = None):
|
||||||
super(datetime, self).__init__(year, month, day)
|
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
|
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
|
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.second = second
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@ -34,3 +101,42 @@ class datetime(date):
|
|||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
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:
|
||||||
|
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:
|
||||||
|
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:
|
||||||
|
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:
|
||||||
|
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:
|
||||||
|
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:
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
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