-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwork.py
60 lines (52 loc) · 1.6 KB
/
work.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from datetime import datetime
from datetime import timedelta
def get_period(s: str) -> str:
"""
提取产品周期
>>> s = '债券2018年第235期三个月定开成长净值型'
>>> get_period(s)
'三个月'
"""
start = s.index('期') + 1
end = s.index('定开')
return s[start:end]
def get_days(s: str, period: dict) -> int:
"""
计算日期
>>> s = '三个月'
>>> period = {'三个月': 30}
>>> get_days(s, period)
90
"""
return period[s]
def get_date(loop_days: int, start: datetime, now: datetime) -> str:
"""
日期区间
>>> loop_days = 30
>>> start = start = datetime(2018, 5, 18)
>>> now = datetime.today()
>>> get_date(loop_days, start, now)
(datetime.datetime(2018, 7, 19, 0, 0), datetime.datetime(2018, 8, 16, 0, 0))
"""
x1 = start
x2 = x1 + timedelta(days=loop_days)
#开始日期是start, 循环加loop_days
while True:
if (x1 < now) and (now < x2):
return (x1, x2)
x1 = x1 + timedelta(days=loop_days+1)
x2 = x2 + timedelta(days=loop_days)
def get_days_02(loop_days: int, start: datetime, now: datetime) -> str:
"""
日期区间
>>> loop_days = 30
>>> start = start = datetime(2018, 5, 18)
>>> now = datetime.today()
>>> get_date(loop_days, start, now)
(datetime.datetime(2018, 7, 19, 0, 0), datetime.datetime(2018, 8, 16, 0, 0))
"""
diff = (now - start).days
n = diff // loop_days
x1 = start + timedelta(days=n*loop_days+n)
x2 = x1 + timedelta(days=loop_days-n)
return (x1, x2)