datetime.py 5.4 KB

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