staran 0.1.2__tar.gz → 0.1.3__tar.gz
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-0.1.3/PKG-INFO +140 -0
- staran-0.1.3/README.md +114 -0
- {staran-0.1.2 → staran-0.1.3}/setup.py +4 -3
- staran-0.1.3/staran/__init__.py +143 -0
- staran-0.1.3/staran/tools/__init__.py +43 -0
- {staran-0.1.2 → staran-0.1.3}/staran/tools/date.py +101 -8
- staran-0.1.3/staran.egg-info/PKG-INFO +140 -0
- staran-0.1.3/staran.egg-info/requires.txt +3 -0
- staran-0.1.2/PKG-INFO +0 -225
- staran-0.1.2/README.md +0 -200
- staran-0.1.2/staran/__init__.py +0 -21
- staran-0.1.2/staran/tools/__init__.py +0 -20
- staran-0.1.2/staran.egg-info/PKG-INFO +0 -225
- staran-0.1.2/staran.egg-info/requires.txt +0 -2
- {staran-0.1.2 → staran-0.1.3}/LICENSE +0 -0
- {staran-0.1.2 → staran-0.1.3}/setup.cfg +0 -0
- {staran-0.1.2 → staran-0.1.3}/staran.egg-info/SOURCES.txt +0 -0
- {staran-0.1.2 → staran-0.1.3}/staran.egg-info/dependency_links.txt +0 -0
- {staran-0.1.2 → staran-0.1.3}/staran.egg-info/top_level.txt +0 -0
staran-0.1.3/PKG-INFO
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: staran
|
3
|
+
Version: 0.1.3
|
4
|
+
Summary: staran - 高性能Python工具库
|
5
|
+
Home-page: https://github.com/starlxa/staran
|
6
|
+
Author: StarAn
|
7
|
+
Author-email: starlxa@icloud.com
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
10
|
+
Requires-Python: >=3.7
|
11
|
+
Description-Content-Type: text/markdown
|
12
|
+
License-File: LICENSE
|
13
|
+
Requires-Dist: datetime
|
14
|
+
Requires-Dist: calendar
|
15
|
+
Requires-Dist: re
|
16
|
+
Dynamic: author
|
17
|
+
Dynamic: author-email
|
18
|
+
Dynamic: classifier
|
19
|
+
Dynamic: description
|
20
|
+
Dynamic: description-content-type
|
21
|
+
Dynamic: home-page
|
22
|
+
Dynamic: license-file
|
23
|
+
Dynamic: requires-dist
|
24
|
+
Dynamic: requires-python
|
25
|
+
Dynamic: summary
|
26
|
+
|
27
|
+
# Staran - 简化的Python工具库
|
28
|
+
|
29
|
+
## � 轻量级Python实用工具集
|
30
|
+
|
31
|
+
Staran是一个基于Python标准库的实用工具库,提供日期处理等常用功能,无需复杂依赖。
|
32
|
+
|
33
|
+
## 🚀 快速开始
|
34
|
+
|
35
|
+
### 安装
|
36
|
+
```bash
|
37
|
+
pip install staran
|
38
|
+
```
|
39
|
+
|
40
|
+
### 基本使用
|
41
|
+
|
42
|
+
```python
|
43
|
+
from staran import Date
|
44
|
+
|
45
|
+
# 创建日期 - 智能格式记忆
|
46
|
+
date1 = Date('202504') # 输出: 202504 (记住年月格式)
|
47
|
+
date2 = Date('20250415') # 输出: 20250415 (记住完整格式)
|
48
|
+
date3 = Date(2025, 4, 15) # 输出: 2025-04-15
|
49
|
+
|
50
|
+
# 日期运算保持格式
|
51
|
+
new_date = date1.add_months(2) # 输出: 202506 (保持YYYYMM格式)
|
52
|
+
```
|
53
|
+
|
54
|
+
## 📖 主要功能
|
55
|
+
|
56
|
+
### 1. 智能格式记忆
|
57
|
+
Date类会根据输入格式自动设置默认输出格式:
|
58
|
+
|
59
|
+
| 输入方式 | 默认输出 | 说明 |
|
60
|
+
|---------|---------|------|
|
61
|
+
| `Date('202504')` | `202504` | 年月紧凑格式 |
|
62
|
+
| `Date('20250415')` | `20250415` | 完整紧凑格式 |
|
63
|
+
| `Date(2025, 4)` | `2025-04` | 年月格式 |
|
64
|
+
| `Date(2025, 4, 15)` | `2025-04-15` | 完整格式 |
|
65
|
+
|
66
|
+
### 2. 多种输出格式
|
67
|
+
```python
|
68
|
+
date = Date('202504')
|
69
|
+
|
70
|
+
# 默认格式(保持输入风格)
|
71
|
+
print(date) # 202504
|
72
|
+
|
73
|
+
# 常用格式
|
74
|
+
print(date.format_full()) # 2025-04-01
|
75
|
+
print(date.format_chinese()) # 2025年04月01日
|
76
|
+
print(date.format_year_month()) # 2025-04
|
77
|
+
print(date.format_compact()) # 20250401
|
78
|
+
```
|
79
|
+
|
80
|
+
### 3. 日期运算
|
81
|
+
```python
|
82
|
+
date = Date('202504')
|
83
|
+
|
84
|
+
# 运算后保持原格式
|
85
|
+
next_month = date.add_months(1) # 202505
|
86
|
+
tomorrow = date.add_days(1) # 202504 (智能处理)
|
87
|
+
|
88
|
+
# 日期差计算
|
89
|
+
diff = date.difference(Date('202502')) # 天数差
|
90
|
+
```
|
91
|
+
|
92
|
+
### 4. 基本信息
|
93
|
+
```python
|
94
|
+
date = Date(2024, 2, 29)
|
95
|
+
print(date.is_leap_year()) # True
|
96
|
+
print(date.weekday()) # 星期几
|
97
|
+
print(date.days_in_month()) # 当月天数
|
98
|
+
```
|
99
|
+
|
100
|
+
## 🎯 设计特色
|
101
|
+
|
102
|
+
- **格式智能** - 自动记忆输入格式,保持输出一致性
|
103
|
+
- **零依赖** - 仅基于Python标准库
|
104
|
+
- **直观API** - 符合Python习惯的设计
|
105
|
+
- **类型安全** - 完整的参数验证和错误处理
|
106
|
+
|
107
|
+
## 📁 项目结构
|
108
|
+
|
109
|
+
```
|
110
|
+
staran/
|
111
|
+
├── __init__.py # 主包入口,包含使用示例
|
112
|
+
├── tools/
|
113
|
+
│ ├── __init__.py # 工具模块
|
114
|
+
│ └── date.py # Date类实现
|
115
|
+
├── setup.py # 安装配置
|
116
|
+
└── README.md # 本文档
|
117
|
+
```
|
118
|
+
|
119
|
+
## 🧪 快速测试
|
120
|
+
|
121
|
+
```python
|
122
|
+
from staran import Date
|
123
|
+
|
124
|
+
# 测试格式记忆
|
125
|
+
date = Date('202504')
|
126
|
+
print(f"原始: {date}") # 202504
|
127
|
+
print(f"加2月: {date.add_months(2)}") # 202506
|
128
|
+
|
129
|
+
# 测试多格式输出
|
130
|
+
print(f"中文: {date.format_chinese()}") # 2025年04月01日
|
131
|
+
print(f"完整: {date.format_full()}") # 2025-04-01
|
132
|
+
```
|
133
|
+
|
134
|
+
## 📄 许可证
|
135
|
+
|
136
|
+
MIT License
|
137
|
+
|
138
|
+
---
|
139
|
+
|
140
|
+
**Staran** - 让Python工具使用更简单 🌟
|
staran-0.1.3/README.md
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
# Staran - 简化的Python工具库
|
2
|
+
|
3
|
+
## � 轻量级Python实用工具集
|
4
|
+
|
5
|
+
Staran是一个基于Python标准库的实用工具库,提供日期处理等常用功能,无需复杂依赖。
|
6
|
+
|
7
|
+
## 🚀 快速开始
|
8
|
+
|
9
|
+
### 安装
|
10
|
+
```bash
|
11
|
+
pip install staran
|
12
|
+
```
|
13
|
+
|
14
|
+
### 基本使用
|
15
|
+
|
16
|
+
```python
|
17
|
+
from staran import Date
|
18
|
+
|
19
|
+
# 创建日期 - 智能格式记忆
|
20
|
+
date1 = Date('202504') # 输出: 202504 (记住年月格式)
|
21
|
+
date2 = Date('20250415') # 输出: 20250415 (记住完整格式)
|
22
|
+
date3 = Date(2025, 4, 15) # 输出: 2025-04-15
|
23
|
+
|
24
|
+
# 日期运算保持格式
|
25
|
+
new_date = date1.add_months(2) # 输出: 202506 (保持YYYYMM格式)
|
26
|
+
```
|
27
|
+
|
28
|
+
## 📖 主要功能
|
29
|
+
|
30
|
+
### 1. 智能格式记忆
|
31
|
+
Date类会根据输入格式自动设置默认输出格式:
|
32
|
+
|
33
|
+
| 输入方式 | 默认输出 | 说明 |
|
34
|
+
|---------|---------|------|
|
35
|
+
| `Date('202504')` | `202504` | 年月紧凑格式 |
|
36
|
+
| `Date('20250415')` | `20250415` | 完整紧凑格式 |
|
37
|
+
| `Date(2025, 4)` | `2025-04` | 年月格式 |
|
38
|
+
| `Date(2025, 4, 15)` | `2025-04-15` | 完整格式 |
|
39
|
+
|
40
|
+
### 2. 多种输出格式
|
41
|
+
```python
|
42
|
+
date = Date('202504')
|
43
|
+
|
44
|
+
# 默认格式(保持输入风格)
|
45
|
+
print(date) # 202504
|
46
|
+
|
47
|
+
# 常用格式
|
48
|
+
print(date.format_full()) # 2025-04-01
|
49
|
+
print(date.format_chinese()) # 2025年04月01日
|
50
|
+
print(date.format_year_month()) # 2025-04
|
51
|
+
print(date.format_compact()) # 20250401
|
52
|
+
```
|
53
|
+
|
54
|
+
### 3. 日期运算
|
55
|
+
```python
|
56
|
+
date = Date('202504')
|
57
|
+
|
58
|
+
# 运算后保持原格式
|
59
|
+
next_month = date.add_months(1) # 202505
|
60
|
+
tomorrow = date.add_days(1) # 202504 (智能处理)
|
61
|
+
|
62
|
+
# 日期差计算
|
63
|
+
diff = date.difference(Date('202502')) # 天数差
|
64
|
+
```
|
65
|
+
|
66
|
+
### 4. 基本信息
|
67
|
+
```python
|
68
|
+
date = Date(2024, 2, 29)
|
69
|
+
print(date.is_leap_year()) # True
|
70
|
+
print(date.weekday()) # 星期几
|
71
|
+
print(date.days_in_month()) # 当月天数
|
72
|
+
```
|
73
|
+
|
74
|
+
## 🎯 设计特色
|
75
|
+
|
76
|
+
- **格式智能** - 自动记忆输入格式,保持输出一致性
|
77
|
+
- **零依赖** - 仅基于Python标准库
|
78
|
+
- **直观API** - 符合Python习惯的设计
|
79
|
+
- **类型安全** - 完整的参数验证和错误处理
|
80
|
+
|
81
|
+
## 📁 项目结构
|
82
|
+
|
83
|
+
```
|
84
|
+
staran/
|
85
|
+
├── __init__.py # 主包入口,包含使用示例
|
86
|
+
├── tools/
|
87
|
+
│ ├── __init__.py # 工具模块
|
88
|
+
│ └── date.py # Date类实现
|
89
|
+
├── setup.py # 安装配置
|
90
|
+
└── README.md # 本文档
|
91
|
+
```
|
92
|
+
|
93
|
+
## 🧪 快速测试
|
94
|
+
|
95
|
+
```python
|
96
|
+
from staran import Date
|
97
|
+
|
98
|
+
# 测试格式记忆
|
99
|
+
date = Date('202504')
|
100
|
+
print(f"原始: {date}") # 202504
|
101
|
+
print(f"加2月: {date.add_months(2)}") # 202506
|
102
|
+
|
103
|
+
# 测试多格式输出
|
104
|
+
print(f"中文: {date.format_chinese()}") # 2025年04月01日
|
105
|
+
print(f"完整: {date.format_full()}") # 2025-04-01
|
106
|
+
```
|
107
|
+
|
108
|
+
## 📄 许可证
|
109
|
+
|
110
|
+
MIT License
|
111
|
+
|
112
|
+
---
|
113
|
+
|
114
|
+
**Staran** - 让Python工具使用更简单 🌟
|
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
|
|
2
2
|
|
3
3
|
setup(
|
4
4
|
name='staran',
|
5
|
-
version='0.1.
|
5
|
+
version='0.1.3',
|
6
6
|
description='staran - 高性能Python工具库',
|
7
7
|
long_description=open('README.md', encoding='utf-8').read(),
|
8
8
|
long_description_content_type='text/markdown',
|
@@ -11,8 +11,9 @@ setup(
|
|
11
11
|
url='https://github.com/starlxa/staran',
|
12
12
|
packages=['staran', 'staran.tools'],
|
13
13
|
install_requires=[
|
14
|
-
'
|
15
|
-
'
|
14
|
+
'datetime',
|
15
|
+
'calendar',
|
16
|
+
're',
|
16
17
|
],
|
17
18
|
classifiers=[
|
18
19
|
'Programming Language :: Python :: 3',
|
@@ -0,0 +1,143 @@
|
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
|
4
|
+
"""
|
5
|
+
Staran - 简化的Python工具库
|
6
|
+
=========================
|
7
|
+
|
8
|
+
基于Python标准库的轻量级实用工具集,提供日期处理等常用功能。
|
9
|
+
|
10
|
+
主要特性:
|
11
|
+
- 零依赖:仅基于Python标准库
|
12
|
+
- 智能格式:自动记忆输入格式并保持一致性
|
13
|
+
- 直观API:符合Python习惯的设计
|
14
|
+
- 类型安全:完整的参数验证
|
15
|
+
|
16
|
+
快速开始:
|
17
|
+
---------
|
18
|
+
|
19
|
+
基本用法::
|
20
|
+
|
21
|
+
from staran import Date
|
22
|
+
|
23
|
+
# 智能格式记忆
|
24
|
+
date1 = Date('202504') # 输出: 202504 (年月格式)
|
25
|
+
date2 = Date('20250415') # 输出: 20250415 (完整格式)
|
26
|
+
date3 = Date(2025, 4, 15) # 输出: 2025-04-15
|
27
|
+
|
28
|
+
# 格式保持
|
29
|
+
new_date = date1.add_months(2) # 输出: 202506 (保持原格式)
|
30
|
+
|
31
|
+
多种输出格式::
|
32
|
+
|
33
|
+
date = Date('202504')
|
34
|
+
|
35
|
+
print(date) # 202504 (默认格式)
|
36
|
+
print(date.format_full()) # 2025-04-01
|
37
|
+
print(date.format_chinese()) # 2025年04月01日
|
38
|
+
print(date.format_compact()) # 20250401
|
39
|
+
|
40
|
+
日期运算::
|
41
|
+
|
42
|
+
date = Date(2025, 4, 15)
|
43
|
+
|
44
|
+
# 日期运算
|
45
|
+
tomorrow = date.add_days(1) # 2025-04-16
|
46
|
+
next_month = date.add_months(1) # 2025-05-15
|
47
|
+
|
48
|
+
# 日期比较
|
49
|
+
other = Date(2025, 4, 20)
|
50
|
+
diff = other.difference(date) # 5 (天数差)
|
51
|
+
print(date < other) # True
|
52
|
+
|
53
|
+
创建方式::
|
54
|
+
|
55
|
+
# 多种创建方式
|
56
|
+
today = Date() # 今日
|
57
|
+
birthday = Date(1990, 5, 15) # 指定日期
|
58
|
+
event = Date('20240615') # 从字符串
|
59
|
+
deadline = Date(year=2024, month=12, day=31) # 关键字参数
|
60
|
+
|
61
|
+
格式化选项::
|
62
|
+
|
63
|
+
date = Date('202504')
|
64
|
+
|
65
|
+
# 常用格式
|
66
|
+
date.format_default() # 202504 (默认)
|
67
|
+
date.format_full() # 2025-04-01 (完整)
|
68
|
+
date.format_year_month() # 2025-04 (年月)
|
69
|
+
date.format_chinese() # 2025年04月01日 (中文)
|
70
|
+
date.format_iso() # 2025-04-01 (ISO)
|
71
|
+
date.format_us() # 04/01/2025 (美式)
|
72
|
+
date.format_european() # 01/04/2025 (欧式)
|
73
|
+
|
74
|
+
核心功能:
|
75
|
+
---------
|
76
|
+
- Date: 智能日期类,支持格式记忆和多种输出
|
77
|
+
- 日期运算:add_days(), add_months()
|
78
|
+
- 格式化:多种预设格式方法
|
79
|
+
- 比较:支持标准比较操作符
|
80
|
+
- 转换:to_timestamp(), to_date(), to_datetime()
|
81
|
+
- 信息:is_leap_year(), weekday(), days_in_month()
|
82
|
+
|
83
|
+
版本信息:
|
84
|
+
---------
|
85
|
+
- 版本: 1.0.0
|
86
|
+
- 许可: MIT
|
87
|
+
- 作者: Staran Team
|
88
|
+
"""
|
89
|
+
|
90
|
+
# 导入主要功能
|
91
|
+
from .tools import Date
|
92
|
+
|
93
|
+
# 主要导出
|
94
|
+
__all__ = [
|
95
|
+
'Date'
|
96
|
+
]
|
97
|
+
|
98
|
+
# 包信息
|
99
|
+
__version__ = '1.0.0'
|
100
|
+
__author__ = 'Staran Team'
|
101
|
+
__description__ = 'Simple Python utilities with smart format memory'
|
102
|
+
__license__ = 'MIT'
|
103
|
+
|
104
|
+
# 便捷函数示例
|
105
|
+
def help_examples():
|
106
|
+
"""
|
107
|
+
显示Staran使用示例
|
108
|
+
|
109
|
+
Returns:
|
110
|
+
None: 打印示例到控制台
|
111
|
+
"""
|
112
|
+
print("""
|
113
|
+
Staran 使用示例
|
114
|
+
===============
|
115
|
+
|
116
|
+
1. 基本创建:
|
117
|
+
from staran import Date
|
118
|
+
|
119
|
+
date1 = Date('202504') # 202504
|
120
|
+
date2 = Date('20250415') # 20250415
|
121
|
+
date3 = Date(2025, 4, 15) # 2025-04-15
|
122
|
+
|
123
|
+
2. 格式保持:
|
124
|
+
date = Date('202504')
|
125
|
+
new_date = date.add_months(2) # 202506 (保持格式)
|
126
|
+
|
127
|
+
3. 多种格式:
|
128
|
+
date.format_full() # 2025-04-01
|
129
|
+
date.format_chinese() # 2025年04月01日
|
130
|
+
date.format_compact() # 20250401
|
131
|
+
|
132
|
+
4. 日期运算:
|
133
|
+
date.add_days(30) # 增加30天
|
134
|
+
date.add_months(2) # 增加2个月
|
135
|
+
date.difference(other) # 计算天数差
|
136
|
+
|
137
|
+
更多信息请查看: help(Date)
|
138
|
+
""")
|
139
|
+
|
140
|
+
# 快捷访问帮助
|
141
|
+
def examples():
|
142
|
+
"""显示使用示例的快捷方法"""
|
143
|
+
help_examples()
|
@@ -0,0 +1,43 @@
|
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
|
4
|
+
"""
|
5
|
+
Staran Tools - 工具集合模块
|
6
|
+
==========================
|
7
|
+
|
8
|
+
提供各种实用工具类和函数:
|
9
|
+
|
10
|
+
Date类 - 智能日期处理:
|
11
|
+
- 格式记忆:根据输入自动设置默认格式
|
12
|
+
- 多种创建方式:支持字符串、参数、关键字等
|
13
|
+
- 丰富格式化:提供多种预设输出格式
|
14
|
+
- 日期运算:支持天数、月数的加减运算
|
15
|
+
- 标准比较:支持日期间的比较操作
|
16
|
+
|
17
|
+
示例::
|
18
|
+
|
19
|
+
from staran.tools import Date
|
20
|
+
|
21
|
+
# 创建日期
|
22
|
+
date = Date('202504') # 自动记住YYYYMM格式
|
23
|
+
full_date = Date('20250415') # 自动记住YYYYMMDD格式
|
24
|
+
|
25
|
+
# 运算保持格式
|
26
|
+
next_month = date.add_months(1) # 输出: 202505
|
27
|
+
|
28
|
+
# 多种格式输出
|
29
|
+
print(date.format_chinese()) # 2025年04月01日
|
30
|
+
"""
|
31
|
+
|
32
|
+
# 导入date模块的主要类和函数
|
33
|
+
from .date import Date
|
34
|
+
|
35
|
+
# 主要导出
|
36
|
+
__all__ = [
|
37
|
+
'Date'
|
38
|
+
]
|
39
|
+
|
40
|
+
# 模块信息
|
41
|
+
__version__ = '1.0.0'
|
42
|
+
__author__ = 'Staran Team'
|
43
|
+
__description__ = 'Staran utilities with smart format memory'
|
@@ -22,10 +22,16 @@ class Date:
|
|
22
22
|
- Date('20240615') - 从字符串
|
23
23
|
- Date(year=2024, month=6, day=15) - 关键字参数
|
24
24
|
"""
|
25
|
+
# 默认格式设置
|
26
|
+
self._default_format = '%Y-%m-%d' # 默认完整日期格式
|
27
|
+
self._input_format_type = 'full' # 输入格式类型: 'full', 'year_month', 'timestamp', 'today'
|
28
|
+
|
25
29
|
if not args and not kwargs:
|
26
30
|
# 无参数 - 今日
|
27
31
|
today = datetime.date.today()
|
28
32
|
self.year, self.month, self.day = today.year, today.month, today.day
|
33
|
+
self._input_format_type = 'today'
|
34
|
+
self._default_format = '%Y-%m-%d'
|
29
35
|
elif len(args) == 1 and isinstance(args[0], str):
|
30
36
|
# 字符串参数
|
31
37
|
self._init_from_string(args[0])
|
@@ -33,6 +39,8 @@ class Date:
|
|
33
39
|
# 单个整数参数 - 视为时间戳
|
34
40
|
dt = datetime.datetime.fromtimestamp(args[0])
|
35
41
|
self.year, self.month, self.day = dt.year, dt.month, dt.day
|
42
|
+
self._input_format_type = 'timestamp'
|
43
|
+
self._default_format = '%Y-%m-%d'
|
36
44
|
elif len(args) in [2, 3]:
|
37
45
|
# 位置参数
|
38
46
|
self._init_from_args(args)
|
@@ -54,10 +62,14 @@ class Date:
|
|
54
62
|
self.year = int(clean_string[:4])
|
55
63
|
self.month = int(clean_string[4:6])
|
56
64
|
self.day = 1
|
65
|
+
self._input_format_type = 'year_month'
|
66
|
+
self._default_format = '%Y%m'
|
57
67
|
elif len(clean_string) == 8: # YYYYMMDD
|
58
68
|
self.year = int(clean_string[:4])
|
59
69
|
self.month = int(clean_string[4:6])
|
60
70
|
self.day = int(clean_string[6:8])
|
71
|
+
self._input_format_type = 'full'
|
72
|
+
self._default_format = '%Y%m%d'
|
61
73
|
else:
|
62
74
|
raise ValueError(f"Date string must be 6 (YYYYMM) or 8 (YYYYMMDD) digits after removing separators, got {len(clean_string)}")
|
63
75
|
|
@@ -66,8 +78,12 @@ class Date:
|
|
66
78
|
if len(args) == 2:
|
67
79
|
self.year, self.month = args
|
68
80
|
self.day = 1
|
81
|
+
self._input_format_type = 'year_month'
|
82
|
+
self._default_format = '%Y-%m'
|
69
83
|
elif len(args) == 3:
|
70
84
|
self.year, self.month, self.day = args
|
85
|
+
self._input_format_type = 'full'
|
86
|
+
self._default_format = '%Y-%m-%d'
|
71
87
|
else:
|
72
88
|
raise ValueError("Expected 2 or 3 positional arguments")
|
73
89
|
|
@@ -79,6 +95,14 @@ class Date:
|
|
79
95
|
|
80
96
|
if self.year is None or self.month is None:
|
81
97
|
raise ValueError("year and month are required")
|
98
|
+
|
99
|
+
# 根据是否提供day参数设置格式
|
100
|
+
if 'day' in kwargs:
|
101
|
+
self._input_format_type = 'full'
|
102
|
+
self._default_format = '%Y-%m-%d'
|
103
|
+
else:
|
104
|
+
self._input_format_type = 'year_month'
|
105
|
+
self._default_format = '%Y-%m'
|
82
106
|
|
83
107
|
def _validate_date(self):
|
84
108
|
"""验证日期有效性"""
|
@@ -112,10 +136,58 @@ class Date:
|
|
112
136
|
"""转换为Python datetime.datetime对象"""
|
113
137
|
return datetime.datetime(self.year, self.month, self.day)
|
114
138
|
|
115
|
-
def format(self, fmt=
|
116
|
-
"""
|
139
|
+
def format(self, fmt=None):
|
140
|
+
"""
|
141
|
+
格式化日期字符串
|
142
|
+
如果不指定格式,使用默认格式(根据输入时的格式确定)
|
143
|
+
"""
|
144
|
+
if fmt is None:
|
145
|
+
fmt = self._default_format
|
117
146
|
return self.to_date().strftime(fmt)
|
118
147
|
|
148
|
+
def format_default(self):
|
149
|
+
"""使用默认格式输出(保持输入时的格式风格)"""
|
150
|
+
return self.format()
|
151
|
+
|
152
|
+
def format_full(self):
|
153
|
+
"""完整日期格式: YYYY-MM-DD"""
|
154
|
+
return self.format('%Y-%m-%d')
|
155
|
+
|
156
|
+
def format_compact(self):
|
157
|
+
"""紧凑格式: YYYYMMDD"""
|
158
|
+
return self.format('%Y%m%d')
|
159
|
+
|
160
|
+
def format_year_month(self):
|
161
|
+
"""年月格式: YYYY-MM"""
|
162
|
+
return self.format('%Y-%m')
|
163
|
+
|
164
|
+
def format_year_month_compact(self):
|
165
|
+
"""年月紧凑格式: YYYYMM"""
|
166
|
+
return self.format('%Y%m')
|
167
|
+
|
168
|
+
def format_chinese(self):
|
169
|
+
"""中文格式: YYYY年MM月DD日"""
|
170
|
+
return self.format('%Y年%m月%d日')
|
171
|
+
|
172
|
+
def format_chinese_short(self):
|
173
|
+
"""中文短格式: YYYY年MM月(适用于年月类型)"""
|
174
|
+
if self._input_format_type == 'year_month':
|
175
|
+
return self.format('%Y年%m月')
|
176
|
+
else:
|
177
|
+
return self.format('%Y年%m月%d日')
|
178
|
+
|
179
|
+
def format_iso(self):
|
180
|
+
"""ISO 8601格式: YYYY-MM-DD"""
|
181
|
+
return self.format('%Y-%m-%d')
|
182
|
+
|
183
|
+
def format_us(self):
|
184
|
+
"""美式格式: MM/DD/YYYY"""
|
185
|
+
return self.format('%m/%d/%Y')
|
186
|
+
|
187
|
+
def format_european(self):
|
188
|
+
"""欧式格式: DD/MM/YYYY"""
|
189
|
+
return self.format('%d/%m/%Y')
|
190
|
+
|
119
191
|
def weekday(self):
|
120
192
|
"""返回星期几(0=Monday, 6=Sunday)"""
|
121
193
|
return self.to_date().weekday()
|
@@ -136,13 +208,30 @@ class Date:
|
|
136
208
|
"""返回当年天数"""
|
137
209
|
return 366 if self.is_leap_year() else 365
|
138
210
|
|
211
|
+
def get_default_format(self):
|
212
|
+
"""获取默认格式字符串"""
|
213
|
+
return self._default_format
|
214
|
+
|
215
|
+
def get_input_format_type(self):
|
216
|
+
"""获取输入格式类型"""
|
217
|
+
return self._input_format_type
|
218
|
+
|
219
|
+
def set_default_format(self, fmt):
|
220
|
+
"""设置新的默认格式"""
|
221
|
+
self._default_format = fmt
|
222
|
+
return self
|
223
|
+
|
139
224
|
def add_days(self, days):
|
140
|
-
"""增加天数,返回新的Date
|
225
|
+
"""增加天数,返回新的Date对象,保持原始格式"""
|
141
226
|
new_date = self.to_date() + datetime.timedelta(days=days)
|
142
|
-
|
227
|
+
result = Date(new_date.year, new_date.month, new_date.day)
|
228
|
+
# 保持原始格式设置
|
229
|
+
result._default_format = self._default_format
|
230
|
+
result._input_format_type = self._input_format_type
|
231
|
+
return result
|
143
232
|
|
144
233
|
def add_months(self, months):
|
145
|
-
"""增加月数,返回新的Date
|
234
|
+
"""增加月数,返回新的Date对象,保持原始格式"""
|
146
235
|
new_month = self.month + months
|
147
236
|
new_year = self.year
|
148
237
|
|
@@ -157,7 +246,11 @@ class Date:
|
|
157
246
|
max_day = calendar.monthrange(new_year, new_month)[1]
|
158
247
|
new_day = min(self.day, max_day)
|
159
248
|
|
160
|
-
|
249
|
+
result = Date(new_year, new_month, new_day)
|
250
|
+
# 保持原始格式设置
|
251
|
+
result._default_format = self._default_format
|
252
|
+
result._input_format_type = self._input_format_type
|
253
|
+
return result
|
161
254
|
|
162
255
|
def difference(self, other):
|
163
256
|
"""计算与另一个日期的天数差"""
|
@@ -169,8 +262,8 @@ class Date:
|
|
169
262
|
return (date1 - date2).days
|
170
263
|
|
171
264
|
def __str__(self):
|
172
|
-
"""
|
173
|
-
return
|
265
|
+
"""字符串表示,使用默认格式"""
|
266
|
+
return self.format_default()
|
174
267
|
|
175
268
|
def __repr__(self):
|
176
269
|
"""调试表示"""
|