D_dayCalculator.py 1.42 KB
#-*- 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())
'''