From a140040f56543fa7dba6ea9fda4157e296ce61ff Mon Sep 17 00:00:00 2001 From: kingzeus Date: Sat, 14 Dec 2024 11:44:24 +0800 Subject: [PATCH] =?UTF-8?q?feat(kz=5Fdash):=20=E9=80=9A=E7=94=A8=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E8=BF=81=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitmodules | 3 + kz_dash | 1 + models/database.py | 2 +- pages/account/table.py | 2 +- pages/task/task_detail.py | 2 +- pages/task/task_utils.py | 2 +- scheduler/tasks/fund_nav.py | 2 +- scheduler/tasks/sync_fund_list.py | 2 +- scheduler/tasks/sync_fund_nav.py | 2 +- tests/test_datetime.py | 2 +- utils/datetime_helper.py | 171 ------------------------------ 11 files changed, 12 insertions(+), 179 deletions(-) create mode 100644 .gitmodules create mode 160000 kz_dash delete mode 100644 utils/datetime_helper.py diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..9da420d --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "kz_dash"] + path = kz_dash + url = git@192.168.10.51:dash/kz_dash.git diff --git a/kz_dash b/kz_dash new file mode 160000 index 0000000..13866c6 --- /dev/null +++ b/kz_dash @@ -0,0 +1 @@ +Subproject commit 13866c6c7e805ef031b52a939cdffa7e90ec49a8 diff --git a/models/database.py b/models/database.py index ddf8af4..b1d2b09 100644 --- a/models/database.py +++ b/models/database.py @@ -5,7 +5,7 @@ from playhouse.shortcuts import update_model_from_dict -from utils.datetime_helper import format_datetime +from kz_dash.utility.datetime_helper import format_datetime from utils.string_helper import get_uuid from .account import ModelAccount, ModelPortfolio diff --git a/pages/account/table.py b/pages/account/table.py index 5f59cbc..d8a15b1 100644 --- a/pages/account/table.py +++ b/pages/account/table.py @@ -7,7 +7,7 @@ from models.account import ModelAccount, ModelPortfolio from models.database import get_record, get_record_list -from utils.datetime_helper import format_datetime +from kz_dash.utility.datetime_helper import format_datetime from .utils import create_operation_buttons diff --git a/pages/task/task_detail.py b/pages/task/task_detail.py index 49920cc..53c5539 100644 --- a/pages/task/task_detail.py +++ b/pages/task/task_detail.py @@ -27,7 +27,7 @@ ) from scheduler.job_manager import JobManager from scheduler.tasks import TaskStatus -from utils.datetime_helper import format_datetime +from kz_dash.utility.datetime_helper import format_datetime from utils.string_helper import json_str_to_dict logger = logging.getLogger(__name__) diff --git a/pages/task/task_utils.py b/pages/task/task_utils.py index 11dda1c..0883bef 100644 --- a/pages/task/task_utils.py +++ b/pages/task/task_utils.py @@ -17,7 +17,7 @@ from models.database import get_record_count, get_record_list from models.task import ModelTask from scheduler.job_manager import JobManager, TaskStatus -from utils.datetime_helper import format_datetime +from kz_dash.utility.datetime_helper import format_datetime # ============= 状态常量 ============= STATUS_LABELS = { diff --git a/scheduler/tasks/fund_nav.py b/scheduler/tasks/fund_nav.py index e024b9d..7cbd08a 100644 --- a/scheduler/tasks/fund_nav.py +++ b/scheduler/tasks/fund_nav.py @@ -5,7 +5,7 @@ from data_source.proxy import DataSourceProxy from models.database import update_record from models.fund import ModelFundNav -from utils.datetime_helper import get_date_str_after_days, get_days_between_dates +from kz_dash.utility.datetime_helper import get_date_str_after_days, get_days_between_dates from .base import PARAM_FUND_CODE, BaseTask diff --git a/scheduler/tasks/sync_fund_list.py b/scheduler/tasks/sync_fund_list.py index d5b8a8d..a9a1f54 100644 --- a/scheduler/tasks/sync_fund_list.py +++ b/scheduler/tasks/sync_fund_list.py @@ -6,7 +6,7 @@ from models.database import get_record from models.fund import ModelFund from scheduler.tasks.task_factory import TaskFactory -from utils.datetime_helper import get_date_str_after_days, get_days_between_dates +from kz_dash.utility.datetime_helper import get_date_str_after_days, get_days_between_dates from .base import PARAM_FUND_TYPE, BaseTask diff --git a/scheduler/tasks/sync_fund_nav.py b/scheduler/tasks/sync_fund_nav.py index 9d64276..254c938 100644 --- a/scheduler/tasks/sync_fund_nav.py +++ b/scheduler/tasks/sync_fund_nav.py @@ -7,7 +7,7 @@ from models.database import get_record from models.fund import ModelFund from scheduler.tasks.task_factory import TaskFactory -from utils.datetime_helper import get_date_str_after_days, get_days_between_dates +from kz_dash.utility.datetime_helper import get_date_str_after_days, get_days_between_dates from .base import PARAM_FUND_CODE, PARAM_SUB_TASK_DELAY, BaseTask diff --git a/tests/test_datetime.py b/tests/test_datetime.py index 0d4cc66..d5095cf 100644 --- a/tests/test_datetime.py +++ b/tests/test_datetime.py @@ -1,7 +1,7 @@ import unittest from datetime import date, datetime -from utils.datetime_helper import format_date, format_datetime +from kz_dash.utility.datetime_helper import format_date, format_datetime class TestDatetimeFunctions(unittest.TestCase): diff --git a/utils/datetime_helper.py b/utils/datetime_helper.py deleted file mode 100644 index 6ae4595..0000000 --- a/utils/datetime_helper.py +++ /dev/null @@ -1,171 +0,0 @@ -import logging -from datetime import date, datetime, timedelta -from typing import Optional, Union - -logger = logging.getLogger(__name__) - - -def format_datetime( - dt: Union[str, datetime, None], - output_format: str = "%Y-%m-%d %H:%M:%S", - default: str = "未知时间", -) -> str: - """ - 统一的日期时间格式化函数 - - Args: - dt: 要格式化的日期时间,可以是datetime对象或ISO格式的字符串 - output_format: 输出格式,默认为 "%Y-%m-%d %H:%M:%S" - default: 当无法格式化时返回的默认值 - - Returns: - 格式化后的时间字符串 - """ - if not dt: - return default - - try: - if isinstance(dt, str): - dt = datetime.fromisoformat(dt) - if isinstance(dt, datetime): - return dt.strftime(output_format) - except (ValueError, TypeError): - pass - - return default - - -def format_date( - dt: Union[str, date, None], - output_format: str = "%Y-%m-%d", - input_format: Optional[str] = None, - default: str = "未知日期", -) -> str: - """ - 统一的日期格式化函数 - - Args: - dt: 要格式化的日期,可以是date对象、ISO格式字符串或指定格式的日期字符串 - output_format: 输出格式,默认为 "%Y-%m-%d" - input_format: 输入日期字符串的格式,如果提供则使用strptime解析 - default: 当无法格式化时返回的默认值 - - Returns: - 格式化后的时间字符串 - """ - if not dt: - return default - - try: - if isinstance(dt, str): - if input_format: - dt = datetime.strptime(dt.strip(), input_format).date() - else: - dt = date.fromisoformat(dt.strip()) - if isinstance(dt, date): - return dt.strftime(output_format) - except (ValueError, TypeError): - pass - - return default - - -def get_timestamp() -> int: - """获取当前时间戳 - - 返回当前时间的Unix时间戳(从1970年1月1日UTC零点开始的秒数)。 - 时间戳为整数,精确到秒。 - - Returns: - int: 当前时间的Unix时间戳 - - Examples: - >>> get_timestamp() - 1709251200 # 2024-03-01 00:00:00 UTC - """ - return int(datetime.now().timestamp()) - - -def get_timestamp_ms() -> int: - """获取当前时间戳(毫秒) - - 返回当前时间的Unix时间戳(从1970年1月1日UTC零点开始的毫秒数)。 - 时间戳为整数,精确到毫秒。 - - Returns: - int: 当前时间的Unix时间戳(毫秒) - - Examples: - >>> get_timestamp_ms() - 1709251200000 # 2024-03-01 00:00:00 UTC - """ - return int(datetime.now().timestamp() * 1000) - - -def get_date_str_after_days(start_date: Union[str, date], days: int) -> str: - """获取开始日期后几天的日期字符串""" - return format_date(get_date_after_days(start_date, days)) - - -def get_days_between_dates(start_date: Union[str, date], end_date: Union[str, date]) -> int: - """计算两个日期之间的天数差值 - - Args: - start_date: 开始日期,可以是date对象或ISO格式字符串 - end_date: 结束日期,可以是date对象或ISO格式字符串 - - Returns: - int: 两个日期之间的天数差值(end_date - start_date) - - Examples: - >>> get_days_between_dates('2024-03-01', '2024-03-02') - 1 - >>> get_days_between_dates(date(2024, 3, 1), date(2024, 2, 29)) - -1 - """ - try: - if isinstance(start_date, str): - start_date = date.fromisoformat(start_date.strip()) - if isinstance(end_date, str): - end_date = date.fromisoformat(end_date.strip()) - - if not isinstance(start_date, date) or not isinstance(end_date, date): - logger.error("无效的日期格式: start_date=%s, end_date=%s", start_date, end_date) - raise ValueError("无效的日期格式") - - return (end_date - start_date).days - - except (ValueError, TypeError) as e: - logger.error("计算日期差值失败: %s", str(e)) - raise - - -def get_date_after_days(start_date: Union[str, date], days: int) -> date: - """获取开始日期后几天的日期 - - Args: - start_date: 开始日期,可以是date对象或ISO格式字符串 - days: 天数,正数表示往后,负数表示往前 - - Returns: - date: 计算后的日期 - - Examples: - >>> get_date_after_days('2024-03-01', 1) - datetime.date(2024, 3, 2) - >>> get_date_after_days(date(2024, 3, 1), -1) - datetime.date(2024, 2, 29) - """ - try: - if isinstance(start_date, str): - start_date = date.fromisoformat(start_date.strip()) - - if not isinstance(start_date, date): - logger.error("无效的日期格式: %s", start_date) - raise ValueError("无效的日期格式") - - return start_date + timedelta(days=days) - - except (ValueError, TypeError) as e: - logger.error("计算日期失败: %s", str(e)) - raise