Skip to content

Commit

Permalink
增加时间范围解析和增加未来时间计算解析
Browse files Browse the repository at this point in the history
  • Loading branch information
kusen-alpha committed Nov 23, 2023
1 parent 7fe8cbe commit 0b6dcf5
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 9 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ print(dt)
正则使用有名分组形式,对应关系如下:

| 关键词 | 含义 | 示例 |
|:---:|:---:|:------------------------:|
|:---:|:-----------:|:------------------------:|
| Y || (?P\<Y>\d{4})年 |
| m || (?P\<m >\d{1,2})月 |
| d || (?P\<d>\d{1,2})日 |
Expand All @@ -114,7 +114,14 @@ print(dt)
| wM | 在...分内 | (?P\<wM>\d+)\s*(分钟)\s*(内) |
| wS | 在...秒内 | (?P\<wS>\d+)\s*(秒)\s*(内) |
| wa | 在...星期内 | (?P\<wa>\d+)\s*(周)\s*(内) |
| sd | 今天/昨天/前天/刚刚 | (?P\<sd>前天) |
| aY | 在...年后 | (?P\<wY>\d+)\s*(年)\s*(后) |
| am | 在...月后 | (?P\<wm>\d+)\s*(月)\s*(后) |
| ad | 在...日后 | (?P\<wd>\d+)\s*(天)\s*(后) |
| aH | 在...时后 | (?P\<wH>\d+)\s*(小时)\s*(后) |
| aM | 在...分后 | (?P\<wM>\d+)\s*(分钟)\s*(后) |
| aS | 在...秒后 | (?P\<wS>\d+)\s*(秒)\s*(后) |
| aa | 在...星期后 | (?P\<wa>\d+)\s*(周)\s*(后) |
| sd | 今天/昨天/前天/刚刚 | (?P\<sd>前天) |
| apm | 上午下午 | (?P\<apm>am) |

示例如下:
Expand Down
63 changes: 57 additions & 6 deletions gggdtparser/dtparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import random
import logging
import datetime

from gggdtparser.utils import s2dt
from . import dtconfigs


Expand Down Expand Up @@ -260,6 +262,55 @@ def _parse_group_dict(cls, group_dict, result_accurately, max_datetime,
cls._update_use_now_config(
use_now_config, year=True, month=True,
day=True, hour=True, minute=True, second=True)

# 在xxx之后 after
calc_add = False
change_after = dict()
change_after['aY'] = int(group_dict.get('aY') or 0)
change_after['am'] = int(group_dict.get('am') or 0)
change_after['ad'] = int(group_dict.get('ad') or 0)
change_after['aH'] = int(group_dict.get('aH') or 0)
change_after['aM'] = int(group_dict.get('aM') or 0)
change_after['aS'] = int(group_dict.get('aS') or 0)
change_after['aa'] = int(group_dict.get('aa') or 0)
if any(change_after.values()):
calc_add = True
for key in change_after:
if change_after[key] <= 0 or result_accurately:
continue
change_after[key] -= round(random.random(), 1)
if change_after['aY'] > 0:
change_day += change_after['aY'] * 365
cls._update_use_now_config(use_now_config, year=True,
month=not result_accurately,
day=not result_accurately)
if change_after['am'] > 0:
change_day += change_after['am'] * 30
cls._update_use_now_config(use_now_config, year=True,
month=True, day=True)
if change_after['aa'] > 0:
change_day += change_after['aa'] * 7
cls._update_use_now_config(use_now_config, year=True,
month=True, day=True)
if change_after['ad'] > 0:
change_day += change_after['ad']
cls._update_use_now_config(use_now_config, year=True,
month=True, day=True)
if change_after['aH'] > 0:
change_hour += change_after['aH']
cls._update_use_now_config(use_now_config, year=True,
month=True, day=True, hour=True)
if change_after['aM'] > 0:
change_minute += change_after['aM']
cls._update_use_now_config(use_now_config, year=True,
month=True,
day=True, hour=True, minute=True)
if change_after['aS'] > 0:
change_second += change_after['aS']
cls._update_use_now_config(
use_now_config, year=True, month=True,
day=True, hour=True, minute=True, second=True)

# 抽取到具有特殊时间 special
special_day = group_dict.get('sd') or ''
special_other = group_dict.get('so') or ''
Expand Down Expand Up @@ -318,7 +369,11 @@ def _parse_group_dict(cls, group_dict, result_accurately, max_datetime,
parse_datetime = datetime.datetime(
year=year, month=month,
day=day, hour=hour,
minute=minute, second=second) - change_timedelta
minute=minute, second=second)
if calc_add:
parse_datetime = parse_datetime + change_timedelta
else:
parse_datetime = parse_datetime - change_timedelta
if max_datetime and parse_datetime > max_datetime:
raise Exception('解析时间超出最大时间')
if min_datetime and parse_datetime < min_datetime:
Expand Down Expand Up @@ -367,11 +422,7 @@ def parse_by_format(string_datetime, format_list=None):
"""
if not format_list:
format_list = []
for f in format_list:
try:
return datetime.datetime.strptime(string_datetime, f)
except ValueError:
continue
return s2dt(string_datetime, format_list)


def parse(string_datetime, format_list=None, regex_list=None,
Expand Down
9 changes: 9 additions & 0 deletions gggdtparser/langs/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@
r"(?P<wS>\d+)\s*秒内",
r"(?P<wa>\d+)\s*周内",
r"(?P<wa>\d+)\s*星期内",
# after
r"(?P<ad>\d+)\s*天后",
r"(?P<aM>\d+)\s*分钟后",
r"(?P<aH>\d+)\s*小时后",
r"(?P<am>\d+)\s*(个)?月后",
r"(?P<aY>\d+)\s*年后",
r"(?P<aS>\d+)\s*秒后",
r"(?P<aa>\d+)\s*周后",
r"(?P<aa>\d+)\s*星期后",
# 特殊语义
r"(?P<sd>今天)\s*(?P<H>\d+):(?P<M>\d+):(?P<S>\d+)",
r"(?P<sd>今天)\s*(?P<H>\d+):(?P<M>\d+)",
Expand Down
10 changes: 9 additions & 1 deletion gggdtparser/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# author: kusen
# email: 1194542196@qq.com
# date: 2023/4/26

import datetime
import sys


Expand All @@ -22,3 +22,11 @@ def get_sort_dict(d, sort_list=None):
_d = {k: d[k] for k in sort_list if k in d}
_d.update({k: d[k] for k in d if k not in sort_list})
return _d


def s2dt(s, format_list):
for _format in format_list:
try:
return datetime.datetime.strptime(s, _format)
except ValueError:
continue

0 comments on commit 0b6dcf5

Please sign in to comment.