datetime_utils.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # -*- coding: utf-8 -*-
  2. # @Time : 2022/12/1 14:14
  3. # @Author : XuJiakai
  4. # @File : datetime_utils
  5. # @Software: PyCharm
  6. import time
  7. import datetime
  8. import re
  9. def get_ds():
  10. return time.strftime("%Y%m%d")
  11. def get_now():
  12. return datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  13. def datetime_format_transform(datetime_str: str, source_format: str, target_format: str):
  14. middle = datetime.datetime.strptime(datetime_str, source_format)
  15. return datetime.datetime.strftime(middle, target_format)
  16. pass
  17. _date_part_1 = re.compile('^\\d{4}年\\d{2}月\\d{2}日$')
  18. _date_part_2 = re.compile('^\\d{4}-\\d{2}-\\d{2}$')
  19. _datetime_part = re.compile('^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$')
  20. def datetime_format(input_time):
  21. if input_time is not None and _date_part_1.match(input_time):
  22. input_time = datetime_format_transform(input_time, '%Y年%m月%d日', "%Y-%m-%d %H:%M:%S")
  23. pass
  24. if input_time is not None and _date_part_2.match(input_time):
  25. input_time = datetime_format_transform(input_time, '%Y-%m-%d', "%Y-%m-%d %H:%M:%S")
  26. pass
  27. if input_time is not None and not _datetime_part.match(input_time):
  28. input_time = None
  29. pass
  30. return input_time
  31. pass
  32. if __name__ == '__main__':
  33. print(datetime_format('- - '))
  34. pass