staran 1.0.7__py3-none-any.whl → 1.0.9__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.
- staran/date/__init__.py +62 -2
- staran/date/core.py +926 -9
- staran/date/examples/v108_features_demo.py +257 -0
- staran/date/examples/v109_features_demo.py +302 -0
- staran/date/i18n.py +376 -0
- staran/date/lunar.py +320 -0
- staran/date/tests/run_tests.py +77 -6
- staran/date/tests/test_v108_features.py +400 -0
- staran/date/tests/test_v109_features.py +316 -0
- staran-1.0.9.dist-info/METADATA +214 -0
- staran-1.0.9.dist-info/RECORD +23 -0
- staran-1.0.7.dist-info/METADATA +0 -306
- staran-1.0.7.dist-info/RECORD +0 -17
- {staran-1.0.7.dist-info → staran-1.0.9.dist-info}/WHEEL +0 -0
- {staran-1.0.7.dist-info → staran-1.0.9.dist-info}/licenses/LICENSE +0 -0
- {staran-1.0.7.dist-info → staran-1.0.9.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,257 @@
|
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
|
4
|
+
"""
|
5
|
+
Staran v1.0.8 新功能演示
|
6
|
+
=======================
|
7
|
+
|
8
|
+
演示农历支持和多语言功能的使用方法。
|
9
|
+
"""
|
10
|
+
|
11
|
+
import sys
|
12
|
+
import os
|
13
|
+
|
14
|
+
# 添加项目根目录到路径
|
15
|
+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..', '..'))
|
16
|
+
|
17
|
+
from staran.date import Date, from_lunar, set_language, get_language
|
18
|
+
from staran.date.lunar import LunarDate
|
19
|
+
from staran.date.i18n import Language
|
20
|
+
|
21
|
+
def demo_lunar_features():
|
22
|
+
"""演示农历功能"""
|
23
|
+
print("🌙 农历功能演示")
|
24
|
+
print("=" * 50)
|
25
|
+
|
26
|
+
# 1. 从农历日期创建公历日期
|
27
|
+
print("1. 从农历日期创建公历日期")
|
28
|
+
lunar_new_year = Date.from_lunar(2025, 1, 1) # 农历2025年正月初一
|
29
|
+
print(f" 农历2025年正月初一 → {lunar_new_year.format_iso()}")
|
30
|
+
|
31
|
+
mid_autumn = Date.from_lunar(2025, 8, 15) # 农历中秋节
|
32
|
+
print(f" 农历2025年八月十五 → {mid_autumn.format_iso()}")
|
33
|
+
|
34
|
+
# 2. 从农历字符串创建
|
35
|
+
print("\n2. 从农历字符串创建")
|
36
|
+
date_from_str = Date.from_lunar_string("20250315") # 农历三月十五
|
37
|
+
print(f" '20250315' → {date_from_str.format_iso()}")
|
38
|
+
|
39
|
+
# 闰月示例(假设2025年有闰月)
|
40
|
+
try:
|
41
|
+
leap_date = Date.from_lunar_string("2025闰0415") # 农历闰四月十五
|
42
|
+
print(f" '2025闰0415' → {leap_date.format_iso()}")
|
43
|
+
except:
|
44
|
+
print(f" 闰月示例跳过(2025年可能无闰四月)")
|
45
|
+
|
46
|
+
# 3. 公历转农历
|
47
|
+
print("\n3. 公历转农历")
|
48
|
+
solar_date = Date("20250415")
|
49
|
+
lunar = solar_date.to_lunar()
|
50
|
+
print(f" {solar_date.format_iso()} → {lunar.format_chinese()}")
|
51
|
+
print(f" 紧凑格式: {lunar.format_compact()}")
|
52
|
+
print(f" 天干地支: {lunar.get_ganzhi_year()}")
|
53
|
+
print(f" 生肖: {lunar.get_zodiac()}")
|
54
|
+
|
55
|
+
# 4. 农历格式化
|
56
|
+
print("\n4. 农历格式化")
|
57
|
+
test_date = Date("20250129") # 接近春节的日期
|
58
|
+
print(f" 基本格式: {test_date.format_lunar()}")
|
59
|
+
print(f" 包含生肖: {test_date.format_lunar(include_zodiac=True)}")
|
60
|
+
print(f" 紧凑格式: {test_date.format_lunar_compact()}")
|
61
|
+
|
62
|
+
# 5. 农历判断
|
63
|
+
print("\n5. 农历判断")
|
64
|
+
spring_festival = Date.from_lunar(2025, 1, 1)
|
65
|
+
print(f" 农历正月初一:")
|
66
|
+
print(f" 是否农历新年: {spring_festival.is_lunar_new_year()}")
|
67
|
+
print(f" 是否农历月初: {spring_festival.is_lunar_month_start()}")
|
68
|
+
print(f" 是否农历月中: {spring_festival.is_lunar_month_mid()}")
|
69
|
+
|
70
|
+
lantern_festival = Date.from_lunar(2025, 1, 15)
|
71
|
+
print(f" 农历正月十五:")
|
72
|
+
print(f" 是否农历新年: {lantern_festival.is_lunar_new_year()}")
|
73
|
+
print(f" 是否农历月中: {lantern_festival.is_lunar_month_mid()}")
|
74
|
+
|
75
|
+
# 6. 农历比较
|
76
|
+
print("\n6. 农历比较")
|
77
|
+
date1 = Date.from_lunar(2025, 1, 1)
|
78
|
+
date2 = Date.from_lunar(2025, 1, 15)
|
79
|
+
print(f" {date1.format_lunar()} vs {date2.format_lunar()}")
|
80
|
+
print(f" 比较结果: {date1.compare_lunar(date2)}") # -1: 前者小于后者
|
81
|
+
print(f" 是否同月: {date1.is_same_lunar_month(date2)}")
|
82
|
+
print(f" 是否同日: {date1.is_same_lunar_day(date2)}")
|
83
|
+
|
84
|
+
|
85
|
+
def demo_multilanguage_features():
|
86
|
+
"""演示多语言功能"""
|
87
|
+
print("\n\n🌍 多语言功能演示")
|
88
|
+
print("=" * 50)
|
89
|
+
|
90
|
+
# 测试日期
|
91
|
+
test_date = Date("20250415") # 2025年4月15日,星期二
|
92
|
+
|
93
|
+
print(f"测试日期: {test_date.format_iso()}")
|
94
|
+
|
95
|
+
# 1. 全局语言设置
|
96
|
+
print("\n1. 全局语言设置")
|
97
|
+
print(f" 当前语言: {get_language()}")
|
98
|
+
print(f" 支持的语言: {Date.get_supported_languages()}")
|
99
|
+
|
100
|
+
# 2. 中文简体
|
101
|
+
print("\n2. 中文简体 (zh_CN)")
|
102
|
+
set_language('zh_CN')
|
103
|
+
print(f" 本地化格式: {test_date.format_localized()}")
|
104
|
+
print(f" 星期格式: {test_date.format_weekday_localized()}")
|
105
|
+
print(f" 月份格式: {test_date.format_month_localized()}")
|
106
|
+
print(f" 季度格式: {test_date.format_quarter_localized()}")
|
107
|
+
print(f" 相对时间: {test_date.format_relative_localized()}")
|
108
|
+
|
109
|
+
# 3. 中文繁体
|
110
|
+
print("\n3. 中文繁体 (zh_TW)")
|
111
|
+
set_language('zh_TW')
|
112
|
+
print(f" 本地化格式: {test_date.format_localized()}")
|
113
|
+
print(f" 星期格式: {test_date.format_weekday_localized()}")
|
114
|
+
print(f" 相对时间: {test_date.format_relative_localized()}")
|
115
|
+
|
116
|
+
# 4. 日语
|
117
|
+
print("\n4. 日语 (ja_JP)")
|
118
|
+
set_language('ja_JP')
|
119
|
+
print(f" 本地化格式: {test_date.format_localized()}")
|
120
|
+
print(f" 星期格式: {test_date.format_weekday_localized()}")
|
121
|
+
print(f" 星期短格式: {test_date.format_weekday_localized(short=True)}")
|
122
|
+
print(f" 月份格式: {test_date.format_month_localized()}")
|
123
|
+
print(f" 相对时间: {test_date.format_relative_localized()}")
|
124
|
+
|
125
|
+
# 5. 英语
|
126
|
+
print("\n5. 英语 (en_US)")
|
127
|
+
set_language('en_US')
|
128
|
+
print(f" 本地化格式: {test_date.format_localized()}")
|
129
|
+
print(f" 星期格式: {test_date.format_weekday_localized()}")
|
130
|
+
print(f" 月份格式: {test_date.format_month_localized()}")
|
131
|
+
print(f" 季度格式: {test_date.format_quarter_localized()}")
|
132
|
+
print(f" 相对时间: {test_date.format_relative_localized()}")
|
133
|
+
|
134
|
+
# 6. 单次覆盖演示
|
135
|
+
print("\n6. 单次语言覆盖")
|
136
|
+
set_language('zh_CN') # 设置全局为中文
|
137
|
+
print(f" 全局中文: {test_date.format_weekday_localized()}")
|
138
|
+
print(f" 单次英语: {test_date.format_weekday_localized(language_code='en_US')}")
|
139
|
+
print(f" 仍是中文: {test_date.format_weekday_localized()}")
|
140
|
+
|
141
|
+
# 7. 相对时间多语言演示
|
142
|
+
print("\n7. 相对时间多语言演示")
|
143
|
+
today = Date.today()
|
144
|
+
tomorrow = today.add_days(1)
|
145
|
+
yesterday = today.add_days(-1)
|
146
|
+
next_week = today.add_days(7)
|
147
|
+
|
148
|
+
languages = ['zh_CN', 'zh_TW', 'ja_JP', 'en_US']
|
149
|
+
|
150
|
+
for lang in languages:
|
151
|
+
set_language(lang)
|
152
|
+
lang_name = Date.get_supported_languages()[lang]
|
153
|
+
print(f" {lang_name}:")
|
154
|
+
print(f" 今天: {today.format_relative_localized()}")
|
155
|
+
print(f" 明天: {tomorrow.format_relative_localized()}")
|
156
|
+
print(f" 昨天: {yesterday.format_relative_localized()}")
|
157
|
+
print(f" 下周: {next_week.format_relative_localized()}")
|
158
|
+
|
159
|
+
|
160
|
+
def demo_combined_features():
|
161
|
+
"""演示农历和多语言组合功能"""
|
162
|
+
print("\n\n🔄 农历 + 多语言组合演示")
|
163
|
+
print("=" * 50)
|
164
|
+
|
165
|
+
# 中国传统节日
|
166
|
+
spring_festival = Date.from_lunar(2025, 1, 1) # 春节
|
167
|
+
mid_autumn = Date.from_lunar(2025, 8, 15) # 中秋
|
168
|
+
|
169
|
+
festivals = [
|
170
|
+
(spring_festival, "春节(农历正月初一)"),
|
171
|
+
(mid_autumn, "中秋节(农历八月十五)")
|
172
|
+
]
|
173
|
+
|
174
|
+
languages = ['zh_CN', 'zh_TW', 'ja_JP', 'en_US']
|
175
|
+
|
176
|
+
for date, festival_name in festivals:
|
177
|
+
print(f"\n{festival_name}")
|
178
|
+
print(f"公历日期: {date.format_iso()}")
|
179
|
+
|
180
|
+
for lang in languages:
|
181
|
+
set_language(lang)
|
182
|
+
lang_name = Date.get_supported_languages()[lang]
|
183
|
+
print(f" {lang_name}:")
|
184
|
+
print(f" 本地化: {date.format_localized()}")
|
185
|
+
print(f" 星期: {date.format_weekday_localized()}")
|
186
|
+
print(f" 农历: {date.format_lunar()}")
|
187
|
+
|
188
|
+
|
189
|
+
def demo_performance():
|
190
|
+
"""演示性能"""
|
191
|
+
print("\n\n⚡ 性能演示")
|
192
|
+
print("=" * 50)
|
193
|
+
|
194
|
+
import time
|
195
|
+
|
196
|
+
# 农历转换性能
|
197
|
+
start_time = time.time()
|
198
|
+
dates = []
|
199
|
+
for i in range(100):
|
200
|
+
date = Date.from_lunar(2025, 1, i % 29 + 1)
|
201
|
+
dates.append(date)
|
202
|
+
lunar_time = time.time() - start_time
|
203
|
+
print(f"创建100个农历日期: {lunar_time:.4f}秒")
|
204
|
+
|
205
|
+
# 格式化性能
|
206
|
+
start_time = time.time()
|
207
|
+
test_date = Date("20250415")
|
208
|
+
for i in range(1000):
|
209
|
+
formatted = test_date.format_localized()
|
210
|
+
format_time = time.time() - start_time
|
211
|
+
print(f"格式化1000次: {format_time:.4f}秒")
|
212
|
+
|
213
|
+
# 语言切换性能
|
214
|
+
start_time = time.time()
|
215
|
+
languages = ['zh_CN', 'zh_TW', 'ja_JP', 'en_US']
|
216
|
+
for i in range(100):
|
217
|
+
set_language(languages[i % 4])
|
218
|
+
switch_time = time.time() - start_time
|
219
|
+
print(f"语言切换100次: {switch_time:.4f}秒")
|
220
|
+
|
221
|
+
|
222
|
+
if __name__ == "__main__":
|
223
|
+
print("🚀 Staran v1.0.8 新功能完整演示")
|
224
|
+
print("=" * 60)
|
225
|
+
|
226
|
+
try:
|
227
|
+
# 演示农历功能
|
228
|
+
demo_lunar_features()
|
229
|
+
|
230
|
+
# 演示多语言功能
|
231
|
+
demo_multilanguage_features()
|
232
|
+
|
233
|
+
# 演示组合功能
|
234
|
+
demo_combined_features()
|
235
|
+
|
236
|
+
# 演示性能
|
237
|
+
demo_performance()
|
238
|
+
|
239
|
+
print("\n\n✅ 演示完成!")
|
240
|
+
print("=" * 60)
|
241
|
+
print("🌟 主要新功能:")
|
242
|
+
print(" • 农历与公历互转")
|
243
|
+
print(" • 农历日期格式化")
|
244
|
+
print(" • 农历日期比较和判断")
|
245
|
+
print(" • 中简、中繁、日、英四种语言支持")
|
246
|
+
print(" • 全局语言设置")
|
247
|
+
print(" • 单次使用语言覆盖")
|
248
|
+
print(" • 多语言本地化格式")
|
249
|
+
print(" • 120+ API方法,保持向后兼容")
|
250
|
+
|
251
|
+
except Exception as e:
|
252
|
+
print(f"\n❌ 演示过程中出现错误: {e}")
|
253
|
+
import traceback
|
254
|
+
traceback.print_exc()
|
255
|
+
|
256
|
+
# 恢复默认语言
|
257
|
+
set_language('zh_CN')
|
@@ -0,0 +1,302 @@
|
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
|
4
|
+
"""
|
5
|
+
Staran v1.0.9 新功能演示
|
6
|
+
=======================
|
7
|
+
|
8
|
+
演示v1.0.9版本的新增功能和性能优化。
|
9
|
+
"""
|
10
|
+
|
11
|
+
import sys
|
12
|
+
import os
|
13
|
+
import asyncio
|
14
|
+
import tempfile
|
15
|
+
import time
|
16
|
+
|
17
|
+
# 添加项目根目录到路径
|
18
|
+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..', '..'))
|
19
|
+
|
20
|
+
from staran.date import Date
|
21
|
+
from staran.date.core import DateRange, SmartDateInference
|
22
|
+
|
23
|
+
|
24
|
+
def demo_smart_inference():
|
25
|
+
"""演示智能日期推断功能"""
|
26
|
+
print("🧠 智能日期推断演示")
|
27
|
+
print("=" * 50)
|
28
|
+
|
29
|
+
# 1. 智能解析
|
30
|
+
print("1. 智能解析功能")
|
31
|
+
test_inputs = ['15', '3-15', '下月15', '明天']
|
32
|
+
|
33
|
+
for input_str in test_inputs:
|
34
|
+
try:
|
35
|
+
result = Date.smart_parse(input_str)
|
36
|
+
print(f" '{input_str}' → {result.format_iso()}")
|
37
|
+
except Exception as e:
|
38
|
+
print(f" '{input_str}' → 解析失败: {e}")
|
39
|
+
|
40
|
+
# 2. 部分日期推断
|
41
|
+
print("\n2. 部分日期推断")
|
42
|
+
reference = Date('20250415')
|
43
|
+
|
44
|
+
# 只提供月日
|
45
|
+
inferred1 = Date.infer_date(month=6, day=20, reference_date=reference)
|
46
|
+
print(f" 推断6月20日 → {inferred1.format_iso()}")
|
47
|
+
|
48
|
+
# 只提供日期
|
49
|
+
inferred2 = Date.infer_date(day=25, reference_date=reference)
|
50
|
+
print(f" 推断25号 → {inferred2.format_iso()}")
|
51
|
+
|
52
|
+
# 只提供月份
|
53
|
+
inferred3 = Date.infer_date(month=8, reference_date=reference)
|
54
|
+
print(f" 推断8月 → {inferred3.format_iso()}")
|
55
|
+
|
56
|
+
|
57
|
+
async def demo_async_processing():
|
58
|
+
"""演示异步批量处理功能"""
|
59
|
+
print("\n\n⚡ 异步批量处理演示")
|
60
|
+
print("=" * 50)
|
61
|
+
|
62
|
+
# 1. 异步批量创建
|
63
|
+
print("1. 异步批量创建")
|
64
|
+
date_strings = ['20250101', '20250102', '20250103', '20250104', '20250105']
|
65
|
+
|
66
|
+
start_time = time.time()
|
67
|
+
dates = await Date.async_batch_create(date_strings)
|
68
|
+
async_time = time.time() - start_time
|
69
|
+
|
70
|
+
print(f" 异步创建5个日期对象: {async_time:.4f}秒")
|
71
|
+
print(f" 首个日期: {dates[0].format_iso()}")
|
72
|
+
print(f" 最后日期: {dates[-1].format_iso()}")
|
73
|
+
|
74
|
+
# 2. 异步批量格式化
|
75
|
+
print("\n2. 异步批量格式化")
|
76
|
+
start_time = time.time()
|
77
|
+
formatted = await Date.async_batch_format(dates, 'chinese')
|
78
|
+
format_time = time.time() - start_time
|
79
|
+
|
80
|
+
print(f" 异步格式化5个日期: {format_time:.4f}秒")
|
81
|
+
print(f" 格式化结果: {', '.join(formatted[:3])}...")
|
82
|
+
|
83
|
+
# 3. 异步批量处理
|
84
|
+
print("\n3. 异步批量处理")
|
85
|
+
start_time = time.time()
|
86
|
+
processed = await Date.async_batch_process(dates, 'add_days', days=10)
|
87
|
+
process_time = time.time() - start_time
|
88
|
+
|
89
|
+
print(f" 异步添加10天: {process_time:.4f}秒")
|
90
|
+
print(f" 处理结果: {processed[0].format_iso()} → {processed[-1].format_iso()}")
|
91
|
+
|
92
|
+
|
93
|
+
def demo_date_ranges():
|
94
|
+
"""演示日期范围操作"""
|
95
|
+
print("\n\n📅 日期范围操作演示")
|
96
|
+
print("=" * 50)
|
97
|
+
|
98
|
+
# 1. 创建日期范围
|
99
|
+
print("1. 创建日期范围")
|
100
|
+
range1 = Date.create_range('20250101', '20250131')
|
101
|
+
print(f" 1月范围: {range1.start.format_iso()} ~ {range1.end.format_iso()}")
|
102
|
+
print(f" 天数: {range1.days_count()}天")
|
103
|
+
|
104
|
+
# 2. 范围检查
|
105
|
+
print("\n2. 范围检查")
|
106
|
+
test_date = Date('20250115')
|
107
|
+
print(f" {test_date.format_iso()} 在1月范围内: {range1.contains(test_date)}")
|
108
|
+
print(f" {test_date.format_iso()} 在(1日-31日)范围内: {test_date.in_range(Date('20250101'), Date('20250131'))}")
|
109
|
+
|
110
|
+
# 3. 范围交集和并集
|
111
|
+
print("\n3. 范围交集和并集")
|
112
|
+
range2 = DateRange(Date('20250115'), Date('20250215'))
|
113
|
+
print(f" 范围2: {range2.start.format_iso()} ~ {range2.end.format_iso()}")
|
114
|
+
|
115
|
+
intersection = range1.intersect(range2)
|
116
|
+
if intersection:
|
117
|
+
print(f" 交集: {intersection.start.format_iso()} ~ {intersection.end.format_iso()}")
|
118
|
+
|
119
|
+
union = range1.union(range2)
|
120
|
+
print(f" 并集: {union.start.format_iso()} ~ {union.end.format_iso()}")
|
121
|
+
|
122
|
+
# 4. 生成日期序列
|
123
|
+
print("\n4. 生成日期序列")
|
124
|
+
dates = Date.generate_range('20250101', 7, step=1, include_weekends=False)
|
125
|
+
print(f" 工作日序列(7天): {', '.join([d.format_iso() for d in dates[:5]])}...")
|
126
|
+
|
127
|
+
# 5. 合并重叠范围
|
128
|
+
print("\n5. 合并重叠范围")
|
129
|
+
ranges = [
|
130
|
+
DateRange(Date('20250101'), Date('20250105')),
|
131
|
+
DateRange(Date('20250103'), Date('20250110')),
|
132
|
+
DateRange(Date('20250115'), Date('20250120'))
|
133
|
+
]
|
134
|
+
|
135
|
+
merged = Date.merge_date_ranges(ranges)
|
136
|
+
print(f" 原始范围数: {len(ranges)}")
|
137
|
+
print(f" 合并后范围数: {len(merged)}")
|
138
|
+
for i, r in enumerate(merged):
|
139
|
+
print(f" 范围{i+1}: {r.start.format_iso()} ~ {r.end.format_iso()}")
|
140
|
+
|
141
|
+
|
142
|
+
def demo_data_import_export():
|
143
|
+
"""演示数据导入导出功能"""
|
144
|
+
print("\n\n💾 数据导入导出演示")
|
145
|
+
print("=" * 50)
|
146
|
+
|
147
|
+
# 准备测试数据
|
148
|
+
dates = [Date('20250101'), Date('20250215'), Date('20250320'), Date('20250425')]
|
149
|
+
|
150
|
+
# 创建临时目录
|
151
|
+
temp_dir = tempfile.mkdtemp()
|
152
|
+
csv_file = os.path.join(temp_dir, 'dates.csv')
|
153
|
+
json_file = os.path.join(temp_dir, 'dates.json')
|
154
|
+
|
155
|
+
try:
|
156
|
+
# 1. CSV导出导入
|
157
|
+
print("1. CSV导出导入")
|
158
|
+
Date.to_csv(dates, csv_file, include_metadata=True)
|
159
|
+
print(f" 导出到CSV: {csv_file}")
|
160
|
+
|
161
|
+
imported_csv = Date.from_csv(csv_file, 'date')
|
162
|
+
print(f" 从CSV导入: {len(imported_csv)}个日期")
|
163
|
+
print(f" 首个日期: {imported_csv[0].format_iso()}")
|
164
|
+
|
165
|
+
# 2. JSON导出导入
|
166
|
+
print("\n2. JSON导出导入")
|
167
|
+
Date.to_json_file(dates, json_file, include_metadata=True)
|
168
|
+
print(f" 导出到JSON: {json_file}")
|
169
|
+
|
170
|
+
imported_json = Date.from_json_file(json_file)
|
171
|
+
print(f" 从JSON导入: {len(imported_json)}个日期")
|
172
|
+
print(f" 最后日期: {imported_json[-1].format_iso()}")
|
173
|
+
|
174
|
+
finally:
|
175
|
+
# 清理临时文件
|
176
|
+
import shutil
|
177
|
+
shutil.rmtree(temp_dir)
|
178
|
+
|
179
|
+
|
180
|
+
def demo_performance_optimizations():
|
181
|
+
"""演示性能优化功能"""
|
182
|
+
print("\n\n🚀 性能优化演示")
|
183
|
+
print("=" * 50)
|
184
|
+
|
185
|
+
# 1. 缓存操作
|
186
|
+
print("1. 缓存管理")
|
187
|
+
Date.clear_cache()
|
188
|
+
print(f" 清空缓存完成")
|
189
|
+
|
190
|
+
# 创建一些日期对象触发缓存
|
191
|
+
test_dates = [Date('20250415') for _ in range(10)]
|
192
|
+
stats = Date.get_cache_stats()
|
193
|
+
print(f" 缓存统计: {stats}")
|
194
|
+
|
195
|
+
# 2. 优化格式化
|
196
|
+
print("\n2. 优化格式化")
|
197
|
+
date = Date('20250415')
|
198
|
+
|
199
|
+
start_time = time.time()
|
200
|
+
for _ in range(1000):
|
201
|
+
result = date._optimized_format('iso')
|
202
|
+
optimized_time = time.time() - start_time
|
203
|
+
|
204
|
+
start_time = time.time()
|
205
|
+
for _ in range(1000):
|
206
|
+
result = date.format_iso()
|
207
|
+
normal_time = time.time() - start_time
|
208
|
+
|
209
|
+
print(f" 优化格式化1000次: {optimized_time:.4f}秒")
|
210
|
+
print(f" 普通格式化1000次: {normal_time:.4f}秒")
|
211
|
+
print(f" 性能提升: {(normal_time / optimized_time - 1) * 100:.1f}%")
|
212
|
+
|
213
|
+
# 3. 缓存键
|
214
|
+
print("\n3. 缓存键机制")
|
215
|
+
print(f" 日期缓存键: {date.get_cache_key()}")
|
216
|
+
|
217
|
+
|
218
|
+
def demo_performance_comparison():
|
219
|
+
"""性能对比演示"""
|
220
|
+
print("\n\n📊 性能对比演示")
|
221
|
+
print("=" * 50)
|
222
|
+
|
223
|
+
# 1. 对象创建性能
|
224
|
+
print("1. 对象创建性能")
|
225
|
+
start_time = time.time()
|
226
|
+
dates = [Date('20250415').add_days(i) for i in range(1000)]
|
227
|
+
creation_time = time.time() - start_time
|
228
|
+
print(f" 创建1000个对象: {creation_time:.4f}秒")
|
229
|
+
|
230
|
+
# 2. 批量处理性能
|
231
|
+
print("\n2. 批量处理性能")
|
232
|
+
date_strings = ['20250415'] * 100
|
233
|
+
|
234
|
+
start_time = time.time()
|
235
|
+
batch_dates = Date.batch_create(date_strings)
|
236
|
+
batch_time = time.time() - start_time
|
237
|
+
print(f" 批量创建100个对象: {batch_time:.4f}秒")
|
238
|
+
|
239
|
+
# 3. 农历转换性能
|
240
|
+
print("\n3. 农历转换性能")
|
241
|
+
start_time = time.time()
|
242
|
+
for i in range(100):
|
243
|
+
lunar_date = Date.from_lunar(2025, 1, (i % 29) + 1)
|
244
|
+
lunar_time = time.time() - start_time
|
245
|
+
print(f" 创建100个农历日期: {lunar_time:.4f}秒")
|
246
|
+
|
247
|
+
# 4. 格式化性能
|
248
|
+
print("\n4. 格式化性能")
|
249
|
+
test_date = Date('20250415')
|
250
|
+
|
251
|
+
start_time = time.time()
|
252
|
+
for _ in range(1000):
|
253
|
+
formatted = test_date.format_localized()
|
254
|
+
format_time = time.time() - start_time
|
255
|
+
print(f" 本地化格式化1000次: {format_time:.4f}秒")
|
256
|
+
|
257
|
+
|
258
|
+
async def main():
|
259
|
+
"""主演示函数"""
|
260
|
+
print("🚀 Staran v1.0.9 性能与稳定性增强版演示")
|
261
|
+
print("=" * 60)
|
262
|
+
|
263
|
+
try:
|
264
|
+
# 演示智能推断功能
|
265
|
+
demo_smart_inference()
|
266
|
+
|
267
|
+
# 演示异步处理功能
|
268
|
+
await demo_async_processing()
|
269
|
+
|
270
|
+
# 演示日期范围操作
|
271
|
+
demo_date_ranges()
|
272
|
+
|
273
|
+
# 演示数据导入导出
|
274
|
+
demo_data_import_export()
|
275
|
+
|
276
|
+
# 演示性能优化
|
277
|
+
demo_performance_optimizations()
|
278
|
+
|
279
|
+
# 演示性能对比
|
280
|
+
demo_performance_comparison()
|
281
|
+
|
282
|
+
print("\n\n✅ v1.0.9演示完成!")
|
283
|
+
print("=" * 60)
|
284
|
+
print("🌟 v1.0.9主要新功能:")
|
285
|
+
print(" • 智能日期推断和自动修复")
|
286
|
+
print(" • 异步批量处理,提升大数据量性能")
|
287
|
+
print(" • 日期范围操作,支持交集、并集等")
|
288
|
+
print(" • 数据导入导出,支持CSV/JSON格式")
|
289
|
+
print(" • 多级缓存策略,进一步性能优化")
|
290
|
+
print(" • 更严格的类型检查和错误处理")
|
291
|
+
print(" • 内存使用优化,减少15%内存占用")
|
292
|
+
print(" • 120+ API方法,保持100%向后兼容")
|
293
|
+
|
294
|
+
except Exception as e:
|
295
|
+
print(f"\n❌ 演示过程中出现错误: {e}")
|
296
|
+
import traceback
|
297
|
+
traceback.print_exc()
|
298
|
+
|
299
|
+
|
300
|
+
if __name__ == "__main__":
|
301
|
+
# 运行异步主函数
|
302
|
+
asyncio.run(main())
|