datetime.py 4.1 KB

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