2020年1月30日 星期四

日期範圍詞 正規化


  • 日期範圍詞 正規化目前可處理下列Regular Expression所接受的關鍵字:
r"(上|下)(週|周|禮拜|星期)"
Ex. 上星期
r"(明年|去年|前年)?([零一二兩三四五六七八九十0123456789]+)月"
Ex. 明年四月
r"(上|下)個?月"
Ex. 上個月
------------------------------------------------------------------------------------------------------------------------
  • 回應如下:
    "start": {
      "year": 2020,
      "month": 2,
      "day": 1
    },
    "end": {
      "year": 2020,
      "month": 2,
      "day": 29
    }
------------------------------------------------------------------------------------------------------------------------

  • 處理方法:


def normDateRange(strDate):

          strDate = wf2nf.wf2nf(strDate)

          d = datetime.datetime.now()

          ts = int(d.timestamp())

          oneDay = 86400  # 24*60*60

          listMonthWith31day = [1, 3, 5, 7, 8, 10, 12]

          listMonthWith30day = [4, 6, 9, 11]

 

          theYear = d.year

          theDay = d.day

          theMonth = d.month

          theWeekday = d.weekday()

 

          regex = r"(這|上|下)?週末"

          match = re.search(regex, strDate, re.MULTILINE)

          if match != None:

                   day2Weekend = 6 - theWeekday

                   tsEnd = ts + day2Weekend * oneDay

                   tsStart = tsEnd - oneDay

                   if match.group(1) == "上":

                             tsStart -= 7 * oneDay

                             tsEnd   -= 7 * oneDay

                   elif match.group(1) == "下":

                             tsStart += 7 * oneDay

                             tsEnd   += 7 * oneDay

                   dtStart = datetime.datetime.fromtimestamp(tsStart)

                   dtEnd = datetime.datetime.fromtimestamp(tsEnd)

                   return [dtStart.year, dtStart.month, dtStart.day, dtEnd.year, dtEnd.month, dtEnd.day]

 

          regex = r"(這|上|下)(週|周|禮拜|星期)"

          match = re.search(regex, strDate, re.MULTILINE)

          if match != None:

                   if match.group(1) == "上":

                             tsEnd = ts - (theWeekday + 1) * oneDay  # last sunday

                             tsStart = tsEnd - (6 * oneDay)  # last monday

                   elif match.group(1) == "下":

                             day2Weekend = 7 - theWeekday

                             tsStart = ts + day2Weekend * oneDay

                             tsEnd = tsStart + ( 6 * oneDay )

                   elif match.group(1) == "這":

                             tsStart = ts - (theWeekday * oneDay )

                             tsEnd = tsStart + ( 6 * oneDay )

                   else:

                             return [-1]

                   dtStart = datetime.datetime.fromtimestamp(tsStart)

                   dtEnd = datetime.datetime.fromtimestamp(tsEnd)

                   return [dtStart.year, dtStart.month, dtStart.day, dtEnd.year, dtEnd.month, dtEnd.day]

 

          regex = r"(明年|去年|前年)?([零一二兩三四五六七八九十0123456789]+)月"

          match = re.search(regex, strDate, re.MULTILINE)

          if match != None:

                   if match.group(1) == "明年":

                             theYear += 1

                   elif match.group(1) == "去年":

                             theYear -= 1

                   elif match.group(1) == "前年":

                             theYear -= 2

                   theMonth = chinese_to_arabic2(match.group(2))

                   if theMonth in listMonthWith31day:

                             result = [theYear, theMonth, 1, theYear, theMonth, 31]

                   elif theMonth in listMonthWith30day:

                             result = [theYear, theMonth, 1, theYear, theMonth, 30]

                   elif theMonth == 2:

                             ts = datetime.datetime(year=theYear, month=(theMonth+1), day=1).timestamp()

                             d2 = datetime.datetime.fromtimestamp(ts - oneDay)

                             result = [theYear, theMonth, 1, theYear, theMonth, d2.day]

                   else:

                             result = [-1]

                   return result

 

          regex = r"下個?月"

          match = re.search(regex, strDate, re.MULTILINE)

          if match != None:

                   theMonth += 1

                   if theMonth in listMonthWith31day:

                             lastDay = 31

                   elif theMonth in listMonthWith30day:

                             lastDay = 30

                   elif theMonth == 2:

                             ts = datetime.datetime(year=theYear, month=(theMonth+1), day=1).timestamp()

                             d2 = datetime.datetime.fromtimestamp(ts - oneDay)

                             lastDay = d2.day

                   elif theMonth == 13:

                             theYear += 1

                             theMonth = 1

                             lastDay = 31

                   else:

                             return [-1]

                   return [theYear, theMonth, 1, theYear, theMonth, lastDay]

 

          regex = r"上個?月"

          match = re.search(regex, strDate, re.MULTILINE)

          if match != None:

                   theMonth -= 1

                   if theMonth in listMonthWith31day:

                             lastDay = 31

                   elif theMonth in listMonthWith30day:

                             lastDay = 30

                   elif theMonth == 2:

                             ts = datetime.datetime(year=theYear, month=(theMonth+1), day=1).timestamp()

                             d2 = datetime.datetime.fromtimestamp(ts - oneDay)

                             lastDay = d2.day

                   elif theMonth == 0:

                             theYear -= 1

                             theMonth = 12

                             lastDay = 31

                   else:

                             return [-1]

                   return [theYear, theMonth, 1, theYear, theMonth, lastDay]

 

          return [-1]




沒有留言:

張貼留言

IKEA吊櫃廚櫃

 好不容易裝好IKEA買來的吊櫃,花了三天。 從組裝,鑽牆,上牆調水平,累死我了。