diff --git a/test/test_date.py b/test/test_date.py index 66ba9c4..17c6acb 100644 --- a/test/test_date.py +++ b/test/test_date.py @@ -1,17 +1,76 @@ import json +import inspect import unittest import datetime import utils.date_tool +#TEST_FUNCTIONS = ["test_dateformat", "test_parseFormula", "test_parseDate"] +TEST_FUNCTIONS = ["test_parseFormula"] + class MyTestCase(unittest.TestCase): + mymsg = "--------------------------------------------------------------" + def test_dateformat(self): + actfunction = str(inspect.currentframe().f_code.co_name) + cnttest = 0 + if actfunction not in TEST_FUNCTIONS: + return stime = datetime.datetime.now() print(stime) tdate = (2022, 2, 10) sdate = datetime.datetime(tdate[0], tdate[1],tdate[2], 3, 32, 23) sdate = datetime.datetime(stime) print(sdate) + MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest) + + + def test_parseFormula(self): + actfunction = str(inspect.currentframe().f_code.co_name) + cnttest = 0 + if actfunction not in TEST_FUNCTIONS: + return + res = utils.date_tool.parseFormula("{(21.12.2012 +1Y)}") + print(str(res)) + self.assertEqual(res[0], 2013) + self.assertEqual(res[3], 0) + res = utils.date_tool.parseFormula("{(21.12.2012 +1Y -1M)}") + print(str(res)) + self.assertEqual(res[0], 2013) + self.assertEqual(res[1], 11) + self.assertEqual(res[3], 0) + res = utils.date_tool.parseFormula("{(21.12.2012 +1 Jahre +20 Tage)}") + print(str(res)) + self.assertEqual(res[0], 2014) + self.assertEqual(res[1], 1) + self.assertEqual(res[2], 10) + self.assertEqual(res[3], 0) + res = utils.date_tool.parseFormula("{(21.12.2012_11:12:43 +1Y)}") + print(str(res)) + self.assertEqual(res[0], 2013) + self.assertEqual(res[5], 43) + res = utils.date_tool.parseFormula("{(21.12.2012 -60M)}") + print(str(res)) + self.assertEqual(res[0], 2007) + self.assertEqual(res[1], 12) + self.assertEqual(res[3], 0) + MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest) + + + def test_parseDate(self): + actfunction = str(inspect.currentframe().f_code.co_name) + cnttest = 0 + if actfunction not in TEST_FUNCTIONS: + return + res = utils.date_tool.parseDate("21.12.2012") + print(str(res)) + self.assertEqual(res[0], 2012) + self.assertEqual(res[3], 0) + res = utils.date_tool.parseDate("{(21.12.2012 +1Y)}") + print(str(res)) + self.assertEqual(res[0], 2012) + self.assertEqual(res[3], 0) + MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest) if __name__ == '__main__': unittest.main() diff --git a/utils/date_tool.py b/utils/date_tool.py index e45207f..76d493a 100644 --- a/utils/date_tool.py +++ b/utils/date_tool.py @@ -4,6 +4,10 @@ additionally functions for calculating date with formulas like [DATE+2M] and for comparison of date related on two reference-dates """ import datetime +#from dateutil.relativedelta import relativedelta +import re +import utils.data_const as D + F_DIR = "%Y-%m-%d_%H-%M-%S" F_DE = "%d.%m.%Y" @@ -15,3 +19,87 @@ def getActdate(format): def getFormatdate(date, format): """ it return the date as string in the format """ return date.strftime(format) + + +def parseFormula(instring): + instring = instring.upper() + if instring[2:6] == "DATE": + refdate = datetime.datetime.today() + formula = instring[7:-2].upper() + else: + dstring = instring[2:instring.find(" ")] + res = parseDate(dstring) + refdate = datetime.datetime(res[0], res[1], res[2], res[3], res[4], res[5]) + formula = instring[2+len(dstring):-2] + formula = re.sub(r' ', '', formula) + year = refdate.year + mon = refdate.month + day = refdate.day + hour = refdate.hour + min = refdate.minute + sec = refdate.second + if re.match(r"[-+]\d+[JYMDT]", formula): + ress = re.compile(r"([-+])(\d+)([JYMDT])") + for res in ress.finditer(formula): + summand = int(res.group(2)) + if res.group(1) == "-": + summand = summand * (-1) + if res.group(3) in "JY": + year = year + summand + if res.group(3) in "M": + mon = mon + summand + while mon <= 0: + mon = mon + 12 + year = year - 1 + while mon > 12: + mon = mon - 12 + year = year + 1 + if res.group(3) in "DT": + refdate = datetime.datetime(year, mon, day, hour, min, sec) + refdate = refdate + datetime.timedelta(days=summand) + year = refdate.year + mon = refdate.month + day = refdate.day + hour = refdate.hour + min = refdate.minute + sec = refdate.second + return (year, mon, day, hour, min, sec) + else: + print("re matcht nicht") + return (year, mon, day, hour, min, sec) + + +def parseDate(instring): + year = 0 + mon = 0 + day = 0 + hour = 0 + min = 0 + sec = 0 + if instring[0:2] == "{(" and instring[-2:] == ")}": + return parseFormula(instring) + for d in ["_", " "]: + if d in instring and instring.find(d) > 8: + dstring = instring[0:instring.find(d)] + tstring = instring[instring.find(d)+1:] + dres = parseDate(dstring) + tres = parseDate(tstring) + return (dres[0], dres[1], dres[2], tres[3], tres[4], tres[5]) + if re.match(r"\d{4}[-./]\d{2}[-./]\d{2}", instring): + res = re.match(r"(\d{4})[-./](\d{2})[-./](\d{2})", instring) + year = int(res.group(1)) + mon = int(res.group(2)) + day = int(res.group(3)) + return (year, mon, day, hour, min, sec) + if re.match(r"\d{1,2}[-./]\d{1,2}[-./]\d{1,2}", instring): + res = re.match(r"(\d{1,2})[-./](\d{1,2})[-./](\d{4})", instring) + year = int(res.group(3)) + mon = int(res.group(2)) + day = int(res.group(1)) + return (year, mon, day, hour, min, sec) + if re.match(r"\d{2}[-:]\d{2}[-:/]\d{2}", instring): + res = re.match(r"(\d{2})[-:/](\d{2})[-:/](\d{2})", instring) + hour = int(res.group(1)) + min = int(res.group(2)) + sec = int(res.group(3)) + return (year, mon, day, hour, min, sec) \ No newline at end of file