Ver Fonte

feat: 添加时间统一格式化

许家凯 há 2 anos atrás
pai
commit
0613667e97
2 ficheiros alterados com 26 adições e 7 exclusões
  1. 3 7
      spider/cpa_agg.py
  2. 23 0
      utils/datetime_utils.py

+ 3 - 7
spider/cpa_agg.py

@@ -5,7 +5,7 @@
 # @Software: PyCharm
 import json
 import time
-
+from utils.datetime_utils import datetime_format
 from log import get_log
 from sdk.WinhcAllClient import get_all_client
 from utils.datetime_utils import get_ds, get_now, datetime_format_transform
@@ -90,17 +90,13 @@ def data_transform(data: list):
 
         winhc_dim_num = json_path(winhc_data, '$.summary.' + i)
 
-        if latest_date_max is not None and date_part.match(latest_date_max):
-            latest_date_max = datetime_format_transform(latest_date_max, '%Y年%m月%d日', "%Y-%m-%d %H:%M:%S")
-            pass
+        latest_date_max = datetime_format(latest_date_max)
 
         winhc_dim_date = json_path(winhc_data, '$.latest_date.' + i)
         if winhc_dim_date is not None and winhc_dim_date == '':
             winhc_dim_date = None
 
-        if winhc_dim_date is not None and date_part.match(winhc_dim_date):
-            winhc_dim_date = datetime_format_transform(winhc_dim_date, '%Y年%m月%d日', "%Y-%m-%d %H:%M:%S")
-            pass
+        winhc_dim_date = datetime_format(winhc_dim_date)
 
         if (latest_date_max is None or latest_date_max == '') and (
                 summary_max is None or summary_max == 0) and winhc_dim_date is None and (

+ 23 - 0
utils/datetime_utils.py

@@ -5,6 +5,7 @@
 # @Software: PyCharm
 import time
 import datetime
+import re
 
 
 def get_ds():
@@ -21,5 +22,27 @@ def datetime_format_transform(datetime_str: str, source_format: str, target_form
     pass
 
 
+_date_part_1 = re.compile('^\\d{4}年\\d{2}月\\d{2}日$')
+_date_part_2 = re.compile('^\\d{4}-\\d{2}-\\d{2}$')
+_datetime_part = re.compile('^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$')
+
+
+def datetime_format(input_time):
+    if input_time is not None and _date_part_1.match(input_time):
+        input_time = datetime_format_transform(input_time, '%Y年%m月%d日', "%Y-%m-%d %H:%M:%S")
+        pass
+
+    if input_time is not None and _date_part_2.match(input_time):
+        input_time = datetime_format_transform(input_time, '%Y-%m-%d', "%Y-%m-%d %H:%M:%S")
+        pass
+
+    if input_time is not None and not _datetime_part.match(input_time):
+        input_time = None
+        pass
+    return input_time
+    pass
+
+
 if __name__ == '__main__':
+    print(datetime_format('- - '))
     pass