staran 1.0.9__py3-none-any.whl → 1.0.11__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.
@@ -2,18 +2,18 @@
2
2
  # -*- coding: utf-8 -*-
3
3
 
4
4
  """
5
- Staran 核心日期处理模块 v1.0.9
5
+ Staran 核心日期处理模块 v1.0.10
6
6
  ============================
7
7
 
8
- 提供Date类的完整实现,包括120+个企业级API方法、
8
+ 提供Date类的完整实现,包括150+个企业级API方法、
9
9
  智能格式记忆、日期运算和多种格式化选项。
10
10
 
11
- v1.0.9 新增功能:
12
- - 智能日期推断
13
- - 异步批量处理支持
14
- - 高级业务规则引擎
15
- - 日期范围操作
16
- - 性能优化和内存优化
11
+ v1.0.10 新增功能:
12
+ - 完整时区支持
13
+ - 日期表达式解析
14
+ - 二十四节气扩展
15
+ - 数据可视化集成
16
+ - REST API接口
17
17
  """
18
18
 
19
19
  import datetime
@@ -33,6 +33,21 @@ import threading
33
33
  from .lunar import LunarDate
34
34
  from .i18n import Language
35
35
 
36
+ # 导入 v1.0.10 新增模块
37
+ try:
38
+ from ..extensions.timezone import Timezone, TimezoneInfo
39
+ from ..extensions.expressions import DateExpressionParser, ParseResult
40
+ from ..extensions.solar_terms import SolarTerms, SolarTerm
41
+ from ..integrations.visualization import DateVisualization, ChartData, TimeSeriesPoint
42
+ ENHANCED_FEATURES_AVAILABLE = True
43
+ except ImportError as e:
44
+ # 向后兼容,如果新模块不可用
45
+ ENHANCED_FEATURES_AVAILABLE = False
46
+ Timezone = None
47
+ DateExpressionParser = None
48
+ SolarTerms = None
49
+ DateVisualization = None
50
+
36
51
  class DateError(ValueError):
37
52
  """Date模块的特定异常基类"""
38
53
  pass
@@ -2036,3 +2051,519 @@ class Date:
2036
2051
  def month_end(self) -> 'Date':
2037
2052
  """旧API: 获取月末"""
2038
2053
  return self.get_month_end()
