datetime.py 5.3 KB

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