time_util.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. # -*- coding: utf-8 -*-
  2. import re
  3. from datetime import datetime
  4. from typing import Any
  5. class TimeUtil:
  6. """
  7. 时间格式化工具类
  8. """
  9. DEFAULT_DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'
  10. @classmethod
  11. def object_format_datetime(cls, obj: Any) -> Any:
  12. """
  13. 格式化对象中的 datetime 属性为默认字符串格式。
  14. 参数:
  15. - obj (Any): 输入对象。
  16. 返回:
  17. - Any: 格式化后的对象。
  18. """
  19. for attr in dir(obj):
  20. if not attr.startswith('_'): # 跳过私有属性
  21. value = getattr(obj, attr)
  22. if isinstance(value, datetime):
  23. setattr(obj, attr, value.strftime(cls.DEFAULT_DATETIME_FORMAT))
  24. return obj
  25. @classmethod
  26. def list_format_datetime(cls, lst: list[Any]) -> list[Any]:
  27. """
  28. 格式化列表内每个对象的 datetime 属性。
  29. 参数:
  30. - lst (List[Any]): 对象列表。
  31. 返回:
  32. - list[Any]: 格式化后的对象列表。
  33. """
  34. return [cls.object_format_datetime(obj) for obj in lst]
  35. @classmethod
  36. def format_datetime_dict_list(cls, dicts: list[dict]) -> list[dict]:
  37. """
  38. 递归格式化字典列表中的 datetime 值为默认字符串格式。
  39. 参数:
  40. - dicts (list[dict]): 字典列表。
  41. 返回:
  42. - list[dict]: 格式化后的字典列表。
  43. """
  44. def _format_value(value: Any) -> Any:
  45. if isinstance(value, dict):
  46. return {k: _format_value(v) for k, v in value.items()}
  47. elif isinstance(value, list):
  48. return [_format_value(item) for item in value]
  49. elif isinstance(value, datetime):
  50. return value.strftime(cls.DEFAULT_DATETIME_FORMAT)
  51. return value
  52. return [_format_value(item) for item in dicts]
  53. @classmethod
  54. def __valid_range(cls, search_str: str, start_range: int, end_range: int) -> bool:
  55. """
  56. 校验范围字符串是否合法。
  57. 参数:
  58. - search_str (str): 范围字符串(例如:"1-5")。
  59. - start_range (int): 允许的最小范围值。
  60. - end_range (int): 允许的最大范围值。
  61. 返回:
  62. - bool: 校验是否通过。
  63. """
  64. match = re.match(r'^(\d+)-(\d+)$', search_str)
  65. if match:
  66. start, end = int(match.group(1)), int(match.group(2))
  67. return start_range <= start < end <= end_range
  68. return False
  69. @classmethod
  70. def __valid_sum(cls, search_str: str, start_range_a: int, start_range_b: int, end_range_a: int, end_range_b: int, sum_range: int) -> bool:
  71. """
  72. 校验和字符串是否合法。
  73. 参数:
  74. - search_str (str): 和字符串(例如:"1/5")。
  75. - start_range_a (int): 允许的最小范围值A。
  76. - start_range_b (int): 允许的最大范围值A。
  77. - end_range_a (int): 允许的最小范围值B。
  78. - end_range_b (int): 允许的最大范围值B。
  79. - sum_range (int): 允许的最大和值。
  80. 返回:
  81. - bool: 校验是否通过。
  82. """
  83. match = re.match(r'^(\d+)/(\d+)$', search_str)
  84. if match:
  85. start, end = int(match.group(1)), int(match.group(2))
  86. return (
  87. start_range_a <= start <= start_range_b
  88. and end_range_a <= end <= end_range_b
  89. and start + end <= sum_range
  90. )
  91. return False
  92. @classmethod
  93. def validate_second_or_minute(cls, second_or_minute: str):
  94. """
  95. 校验秒或分钟字段的合法性。
  96. 参数:
  97. - second_or_minute (str): 秒或分钟值。
  98. 返回:
  99. - bool: 校验是否通过。
  100. """
  101. if (
  102. second_or_minute == '*'
  103. or ('-' in second_or_minute and cls.__valid_range(second_or_minute, 0, 59))
  104. or ('/' in second_or_minute and cls.__valid_sum(second_or_minute, 0, 58, 1, 59, 59))
  105. or re.match(r'^(?:[0-5]?\d|59)(?:,[0-5]?\d|59)*$', second_or_minute)
  106. ):
  107. return True
  108. return False
  109. @classmethod
  110. def validate_hour(cls, hour: str):
  111. """
  112. 校验小时字段的合法性。
  113. 参数:
  114. - hour (str): 小时值。
  115. 返回:
  116. - bool: 校验是否通过。
  117. """
  118. if (
  119. hour == '*'
  120. or ('-' in hour and cls.__valid_range(hour, 0, 23))
  121. or ('/' in hour and cls.__valid_sum(hour, 0, 22, 1, 23, 23))
  122. or re.match(r'^(?:0|[1-9]|1\d|2[0-3])(?:,(?:0|[1-9]|1\d|2[0-3]))*$', hour)
  123. ):
  124. return True
  125. return False
  126. @classmethod
  127. def validate_day(cls, day: str):
  128. """
  129. 校验日期字段的合法性。
  130. 参数:
  131. - day (str): 日值。
  132. 返回:
  133. - bool: 校验是否通过。
  134. """
  135. if (
  136. day in ['*', '?', 'L']
  137. or ('-' in day and cls.__valid_range(day, 1, 31))
  138. or ('/' in day and cls.__valid_sum(day, 1, 30, 1, 30, 31))
  139. or ('W' in day and re.match(r'^(?:[1-9]|1\d|2\d|3[01])W$', day))
  140. or re.match(r'^(?:0|[1-9]|1\d|2[0-9]|3[0-1])(?:,(?:0|[1-9]|1\d|2[0-9]|3[0-1]))*$', day)
  141. ):
  142. return True
  143. return False
  144. @classmethod
  145. def validate_month(cls, month: str):
  146. """
  147. 校验月份字段的合法性。
  148. 参数:
  149. - month (str): 月值。
  150. 返回:
  151. - bool: 校验是否通过。
  152. """
  153. if (
  154. month == '*'
  155. or ('-' in month and cls.__valid_range(month, 1, 12))
  156. or ('/' in month and cls.__valid_sum(month, 1, 11, 1, 11, 12))
  157. or re.match(r'^(?:0|[1-9]|1[0-2])(?:,(?:0|[1-9]|1[0-2]))*$', month)
  158. ):
  159. return True
  160. return False
  161. @classmethod
  162. def validate_week(cls, week: str):
  163. """
  164. 校验星期字段的合法性。
  165. 参数:
  166. - week (str): 周值。
  167. 返回:
  168. - bool: 校验是否通过。
  169. """
  170. if (
  171. week in ['*', '?']
  172. or ('-' in week and cls.__valid_range(week, 1, 7))
  173. or ('#' in week and re.match(r'^[1-7]#[1-4]$', week))
  174. or ('L' in week and re.match(r'^[1-7]L$', week))
  175. or re.match(r'^[1-7](?:(,[1-7]))*$', week)
  176. ):
  177. return True
  178. return False
  179. @classmethod
  180. def validate_year(cls, year: str):
  181. """
  182. 校验年份字段的合法性。
  183. 参数:
  184. - year (str): 年值。
  185. 返回:
  186. - bool: 校验是否通过。
  187. """
  188. current_year = int(datetime.now().year)
  189. future_years = [current_year + i for i in range(9)]
  190. if (
  191. year == '*'
  192. or ('-' in year and cls.__valid_range(year, current_year, 2099))
  193. or ('/' in year and cls.__valid_sum(year, current_year, 2098, 1, 2099 - current_year, 2099))
  194. or ('#' in year and re.match(r'^[1-7]#[1-4]$', year))
  195. or ('L' in year and re.match(r'^[1-7]L$', year))
  196. or (
  197. (len(year) == 4 or ',' in year)
  198. and all(int(item) in future_years and current_year <= int(item) <= 2099 for item in year.split(','))
  199. )
  200. ):
  201. return True
  202. return False
  203. @classmethod
  204. def validate_cron_expression(cls, cron_expression: str):
  205. """
  206. 校验 Cron 表达式是否正确。
  207. * * * * * *
  208. | | | | | |
  209. | | | | | +--- 星期(0-7,0和7都表示星期日)
  210. | | | | +----- 月份(1-12)
  211. | | | +------- 日期(1-31)
  212. | | +--------- 小时(0-23)
  213. | +----------- 分钟(0-59)
  214. +------------- 秒(0-59),部分环境不支持秒字段。
  215. 参数:
  216. - cron_expression (str): Cron 表达式。
  217. 返回:
  218. - bool: 校验是否通过。
  219. """
  220. values = cron_expression.split()
  221. if len(values) != 6 and len(values) != 7:
  222. return False
  223. second_validation = cls.validate_second_or_minute(values[0])
  224. minute_validation = cls.validate_second_or_minute(values[1])
  225. hour_validation = cls.validate_hour(values[2])
  226. day_validation = cls.validate_day(values[3])
  227. month_validation = cls.validate_month(values[4])
  228. week_validation = cls.validate_week(values[5])
  229. validation = (
  230. second_validation
  231. and minute_validation
  232. and hour_validation
  233. and day_validation
  234. and month_validation
  235. and week_validation
  236. )
  237. if len(values) == 6:
  238. return validation
  239. if len(values) == 7:
  240. year_validation = cls.validate_year(values[6])
  241. return validation and year_validation