2054
+
2055
+ # =============================================
2056
+ # v1.0.10 新增功能 - 时区支持
2057
+ # =============================================
2058
+
2059
+ def to_timezone(self, timezone_code: str, time_part: Optional[datetime.time] = None) -> datetime.datetime:
2060
+ """转换到指定时区 (v1.0.10)
2061
+
2062
+ Args:
2063
+ timezone_code: 时区代码
2064
+ time_part: 时间部分,默认为00:00:00
2065
+
2066
+ Returns:
2067
+ 指定时区的datetime对象
2068
+ """
2069
+ if not ENHANCED_FEATURES_AVAILABLE or not Timezone:
2070
+ raise NotImplementedError("时区功能需要安装完整版本")
2071
+
2072
+ if time_part is None:
2073
+ time_part = datetime.time(0, 0, 0)
2074
+
2075
+ dt = datetime.datetime.combine(self.to_date_object(), time_part)
2076
+ return Timezone.convert_timezone(dt, 'UTC', timezone_code)
2077
+
2078
+ def from_timezone(self, dt: datetime.datetime, timezone_code: str) -> 'Date':
2079
+ """从指定时区的datetime创建Date (v1.0.10)
2080
+
2081
+ Args:
2082
+ dt: datetime对象
2083
+ timezone_code: 时区代码
2084
+
2085
+ Returns:
2086
+ Date对象
2087
+ """
2088
+ if not ENHANCED_FEATURES_AVAILABLE or not Timezone:
2089
+ raise NotImplementedError("时区功能需要安装完整版本")
2090
+
2091
+ utc_dt = Timezone.convert_timezone(dt, timezone_code, 'UTC')
2092
+ return Date(utc_dt.date())
2093
+
2094
+ def get_timezone_info(self, timezone_code: str) -> Dict[str, Any]:
2095
+ """获取时区信息 (v1.0.10)
2096
+
2097
+ Args:
2098
+ timezone_code: 时区代码
2099
+
2100
+ Returns:
2101
+ 时区信息字典
2102
+ """
2103
+ if not ENHANCED_FEATURES_AVAILABLE or not Timezone:
2104
+ raise NotImplementedError("时区功能需要安装完整版本")
2105
+
2106
+ return Timezone.get_timezone_display_info(timezone_code)
2107
+
2108
+ @classmethod
2109
+ def get_supported_timezones(cls) -> List[str]:
2110
+ """获取支持的时区列表 (v1.0.10)
2111
+
2112
+ Returns:
2113
+ 时区代码列表
2114
+ """
2115
+ if not ENHANCED_FEATURES_AVAILABLE or not Timezone:
2116
+ return []
2117
+
2118
+ return Timezone.list_timezones()
2119
+
2120
+ # =============================================
2121
+ # v1.0.10 新增功能 - 日期表达式解析
2122
+ # =============================================
2123
+
2124
+ @classmethod
2125
+ def parse_expression(cls, expression: str) -> Optional['Date']:
2126
+ """解析自然语言日期表达式 (v1.0.10)
2127
+
2128
+ Args:
2129
+ expression: 日期表达式,如"明天"、"下周一"等
2130
+
2131
+ Returns:
2132
+ 解析成功返回Date对象,失败返回None
2133
+ """
2134
+ if not ENHANCED_FEATURES_AVAILABLE or not DateExpressionParser:
2135
+ raise NotImplementedError("表达式解析功能需要安装完整版本")
2136
+
2137
+ parser = DateExpressionParser()
2138
+ result = parser.parse(expression)
2139
+
2140
+ if result.success and result.date:
2141
+ return cls(result.date)
2142
+ return None
2143
+
2144
+ @classmethod
2145
+ def parse_expression_detailed(cls, expression: str) -> Dict[str, Any]:
2146
+ """详细解析日期表达式 (v1.0.10)
2147
+
2148
+ Args:
2149
+ expression: 日期表达式
2150
+
2151
+ Returns:
2152
+ 详细解析结果
2153
+ """
2154
+ if not ENHANCED_FEATURES_AVAILABLE or not DateExpressionParser:
2155
+ raise NotImplementedError("表达式解析功能需要安装完整版本")
2156
+
2157
+ parser = DateExpressionParser()
2158
+ result = parser.parse(expression)
2159
+
2160
+ return {
2161
+ 'success': result.success,
2162
+ 'date': cls(result.date) if result.success and result.date else None,
2163
+ 'confidence': result.confidence,
2164
+ 'matched_pattern': result.matched_pattern,
2165
+ 'extracted_components': result.extracted_components,
2166
+ 'expression': result.expression
2167
+ }
2168
+
2169
+ def matches_expression(self, expression: str) -> bool:
2170
+ """检查是否匹配日期表达式 (v1.0.10)
2171
+
2172
+ Args:
2173
+ expression: 日期表达式
2174
+
2175
+ Returns:
2176
+ 是否匹配
2177
+ """
2178
+ parsed_date = self.parse_expression(expression)
2179
+ return parsed_date == self if parsed_date else False
2180
+
2181
+ # =============================================
2182
+ # v1.0.10 新增功能 - 二十四节气支持
2183
+ # =============================================
2184
+
2185
+ def get_solar_term(self) -> Optional[SolarTerm]:
2186
+ """获取最接近的节气 (v1.0.10)
2187
+
2188
+ Returns:
2189
+ 节气对象,如果没有找到返回None
2190
+ """
2191
+ if not ENHANCED_FEATURES_AVAILABLE or not SolarTerms:
2192
+ raise NotImplementedError("节气功能需要安装完整版本")
2193
+
2194
+ return SolarTerms.find_solar_term_by_date(self.to_date_object())
2195
+
2196
+ def is_solar_term(self) -> bool:
2197
+ """是否为节气日 (v1.0.10)
2198
+
2199
+ Returns:
2200
+ 是否为节气日
2201
+ """
2202
+ if not ENHANCED_FEATURES_AVAILABLE or not SolarTerms:
2203
+ return False
2204
+
2205
+ return SolarTerms.is_solar_term_date(self.to_date_object())
2206
+
2207
+ def get_next_solar_term(self) -> SolarTerm:
2208
+ """获取下一个节气 (v1.0.10)
2209
+
2210
+ Returns:
2211
+ 下一个节气对象
2212
+ """
2213
+ if not ENHANCED_FEATURES_AVAILABLE or not SolarTerms:
2214
+ raise NotImplementedError("节气功能需要安装完整版本")
2215
+
2216
+ return SolarTerms.get_next_solar_term(self.to_date_object())
2217
+
2218
+ def get_previous_solar_term(self) -> SolarTerm:
2219
+ """获取上一个节气 (v1.0.10)
2220
+
2221
+ Returns:
2222
+ 上一个节气对象
2223
+ """
2224
+ if not ENHANCED_FEATURES_AVAILABLE or not SolarTerms:
2225
+ raise NotImplementedError("节气功能需要安装完整版本")
2226
+
2227
+ return SolarTerms.get_previous_solar_term(self.to_date_object())
2228
+
2229
+ def get_solar_term_season(self) -> str:
2230
+ """获取当前节气季节 (v1.0.10)
2231
+
2232
+ Returns:
2233
+ 季节名称
2234
+ """
2235
+ solar_term = self.get_solar_term()
2236
+ return solar_term.season if solar_term else "未知"
2237
+
2238
+ def days_to_next_solar_term(self) -> int:
2239
+ """到下一个节气的天数 (v1.0.10)
2240
+
2241
+ Returns:
2242
+ 天数
2243
+ """
2244
+ if not ENHANCED_FEATURES_AVAILABLE or not SolarTerms:
2245
+ raise NotImplementedError("节气功能需要安装完整版本")
2246
+
2247
+ return SolarTerms.get_days_to_next_solar_term(self.to_date_object())
2248
+
2249
+ @classmethod
2250
+ def get_year_solar_terms(cls, year: int) -> List[SolarTerm]:
2251
+ """获取指定年份的所有节气 (v1.0.10)
2252
+
2253
+ Args:
2254
+ year: 年份
2255
+
2256
+ Returns:
2257
+ 节气列表
2258
+ """
2259
+ if not ENHANCED_FEATURES_AVAILABLE or not SolarTerms:
2260
+ raise NotImplementedError("节气功能需要安装完整版本")
2261
+
2262
+ return SolarTerms.get_all_solar_terms(year)
2263
+
2264
+ @classmethod
2265
+ def get_season_solar_terms(cls, year: int, season: str) -> List[SolarTerm]:
2266
+ """获取指定年份某季节的节气 (v1.0.10)
2267
+
2268
+ Args:
2269
+ year: 年份
2270
+ season: 季节名称
2271
+
2272
+ Returns:
2273
+ 节气列表
2274
+ """
2275
+ if not ENHANCED_FEATURES_AVAILABLE or not SolarTerms:
2276
+ raise NotImplementedError("节气功能需要安装完整版本")
2277
+
2278
+ return SolarTerms.get_solar_terms_by_season(year, season)
2279
+
2280
+ # =============================================
2281
+ # v1.0.10 新增功能 - 数据可视化支持
2282
+ # =============================================
2283
+
2284
+ def create_timeline_chart(self, events: List[str], library: str = 'echarts') -> ChartData:
2285
+ """创建时间轴图表 (v1.0.10)
2286
+
2287
+ Args:
2288
+ events: 事件列表
2289
+ library: 图表库名称
2290
+
2291
+ Returns:
2292
+ 图表数据对象
2293
+ """
2294
+ if not ENHANCED_FEATURES_AVAILABLE or not DateVisualization:
2295
+ raise NotImplementedError("可视化功能需要安装完整版本")
2296
+
2297
+ viz = DateVisualization()
2298
+ return viz.create_timeline_data([self.to_date_object()], events, library)
2299
+
2300
+ @classmethod
2301
+ def create_calendar_heatmap(cls, date_values: Dict['Date', float], year: int, library: str = 'echarts') -> ChartData:
2302
+ """创建日历热力图 (v1.0.10)
2303
+
2304
+ Args:
2305
+ date_values: 日期-数值映射
2306
+ year: 年份
2307
+ library: 图表库名称
2308
+
2309
+ Returns:
2310
+ 图表数据对象
2311
+ """
2312
+ if not ENHANCED_FEATURES_AVAILABLE or not DateVisualization:
2313
+ raise NotImplementedError("可视化功能需要安装完整版本")
2314
+
2315
+ # 转换Date对象为datetime.date
2316
+ converted_values = {date.to_date_object(): value for date, value in date_values.items()}
2317
+
2318
+ viz = DateVisualization()
2319
+ return viz.create_calendar_heatmap(converted_values, year, library)
2320
+
2321
+ @classmethod
2322
+ def create_time_series_chart(cls, time_series_data: List[Tuple['Date', float]], library: str = 'echarts') -> ChartData:
2323
+ """创建时间序列图表 (v1.0.10)
2324
+
2325
+ Args:
2326
+ time_series_data: 时间序列数据点列表
2327
+ library: 图表库名称
2328
+
2329
+ Returns:
2330
+ 图表数据对象
2331
+ """
2332
+ if not ENHANCED_FEATURES_AVAILABLE or not DateVisualization or not TimeSeriesPoint:
2333
+ raise NotImplementedError("可视化功能需要安装完整版本")
2334
+
2335
+ # 转换为TimeSeriesPoint对象
2336
+ time_series = []
2337
+ for date, value in time_series_data:
2338
+ point = TimeSeriesPoint(date.to_date_object(), value)
2339
+ time_series.append(point)
2340
+
2341
+ viz = DateVisualization()
2342
+ return viz.create_time_series_chart(time_series, library)
2343
+
2344
+ @classmethod
2345
+ def create_date_distribution_chart(cls, dates: List['Date'], group_by: str = 'month', library: str = 'echarts') -> ChartData:
2346
+ """创建日期分布图 (v1.0.10)
2347
+
2348
+ Args:
2349
+ dates: 日期列表
2350
+ group_by: 分组方式
2351
+ library: 图表库名称
2352
+
2353
+ Returns:
2354
+ 图表数据对象
2355
+ """
2356
+ if not ENHANCED_FEATURES_AVAILABLE or not DateVisualization:
2357
+ raise NotImplementedError("可视化功能需要安装完整版本")
2358
+
2359
+ # 转换Date对象为datetime.date
2360
+ date_objects = [date.to_date_object() for date in dates]
2361
+
2362
+ viz = DateVisualization()
2363
+ return viz.create_date_distribution_chart(date_objects, group_by, library)
2364
+
2365
+ def export_chart_data(self, chart_data: ChartData, format: str = 'json') -> str:
2366
+ """导出图表数据 (v1.0.10)
2367
+
2368
+ Args:
2369
+ chart_data: 图表数据对象
2370
+ format: 导出格式
2371
+
2372
+ Returns:
2373
+ 导出的数据字符串
2374
+ """
2375
+ if not ENHANCED_FEATURES_AVAILABLE or not DateVisualization:
2376
+ raise NotImplementedError("可视化功能需要安装完整版本")
2377
+
2378
+ viz = DateVisualization()
2379
+ return viz.export_chart_data(chart_data, format)
2380
+
2381
+ # =============================================
2382
+ # v1.0.10 增强功能 - 扩展日期范围操作
2383
+ # =============================================
2384
+
2385
+ def create_range_to(self, end_date: 'Date') -> DateRange:
2386
+ """创建到指定日期的范围 (v1.0.10)
2387
+
2388
+ Args:
2389
+ end_date: 结束日期
2390
+
2391
+ Returns:
2392
+ 日期范围对象
2393
+ """
2394
+ return DateRange(self, end_date)
2395
+
2396
+ def create_range_with_days(self, days: int) -> DateRange:
2397
+ """创建指定天数的范围 (v1.0.10)
2398
+
2399
+ Args:
2400
+ days: 天数(正数表示未来,负数表示过去)
2401
+
2402
+ Returns:
2403
+ 日期范围对象
2404
+ """
2405
+ if days >= 0:
2406
+ return DateRange(self, self.add_days(days))
2407
+ else:
2408
+ return DateRange(self.add_days(days), self)
2409
+
2410
+ def in_range(self, start_date: 'Date', end_date: 'Date') -> bool:
2411
+ """检查是否在指定范围内 (v1.0.10)
2412
+
2413
+ Args:
2414
+ start_date: 开始日期
2415
+ end_date: 结束日期
2416
+
2417
+ Returns:
2418
+ 是否在范围内
2419
+ """
2420
+ return start_date <= self <= end_date
2421
+
2422
+ @classmethod
2423
+ def create_date_sequence(cls, start_date: 'Date', end_date: 'Date', step_days: int = 1) -> List['Date']:
2424
+ """创建日期序列 (v1.0.10)
2425
+
2426
+ Args:
2427
+ start_date: 开始日期
2428
+ end_date: 结束日期
2429
+ step_days: 步长天数
2430
+
2431
+ Returns:
2432
+ 日期序列
2433
+ """
2434
+ sequence = []
2435
+ current = start_date
2436
+
2437
+ while current <= end_date:
2438
+ sequence.append(current)
2439
+ current = current.add_days(step_days)
2440
+
2441
+ return sequence
2442
+
2443
+ @classmethod
2444
+ def find_common_dates(cls, date_lists: List[List['Date']]) -> List['Date']:
2445
+ """查找多个日期列表的共同日期 (v1.0.10)
2446
+
2447
+ Args:
2448
+ date_lists: 日期列表的列表
2449
+
2450
+ Returns:
2451
+ 共同日期列表
2452
+ """
2453
+ if not date_lists:
2454
+ return []
2455
+
2456
+ common_dates = set(date_lists[0])
2457
+ for date_list in date_lists[1:]:
2458
+ common_dates.intersection_update(date_list)
2459
+
2460
+ return sorted(list(common_dates))
2461
+
2462
+ # =============================================
2463
+ # v1.0.10 实用工具方法
2464
+ # =============================================
2465
+
2466
+ def get_version_info(self) -> Dict[str, Any]:
2467
+ """获取版本信息 (v1.0.10)
2468
+
2469
+ Returns:
2470
+ 版本信息字典
2471
+ """
2472
+ return {
2473
+ 'version': '1.0.10',
2474
+ 'enhanced_features': ENHANCED_FEATURES_AVAILABLE,
2475
+ 'available_modules': {
2476
+ 'timezone': Timezone is not None,
2477
+ 'expressions': DateExpressionParser is not None,
2478
+ 'solar_terms': SolarTerms is not None,
2479
+ 'visualization': DateVisualization is not None
2480
+ },
2481
+ 'api_count': len([attr for attr in dir(self) if not attr.startswith('_')]),
2482
+ 'core_features': [
2483
+ 'date_creation', 'formatting', 'calculations', 'comparisons',
2484
+ 'lunar_calendar', 'multilingual', 'business_rules', 'caching'
2485
+ ],
2486
+ 'v1010_features': [
2487
+ 'timezone_support', 'expression_parsing', 'solar_terms',
2488
+ 'data_visualization', 'rest_api'
2489
+ ] if ENHANCED_FEATURES_AVAILABLE else []
2490
+ }
2491
+
2492
+ @classmethod
2493
+ def get_feature_status(cls) -> Dict[str, bool]:
2494
+ """获取功能状态 (v1.0.10)
2495
+
2496
+ Returns:
2497
+ 功能状态字典
2498
+ """
2499
+ return {
2500
+ 'core_date_operations': True,
2501
+ 'lunar_calendar': True,
2502
+ 'multilingual_support': True,
2503
+ 'batch_processing': True,
2504
+ 'business_rules': True,
2505
+ 'timezone_support': ENHANCED_FEATURES_AVAILABLE and Timezone is not None,
2506
+ 'expression_parsing': ENHANCED_FEATURES_AVAILABLE and DateExpressionParser is not None,
2507
+ 'solar_terms': ENHANCED_FEATURES_AVAILABLE and SolarTerms is not None,
2508
+ 'data_visualization': ENHANCED_FEATURES_AVAILABLE and DateVisualization is not None,
2509
+ 'rest_api': ENHANCED_FEATURES_AVAILABLE
2510
+ }
2511
+
2512
+ def help(self, category: str = 'all') -> str:
2513
+ """获取帮助信息 (v1.0.10)
2514
+
2515
+ Args:
2516
+ category: 帮助类别
2517
+
2518
+ Returns:
2519
+ 帮助文本
2520
+ """
2521
+ help_text = {
2522
+ 'creation': """
2523
+ Date对象创建方法:
2524
+ - Date() / Date.today() - 今天
2525
+ - Date(year, month, day) - 指定日期
2526
+ - Date("2025-07-29") - 字符串解析
2527
+ - Date.parse_expression("明天") - 表达式解析 (v1.0.10)
2528
+ - Date.from_lunar(2025, 1, 1) - 农历创建
2529
+ """,
2530
+ 'formatting': """
2531
+ 日期格式化方法:
2532
+ - format_iso() - ISO格式
2533
+ - format_chinese() - 中文格式
2534
+ - format_localized() - 本地化格式
2535
+ - format_custom(fmt) - 自定义格式
2536
+ """,
2537
+ 'calculations': """
2538
+ 日期计算方法:
2539
+ - add_days(n) / subtract_days(n) - 天数计算
2540
+ - add_months(n) / subtract_months(n) - 月份计算
2541
+ - add_years(n) / subtract_years(n) - 年份计算
2542
+ - calculate_difference_days(other) - 计算差值
2543
+ """,
2544
+ 'timezone': """
2545
+ 时区功能 (v1.0.10):
2546
+ - to_timezone(tz) - 转换到指定时区
2547
+ - get_timezone_info(tz) - 获取时区信息
2548
+ - get_supported_timezones() - 支持的时区列表
2549
+ """,
2550
+ 'solar_terms': """
2551
+ 二十四节气 (v1.0.10):
2552
+ - get_solar_term() - 获取当前节气
2553
+ - is_solar_term() - 是否节气日
2554
+ - get_next_solar_term() - 下一个节气
2555
+ - days_to_next_solar_term() - 到下个节气天数
2556
+ """,
2557
+ 'visualization': """
2558
+ 数据可视化 (v1.0.10):
2559
+ - create_timeline_chart() - 时间轴图表
2560
+ - create_calendar_heatmap() - 日历热力图
2561
+ - create_time_series_chart() - 时间序列图
2562
+ - create_date_distribution_chart() - 分布图
2563
+ """
2564
+ }
2565
+
2566
+ if category == 'all':
2567
+ return '\n'.join(help_text.values())
2568
+ else:
2569
+ return help_text.get(category, f"未知类别: {category}")