diff --git a/appPublic/timeUtils.py b/appPublic/timeUtils.py index 8995e8a..0fe6c70 100755 --- a/appPublic/timeUtils.py +++ b/appPublic/timeUtils.py @@ -1,12 +1,12 @@ import os,sys import time -import datetime +from datetime import date, timedelta, datetime leapMonthDays = [0,31,29,31,30,31,30,31,31,30,31,30,31] unleapMonthDays = [0,31,28,31,30,31,30,31,31,30,31,30,31] def curDatetime(): - return datetime.datetime.now() + return datetime.now() def curDateString(): d = curDatetime() @@ -27,7 +27,7 @@ def timestampstr(): d.microsecond/1000) def isMonthLastDay(d): - dd = datetime.timedelta(1) + dd = timedelta(1) d1 = d + dd if d1.month != d.month: return True @@ -45,7 +45,7 @@ def timeStampSecond(dt): return int(time.mktime((dt.year,dt.month,dt.day,dt.hour,dt.minute,dt.second,0,0,0))) def addSeconds(dt,s): - ndt = dt + datetime.timedelta(0,s) + ndt = dt + timedelta(0,s) return ndt def monthMaxDay(y,m): @@ -74,7 +74,7 @@ def str2Date(dstr): return None def ymdDate(y,m,d,H=0,M=0,S=0): - return datetime.datetime(y,m,d,H,M,S) + return datetime(y,m,d,H,M,S) def str2Datetime(dstr): x = dstr.split(' ') @@ -84,7 +84,7 @@ def str2Datetime(dstr): t = x[1] y,m,d = d.split('-') H,M,S = t.split(':') - return datetime.datetime(int(y),int(m),int(d),int(H),int(M),int(S)) + return datetime(int(y),int(m),int(d),int(H),int(M),int(S)) def strdate_add(date_str, days=0, months=0, years=0): dt = str2Datetime(date_str) @@ -116,7 +116,7 @@ def addYears(dt,years): def dateAdd(dt,days=0,months=0,years=0): if days != 0: - dd = datetime.timedelta(days) + dd = timedelta(days) dt = dt + dd if months != 0: dt = addMonths(dt,months) @@ -127,7 +127,7 @@ def dateAdd(dt,days=0,months=0,years=0): def firstSunday(dt): f = dt.weekday() if f<6: - return dt + datetime.timedelta(7 - f) + return dt + timedelta(7 - f) return dt DTFORMAT = '%Y%m%d %H%M%S' @@ -166,10 +166,10 @@ def timestampSub(ts1,ts2) : return int(ret) def timestamp2dt(t): - return datetime.datetime.fromtimestamp(t) + return datetime.fromtimestamp(t) def date_weekinyear(date_str): - w = datetime.datetime.strptime(date_str, '%Y-%m-%d').strftime('%W') + w = datetime.strptime(date_str, '%Y-%m-%d').strftime('%W') return date_str[:5] + w def date_season(date_str): @@ -191,3 +191,55 @@ def date_season(date_str): s = sl.get(m) return date_str[:5] + s +""" +Patterns = + 'D' + 'W[0-6]' + 'M[00-31]' + 'S[0-2]-[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 = str2date(strdate) + if pattern.startswith('W'): + w = (int(pattern[1]) + 6) % 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('-') ] + print(f'{m=}-{d=}, {dt.month=} {dt.day}') + m %= 4 + if m == dt.month % 4 and d == dt.day: + return True + return False + if pattern.startswith('Y'): + m,d = [ int(i) for i in pattern[1:].split('-') ] + print(f'{m=}-{d=}, {dt.month=} {dt.day}') + if m == dt.month and d == dt.day: + return True + return False +