Skip to content

Commit

Permalink
完善时间范围解析
Browse files Browse the repository at this point in the history
  • Loading branch information
kusen-alpha committed Nov 28, 2023
1 parent 9a63c78 commit 2f9c02d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
......
```

4. 指定formats
4. 指定formats解析

```python
import gggdtparser
Expand All @@ -88,7 +88,7 @@ dt = gggdtparser.parse("2023-02-20", format_list=["%Y-%m-%d"])
print(dt)
```

5. 指定正则
5. 指定正则解析

正则使用有名分组形式,对应关系如下:

Expand Down Expand Up @@ -150,6 +150,17 @@ print(dt)

```

7. 时间范围解析
```python

import datetime
from gggdtparser import parse_frame

print(parse_frame('2022年10月1日至2023年10月1日', regex_list=[None, ('(?P<am>\d+)\s*(个)?月',)], base_datetime=datetime.datetime(year=2023, month=1, day=1)))
print(parse_frame('10个月', regex_list=[None, ('(?P<am>\d+)\s*(个)?月',)], base_datetime=datetime.datetime(year=2023, month=1, day=1)))

```

## 待完善

1. 兼容更多语言
Expand Down
1 change: 1 addition & 0 deletions gggdtparser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
from .dtparser import check
from .dtparser import parse_by_format
from .dtparser import parse_by_regex
from .dtframe import parse as parse_frame
34 changes: 24 additions & 10 deletions gggdtparser/dtframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,36 @@
from . import dtparser

SEPS = (
'至'
'至',
'-'
)


def _parse(s, sep, start_format_list, end_format_list, start_regex_list,
end_regex_list):
end_regex_list, base_datetime):
start, end = None, None
try:
left, right = s.split(sep)
except ValueError:
if base_datetime:
start = base_datetime
end = dtparser.parse(
s, end_format_list, end_regex_list,
base_datetime=base_datetime)
return start, end
if left:
start = dtparser.parse(left, start_format_list, start_regex_list)
start = dtparser.parse(
left, start_format_list, start_regex_list,
base_datetime=base_datetime)
if right:
end = dtparser.parse(right, end_format_list, end_regex_list)
end = dtparser.parse(
right, end_format_list, end_regex_list,
base_datetime=base_datetime)
return start, end


def parse(s, seps=None, format_list=None, regex_list=None):
def parse(s, seps=None, format_list=None, regex_list=None,
base_datetime=None, ):
"""
:param s:
Expand All @@ -37,28 +48,31 @@ def parse(s, seps=None, format_list=None, regex_list=None):
[(start, start, ....), (end, end, ....)]
:param regex_list: 正则表达式
[(start, start, ....), (end, end, ....)]
:param base_datetime: 相对时间的基准,当起始时间没有解析到时,当做时间的范围的起始
:return:
"""
if not seps:
seps = []
start_format_list, end_format_list = format_list if format_list else (
None, None)
start_regex_list, end_regex_list = regex_list if regex_list else [], []

start_regex_list, end_regex_list = regex_list if regex_list else (
None, None)
for sep in seps:
start, end = _parse(
s, sep, start_format_list, end_format_list,
start_regex_list, end_regex_list)
start_regex_list, end_regex_list, base_datetime)
if any([start, end]):
return start, end
for sep in SEPS:
start, end = _parse(
s, sep, start_format_list, end_format_list,
start_regex_list, end_regex_list)
start_regex_list, end_regex_list, base_datetime)
if any([start, end]):
return start, end
return None, None


if __name__ == '__main__':
print(parse('2022年10月1日至2023年10月1日',
format_list=[('%Y年%m月%d日',), None]))
print(parse('2022年10月1日至2023年10月1日',))

0 comments on commit 2f9c02d

Please sign in to comment.