54 lines
1.1 KiB
Python
54 lines
1.1 KiB
Python
|
from datetime import date, timedelta
|
||
|
"""
|
||
|
Patterns =
|
||
|
'D'
|
||
|
'W[0-6]'
|
||
|
'M[00-31]'
|
||
|
'S[1-3]-[00-31]'
|
||
|
'Y[01-12]-[00-31]'
|
||
|
}
|
||
|
"""
|
||
|
|
||
|
def str2date(sd):
|
||
|
a = [ int(i) for i in sd.split('-') ]
|
||
|
return date(*a)
|
||
|
|
||
|
def is_monthend(dt):
|
||
|
if isinstance(dt, str):
|
||
|
dt = str2date(dt)
|
||
|
nxt_day = dt + timedelta(days=1)
|
||
|
if dt.month != nxt_day.month:
|
||
|
return True
|
||
|
return False
|
||
|
|
||
|
def is_match_pattern(pattern, strdate):
|
||
|
if pattern == 'D':
|
||
|
return True
|
||
|
dt = ste2date(strdate)
|
||
|
if pattern.startswith('W'):
|
||
|
w = (int(pattern[1]) + 1) % 7
|
||
|
|
||
|
if dt.weekday() == w:
|
||
|
return True
|
||
|
return False
|
||
|
if pattern.startswith('M'):
|
||
|
day = int(pattern[1:])
|
||
|
if day == 0 and is_monthend(dt):
|
||
|
return True
|
||
|
if day == dt.day:
|
||
|
return True
|
||
|
return False
|
||
|
if pattern.startswith('S'):
|
||
|
m,d = [ int(i) for i in pattern[1:].split('-') ]
|
||
|
m %= 4
|
||
|
if m == dt.month and d == dt.day:
|
||
|
return True
|
||
|
return False
|
||
|
if pattern.startswith('Y'):
|
||
|
m,d = [ int(i) for i in pattern[1:].split('-') ]
|
||
|
if m == dt.month and d == dt.day:
|
||
|
return True
|
||
|
return False
|
||
|
|
||
|
|