datetime.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. from time import localtime, time as current_time
  2. from datetime import datetime as dt, timedelta as td #internally using datetime , timedelta for __add__,__sub__
  3. class timedelta:
  4. def __init__(self, days=0, seconds=0):
  5. self.days = days
  6. self.seconds = seconds
  7. def __repr__(self):
  8. return f"datetime.timedelta({self.days}, {self.seconds})"
  9. def __eq__(self, other: 'timedelta') -> bool:
  10. return (self.days, self.seconds) == (other.days, other.seconds)
  11. def __lt__(self, other: 'timedelta') -> bool:
  12. return (self.days, self.seconds) < (other.days, other.seconds)
  13. def __le__(self, other: 'timedelta') -> bool:
  14. return (self.days, self.seconds) <= (other.days, other.seconds)
  15. def __gt__(self, other: 'timedelta') -> bool:
  16. return (self.days, self.seconds) > (other.days, other.seconds)
  17. def __ge__(self, other: 'timedelta') -> bool:
  18. return (self.days, self.seconds) >= (other.days, other.seconds)
  19. class timezone:
  20. def __init__(self, delta: timedelta):
  21. self._delta = delta
  22. def __repr__(self):
  23. return f"datetime.timezone({self._delta})"
  24. class date:
  25. def __init__(self, year: int, month: int=None, day: int=None):
  26. self.year = year
  27. self.month = month
  28. self.day = day
  29. @staticmethod
  30. def today():
  31. t = localtime()
  32. return date(t.tm_year, t.tm_mon, t.tm_mday)
  33. def __eq__(self, other: 'date') -> bool:
  34. return self.year == other.year and self.month == other.month and self.day == other.day
  35. def __lt__(self, other: 'date') -> bool:
  36. return (self.year, self.month, self.day) < (other.year, other.month, other.day)
  37. def __le__(self, other: 'date') -> bool:
  38. return (self.year, self.month, self.day) <= (other.year, other.month, other.day)
  39. def __gt__(self, other: 'date') -> bool:
  40. return (self.year, self.month, self.day) > (other.year, other.month, other.day)
  41. def __ge__(self, other: 'date') -> bool:
  42. return (self.year, self.month, self.day) >= (other.year, other.month, other.day)
  43. def __add__(self, other: 'timedelta') -> 'date':
  44. new_date=dt(self.year,self.month,self.day)+td(other.days,other.seconds)
  45. return date(new_date.year,new_date.month,new_date.day)
  46. def __sub__(self, other: 'timedelta') -> 'date':
  47. new_date=dt(self.year,self.month,self.day)-td(other.days,other.seconds)
  48. return date(new_date.year,new_date.month,new_date.day)
  49. def __str__(self):
  50. return f"{self.year}-{self.month:02}-{self.day:02}"
  51. def __repr__(self):
  52. return f"datetime.date({self.year}, {self.month}, {self.day})"
  53. class datetime(date):
  54. def __init__(self, year: int, month: int = None, day: int = None, hour: int = None, minute: int = None, second: int = None, tzinfo: timezone = None):
  55. super().__init__(year, month, day)
  56. # Validate and set hour, minute, and second
  57. if hour is not None and not 0 <= hour <= 23:
  58. raise ValueError("Hour must be between 0 and 23")
  59. self.hour = hour
  60. if minute is not None and not 0 <= minute <= 59:
  61. raise ValueError("Minute must be between 0 and 59")
  62. self.minute = minute
  63. if second is not None and not 0 <= second <= 59:
  64. raise ValueError("Second must be between 0 and 59")
  65. self.second = second
  66. self.tzinfo = tzinfo
  67. @staticmethod
  68. def now():
  69. t = localtime()
  70. return datetime(t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec)
  71. def __str__(self):
  72. return f"{self.year}-{self.month:02}-{self.day:02} {self.hour:02}:{self.minute:02}:{self.second:02}"
  73. def __repr__(self):
  74. return f"datetime.datetime({self.year}, {self.month}, {self.day}, {self.hour}, {self.minute}, {self.second})"
  75. def __eq__(self, other) -> bool:
  76. return (self.year, self.month, self.day, self.hour, self.minute, self.second) == \
  77. (other.year, other.month, other.day, other.hour, other.minute, other.second)
  78. def __lt__(self, other) -> bool:
  79. return (self.year, self.month, self.day, self.hour, self.minute, self.second) < \
  80. (other.year, other.month, other.day, other.hour, other.minute, other.second)
  81. def __le__(self, other) -> bool:
  82. return (self.year, self.month, self.day, self.hour, self.minute, self.second) <= \
  83. (other.year, other.month, other.day, other.hour, other.minute, other.second)
  84. def __gt__(self, other) -> bool:
  85. return (self.year, self.month, self.day, self.hour, self.minute, self.second) > \
  86. (other.year, other.month, other.day, other.hour, other.minute, other.second)
  87. def __ge__(self, other) -> bool:
  88. return (self.year, self.month, self.day, self.hour, self.minute, self.second) >= \
  89. (other.year, other.month, other.day, other.hour, other.minute, other.second)
  90. def __add__(self, other: 'timedelta') -> 'datetime':
  91. new_datetime = dt(self.year, self.month, self.day, self.hour, self.minute, self.second) + td(days=other.days, seconds=other.seconds)
  92. return datetime(new_datetime.year, new_datetime.month, new_datetime.day, new_datetime.hour, new_datetime.minute, new_datetime.second)
  93. def __sub__(self, other: 'timedelta') -> 'datetime':
  94. total_seconds = self.second + other.seconds + other.days * 86400
  95. new_datetime = self - timedelta(seconds=total_seconds)
  96. return new_datetime
  97. def timestamp(self) -> float:
  98. delta = self - datetime(1970, 1, 1)
  99. return delta.total_seconds()