date-parser-tool 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
__init__.py
ADDED
|
File without changes
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
date_parser_tool.py,sha256=HF7mkrxNyh9kWyXLefM2eIekdt4EztmOv2n3GCjSrk4,3667
|
|
3
|
+
date_parser_tool-0.0.1.dist-info/METADATA,sha256=u6A9soM7Q-vS54rCaVhFHzyNRnE2Unj9ebMeOXFRcQY,181
|
|
4
|
+
date_parser_tool-0.0.1.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
5
|
+
date_parser_tool-0.0.1.dist-info/top_level.txt,sha256=WRD12Agnlux1UtTJ9rnggUY5cwNt1Ox-YnHXiUIFTWI,26
|
|
6
|
+
date_parser_tool-0.0.1.dist-info/RECORD,,
|
date_parser_tool.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
from datetime import datetime, timedelta
|
|
2
|
+
from mcp.server.fastmcp import FastMCP
|
|
3
|
+
import json
|
|
4
|
+
|
|
5
|
+
mcp = FastMCP()
|
|
6
|
+
|
|
7
|
+
def ok(date: str):
|
|
8
|
+
return json.dumps({"success": True, "date": date}, ensure_ascii=False)
|
|
9
|
+
|
|
10
|
+
def err(code: str, msg: str):
|
|
11
|
+
return json.dumps({"success": False, "error": code, "message": msg}, ensure_ascii=False)
|
|
12
|
+
|
|
13
|
+
def get_week_start_date(month: int, week_num: int, year: int = None) -> str:
|
|
14
|
+
if year is None:
|
|
15
|
+
year = datetime.now().year
|
|
16
|
+
|
|
17
|
+
if not 1 <= month <= 12:
|
|
18
|
+
return err("INVALID_MONTH", "月份必须是1-12")
|
|
19
|
+
if week_num < 1:
|
|
20
|
+
return err("INVALID_WEEK", "周数必须大于等于1")
|
|
21
|
+
|
|
22
|
+
try:
|
|
23
|
+
first_day = datetime(year, month, 1)
|
|
24
|
+
days_to_monday = (0 - first_day.weekday()) % 7
|
|
25
|
+
first_monday = first_day + timedelta(days=days_to_monday)
|
|
26
|
+
|
|
27
|
+
if first_day.weekday() in [4, 5, 6]:
|
|
28
|
+
week1_start = first_monday
|
|
29
|
+
else:
|
|
30
|
+
week1_start = first_day
|
|
31
|
+
|
|
32
|
+
if week_num == 1:
|
|
33
|
+
target = week1_start
|
|
34
|
+
else:
|
|
35
|
+
# 规则:如果1W就是第一个周一 → 2W自动+7天,避免重复
|
|
36
|
+
if week1_start == first_monday:
|
|
37
|
+
target = first_monday + timedelta(days=(week_num - 1) * 7)
|
|
38
|
+
# 正常情况:2W = 第一个周一
|
|
39
|
+
else:
|
|
40
|
+
target = first_monday + timedelta(days=(week_num - 2) * 7)
|
|
41
|
+
|
|
42
|
+
if target.month != month:
|
|
43
|
+
return err("OUT_OF_RANGE", f"{month}月没有第{week_num}周")
|
|
44
|
+
|
|
45
|
+
return ok(target.strftime("%Y-%m-%d"))
|
|
46
|
+
|
|
47
|
+
except ValueError as e:
|
|
48
|
+
return err("VALUE_ERROR", str(e))
|
|
49
|
+
|
|
50
|
+
def iso_week_start(week_num: int, year: int = None) -> str:
|
|
51
|
+
if year is None:
|
|
52
|
+
year = datetime.today().year
|
|
53
|
+
|
|
54
|
+
try:
|
|
55
|
+
date = datetime.fromisocalendar(year, week_num, 1)
|
|
56
|
+
return ok(date.strftime("%Y-%m-%d"))
|
|
57
|
+
except ValueError:
|
|
58
|
+
return err("INVALID_ISO_WEEK", "ISO周数不合法")
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def month_day_date(month_day_str: str, year: int = None) -> str:
|
|
62
|
+
if year is None:
|
|
63
|
+
year = datetime.now().year
|
|
64
|
+
|
|
65
|
+
try:
|
|
66
|
+
month, day = month_day_str.split('/')
|
|
67
|
+
date = datetime(year, int(month), int(day))
|
|
68
|
+
return ok(date.strftime("%Y-%m-%d"))
|
|
69
|
+
except ValueError:
|
|
70
|
+
return err("INVALID_DATE", "月/日格式错误,例如 6/12")
|
|
71
|
+
|
|
72
|
+
@mcp.tool()
|
|
73
|
+
def date_parser_tool(input_str: str) -> str:
|
|
74
|
+
"""
|
|
75
|
+
智能解析日期字符串,返回标准YYYY-MM-DD格式
|
|
76
|
+
|
|
77
|
+
支持格式:
|
|
78
|
+
1. 自然月+周:4月1W → 2026-04-01
|
|
79
|
+
2. ISO周:W12 → 当年第12周周一
|
|
80
|
+
3. 月/日:4/23 → 当年4月23日
|
|
81
|
+
|
|
82
|
+
:param input_str:待解析的日期字符串,如:4月1W,W12,4/23
|
|
83
|
+
:return:
|
|
84
|
+
{
|
|
85
|
+
"success": true,
|
|
86
|
+
"date": "YYYY-MM-DD"
|
|
87
|
+
}
|
|
88
|
+
或
|
|
89
|
+
{
|
|
90
|
+
"success": false,
|
|
91
|
+
"error": "...",
|
|
92
|
+
"message": "..."
|
|
93
|
+
}
|
|
94
|
+
"""
|
|
95
|
+
try:
|
|
96
|
+
s = input_str.strip().upper()
|
|
97
|
+
s = s.replace(" ", "")
|
|
98
|
+
|
|
99
|
+
if "月" in s and "W" in s:
|
|
100
|
+
month = int(s.split("月")[0])
|
|
101
|
+
week = int(s.split("月")[1].replace("W", ""))
|
|
102
|
+
return get_week_start_date(month, week)
|
|
103
|
+
elif s.startswith("W"):
|
|
104
|
+
week_num = int(s[1:])
|
|
105
|
+
return iso_week_start(week_num)
|
|
106
|
+
elif "/" in s:
|
|
107
|
+
return month_day_date(s)
|
|
108
|
+
else:
|
|
109
|
+
return err("UNSUPPORTED_FORMAT", "不支持的日期格式")
|
|
110
|
+
except Exception as e:
|
|
111
|
+
return err("PARSE_ERROR", str(e))
|
|
112
|
+
|
|
113
|
+
if __name__ == '__main__':
|
|
114
|
+
mcp.run(transport='stdio')
|