D_dayCalculator.py
1.42 KB
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
#-*- coding: utf-8 -*-
from datetime import datetime, timedelta
import re
class D_day:
def __init__(self, date_str):
self.date = date_str
def dday_calculate(self, date):
y_m_d = date.split("-")
return (datetime.now() - datetime(int(y_m_d[0]), int(y_m_d[1]), int(y_m_d[2]))).days
def date_calculate(self):
dates = re.findall(r'[0-9]{2,4}[.|-][0-9]{1,2}[.|-][0-9]{1,2}|~', self.date)
for i in range(len(dates)):
dates[i] = dates[i].replace(".", "-")
# 날짜가 없는 경우
if len(dates) == 0:
return "상시"
# XXX ~
if dates[-1] == "~":
dday = self.dday_calculate(dates[0])
if dday > 0:
return "진행중"
else:
return "준비중"
# ~ XXX and XXX ~ XXX
if dates[-1] != "~":
dday = self.dday_calculate(dates[-1])
if dday < 0:
return "D"+str(dday)
else:
return "종료"
# except
return "상 시"
def update_date(self, date):
self.date = date
#### example
'''
a=D_day("2019.11.20")
print(a.date_calculate())
a.update_date("2019.11.21")
print(a.date_calculate())
a.update_date("~2019.11.30")
print(a.date_calculate())
a.update_date("2019.10.11~2019.11.27")
print(a.date_calculate())
a.update_date("2019.10.11~")
print(a.date_calculate())
'''