date.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. from datetime import datetime, timedelta
  2. class Date:
  3. def __init__(self, date_input):
  4. formats = [
  5. "%Y-%m-%d %H:%M:%S",
  6. "%Y-%m-%d %H:%M:%S.%f %z %Z",
  7. "%Y-%m-%dT%H:%M:%S%z",
  8. "%Y-%m-%dT%H:%M:%SZ",
  9. "%Y-%m-%dT%H:%M:%S",
  10. "%Y-%m-%dT%H:%M:%S.%f",
  11. ]
  12. self.date = None
  13. for format in formats:
  14. try:
  15. self.date = datetime.strptime(date_input, format)
  16. break
  17. except ValueError:
  18. continue
  19. if self.date is None:
  20. raise ValueError(f"unable to parse date: {date_input}")
  21. def strftime(self, layout):
  22. layout = layout.replace("yyyy", "%Y")
  23. layout = layout.replace("MM", "%m")
  24. layout = layout.replace("dd", "%d")
  25. layout = layout.replace("hh", "%H")
  26. layout = layout.replace("mm", "%M")
  27. layout = layout.replace("ss", "%S")
  28. layout = layout.replace("a", "%p")
  29. layout = layout.replace("EEEE", "%A")
  30. layout = layout.replace("E", "%a")
  31. return self.date.strftime(layout)
  32. def timestamp(self):
  33. return int(self.date.timestamp())
  34. def sub(self, unit, amount):
  35. if unit in ["second", "seconds"]:
  36. return Date((self.date - timedelta(seconds=amount)).isoformat())
  37. elif unit in ["minute", "minutes"]:
  38. return Date((self.date - timedelta(minutes=amount)).isoformat())
  39. elif unit in ["hour", "hours"]:
  40. return Date((self.date - timedelta(hours=amount)).isoformat())
  41. elif unit in ["day", "days"]:
  42. return Date((self.date - timedelta(days=amount)).isoformat())
  43. elif unit in ["week", "weeks"]:
  44. return Date((self.date - timedelta(weeks=amount)).isoformat())
  45. elif unit in ["month", "months"]:
  46. return Date((self.date.replace(month=self.date.month - amount)).isoformat())
  47. elif unit in ["year", "years"]:
  48. return Date((self.date.replace(year=self.date.year - amount)).isoformat())
  49. def add(self, unit, amount):
  50. if unit in ["second", "seconds"]:
  51. return Date((self.date + timedelta(seconds=amount)).isoformat())
  52. elif unit in ["minute", "minutes"]:
  53. return Date((self.date + timedelta(minutes=amount)).isoformat())
  54. elif unit in ["hour", "hours"]:
  55. return Date((self.date + timedelta(hours=amount)).isoformat())
  56. elif unit in ["day", "days"]:
  57. return Date((self.date + timedelta(days=amount)).isoformat())
  58. elif unit in ["week", "weeks"]:
  59. return Date((self.date + timedelta(weeks=amount)).isoformat())
  60. elif unit in ["month", "months"]:
  61. new_month = self.date.month + amount
  62. new_year = self.date.year + (new_month - 1) // 12
  63. new_month = (new_month - 1) % 12 + 1
  64. if self.date.day > 28:
  65. if new_month == 2:
  66. new_day = min(self.date.day, 29)
  67. if new_day == 29 and not (new_year % 4 == 0 and (new_year % 100 != 0 or new_year % 400 == 0)):
  68. new_day = 28
  69. else:
  70. new_day = min(self.date.day, [31, 30][new_month % 2])
  71. else:
  72. new_day = self.date.day
  73. new_date = self.date.replace(year=new_year, month=new_month, day=new_day)
  74. return Date(new_date.isoformat())
  75. elif unit in ["year", "years"]:
  76. return Date((self.date.replace(year=self.date.year + amount)).isoformat())
  77. def diff(self, unit, diff_date):
  78. if unit in ["second", "seconds"]:
  79. return int((self.date - diff_date.date).total_seconds())
  80. elif unit in ["minute", "minutes"]:
  81. return int((self.date - diff_date.date).total_seconds() / 60)
  82. elif unit in ["hour", "hours"]:
  83. return int((self.date - diff_date.date).total_seconds() / 3600)
  84. elif unit in ["day", "days"]:
  85. return int((self.date - diff_date.date).total_seconds() / (3600 * 24))
  86. elif unit in ["week", "weeks"]:
  87. return int((self.date - diff_date.date).total_seconds() / (3600 * 24 * 7))
  88. elif unit in ["month", "months"]:
  89. return (self.date.year - diff_date.date.year) * 12 + (self.date.month - diff_date.date.month)
  90. elif unit in ["year", "years"]:
  91. return self.date.year - diff_date.date.year
  92. def hour(self):
  93. return self.date.hour
  94. def minute(self):
  95. return self.date.minute
  96. def second(self):
  97. return self.date.second
  98. def month(self):
  99. return self.date.month
  100. def day_of_month(self):
  101. return self.date.day
  102. def day_of_week(self):
  103. weekday = self.date.weekday() + 1 # Monday is 0 in python, so
  104. return weekday % 7 or 7 # Convert to Sunday = 7
  105. def week_of_year(self):
  106. return self.date.isocalendar()[1]
  107. def year(self):
  108. return self.date.year
  109. def UTC(self):
  110. return self.date.strftime("%Y-%m-%d %H:%M:%S.%f %z %Z")