forked from Lesin/reposync
调整时间格式
This commit is contained in:
parent
fe381c3481
commit
b8091f0f05
|
@ -2,7 +2,7 @@ import re
|
|||
from typing import List, Union, Optional, Dict
|
||||
from .service import Service
|
||||
from src.utils import base
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timedelta
|
||||
from src.dao.sync_config import SyncBranchDAO, SyncRepoDAO, LogDAO
|
||||
from src.dto.sync_config import SyncBranchDTO, SyncRepoDTO, RepoDTO, AllRepoDTO, GetBranchDTO, LogDTO, BranchDTO, ModifyRepoDTO
|
||||
from src.do.sync_config import SyncDirect, SyncType
|
||||
|
@ -156,6 +156,13 @@ class SyncService(Service):
|
|||
return SYNCException(Status.SUCCESS)
|
||||
|
||||
|
||||
def time_trans(original_time_str):
|
||||
original_time = datetime.strptime(original_time_str, '%Y-%m-%d %H:%M:%S')
|
||||
new_time = original_time + timedelta(hours=8)
|
||||
new_time_str = new_time.strftime('%Y-%m-%d %H:%M:%S')
|
||||
return new_time_str
|
||||
|
||||
|
||||
class LogService(Service):
|
||||
def __init__(self) -> None:
|
||||
self.sync_log_dao = LogDAO()
|
||||
|
@ -169,6 +176,8 @@ class LogService(Service):
|
|||
log_.write(log_content)
|
||||
# 使用正则表达式匹配所有的日期和时间格式
|
||||
timestamps = re.findall(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}', log_content)
|
||||
# first_time = time_trans(timestamps[0]) if timestamps else datetime.now()
|
||||
# last_time = time_trans(timestamps[-1]) if timestamps else datetime.now()
|
||||
first_time = timestamps[0] if timestamps else datetime.now()
|
||||
last_time = timestamps[-1] if timestamps else datetime.now()
|
||||
await self.sync_log_dao.insert_sync_repo_log(repo_name=repo_name, direct=direct, log_content=log_content,
|
||||
|
@ -189,6 +198,8 @@ class LogService(Service):
|
|||
log_.write(log_content)
|
||||
# 使用正则表达式匹配所有的日期和时间格式
|
||||
timestamps = re.findall(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}', log_content)
|
||||
# first_time = time_trans(timestamps[0]) if timestamps else datetime.now()
|
||||
# last_time = time_trans(timestamps[-1]) if timestamps else datetime.now()
|
||||
first_time = timestamps[0] if timestamps else datetime.now()
|
||||
last_time = timestamps[-1] if timestamps else datetime.now()
|
||||
await self.sync_log_dao.insert_branch_log(repo_name, direct, branch_id, commit_id,
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import os
|
||||
import time
|
||||
import logging
|
||||
from datetime import datetime, timezone, timedelta
|
||||
|
||||
basedir = os.path.dirname(os.path.dirname(
|
||||
os.path.dirname(os.path.abspath(__file__))))
|
||||
|
@ -16,6 +17,9 @@ api_log_name = os.path.join(
|
|||
sync_log_name = os.path.join(
|
||||
log_path, f'{time.strftime("%Y-%m-%d")}_sync.log')
|
||||
|
||||
# 创建一个时区为UTC+8的timezone对象
|
||||
utc_plus_8_timezone = timezone(timedelta(hours=8))
|
||||
|
||||
|
||||
class LogType:
|
||||
INFO = 'info'
|
||||
|
@ -24,6 +28,14 @@ class LogType:
|
|||
DEBUG = "debug"
|
||||
|
||||
|
||||
# 自定义时间格式函数,不依赖于系统时区
|
||||
def time_formatter(timestamp):
|
||||
# 将时间戳转换为UTC+8时区的时间
|
||||
utc_time = datetime.fromtimestamp(timestamp, utc_plus_8_timezone)
|
||||
# 将datetime对象转换为time.struct_time
|
||||
return utc_time.timetuple()
|
||||
|
||||
|
||||
def sync_log(log_type: str, msg: str, log_name: str, user="robot"):
|
||||
name = os.path.join(log_path, log_name)
|
||||
# 创建一个输出到控制台的handler,并设置日志级别为INFO
|
||||
|
@ -33,6 +45,7 @@ def sync_log(log_type: str, msg: str, log_name: str, user="robot"):
|
|||
# 创建一个格式化器,指定日志格式
|
||||
formatter = logging.Formatter('%(asctime)s | %(levelname)s | %(op_name)s - %(message)s',
|
||||
datefmt='%Y-%m-%d %H:%M:%S')
|
||||
formatter.converter = time_formatter
|
||||
file_handler.setFormatter(formatter)
|
||||
|
||||
# 创建一个logger
|
||||
|
|
Loading…
Reference in New Issue