1
0

datetime.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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, day: int):
  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, day: int, hour: int, minute: int, second: int):
  63. super().__init__(year, month, day)
  64. # Validate and set hour, minute, and second
  65. if not 0 <= hour <= 23:
  66. raise ValueError("Hour must be between 0 and 23")
  67. self.hour = hour
  68. if not 0 <= minute <= 59:
  69. raise ValueError("Minute must be between 0 and 59")
  70. self.minute = minute
  71. if not 0 <= second <= 59:
  72. raise ValueError("Second must be between 0 and 59")
  73. self.second = second
  74. def date(self) -> date:
  75. return date(self.year, self.month, self.day)
  76. @staticmethod
  77. def now():
  78. t = localtime()
  79. tm_sec = t.tm_sec
  80. if tm_sec == 60:
  81. tm_sec = 59
  82. return datetime(t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, tm_sec)
  83. def __str__(self):
  84. return f"{self.year}-{self.month:02}-{self.day:02} {self.hour:02}:{self.minute:02}:{self.second:02}"
  85. def __repr__(self):
  86. return f"datetime.datetime({self.year}, {self.month}, {self.day}, {self.hour}, {self.minute}, {self.second})"
  87. def __eq__(self, other) -> bool:
  88. if type(other) is not datetime:
  89. return NotImplemented
  90. return (self.year, self.month, self.day, self.hour, self.minute, self.second) ==\
  91. (other.year, other.month, other.day,
  92. other.hour, other.minute, other.second)
  93. def __lt__(self, other) -> bool:
  94. if type(other) is not datetime:
  95. return NotImplemented
  96. return (self.year, self.month, self.day, self.hour, self.minute, self.second) <\
  97. (other.year, other.month, other.day,
  98. other.hour, other.minute, other.second)
  99. def __le__(self, other) -> bool:
  100. if type(other) is not datetime:
  101. return NotImplemented
  102. return (self.year, self.month, self.day, self.hour, self.minute, self.second) <=\
  103. (other.year, other.month, other.day,
  104. other.hour, other.minute, other.second)
  105. def __gt__(self, other) -> bool:
  106. if type(other) is not datetime:
  107. return NotImplemented
  108. return (self.year, self.month, self.day, self.hour, self.minute, self.second) >\
  109. (other.year, other.month, other.day,
  110. other.hour, other.minute, other.second)
  111. def __ge__(self, other) -> bool:
  112. if type(other) is not datetime:
  113. return NotImplemented
  114. return (self.year, self.month, self.day, self.hour, self.minute, self.second) >=\
  115. (other.year, other.month, other.day,
  116. other.hour, other.minute, other.second)
  117. def timestamp(self) -> float:
  118. raise NotImplementedError